/
index.js
67 lines (53 loc) · 1.57 KB
/
index.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
var spawn = require('child_process').spawn
var parse = require('shell-quote').parse
var map = require('map-limit')
var Emitter = require('events/')
var findup = require('findup')
var path = require('path')
var once = require('once')
var fs = require('fs')
module.exports = bulk
function bulk(namespace, command, dirname, done) {
dirname = path.resolve(dirname)
done = once(done)
var emitter = new Emitter
var arg = parse(command)
var cmd = arg.shift()
var env = process.env
findup(dirname, 'package.json', found)
return emitter
function found(err, root) {
if (err) return done(err)
var node_modules = path.join(
root = path.resolve(root || dirname)
, 'node_modules', '@' + namespace)
fs.exists(node_modules, function(exists) {
if (!exists) return done(new Error(
'Package scope "@'+namespace+'" does not exist'
))
fs.readdir(node_modules, function(err, children) {
if (err) return done(err)
children = children.map(function(child) {
return path.join(node_modules, child)
})
doBulk(children)
})
})
}
function doBulk(packages) {
map(packages, 1, function(package, next) {
setTimeout(function() {
var proc = spawn(cmd, arg, {
cwd: package
, env: env
}).once('exit', function(code) {
if (code === 0) return next()
return next(
new Error('Invalid error code: ' + code)
)
})
emitter.emit('spawn', package, proc)
}, 5)
}, done)
}
}