Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
Merged
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
71 changes: 71 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env node

const path = require('path')
const watcher = require('./index')

function usage () {
console.log('Usage: watcher <pattern> [<pattern>...] [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)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down