diff --git a/lib/src/bin/nsfw.js b/lib/src/bin/nsfw.js new file mode 100755 index 00000000..54d0b645 --- /dev/null +++ b/lib/src/bin/nsfw.js @@ -0,0 +1,78 @@ +#!/usr/bin/env node +/* eslint no-console: 0 */ + +const path = require('path'); +const nsfw = require('../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..cd9d8749 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,9 @@ "bugs": { "url": "https://github.com/axosoft/node-simple-file-watcher/issues" }, + "bin": "lib/src/bin/nsfw.js", "files": [ + "nsfw.js", "lib", "src", "includes",