Skip to content

Commit

Permalink
feat(run): support pre/post script
Browse files Browse the repository at this point in the history
  • Loading branch information
watilde committed Aug 2, 2017
1 parent 218d67e commit 656d70d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
1 change: 0 additions & 1 deletion lib/install/resolver/fetchers/remote.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const path = require('path')
const request = require('request')
const tar = require('tar-stream')
const gunzip = require('gunzip-maybe')
Expand Down
29 changes: 5 additions & 24 deletions lib/run.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
const exec = require('child_process').exec
const fs = require('fs')
const npmPath = require('npm-path')
const path = require('path')
const spawn = require('cross-spawn')
const which = require('which')
const nm = require('./utils/nm')
const list = require('./run/list')
const runner = require('./run/runner')

const run = (argv) => {
argv._handled = true
const pkgJSON = require(path.join(process.cwd(), 'package.json'))
const scripts = pkgJSON.scripts
if (!pkgJSON.scripts) return
if (argv._.length === 1) {
process.stdout.write(
'Available scripts via `dep run`\n\n' +
Object.keys(scripts).map((key) => {
return 'dep run ' + key + ':\n ' + scripts[key]
}).join('\n') + '\n'
)
list(pkgJSON)
} else {
var env = process.env
var newPath = npmPath.getSync({})
env[npmPath.PATH] = newPath
const args = argv._.slice(1)
var cmds = scripts[args.shift()] || ''
cmds = cmds.split(' ')
const cmd = cmds.shift()
const script = spawn(cmd, args.concat(cmds), {env: env})
script.stdout.on('data', (data) => {
process.stdout.write(data)
})
runner(argv._, pkgJSON)
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/run/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = (pkg) => {
const scripts = pkg.scripts
process.stdout.write(
'Available scripts via `dep run`\n\n' +
Object.keys(scripts).map((key) => {
return 'dep run ' + key + ':\n ' + scripts[key]
}).join('\n') + '\n'
)
}
35 changes: 35 additions & 0 deletions lib/run/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const spawn = require('cross-spawn')
const npmPath = require('npm-path')
const each = require('promise-each')

module.exports = (_, pkg) => {
const args = _.slice(1)
const scripts = pkg.scripts
const key = args.shift()
var cmds = Object.keys(scripts).filter((script) => {
return script === 'pre' + key
|| script === key
|| script === 'post' + key
}).map((script) => {
return scripts[script].split(' ')
})
var env = process.env
var newPath = npmPath.getSync({})
env[npmPath.PATH] = newPath

Promise.resolve(cmds).then(each((item) => {
return new Promise((resolve, reject) => {
const cmd = item.shift()
const script = spawn(cmd, args.concat(item), {env: env})
script.stdout.on('data', (data) => {
process.stdout.write(data)
})
script.stderr.on('data', (data) => {
reject(data)
})
script.on('close', (data) => {
resolve()
})
})
}))
}

0 comments on commit 656d70d

Please sign in to comment.