2017-10-28 13:23:56 +02:00
|
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
|
|
const { delimiter } = require('path')
|
|
|
|
|
|
const { forEach, fromEvent } = require('promise-toolbox')
|
|
|
|
|
|
const { spawn } = require('child_process')
|
|
|
|
|
|
|
|
|
|
|
|
const { getPackages } = require('./utils')
|
|
|
|
|
|
|
|
|
|
|
|
const { env } = process
|
|
|
|
|
|
|
|
|
|
|
|
// run a script for each package (also run pre and post)
|
|
|
|
|
|
//
|
|
|
|
|
|
// TODO: https://docs.npmjs.com/misc/scripts#environment
|
2017-12-27 10:01:56 +01:00
|
|
|
|
require('exec-promise')(args => {
|
|
|
|
|
|
const parallel = args[0] === '--parallel'
|
2018-02-09 17:56:03 +01:00
|
|
|
|
const script = args[parallel ? 1 : 0]
|
2017-12-27 10:01:56 +01:00
|
|
|
|
|
|
|
|
|
|
let errors = 0
|
2018-02-09 17:56:03 +01:00
|
|
|
|
return getPackages(true)
|
|
|
|
|
|
[parallel ? 'map' : 'forEach'](({ dir, name, package: { scripts } }) => {
|
|
|
|
|
|
if (scripts == null) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2017-10-28 13:23:56 +02:00
|
|
|
|
|
2018-02-09 17:56:03 +01:00
|
|
|
|
const spawnOpts = {
|
|
|
|
|
|
cwd: dir,
|
|
|
|
|
|
env: Object.assign({}, env, {
|
|
|
|
|
|
PATH: `${dir}/node_modules/.bin${delimiter}${env.PATH}`,
|
|
|
|
|
|
}),
|
|
|
|
|
|
shell: true,
|
|
|
|
|
|
stdio: 'inherit',
|
|
|
|
|
|
}
|
|
|
|
|
|
return forEach.call([`pre${script}`, script, `post${script}`], script => {
|
2017-10-28 13:23:56 +02:00
|
|
|
|
const command = scripts[script]
|
|
|
|
|
|
if (command !== undefined) {
|
2017-12-27 10:01:56 +01:00
|
|
|
|
console.log(`* ${name}:${script} −`, command)
|
2017-10-28 13:23:56 +02:00
|
|
|
|
return fromEvent(spawn(command, spawnOpts), 'exit').then(code => {
|
|
|
|
|
|
if (code !== 0) {
|
2017-12-27 10:01:56 +01:00
|
|
|
|
++errors
|
|
|
|
|
|
console.log(`* ${name}:${script} − Error:`, code)
|
2017-10-28 13:23:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2018-02-09 17:56:03 +01:00
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(() => {
|
|
|
|
|
|
if (errors !== 0) {
|
|
|
|
|
|
throw errors
|
2017-10-28 13:23:56 +02:00
|
|
|
|
}
|
2018-02-09 17:56:03 +01:00
|
|
|
|
})
|
2017-12-27 10:01:56 +01:00
|
|
|
|
})
|