Skip to content

Commit 624665a

Browse files
committed
v5.0.0 / 2020-07-08
1 parent a429a0c commit 624665a

14 files changed

Lines changed: 228 additions & 141 deletions

File tree

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"extends": [
66
"airbnb-base/legacy"
77
],
8+
"parserOptions": {
9+
"ecmaVersion": 9
10+
},
811
"rules": {
912
"comma-dangle": [2, "never"],
1013
"consistent-return": 0,

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# node-dev
22

3+
## v5.0.0 / 2020-07-08
4+
5+
- Remove `--all-deps` and `--no-deps` CLI options, use `--deps=-1` or `--deps=0` respectively
6+
- Unify `cli` and `cfg` logic to ensure CLI always overrides config files
7+
- Load order for config files now matches what is in the `README`
8+
- Add tests for notify, CLI should override config files
9+
- All config now have clear default values
10+
- Use more ES6 code
11+
- Rename `resolveMain.js` to `resolve-main.js`
12+
313
## v4.3.0 / 2020-07-03
414

515
- Enable `--notify` by default and add tests

README.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,20 @@ node-dev foo.js
3535
There are a couple of command line options that can be used to control which
3636
files are watched and what happens when they change:
3737

38-
* `--no-deps` Watch only the project's own files and linked modules (via `npm link`)
39-
* `--all-deps` Watch the whole dependency tree
40-
* `--respawn` Keep watching for changes after the script has exited
41-
* `--dedupe` [Dedupe dynamically](https://www.npmjs.org/package/dynamic-dedupe)
42-
* `--graceful_ipc <msg>` Send 'msg' as an IPC message instead of SIGTERM for restart/shutdown
43-
* `--poll` Force polling for file changes (Caution! CPU-heavy!)
38+
* `--clear` - Clear the screen on restart
39+
* `--dedupe` - [Dedupe dynamically](https://www.npmjs.org/package/dynamic-dedupe)
40+
* `--deps`:
41+
* -1 - Watch the whole dependency tree
42+
* 0 - Watch only the project's own files and linked modules (via `npm link`)
43+
* 1 (_Default_) - Watch all first level dependencies
44+
* `--fork` - Hook into child_process.fork
45+
* `--graceful_ipc <msg>` - Send 'msg' as an IPC message instead of SIGTERM for restart/shutdown
46+
* `--ignore` - A file whose changes should not cause a restart
47+
* `--notify` - Display desktop notifications
48+
* `--poll` - Force polling for file changes (Caution! CPU-heavy!)
49+
* `--respawn` - Keep watching for changes after the script has exited
50+
* `--timestamp` - The timestamp format to use for logging restarts
51+
* `--vm` - Load files using Node's VM
4452

4553
By default node-dev will watch all first-level dependencies, i.e. the ones in
4654
the project's `node_modules`folder.
@@ -76,13 +84,16 @@ Usually node-dev doesn't require any configuration at all, but there are some
7684
options you can set to tweak its behaviour:
7785

7886
* `clear` – Whether to clear the screen upon restarts. _Default:_ `false`
87+
* `dedupe` – Whether modules should by [dynamically deduped](https://www.npmjs.org/package/dynamic-dedupe). _Default:_ `false`
88+
* `deps` – How many levels of dependencies should be watched. _Default:_ `1`
89+
* `fork` – Whether to hook into [child_process.fork](http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_fork_modulepath_args_options) (required for [clustered](http://nodejs.org/docs/latest/api/cluster.html) programs). _Default:_ `true`
90+
* `graceful_ipc` - Send the argument provided as an IPC message instead of SIGTERM during restart events. _Default:_ `""` (off)
91+
* `ignore` - A single file or an array of files to ignore. _Default:_ `[]`
7992
* `notify` – Whether to display desktop notifications. _Default:_ `true`
93+
* `poll` - Force polling for file changes, this can be CPU-heavy. _Default:_ `false`
94+
* `respawn` - Keep watching for changes after the script has exited. _Default:_ `false`
8095
* `timestamp` – The timestamp format to use for logging restarts. _Default:_ `"HH:MM:ss"`
8196
* `vm` – Whether to watch files loaded via Node's [VM](http://nodejs.org/docs/latest/api/vm.html) module. _Default:_ `true`
82-
* `fork` – Whether to hook into [child_process.fork](http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_fork_modulepath_args_options) (required for [clustered](http://nodejs.org/docs/latest/api/cluster.html) programs). _Default:_ `true`
83-
* `deps` – How many levels of dependencies should be watched. _Default:_ `1`
84-
* `dedupe` – Whether modules should by [dynamically deduped](https://www.npmjs.org/package/dynamic-dedupe). _Default:_ `false`
85-
* `graceful_ipc` - Send the argument provided as an IPC message instead of SIGTERM during restart events. _Default:_ `""` (off)
8697

8798
Upon startup node-dev looks for a `.node-dev.json` file in the following directories:
8899
* user's HOME directory

lib/cfg.js

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,44 @@
1-
var fs = require('fs');
2-
var path = require('path');
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const resolveMain = require('./resolve-main');
5+
6+
const defaultConfig = {
7+
clear: false,
8+
dedupe: false,
9+
deps: 1,
10+
extensions: {
11+
coffee: 'coffeescript/register',
12+
ls: 'LiveScript'
13+
},
14+
fork: true,
15+
graceful_ipc: '',
16+
ignore: [],
17+
notify: true,
18+
poll: false,
19+
respawn: false,
20+
timestamp: 'HH:MM:ss',
21+
vm: true
22+
};
323

424
function read(dir) {
5-
var f = path.resolve(dir, '.node-dev.json');
25+
const f = path.resolve(dir, '.node-dev.json');
626
return fs.existsSync(f) ? JSON.parse(fs.readFileSync(f)) : {};
727
}
828

9-
function resolvePath(unresolvedPath) {
10-
return path.resolve(process.cwd(), unresolvedPath);
11-
}
12-
13-
module.exports = function (main, opts) {
14-
var dir = main ? path.dirname(main) : '.';
15-
var c = Object.assign(read(process.cwd()), read(dir));
16-
17-
/* eslint-disable no-proto */
18-
c.__proto__ = read(process.env.HOME || process.env.USERPROFILE);
29+
function getConfig(script) {
30+
const main = resolveMain(script);
31+
const dir = main ? path.dirname(main) : '.';
1932

20-
// Truthy == --all-deps, false: one level of deps
21-
if (typeof c.deps !== 'number') c.deps = c.deps ? -1 : 1;
22-
23-
if (opts) {
24-
// Overwrite with CLI opts ...
25-
if (opts.allDeps) c.deps = -1;
26-
if (!opts.deps) c.deps = 0;
27-
if (opts.dedupe) c.dedupe = true;
28-
if (opts.graceful_ipc) c.graceful_ipc = opts.graceful_ipc;
29-
if (opts.respawn) c.respawn = true;
30-
c.notify = opts.notify;
31-
}
32-
33-
var ignore = (c.ignore || []).map(resolvePath);
33+
return Object.assign(
34+
defaultConfig,
35+
read(process.env.HOME || process.env.USERPROFILE),
36+
read(process.cwd()),
37+
read(dir)
38+
);
39+
}
3440

35-
return {
36-
vm: c.vm !== false,
37-
fork: c.fork !== false,
38-
notify: c.notify,
39-
deps: c.deps,
40-
timestamp: c.timestamp || (c.timestamp !== false && 'HH:MM:ss'),
41-
clear: !!c.clear,
42-
dedupe: !!c.dedupe,
43-
graceful_ipc: c.graceful_ipc,
44-
ignore: ignore,
45-
respawn: c.respawn || false,
46-
extensions: c.extensions || {
47-
coffee: 'coffeescript/register',
48-
ls: 'LiveScript'
49-
}
50-
};
41+
module.exports = {
42+
defaultConfig,
43+
getConfig
5144
};

lib/cli.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
const minimist = require('minimist');
2+
const path = require('path');
3+
4+
const { defaultConfig, getConfig } = require('./cfg');
5+
6+
const configKeys = Object.keys(defaultConfig);
7+
8+
function resolvePath(unresolvedPath) {
9+
return path.resolve(process.cwd(), unresolvedPath);
10+
}
211

312
function getFirstNonOptionArgIndex(args) {
413
for (let i = 2; i < args.length; i += 1) {
514
if (args[i][0] != '-') return i;
615
}
16+
717
return args.length;
818
}
919

1020
function removeValueArgs(args, names) {
1121
let i = 0;
1222
let removed = [];
23+
1324
while (i < args.length) {
1425
if (names.includes(args[i])) {
1526
removed = removed.concat(args.splice(i, 2));
1627
} else {
1728
i += 1;
1829
}
1930
}
31+
2032
return removed;
2133
}
2234

@@ -29,14 +41,22 @@ module.exports = function (argv) {
2941
const devArgs = argv.slice(2, scriptIndex);
3042

3143
const opts = minimist(devArgs, {
32-
boolean: ['all-deps', 'deps', 'dedupe', 'poll', 'respawn', 'notify'],
33-
string: ['graceful_ipc'],
34-
default: { deps: true, notify: true, graceful_ipc: '' },
44+
boolean: ['clear', 'dedupe', 'fork', 'notify', 'poll', 'respawn', 'vm'],
45+
string: ['graceful_ipc', 'ignore', 'timestamp'],
46+
default: getConfig(script),
3547
unknown: function (arg) {
48+
const argKeys = Object.keys(minimist([arg]));
49+
50+
if (configKeys.some(k => argKeys.includes(k))) {
51+
return true;
52+
}
53+
3654
nodeArgs.push(arg);
3755
}
3856
});
3957

58+
opts.ignore = [...Array.isArray(opts.ignore) ? opts.ignore : [opts.ignore]].map(resolvePath);
59+
4060
return {
4161
script,
4262
scriptArgs,

lib/hook.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
var vm = require('vm');
2-
3-
module.exports = function (cfg, wrapper, callback) {
1+
const vm = require('vm');
42

3+
module.exports = function (patchVM, wrapper, callback) {
54
// Hook into Node's `require(...)`
65
updateHooks();
76

87
// Patch the vm module to watch files executed via one of these methods:
9-
if (cfg.vm) {
8+
if (patchVM) {
109
patch(vm, 'createScript', 1);
1110
patch(vm, 'runInThisContext', 1);
1211
patch(vm, 'runInNewContext', 2);
@@ -18,11 +17,11 @@ module.exports = function (cfg, wrapper, callback) {
1817
* index.
1918
*/
2019
function patch(obj, method, optionsArgIndex) {
21-
var orig = obj[method];
20+
const orig = obj[method];
2221
if (!orig) return;
2322
obj[method] = function () {
24-
var opts = arguments[optionsArgIndex];
25-
var file = null;
23+
const opts = arguments[optionsArgIndex];
24+
let file = null;
2625
if (opts) {
2726
file = typeof opts == 'string' ? opts : opts.filename;
2827
}
@@ -36,7 +35,7 @@ module.exports = function (cfg, wrapper, callback) {
3635
*/
3736
function updateHooks() {
3837
Object.keys(require.extensions).forEach(function (ext) {
39-
var fn = require.extensions[ext];
38+
const fn = require.extensions[ext];
4039
if (typeof fn === 'function' && fn.name !== 'nodeDevHook') {
4140
require.extensions[ext] = createHook(fn);
4241
}

0 commit comments

Comments
 (0)