From e66c60ecdbc9fdb4d1aa58bb75b34146b85123f2 Mon Sep 17 00:00:00 2001 From: Bruno Michel Date: Wed, 24 Oct 2018 17:37:58 +0200 Subject: [PATCH 1/2] Add a CLI The CLI can be used like this: $ watcher path/to/watch It should help for testing @atom/watcher and reporting issues. --- lib/cli.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 2 files changed, 72 insertions(+) create mode 100755 lib/cli.js diff --git a/lib/cli.js b/lib/cli.js new file mode 100755 index 00000000..87bc255b --- /dev/null +++ b/lib/cli.js @@ -0,0 +1,71 @@ +#!/usr/bin/env node + +const path = require('path') +const watcher = require('./index') + +function usage () { + console.log('Usage: watcher [...] [options]') + console.log(' -h, --help\tShow help') + console.log(' -v, --verbose\tMake output more verbose') +} + +function start (dirs, verbose) { + const options = { recursive: true } + + const eventCallback = events => { + for (const event of events) { + if (event.action === 'modified' && !verbose) { + return + } else if (event.action === 'renamed') { + console.log( + `${event.action} ${event.kind}: ${event.oldPath} → ${event.path}` + ) + } else { + console.log(`${event.action} ${event.kind}: ${event.path}`) + } + } + } + + for (const dir of dirs) { + watcher + .watchPath(dir, options, eventCallback) + .then(w => { + if (verbose) { + console.log('Watching', dir) + } + w.onDidError(err => console.error('Error:', err)) + }) + .catch(err => { + console.error('Error:', err) + }) + } +} + +function main (argv) { + const dirs = [] + let verbose = false + + argv.forEach((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 3d3ecec3..94a7f553 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.0.8", "description": "Atom filesystem watcher", "main": "lib/index.js", + "bin": "lib/cli.js", "scripts": { "install": "node-pre-gyp install --fallback-to-build", "publish": "node-pre-gyp package && node-pre-gyp-github publish --release", From 7da5b7e6a54b942e863ff31ae1487795bf7c6345 Mon Sep 17 00:00:00 2001 From: Bruno Michel Date: Wed, 24 Oct 2018 18:37:41 +0200 Subject: [PATCH 2/2] Explain the CLI in the README --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 786dd15c..570c549c 100644 --- a/README.md +++ b/README.md @@ -152,3 +152,20 @@ const watcher = await watchPath('/var/log', {}, () => {}) watcher.dispose() ``` + +## CLI + +It's possible to call `@atom/watcher` from the command-line, like this: + +```sh +$ watcher /path/to/watch +``` + +Example: + +``` +created directory: /path/to/watch/foo +deleted directory: /path/to/watch/foo +``` + +It can be useful for testing the watcher and to describe a scenario when reporting an issue.