Code Monkey home page Code Monkey logo

fswatch's Introduction

fswatch

Build Status Build status

fswatch is a developer tool that triggers commands in response to filesystem changes. Works well on Mac, Linux, and should also works on Windows(not well tested).

Install

go get -u -v github.com/codeskyblue/fswatch

Simple way to use fswatch

Usage example

$ fswatch -v
3.0
$ fswtach --help
show help message ...

Watch file change event and trigger command

TODO: now only watch *.go, *.c, *.py files, watch depth = 0 (current directory)

$ fswatch sh -c "ls -l | wc -l"
fswatch >>> [] exec start: sh -c 'ls -l | wc -l'
       8
fswatch >>> [] finish in 11.822873ms
fswatch >>> changed: fswatch.go
fswatch >>> delay: 100ms
fswatch >>> [] exec start: [sh -c ls -l | wc -l]
       8
fswatch >>> [] finish in 13.606428ms
^Cfswatch >>> Catch signal interrupt!
fswatch >>> Kill all running ... Done

Hard way to use fswatch (not recommend)

Step 1

Create a config file fsw.yml

config file example though command fswatch with no arguments

desc: Auto generated by fswatch [fswatch]
triggers:
- pattens:
  - '**/*.go'
  - '**/*.c'
  # also support '!**/test_*.go'
  env:
    DEBUG: "1"
  # if shell is true, $cmd will be wrapped with `bash -c`
  shell: true
  cmd: go test -v
  delay: 100ms
  stop_timeout:1s
  signal: "KILL"
  kill_signal: "SIGTERM"
watch_paths:
- .
watch_depth: 5

Step 2

Run fswatch directly. Every time you edit a file. Command go test -v will be called.

$ fswatch
fswatch >>> exec start: go test -v
# github.com/codeskyblue/fswatch
./fswatch.go:281: main redeclared in this block
	previous declaration at ./config.go:354
fswatch >>> program exited: exit status 2
fswatch >>> finish in 145.499911ms

support specify config file through fswatch --config myfsw.yml

You should know

How fswatch kill process

fswatch send signal to all process when restart. (mac and linux killed by pgid, windows by taskkill) Ctrl+C will trigger fswatch quit and kill all process it started.

Pattens

More about the pattens. The patten has the same rule like .gitignore. So you can write like this.

- pattens:
  - '*.go'
  - '!*_test.go'
  - '!**/bindata.go'

main.go changed will trigger command, but a_test.go and libs/bindata.go will be ignored.

FAQs

too many open files

For mac, run the following command

sysctl -w kern.maxfiles=20480
sysctl -w kern.maxfilesperproc=18000
ulimit -S -n 2048

reference

Other

Chinese Blog: http://my.oschina.net/goskyblue/blog/194240

Friendly link:

Code History

I reviewed the first version of fswatch(which was taged 0.1). The code I look now is shit. So I deleted almost 80% code, And left some very useful functions.

Alternative

LICENSE

Under MIT

fswatch's People

Contributors

codeskyblue avatar franciscocpg avatar jeromenerf avatar jvatic avatar lantins avatar vasconcelosvcd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fswatch's Issues

Too many opened files

I have an error - fswatch >>> too many open files
Config like so:
{
    "paths": [
        "/path/to/project"
    ],
    "depth": 5,
    "exclude": [],
    "include": [
        "\\.(go|tmpl)$"
    ],
    "command": "echo helloworld",
    "env": {
        "POWERD_BY": "github.com/codeskyblue/fswatch"
    },
    "autorestart": false,
    "restart-interval": 0,
    "kill-signal": "KILL"
}

If I set depth to 2, all works. But I want deeper nesting.
It seems to fswatch open all files, not only that in "include"
I have folder ./static/ which has many files. How can I excude it?

Can't seem to ignore a path.

Good evening,

I can't seem to ignore an entire directory. I've tried several patterns and they all seem to fail me.

  pattens:
  - '**/*.go'
  - '!./frontend/'

Basically what happens is that it starts watching frontend/node_modules and then explodes with too many open files.

Is there a way to configure this so that it does not walk that directory?

strange things

ubuntu@ip-172-31-22-178:~/go/src/github.com/shxsun/klog$ go build

github.com/shxsun/klog

./klog.go:135: undefined: strings.TrimSuffix

Vagrant

I'm using NFS with vagrant to build a project. Modifying files on the host doesn't get picked up inside the guest. Is there any support for these type of events?

Passing matches down to command

This is a feature request.
Would it be possible to pass a list of files which were updated to the command.
Or provide an option to execute command for every event and pass the name of a file to the command as well.

Fail to restart process

I'm using macaron with fswatch. Sometimes when edit a file, it will cause

[Macaron] listening on 0.0.0.0:8181 (development)
[Macaron] listen tcp 0.0.0.0:8181: bind: address already in use
fswatch >>> [watch] program exited: exit status 1

Seems the old process is not killed successfully.

BTW, this problem does not always happen. But it has already happend many times since I migrate to the new version of fswatch.

cmd 使用sudo问题

使用 sudo的时候会提示sudo: no tty present and no askpass program specified
但是我又不想免密 请问如何解决啊

Watch external directory

Is there a way to watch external directories (outside the current project)

Like:

 pattens:
  - '**/*.go'
  - '**/*.yml'
  - '$GOPATH/src/gitlab.com/my-library/**/*.go'

I am building some packages that live outside the project, and I want to have my project restart when those files are changed

It may be I just have the incorrect syntax

Not backwards compatible

More of an FYI

the commit fe2525c from 12 days ago broke our dev environment, since we have our fsw.yml file in specific locations and use -config for that.

Not really an issue for us, forking and rolling back works fine. But a somewhat alienating experience.

Bring back github releases

Hi, it would be very helpful if you could add github releases to your versions so that it's easier to pin down to a specific version. Thanks a lot!!!

fswatch not works.

Before I used the old version of fswatch, but sometimes would meet some bugs. So I decide to update to the newest version. But I find that it not works. Below is the layout of my sample project.

- bin
- src
    - github.com
    - gopkg.in
    - main
        - main.go
        - routers.go

main.go

package main

import "gopkg.in/macaron.v1"

var (
    m *macaron.Macaron
)

func main() {
    m = macaron.New()

    initRouters()
    m.Run(8181)
}

routers.go

package main

func initRouters() {
    m.Get("/test", func() string {
        return "test"
    })
}

The .fsw.yml file

desc: Auto generated by fswatch [student-card]
triggers:
- name: "watch"
  pattens:
  - '*.go'
  env:
    DEBUG: "1"
  cmd: go build main && mv main bin && ./bin/main
  delay: 100ms
  signal: HUP
watch_paths:
- .
watch_depth: 5

I run below code in the root folder of my project

$ ./bin/main/fswatch -version
2.0
$ ./bin/main/fswatch
fswatch
fswatch >>> [watch] exec start: go build main && mv main bin && ./bin/main
2016/02/17 20:37:14 [main/config.go:36] Reading config file in /home/clinyong/workfile/student-card/conf/default.toml...
[Macaron] listening on 0.0.0.0:8181 (development)

Then I modified routers.go

package main

func initRouters() {
    m.Get("/hello", func() string {
        return "hello"
    })
}

But fswatch didn't trigger the command. My go version is go version go1.5.2 linux/amd64.

Hope you can help me to fix it, thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.