Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a CLI #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions lib/src/bin/nsfw.js
Original file line number Diff line number Diff line change
@@ -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 <pattern> [<pattern>...] [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);
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": "lib/src/bin/nsfw.js",
"files": [
"nsfw.js",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line is now irrelevant.

"lib",
"src",
"includes",
Expand Down