forked from Qard/onchange
/
cli.js
executable file
·95 lines (83 loc) · 2.28 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
var onchange = require('./')
var arrify = require('arrify')
var ignore = require('ignore')
var fs = require('fs')
var path = require('path')
// Parse argv with minimist...it's easier this way.
var argv = require('minimist')(process.argv.slice(2), {
'--': true,
boolean: ['v', 'i', 'k', 'a'],
string: ['e', 'c', 'killSignal'],
alias: {
add: 'a',
jobs: 'j',
kill: 'k',
verbose: 'v',
initial: 'i',
exclude: 'e',
cwd: 'c',
delay: 'd',
poll: 'p',
outpipe: 'o',
filter: 'f'
},
default: {
exclude: '**/node_modules/**'
}
})
// Print usage info
if (!argv._.length || argv.help) {
console.log('Usage: onchange [...file] -- <command> [...args]')
process.exit()
}
// Setup some storage variables
var matches = argv._.slice()
var args = argv['--'].slice()
var command = args.shift()
var excludePath = typeof argv['exclude-path'] === 'string'
? argv['exclude-path']
: undefined
var exclude = typeof argv.exclude === 'boolean' ? [] : arrify(argv.exclude)
var options = {
exclude: excludePath ? [...exclude, getExcludeFunction(excludePath, argv.cwd)] : exclude,
verbose: argv.verbose,
add: argv.add,
initial: argv.initial,
jobs: argv.jobs,
kill: argv.kill,
cwd: argv.cwd,
delay: argv.delay,
poll: argv.poll,
killSignal: argv.killSignal,
outpipe: argv.outpipe,
filter: argv.filter && (Array.isArray(argv.filter) ? argv.filter : argv.filter.split(/\W+/)),
awaitWriteFinish: argv['await-write-finish']
}
function getExcludeFunction(excludePath, cwd = process.cwd()) {
if (isFileSync(excludePath)) {
var ignorer = ignore()
ignorer.add(fs.readFileSync(excludePath, 'utf8'))
return function (changePath) {
var relPath = path.relative(cwd, changePath)
return relPath ? ignorer.ignores(relPath) : false
}
}
console.error('Unable to load file from `--exclude-path`:')
console.error(' ' + path.resolve(excludePath))
process.exit(1)
}
function isFileSync(path) {
try {
return fs.statSync(path).isFile()
} catch (e) {
return false
}
}
if (!command && !options.outpipe) {
console.error('Remember to pass the command after "--":')
console.error(' onchange \'**/*.js\' -- echo \'{{changed}}\'')
process.exit(1)
}
// Start watcher
onchange(matches, command, args, options)