npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

watch-npm

v1.0.1

Published

Run scripts from package.json when files change

Downloads

7

Readme

watch-npm

A fork of npm-watch that keeps vulnerabilities in check

Run scripts from package.json when files change

Installation

npm i watch-npm --save-dev
yarn add watch-npm -D

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": "watch-npm"
  }
}

Watching different tasks

{
  "watch": {
    "run_android": {
      "patterns": ["app"],
      "extensions": "ts,html,scss",
      "quiet": false
    },
    "run_ios": {
      "patterns": ["app"],
      "extensions": "ts,html,scss",
      "quiet": false
    }
  },
  "scripts": {
    "watch_android": "watch-npm run_android",
    "watch_ios": "watch-npm run_ios",
    "run_android": "tns run android --emulator",
    "run_ios": "tns run ios --emulator"
  }
}

The top level 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.

It's 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.

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. You can enable nodemon legacy watch and specify the restart delay in milliseconds with the corresponding flags.

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

Use runOnChangeOnly to set the nodemon option --on-change-only. Setting this to true tells nodemon to execute script on change only, not startup.

{
  "watch": {
    "test": {
      "patterns": ["src", "test"],
      "extensions": "js,jsx",
      "ignore": "src/vendor/external.min.js",
      "quiet": true,
      "legacyWatch": true,
      "delay": 2500,
      "runOnChangeOnly": false
    }
  },
  "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!

Options

patterns

Array of paths to watch

extensions

Array of file extensions to watch

ignore

String or array of paths to ignore

quiet

Boolean to hide the script name in any output on stdout and stderr

inherit

Boolean to preserve the original process' stdout and stderr

legacyWatch

Boolean to enable legacy watch

delay

Number of milliseconds to delay before checking for new files

clearBuffer

Boolean to clear the buffer after detecting a new change

verbose

Boolean to turn on the nodemons verbose mode

silent

Boolean to turn on nodemons silent (quiet) mode Silent was used as we already had an existing flag called quiet. This may change in a future release

Ignore files

Add an ignore property to your watch object. The value of ignore can be a string if you only want to ignore a single glob:

"watch": {
  "build": {
    "ignore": "build",
    ...
  }
  ...
}

Or an array if you want to ignore multiple globs:

"watch": {
  "build": {
    "ignore": [
      "build",
      "node_modules"
    ],
    ...
  }
  ...
}

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.

Thanks for doing all the heavy lifting npm-watch

License

MIT