Skip to content

Commit

Permalink
feat(run): add dep run
Browse files Browse the repository at this point in the history
  • Loading branch information
watilde committed Jul 29, 2017
1 parent a4a9016 commit 6cf3174
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
40 changes: 40 additions & 0 deletions lib/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const exec = require('child_process').exec
const fs = require('fs')
const npmPath = require('npm-path')
const path = require('path')
const spawn = require('child_process').spawn
const which = require('which')
const nm = require('./utils/nm')

const run = (argv) => {
argv._handled = true
const pkgJSON = require(path.join(process.cwd(), 'package.json'))
const scripts = pkgJSON.scripts
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] + '\n'
}).join('\n') + '\n'
)
} else {
var 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)
})
}
}

module.exports = {
command: 'run',
describe: 'Run package.json scripts',
handler: run,
aliases: ['r']
}
2 changes: 1 addition & 1 deletion test/10-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const exec = require('child_process').exec
const tree = require('strong-npm-ls')
const test = require('tap').test
const fixtures = fs.readdirSync(path.join(__dirname, 'deps'))
const bin = path.join(__dirname, '..', 'bin', 'dep.js')

test((t) => {
var items = 3
var count = fixtures.length * items
t.plan(count)
fixtures.forEach(fixture => {
const bin = path.join(__dirname, '..', 'bin', 'dep.js')
const pkg = path.join(__dirname, 'deps', fixture)
const pkgJSON = require(path.join(pkg, 'package.json'))
exec(`node ${bin} install`, {cwd: pkg}, (err, stdout, stderr) => {
Expand Down
2 changes: 1 addition & 1 deletion test/20-lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const path = require('path')
const exec = require('child_process').exec
const test = require('tap').test
const fixtures = fs.readdirSync(path.join(__dirname, 'deps'))
const bin = path.join(__dirname, '..', 'bin', 'dep.js')

test((t) => {
var items = 2
var count = fixtures.length * items
t.plan(count)
fixtures.forEach(fixture => {
const bin = path.join(__dirname, '..', 'bin', 'dep.js')
const pkg = path.join(__dirname, 'deps', fixture)
const pkgJSON = require(path.join(pkg, 'package.json'))
exec(`node ${bin} lock`, {cwd: pkg}, (err, stdout, stderr) => {
Expand Down
12 changes: 12 additions & 0 deletions test/30-run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const path = require('path')
const exec = require('child_process').exec
const test = require('tap').test
const pkg = path.join(__dirname, 'deps', 'run')
const bin = path.join(__dirname, '..', 'bin', 'dep.js')

test((t) => {
exec(`node ${bin} run test`, {cwd: pkg}, (err, stdout, stderr) => {
t.ifError(err, 'run test without error')
t.end()
})
})
3 changes: 1 addition & 2 deletions test/90-help.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require('path')
const exec = require('child_process').exec
const test = require('tap').test
const bin = path.join(__dirname, '..', 'bin', 'dep.js')

test((t) => {
const bin = path.join(__dirname, '..', 'bin', 'dep.js')
exec(`node ${bin} -h`, (err, stdout, stderr) => {
t.ifError(err, 'help ran without error')
t.ok(stdout, 'help displayed a message')
Expand All @@ -12,7 +12,6 @@ test((t) => {
})

test((t) => {
const bin = path.join(__dirname, '..', 'bin', 'dep.js')
exec(`node ${bin}`, (err, stdout, stderr) => {
t.ifError(err, 'help ran without error')
t.ok(stderr, 'help displayed a message')
Expand Down
2 changes: 1 addition & 1 deletion test/90-version.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require('path')
const exec = require('child_process').exec
const test = require('tap').test
const bin = path.join(__dirname, '..', 'bin', 'dep.js')

test((t) => {
const bin = path.join(__dirname, '..', 'bin', 'dep.js')
exec(`node ${bin} -v`, (err, stdout, stderr) => {
t.ifError(err, 'version ran without error')
t.ok(stdout, 'version displayed a message')
Expand Down
11 changes: 11 additions & 0 deletions test/deps/run/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"private": true,
"name": "registry",
"version": "1.0.0",
"scripts": {
"test": "happy-birthday --you=dep"
},
"dependencies": {
"happy-birthday": "0.6.0"
}
}

0 comments on commit 6cf3174

Please sign in to comment.