Code Monkey home page Code Monkey logo

npm-watch's Introduction

npm-watch

Run scripts from package.json when files change.

Synopsis

Install it:

npm install npm-watch

Add a top-level "watch" config to your package.json and a "watch" script to your "scripts":

{
  "watch": {
    "test": "{src,test}/*.js"
  },
  "scripts": {
    "test": "tape test/*.js",
    "watch": "npm-watch"
  }
}

The keys of the "watch" config should match the names of your "scripts", and the values should be a glob pattern or array of glob patterns to watch.

If you need to watch files with extensions other than those that nodemon watches by default (.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.

The quiet flag was changed from a string to a boolean in 0.1.5. Backwards compatability will be kept for two patch versions.

{
  "watch": {
    "test": {
      "patterns": ["src", "test"],
      "extensions": "js,jsx",
      "ignore": "src/vendor/external.min.js",
      "quiet": true
    }
  },
  "scripts": {
    "test": "tape test/*.js"
  }
}

Start the watcher with npm run watch in a terminal, then edit some files:

mkdir src test
npm run watch &
cat <<EOF > test/test-sum.js
var test = require('tape')
test('sum module', function (t) {
  var sum = require('../src/sum.js')
  t.ok(sum(1, 2), 3, "Sums appear correct")
  t.end()
})
EOF

(Feel free to use the editor of your choice, cat just makes for easy demos)

You should see that your tests ran automatically, and failed because src/sum.js is missing. Let's fix that:

cat <<EOF > src/sum.js
module.exports = function (a, b)  {
  return 1
}
EOF

Our tests will run again, and this time they almost work. Let's fix sum.js:

cat <<EOF > src/sum.js
module.exports = function (a, b)  {
  return a + b
}
EOF

Tests run perfectly, ship it to the enterprise!

Acknowledgements

This module does very little but run nodemon for you, all credit for the reliable file watching and process restarting should go to there.

License

MIT

npm-watch's People

Contributors

alxndr avatar grncdr avatar hectormenendez avatar m-zuber avatar posva avatar stuartlangridge avatar

Watchers

 avatar  avatar

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.