forked from timoxley/bulk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk.js
executable file
·78 lines (66 loc) · 1.55 KB
/
bulk.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
#!/usr/bin/env node
'use strict'
var split = require('split')
var through = require('through2')
var minimist = require('minimist')
var spawn = require('child_process').spawn
var parse = require('shell-quote').parse
var fs = require('fs')
var argv = minimist(process.argv, {
boolean: [ 'bail', 'chdir', 'exit' ],
default: { 'bail': false, 'chdir': true, 'exit': false },
alias: {
b: 'bail',
d: 'chdir',
c: 'command',
e: 'exit'
}
})
if (!argv.command) {
console.error('command required!')
process.exit(1)
}
process.stdin
.pipe(split(/\s+/g))
.pipe(filterDirs(argv))
.pipe(execSeries(argv))
function filterDirs (argv) {
if (!argv.chdir) return through()
return through(function (item, enc, next) {
var self = this
item = item.toString()
fs.stat(item, function (err, stat) {
if (err) return next()
if (stat.isDirectory()) self.push(item)
return next()
})
})
}
function execSeries (argv) {
return through(function (item, enc, next) {
var cwd = process.cwd()
item = item.toString()
var opts = {stdio: 'inherit', cwd: cwd}
var args = parse(argv.command)
var cmd = args.shift()
if (argv.chdir) {
opts.cwd = item
} else {
args.push(item)
}
spawn(cmd, args, opts)
.on('error', function (err) {
if (argv.bail) return process.exit(1)
next(err)
next = noop
})
.on('exit', function (code) {
if (argv['exit'] && code !== 0) {
return process.exit(code)
}
next()
next = noop
})
})
}
function noop () {}