Skip to content

Commit b4fdcfb

Browse files
committed
Add a CLI
The CLI can be used like this: $ nsfw path/to/watch It should help for testing nsfw and reporting issues.
1 parent a64ce69 commit b4fdcfb

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

nsfw.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
/* eslint no-console: 0 */
3+
4+
const path = require('path');
5+
const nsfw = require('./lib/src/index.js');
6+
7+
function usage () {
8+
console.log('Usage: nsfw <pattern> [<pattern>...] [options]');
9+
console.log(' -h, --help\tShow help');
10+
console.log(' -v, --verbose\tMake output more verbose');
11+
}
12+
13+
function start (dirs, verbose) {
14+
console.log('verbose', verbose);
15+
console.log('dirs', dirs);
16+
17+
const options = {
18+
errorCallback: function (err) {
19+
console.error('Error:', err);
20+
}
21+
};
22+
23+
const eventCallback = function (events) {
24+
events.forEach(function (event) {
25+
switch (event.action) {
26+
case nsfw.actions.CREATED:
27+
console.log('Created:', path.join(event.directory, event.file));
28+
break;
29+
case nsfw.actions.DELETED:
30+
console.log('Deleted:', path.join(event.directory, event.file));
31+
break;
32+
case nsfw.actions.MODIFIED:
33+
if (verbose) {
34+
console.log('Modified:', path.join(event.directory, event.file));
35+
}
36+
break;
37+
case nsfw.actions.RENAMED:
38+
console.log('Renamed:', path.join(event.directory, event.oldFile), '→', event.newFile);
39+
break;
40+
}
41+
});
42+
};
43+
44+
dirs.forEach(function (dir) {
45+
nsfw(dir, eventCallback, options).then(function (watcher) {
46+
if (verbose) {
47+
console.log('Watching', dir);
48+
}
49+
return watcher.start();
50+
});
51+
});
52+
}
53+
54+
function main (argv) {
55+
const dirs = [];
56+
let verbose = false;
57+
58+
argv.forEach(function (arg, i) {
59+
if (i === 0) {
60+
return;
61+
}
62+
if (i === 1 && path.basename(argv[0]) === 'node') {
63+
return;
64+
}
65+
if (arg === '-h' || arg === '--help') {
66+
return usage();
67+
} else if (arg === '-v' || arg === '--verbose') {
68+
verbose = true;
69+
} else {
70+
dirs.push(arg);
71+
}
72+
});
73+
74+
if (dirs.length === 0) {
75+
return usage();
76+
}
77+
78+
start(dirs, verbose);
79+
}
80+
81+
main(process.argv);

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
"bugs": {
2020
"url": "https://github.com/axosoft/node-simple-file-watcher/issues"
2121
},
22+
"bin": "nsfw.js",
2223
"files": [
24+
"nsfw.js",
2325
"lib",
2426
"src",
2527
"includes",

0 commit comments

Comments
 (0)