1 | # npm-watch
|
2 |
|
3 | Run scripts from package.json when files change.
|
4 |
|
5 | ## Synopsis
|
6 |
|
7 | Install it:
|
8 |
|
9 | npm install npm-watch
|
10 |
|
11 | Add a top-level `"watch"` config to your `package.json` and a "watch" script to
|
12 | your `"scripts"`:
|
13 |
|
14 | ```javascript
|
15 | {
|
16 | "watch": {
|
17 | "test": "{src,test}/*.js"
|
18 | },
|
19 | "scripts": {
|
20 | "test": "tape test/*.js",
|
21 | "watch": "npm-watch"
|
22 | }
|
23 | }
|
24 | ```
|
25 |
|
26 | Possibilty to watch for different tasks
|
27 |
|
28 | ```javascript
|
29 | {
|
30 | "watch": {
|
31 | "run_android": {
|
32 | "patterns": [
|
33 | "app"
|
34 | ],
|
35 | "extensions": "ts,html,scss",
|
36 | "quiet": false
|
37 | },
|
38 | "run_ios": {
|
39 | "patterns": [
|
40 | "app"
|
41 | ],
|
42 | "extensions": "ts,html,scss",
|
43 | "quiet": false
|
44 | }
|
45 | },
|
46 | "scripts": {
|
47 | "watch_android": "npm-watch run_android",
|
48 | "watch_ios": "npm-watch run_ios",
|
49 | "run_android": "tns run android --emulator",
|
50 | "run_ios": "tns run ios --emulator"
|
51 | }
|
52 | }
|
53 | ```
|
54 |
|
55 | The top level keys of the `"watch"` config should match the names of your `"scripts"`, and
|
56 | the values should be a glob pattern or array of glob patterns to watch.
|
57 |
|
58 | Also it is now possible to obtain a second parameter to define the script which should be run for watching and not watch all possible scripts at once.
|
59 |
|
60 | If you need to watch files with extensions other than those that `nodemon` watches [by default](https://github.com/remy/nodemon#specifying-extension-watch-list) (`.js`, `.coffee`, `.litcoffee`), you can set the value to an object with `patterns` and `extensions` keys. You can also add an `ignore` key (a list or a string) to ignore specific files. Finally, you can add a `quiet` flag to hide the script name in any output on stdout or stderr, or you can use the `inherit` flag to preserve the original's process stdout or stderr. You can enable `nodemon` [legacy watch](https://github.com/remy/nodemon#application-isnt-restarting) and specify the restart [delay](https://github.com/remy/nodemon#delaying-restarting) in milliseconds with the corresponding flags.
|
61 |
|
62 | > The `quiet` flag was changed from a `string` to a `boolean` in `0.1.5`. Backwards compatability will be kept for two patch versions.
|
63 |
|
64 | Use `runOnChangeOnly` to set the nodemon option [--on-change-only](https://github.com/remy/nodemon/blob/master/doc/cli/options.txt "--on-change-only"). Setting this to `true` tells nodemon to execute script on change only, not startup.
|
65 |
|
66 | ```javascript
|
67 | {
|
68 | "watch": {
|
69 | "test": {
|
70 | "patterns": ["src", "test"],
|
71 | "extensions": "js,jsx",
|
72 | "ignore": "src/vendor/external.min.js",
|
73 | "quiet": true,
|
74 | "legacyWatch": true,
|
75 | "delay": 2500,
|
76 | "runOnChangeOnly": false
|
77 | }
|
78 | },
|
79 | "scripts": {
|
80 | "test": "tape test/*.js"
|
81 | }
|
82 | }
|
83 | ```
|
84 |
|
85 | Start the watcher with `npm run watch` in a terminal, then edit some files:
|
86 |
|
87 | ```bash
|
88 | mkdir src test
|
89 | npm run watch &
|
90 | cat <<EOF > test/test-sum.js
|
91 | var test = require('tape')
|
92 | test('sum module', function (t) {
|
93 | var sum = require('../src/sum.js')
|
94 | t.ok(sum(1, 2), 3, "Sums appear correct")
|
95 | t.end()
|
96 | })
|
97 | EOF
|
98 | ```
|
99 |
|
100 | _(Feel free to use the editor of your choice, `cat` just makes for easy demos)_
|
101 |
|
102 | You should see that your tests ran automatically, and failed because `src/sum.js`
|
103 | is missing. Let's fix that:
|
104 |
|
105 | ```bash
|
106 | cat <<EOF > src/sum.js
|
107 | module.exports = function (a, b) {
|
108 | return 1
|
109 | }
|
110 | EOF
|
111 | ```
|
112 |
|
113 | Our tests will run again, and this time they *almost* work. Let's fix `sum.js`:
|
114 |
|
115 | ```bash
|
116 | cat <<EOF > src/sum.js
|
117 | module.exports = function (a, b) {
|
118 | return a + b
|
119 | }
|
120 | EOF
|
121 | ```
|
122 |
|
123 | Tests run *perfectly*, ship it to the enterprise!
|
124 |
|
125 | ### Options
|
126 |
|
127 | #### `patterns`
|
128 |
|
129 | Array of paths to watch
|
130 |
|
131 | #### `extensions`
|
132 |
|
133 | Array of file extensions to watch
|
134 |
|
135 | #### `ignore`
|
136 |
|
137 | String or array of paths to ignore
|
138 |
|
139 | #### `quiet`
|
140 |
|
141 | Boolean to hide the script name in any output on stdout and stderr
|
142 |
|
143 | #### `inherit`
|
144 |
|
145 | Boolean to preserve the original process' stdout and stderr
|
146 |
|
147 | #### `legacyWatch`
|
148 |
|
149 | Boolean to enable [legacy watch](https://github.com/remy/nodemon#application-isnt-restarting)
|
150 |
|
151 | #### `delay`
|
152 |
|
153 | Number of milliseconds to [delay](https://github.com/remy/nodemon#delaying-restarting) before checking for new files
|
154 |
|
155 | #### `clearBuffer`
|
156 |
|
157 | Boolean to clear the buffer after detecting a new change
|
158 |
|
159 | #### `verbose`
|
160 |
|
161 | Boolean to turn on the nodemons verbose mode
|
162 |
|
163 | #### `Ignore files`
|
164 |
|
165 | Add an `ignore` property to your `watch` object. The value of `ignore` can be a string if you only want to ignore
|
166 | a single glob:
|
167 |
|
168 | ```
|
169 | "watch": {
|
170 | "build": {
|
171 | "ignore": "build",
|
172 | ...
|
173 | }
|
174 | ...
|
175 | }
|
176 | ```
|
177 |
|
178 | Or an array if you want to ignore multiple globs:
|
179 |
|
180 | ```
|
181 | "watch": {
|
182 | "build": {
|
183 | "ignore": [
|
184 | "build",
|
185 | "node_modules"
|
186 | ],
|
187 | ...
|
188 | }
|
189 | ...
|
190 | }
|
191 | ```
|
192 |
|
193 | ## Acknowledgements
|
194 |
|
195 | This module does very little but run [`nodemon`](http://npm.im/nodemon) for you, all
|
196 | credit for the reliable file watching and process restarting should go to there.
|
197 |
|
198 | ## License
|
199 |
|
200 | MIT
|