Skip to content

Commit

Permalink
Add a CLI
Browse files Browse the repository at this point in the history
The CLI can be used like this:

    $ nsfw path/to/watch

It should help for testing nsfw and reporting issues.
  • Loading branch information
nono committed Oct 24, 2018
1 parent a64ce69 commit b4fdcfb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
81 changes: 81 additions & 0 deletions nsfw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node
/* eslint no-console: 0 */

const path = require('path');
const nsfw = require('./lib/src/index.js');

function usage () {
console.log('Usage: nsfw <pattern> [<pattern>...] [options]');
console.log(' -h, --help\tShow help');
console.log(' -v, --verbose\tMake output more verbose');
}

function start (dirs, verbose) {
console.log('verbose', verbose);
console.log('dirs', dirs);

const options = {
errorCallback: function (err) {
console.error('Error:', err);
}
};

const eventCallback = function (events) {
events.forEach(function (event) {
switch (event.action) {
case nsfw.actions.CREATED:
console.log('Created:', path.join(event.directory, event.file));
break;
case nsfw.actions.DELETED:
console.log('Deleted:', path.join(event.directory, event.file));
break;
case nsfw.actions.MODIFIED:
if (verbose) {
console.log('Modified:', path.join(event.directory, event.file));
}
break;
case nsfw.actions.RENAMED:
console.log('Renamed:', path.join(event.directory, event.oldFile), '→', event.newFile);
break;
}
});
};

dirs.forEach(function (dir) {
nsfw(dir, eventCallback, options).then(function (watcher) {
if (verbose) {
console.log('Watching', dir);
}
return watcher.start();
});
});
}

function main (argv) {
const dirs = [];
let verbose = false;

argv.forEach(function (arg, i) {
if (i === 0) {
return;
}
if (i === 1 && path.basename(argv[0]) === 'node') {
return;
}
if (arg === '-h' || arg === '--help') {
return usage();
} else if (arg === '-v' || arg === '--verbose') {
verbose = true;
} else {
dirs.push(arg);
}
});

if (dirs.length === 0) {
return usage();
}

start(dirs, verbose);
}

main(process.argv);
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"bugs": {
"url": "https://github.com/axosoft/node-simple-file-watcher/issues"
},
"bin": "nsfw.js",
"files": [
"nsfw.js",
"lib",
"src",
"includes",
Expand Down

0 comments on commit b4fdcfb

Please sign in to comment.