• Stars
    star
    527
  • Rank 84,091 (Top 2 %)
  • Language
    JavaScript
  • Created almost 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

gulp + nodemon + convenience

gulp-nodemon

gulp + nodemon + convenience

Install

$ npm install --save-dev gulp-nodemon

Usage

Gulp-nodemon is almost exactly like regular nodemon, but it's made for use with gulp tasks.

nodemon([options])

Gulp-nodemon takes an options object just like the original.

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.

gulp.task('start', function (done) {
  nodemon({
    script: 'server.js'
  , ext: 'js html'
  , env: { 'NODE_ENV': 'development' }
  , done: done
  })
})

Synchronous Build Tasks

NOTE: This feature requires Node v0.12 because of child_process.spawnSync.

Gulp-nodemon can synchronously perform build tasks on restart.

{ tasks: [Array || Function(changedFiles)] }

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:

nodemon({
  script: 'index.js'
, tasks: ['browserify']
})

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.

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.

nodemon({
  script: './index.js'
, ext: 'js css'
, tasks: function (changedFiles) {
    var tasks = []
    if (!changedFiles) return tasks;
    changedFiles.forEach(function (file) {
      if (path.extname(file) === '.js' && !~tasks.indexOf('lint')) tasks.push('lint')
      if (path.extname(file) === '.css' && !~tasks.indexOf('cssmin')) tasks.push('cssmin')
    })
    return tasks
  }
})

Events

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.

.on([event], [Array || Function])

  1. [event] is an event name as a string. See nodemon events.
  2. [tasks] An array of gulp task names or a function to execute.

.emit([event])

  1. event is an event name as a string. See nodemon events.

Examples

Basic Usage

The following example will run your code with nodemon, lint it when you make changes, and log a message when nodemon runs it again.

// Gulpfile.js
var gulp = require('gulp')
  , nodemon = require('gulp-nodemon')
  , jshint = require('gulp-jshint')

gulp.task('lint', function () {
  gulp.src('./**/*.js')
    .pipe(jshint())
})

gulp.task('develop', function (done) {
  var stream = nodemon({ script: 'server.js'
          , ext: 'html js'
          , ignore: ['ignored.js']
          , tasks: ['lint'] })
          , done: done

  stream
      .on('restart', function () {
        console.log('restarted!')
      })
      .on('crash', function() {
        console.error('Application has crashed!\n')
         stream.emit('restart', 10)  // restart the server in 10 seconds
      })
})

You can also plug an external version or fork of nodemon

gulp.task('pluggable', function() {
  nodemon({ nodemon: require('nodemon'),
            script: 'server.js'})
})

Bunyan Logger integration

The 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:

gulp.task('run', ['default', 'watch'], function(done) {
    var nodemon = require('gulp-nodemon'),
        spawn   = require('child_process').spawn,
        bunyan

    nodemon({
        script: paths.server,
        ext:    'js json',
        ignore: [
            'var/',
            'node_modules/'
        ],
        watch:    [paths.etc, paths.src],
        stdout:   false,
        readable: false,
        done: done
    })
    .on('readable', function() {

        // free memory
        bunyan && bunyan.kill()

        bunyan = spawn('./node_modules/bunyan/bin/bunyan', [
            '--output', 'short',
            '--color'
        ])

        bunyan.stdout.pipe(process.stdout)
        bunyan.stderr.pipe(process.stderr)

        this.stdout.pipe(bunyan.stdin)
        this.stderr.pipe(bunyan.stdin)
    });
})

Using gulp-nodemon with React, Browserify, Babel, ES2015, etc.

Gulp-nodemon is made to work with the "groovy" new tools like Babel, JSX, and other JavaScript compilers/bundlers/transpilers.

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:

var gulp = require('gulp')
  , nodemon = require('gulp-nodemon')
  , babel = require('gulp-babel')
  , Cache = require('gulp-file-cache')

var cache = new Cache();

gulp.task('compile', function () {
  var stream = gulp.src('./src/**/*.js') // your ES2015 code
                   .pipe(cache.filter()) // remember files
                   .pipe(babel({ ... })) // compile new ones
                   .pipe(cache.cache()) // cache them
                   .pipe(gulp.dest('./dist')) // write them
  return stream // important for gulp-nodemon to wait for completion
})

gulp.task('watch', ['compile'], function (done) {
  var stream = nodemon({
                 script: 'dist/' // run ES5 code
               , watch: 'src' // watch ES2015 code
               , tasks: ['compile'] // compile synchronously onChange
               , done: done
               })

  return stream
})

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.

Using gulp-nodemon with browser-sync

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.

More Repositories

1

toy-chest-theme

A fun color scheme.
Vim Script
209
star
2

galileo

Search GitHub from your shell.
Ruby
161
star
3

witch

basically a ruby shim for terminal-notifier
Ruby
77
star
4

yugen

A collaborative text editor for minimalists.
JavaScript
67
star
5

TRAPCITY

A music visualizer for TRAPCITY.
JavaScript
36
star
6

glom

Command line package search.
Ruby
29
star
7

OpenHacker

A weekly competition for problem solvers.
Ruby
17
star
8

retinafy

A lightweight plugin to easily employ high resolution graphics on your website.
JavaScript
15
star
9

derivation-machine

Propositional calculus in Haskell
Haskell
11
star
10

ritz

Make your web app ritzy & sophisticated. You'll be schmoozing with the dames in no time.
9
star
11

Brains-with-Friends

A fast-paced dungeon crawler. Open source.
JavaScript
6
star
12

unfollow-notifications

DMs when you get unfollowed on Twitter.
JavaScript
6
star
13

gulp-soften

A gulp task that converts hard tabs to spaces.
JavaScript
4
star
14

karmen

A fork of the karmen window manager with support for docks and hidpi displays.
C
4
star
15

dotfiles

Emacs Lisp
4
star
16

earthmacs-rs

The old Rust version of the beautiful text editor.
Rust
3
star
17

redfinch.co

CSS
2
star
18

red-is-the-new-black

A red-black tree implementation in JavaScript.
JavaScript
2
star
19

infinitrecords.com

JavaScript
2
star
20

CETCH

CETCH (Code Execution Time CHallenge)
Go
2
star
21

amineliving.com

Aminรฉ (uh-mean-aye)
JavaScript
1
star
22

bwf-closure

A clone of the Google Closure library with soy tempting for Brains with Friends.
JavaScript
1
star
23

all-hail-nsa

blood. desire. animus.
1
star
24

belt

A utility library with standalone methods.
JavaScript
1
star
25

earthmacs

Configure emacs by building it from scratch.
Haskell
1
star
26

transientinsight.com

JavaScript
1
star
27

gojira

JavaScript
1
star
28

nocfgwm

A zero-configuation window manager written in Rust.
Rust
1
star
29

Molly

Make your DMX light react to music.
1
star
30

kepler

Logical operators for handlebars.js hail satan.
JavaScript
1
star
31

euler.lisp

Common Lisp
1
star
32

euler.hs

Haskell solutions to Project Euler problems.
Haskell
1
star
33

recipe-machine

Scheme
1
star
34

at2xxx

Pornography for pixel-density enthusiasts. (and just all other kinds of enthusiasts since my latest commits)
JavaScript
1
star
35

bwf-box2d

A clone of the Box2D physics library for brains with friends.
JavaScript
1
star
36

euler.rb

Ruby
1
star
37

kmp.scm

Scheme
1
star
38

mocking.js

Advanced AI experiment to mimic the thoughts of a Tweeter user
JavaScript
1
star
39

feldman-app

notation for real musicians
JavaScript
1
star
40

Canvas-Game-Example

A top-down zombie shooter with HTML5 Canvas. This project is being moved into a more long-term project which should be available shortly or never.
CoffeeScript
1
star
41

GeoPalz-Minecraft

Run a Minecraft server that encourages exercise.
Java
1
star
42

gulp-headless

[PRE-RELEASE]
JavaScript
1
star