1 | gulp-nodemon
|
2 | ===========
|
3 |
|
4 | gulp + nodemon + convenience
|
5 |
|
6 | ## Install
|
7 |
|
8 | ```bash
|
9 | $ npm install --save-dev gulp-nodemon
|
10 | ```
|
11 |
|
12 | ## Usage
|
13 |
|
14 | Gulp-nodemon is almost exactly like regular nodemon, but it's made for use with gulp tasks.
|
15 |
|
16 | ### **nodemon([options])**
|
17 |
|
18 | Gulp-nodemon takes an options object [just like the original](https://github.com/remy/nodemon#config-files).
|
19 |
|
20 | Example below will start `server.js` in `development` mode and watch for changes, as well as watch all `.html` and `.js` files in the directory.
|
21 | ```js
|
22 | gulp.task('start', function (done) {
|
23 | nodemon({
|
24 | script: 'server.js'
|
25 | , ext: 'js html'
|
26 | , env: { 'NODE_ENV': 'development' }
|
27 | , done: done
|
28 | })
|
29 | })
|
30 | ```
|
31 |
|
32 | ## Synchronous Build Tasks
|
33 |
|
34 | *NOTE: This feature requires Node v0.12 because of `child_process.spawnSync`.*
|
35 |
|
36 | Gulp-nodemon can synchronously perform build tasks on restart.
|
37 |
|
38 | ### **{ tasks: [Array || Function(changedFiles)] }**
|
39 |
|
40 | If you want to lint your code when you make changes that's easy to do with a simple event. But what if you need to wait while your project re-builds before you start it up again? This isn't possible with vanilla nodemon, and can be tedious to implement yourself, but it's easy with gulp-nodemon:
|
41 | ```js
|
42 | nodemon({
|
43 | script: 'index.js'
|
44 | , tasks: ['browserify']
|
45 | })
|
46 | ```
|
47 |
|
48 | What if you want to decouple your build processes by language? Or even by file? Easy, just set the `tasks` option to a function. Gulp-nodemon will pass you the list of changed files and it'll let you return a list of tasks you want run.
|
49 |
|
50 | *NOTE:* If you manually restart the server (`rs`) this function will receive a `changedFiles === undefined` so check it and return the `tasks` because it expects an array to be returned.
|
51 |
|
52 | ```js
|
53 | nodemon({
|
54 | script: './index.js'
|
55 | , ext: 'js css'
|
56 | , tasks: function (changedFiles) {
|
57 | var tasks = []
|
58 | if (!changedFiles) return tasks;
|
59 | changedFiles.forEach(function (file) {
|
60 | if (path.extname(file) === '.js' && !~tasks.indexOf('lint')) tasks.push('lint')
|
61 | if (path.extname(file) === '.css' && !~tasks.indexOf('cssmin')) tasks.push('cssmin')
|
62 | })
|
63 | return tasks
|
64 | }
|
65 | })
|
66 | ```
|
67 |
|
68 | ## Events
|
69 |
|
70 | gulp-nodemon returns a stream just like any other NodeJS stream, **except for the `on` method**, which conveniently accepts gulp task names in addition to the typical function.
|
71 |
|
72 | ### **.on([event], [Array || Function])**
|
73 |
|
74 | 1. `[event]` is an event name as a string. See [nodemon events](https://github.com/remy/nodemon/blob/master/doc/events.md).
|
75 | 2. `[tasks]` An array of gulp task names or a function to execute.
|
76 |
|
77 | ### **.emit([event])**
|
78 | 1. `event` is an event name as a string. See [nodemon events](https://github.com/remy/nodemon/blob/master/doc/events.md#using-nodemon-events).
|
79 |
|
80 | ## Examples
|
81 |
|
82 | ### Basic Usage
|
83 |
|
84 | The following example will run your code with nodemon, lint it when you make changes, and log a message when nodemon runs it again.
|
85 |
|
86 | ```js
|
87 | // Gulpfile.js
|
88 | var gulp = require('gulp')
|
89 | , nodemon = require('gulp-nodemon')
|
90 | , jshint = require('gulp-jshint')
|
91 |
|
92 | gulp.task('lint', function () {
|
93 | gulp.src('./**/*.js')
|
94 | .pipe(jshint())
|
95 | })
|
96 |
|
97 | gulp.task('develop', function (done) {
|
98 | var stream = nodemon({ script: 'server.js'
|
99 | , ext: 'html js'
|
100 | , ignore: ['ignored.js']
|
101 | , tasks: ['lint'] })
|
102 | , done: done
|
103 |
|
104 | stream
|
105 | .on('restart', function () {
|
106 | console.log('restarted!')
|
107 | })
|
108 | .on('crash', function() {
|
109 | console.error('Application has crashed!\n')
|
110 | stream.emit('restart', 10) // restart the server in 10 seconds
|
111 | })
|
112 | })
|
113 | ```
|
114 |
|
115 | _**You can also plug an external version or fork of nodemon**_
|
116 | ```js
|
117 | gulp.task('pluggable', function() {
|
118 | nodemon({ nodemon: require('nodemon'),
|
119 | script: 'server.js'})
|
120 | })
|
121 | ```
|
122 |
|
123 | ### Bunyan Logger integration
|
124 |
|
125 | The [bunyan](https://github.com/trentm/node-bunyan/) logger includes a `bunyan` script that beautifies JSON logging when piped to it. Here's how you can you can pipe your output to `bunyan` when using `gulp-nodemon`:
|
126 |
|
127 | ```js
|
128 | gulp.task('run', ['default', 'watch'], function(done) {
|
129 | var nodemon = require('gulp-nodemon'),
|
130 | spawn = require('child_process').spawn,
|
131 | bunyan
|
132 |
|
133 | nodemon({
|
134 | script: paths.server,
|
135 | ext: 'js json',
|
136 | ignore: [
|
137 | 'var/',
|
138 | 'node_modules/'
|
139 | ],
|
140 | watch: [paths.etc, paths.src],
|
141 | stdout: false,
|
142 | readable: false,
|
143 | done: done
|
144 | })
|
145 | .on('readable', function() {
|
146 |
|
147 | // free memory
|
148 | bunyan && bunyan.kill()
|
149 |
|
150 | bunyan = spawn('./node_modules/bunyan/bin/bunyan', [
|
151 | '--output', 'short',
|
152 | '--color'
|
153 | ])
|
154 |
|
155 | bunyan.stdout.pipe(process.stdout)
|
156 | bunyan.stderr.pipe(process.stderr)
|
157 |
|
158 | this.stdout.pipe(bunyan.stdin)
|
159 | this.stderr.pipe(bunyan.stdin)
|
160 | });
|
161 | })
|
162 | ```
|
163 |
|
164 | ## Using `gulp-nodemon` with React, Browserify, Babel, ES2015, etc.
|
165 |
|
166 | Gulp-nodemon is made to work with the "groovy" new tools like Babel, JSX, and other JavaScript compilers/bundlers/transpilers.
|
167 |
|
168 | In gulp-nodemon land, you'll want one task for compilation that uses an on-disk cache (e.g. `gulp-file-cache`, `gulp-cache-money`) along with your bundler (e.g. `gulp-babel`, `gulp-react`, etc.). Then you'll put `nodemon({})` in another task and pass the entire compile task in your config:
|
169 |
|
170 | ```js
|
171 | var gulp = require('gulp')
|
172 | , nodemon = require('gulp-nodemon')
|
173 | , babel = require('gulp-babel')
|
174 | , Cache = require('gulp-file-cache')
|
175 |
|
176 | var cache = new Cache();
|
177 |
|
178 | gulp.task('compile', function () {
|
179 | var stream = gulp.src('./src/**/*.js') // your ES2015 code
|
180 | .pipe(cache.filter()) // remember files
|
181 | .pipe(babel({ ... })) // compile new ones
|
182 | .pipe(cache.cache()) // cache them
|
183 | .pipe(gulp.dest('./dist')) // write them
|
184 | return stream // important for gulp-nodemon to wait for completion
|
185 | })
|
186 |
|
187 | gulp.task('watch', ['compile'], function (done) {
|
188 | var stream = nodemon({
|
189 | script: 'dist/' // run ES5 code
|
190 | , watch: 'src' // watch ES2015 code
|
191 | , tasks: ['compile'] // compile synchronously onChange
|
192 | , done: done
|
193 | })
|
194 |
|
195 | return stream
|
196 | })
|
197 | ```
|
198 |
|
199 | The cache keeps your development flow moving quickly and the `return stream` line ensure that your tasks get run in order. If you want them to run async, just remove that line.
|
200 |
|
201 | ## Using `gulp-nodemon` with `browser-sync`
|
202 |
|
203 | Some people want to use `browser-sync`. That's totally fine, just start browser sync in the same task as `nodemon({})` and use gulp-nodemon's `.on('start', function () {})` to trigger browser-sync. Don't use the `.on('restart')` event because it will fire before your app is up and running.
|