Skip to content

Commit f660bc3

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 f660bc3

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

nsfw.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
const options = {
15+
errorCallback: function (err) {
16+
console.error('Error:', err);
17+
}
18+
};
19+
20+
const eventCallback = function (events) {
21+
events.forEach(function (event) {
22+
switch (event.action) {
23+
case nsfw.actions.CREATED:
24+
console.log('Created:', path.join(event.directory, event.file));
25+
break;
26+
case nsfw.actions.DELETED:
27+
console.log('Deleted:', path.join(event.directory, event.file));
28+
break;
29+
case nsfw.actions.MODIFIED:
30+
if (verbose) {
31+
console.log('Modified:', path.join(event.directory, event.file));
32+
}
33+
break;
34+
case nsfw.actions.RENAMED:
35+
console.log('Renamed:', path.join(event.directory, event.oldFile), '→', event.newFile);
36+
break;
37+
}
38+
});
39+
};
40+
41+
dirs.forEach(function (dir) {
42+
nsfw(dir, eventCallback, options).then(function (watcher) {
43+
if (verbose) {
44+
console.log('Watching', dir);
45+
}
46+
return watcher.start();
47+
});
48+
});
49+
}
50+
51+
function main (argv) {
52+
const dirs = [];
53+
let verbose = false;
54+
55+
argv.forEach(function (arg, i) {
56+
if (i === 0) {
57+
return;
58+
}
59+
if (i === 1 && path.basename(argv[0]) === 'node') {
60+
return;
61+
}
62+
if (arg === '-h' || arg === '--help') {
63+
return usage();
64+
} else if (arg === '-v' || arg === '--verbose') {
65+
verbose = true;
66+
} else {
67+
dirs.push(arg);
68+
}
69+
});
70+
71+
if (dirs.length === 0) {
72+
return usage();
73+
}
74+
75+
start(dirs, verbose);
76+
}
77+
78+
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)