From f660bc3d61c3ff5134ecb43b8fa826bc36afb302 Mon Sep 17 00:00:00 2001 From: Bruno Michel Date: Wed, 24 Oct 2018 14:58:32 +0200 Subject: [PATCH 1/2] Add a CLI The CLI can be used like this: $ nsfw path/to/watch It should help for testing nsfw and reporting issues. --- nsfw.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 ++ 2 files changed, 80 insertions(+) create mode 100755 nsfw.js diff --git a/nsfw.js b/nsfw.js new file mode 100755 index 00000000..c4243837 --- /dev/null +++ b/nsfw.js @@ -0,0 +1,78 @@ +#!/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 [...] [options]'); + console.log(' -h, --help\tShow help'); + console.log(' -v, --verbose\tMake output more verbose'); +} + +function start (dirs, verbose) { + 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); diff --git a/package.json b/package.json index 6b3285ce..96a65ec1 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,9 @@ "bugs": { "url": "https://github.com/axosoft/node-simple-file-watcher/issues" }, + "bin": "nsfw.js", "files": [ + "nsfw.js", "lib", "src", "includes", From c9a590994ee910dadc689cbe3547871604f3de05 Mon Sep 17 00:00:00 2001 From: Bruno Michel Date: Thu, 25 Oct 2018 09:02:55 +0200 Subject: [PATCH 2/2] Move the CLI to the lib/src/bin directory --- nsfw.js => lib/src/bin/nsfw.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename nsfw.js => lib/src/bin/nsfw.js (97%) diff --git a/nsfw.js b/lib/src/bin/nsfw.js similarity index 97% rename from nsfw.js rename to lib/src/bin/nsfw.js index c4243837..54d0b645 100755 --- a/nsfw.js +++ b/lib/src/bin/nsfw.js @@ -2,7 +2,7 @@ /* eslint no-console: 0 */ const path = require('path'); -const nsfw = require('./lib/src/index.js'); +const nsfw = require('../index.js'); function usage () { console.log('Usage: nsfw [...] [options]'); diff --git a/package.json b/package.json index 96a65ec1..cd9d8749 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "bugs": { "url": "https://github.com/axosoft/node-simple-file-watcher/issues" }, - "bin": "nsfw.js", + "bin": "lib/src/bin/nsfw.js", "files": [ "nsfw.js", "lib",