Comparing version
@@ -6,2 +6,2 @@ #!/usr/bin/env node | ||
homerun(pkg, process.argv).spawn() | ||
homerun(pkg.scripts, process.argv).spawn() |
16
index.js
var cp = require('child_process') | ||
var parse = require('shell-quote').parse | ||
module.exports = function(pkg, argv) { | ||
module.exports = function(scripts, argv) { | ||
process.env.PATH = './node_modules/.bin:' + process.env.PATH | ||
// Extract args | ||
var args = argv.slice(3).join(' ') | ||
var argString = argv.slice(3).join(' ') | ||
// Pick script | ||
var name = argv[2] | ||
var scripts = pkg.scripts | ||
var script | ||
if (name) { | ||
@@ -31,9 +28,10 @@ if (scripts[name]) { | ||
if (script) { | ||
// Extract cmd and rebuild args | ||
var parsed = parse(script + ' ' + args, process.env) | ||
var parsed = parse(script + ' ' + argString, process.env) | ||
var cmd = parsed.shift() | ||
var args = parsed | ||
var argArray = parsed | ||
// Spawn | ||
return cp.spawn(cmd, args, { | ||
return cp.spawn(cmd, argArray, { | ||
stdio: 'inherit' | ||
@@ -53,3 +51,3 @@ }) | ||
if (script) { | ||
return cp.exec(script + ' ' + args, cb) | ||
return cp.exec(script + ' ' + argString, cb) | ||
} else { | ||
@@ -56,0 +54,0 @@ cb({ code: 1 }, '', '') |
{ | ||
"name": "homerun", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Turn package scripts into commands", | ||
@@ -5,0 +5,0 @@ "bin": "./bin.js", |
@@ -1,7 +0,4 @@ | ||
# Homerun [](http://badge.fury.io/js/homerun) [](https://travis-ci.org/typicode/homerun) | ||
# Homerun [](https://travis-ci.org/typicode/homerun) [](https://www.npmjs.com/package/homerun) | ||
 | ||
Since npm 2.0, you can pass arguments to scripts... wait... what if you coud use that for creating CLIs? | ||
> Since npm 2.0, you can pass arguments to scripts... wait... what if you coud use that for creating CLIs? | ||
Homerun is a little experiment that lets you just do that. If you need more, I highly recommend [minimist](https://github.com/substack/minimist). | ||
@@ -14,14 +11,19 @@ | ||
```bash | ||
$ npm run add -- 1 2 | ||
npm run add -- 1 2 | ||
3 | ||
``` | ||
Simply install `homerun` and add it to your `package.json` | ||
Install homerun | ||
`npm install homerun --save` | ||
```bash | ||
npm install homerun --save | ||
``` | ||
Add it to your `package.json` | ||
```javascript | ||
{ | ||
"name": "mycli" | ||
"bin": "./node_modules/.bin/homerun" | ||
"name": "cli" | ||
// Add homerun bin here | ||
"bin": "./node_modules/.bin/homerun" | ||
"scripts": { | ||
@@ -36,5 +38,4 @@ "add": "node commands/add" | ||
```bash | ||
$ mycli add 1 2 | ||
cli add 1 2 | ||
3 | ||
# BOOM! | ||
``` | ||
@@ -48,4 +49,4 @@ | ||
"scripts": { | ||
"blank": "npm start" // mycli | ||
"default": "echo usage: ..." // mycli unknown command | ||
"blank": "npm start" // no command provided | ||
"default": "echo usage: ..." // unknown command provided | ||
} | ||
@@ -59,8 +60,14 @@ ``` | ||
```javascript | ||
// index.js | ||
var homerun = require('homerun') | ||
var pkg = require('./package.json') | ||
var scripts = require('./package.json').scripts | ||
// Do things with pkg.scripts... | ||
homerun(scripts, process.argv).spawn() | ||
``` | ||
homerun(pkg, process.argv).spawn() | ||
```javascript | ||
// package.json | ||
{ | ||
"bin": "index.js" | ||
} | ||
``` | ||
@@ -70,3 +77,3 @@ | ||
Testing is easy too, simply run `homerun.exec()` to test output and exit code. | ||
To test your commands, use `homerun.exec()` | ||
@@ -76,3 +83,3 @@ ```javascript | ||
homerun(pkg, argv).exec(function(err, stdout, stderr) { | ||
homerun(scripts, argv).exec(function(err, stdout, stderr) { | ||
assert.equal(err, null) | ||
@@ -84,4 +91,10 @@ assert.equal('', stderr) | ||
## Limit | ||
Homerun doesn't support multiple commands. For example, `echo foo && echo bar` won't work. | ||
## License | ||
MIT - [Typicode](https://github.com/typicode) | ||
 |
var assert = require('assert') | ||
var homerun = require('../') | ||
var pkg = { | ||
scripts: { | ||
add: 'node test/add' | ||
} | ||
var scripts = { | ||
add: 'node test/add' | ||
} | ||
// Test without default and blank scripts | ||
homerun(pkg, [,, 'add', '1', '2']).exec(function(err, stdout, stderr) { | ||
homerun(scripts, [,, 'add', '1', '2']).exec(function(err, stdout, stderr) { | ||
assert.equal(err, null) | ||
@@ -17,3 +15,3 @@ assert.equal('', stderr) | ||
homerun(pkg, [,, 'unknown']).exec(function(err, stdout, stderr) { | ||
homerun(scripts, [,, 'unknown']).exec(function(err, stdout, stderr) { | ||
assert.notEqual(err, null) | ||
@@ -23,3 +21,3 @@ assert.equal(err.code, 1) | ||
homerun(pkg, [,,]).exec(function(err, stdout, stderr) { | ||
homerun(scripts, [,,]).exec(function(err, stdout, stderr) { | ||
assert.notEqual(err, null) | ||
@@ -30,6 +28,6 @@ assert.equal(err.code, 1) | ||
// Test with default and blank scripts | ||
pkg.scripts.default = 'echo default' | ||
pkg.scripts.blank = "echo blank" | ||
scripts.default = 'echo default' | ||
scripts.blank = "echo blank" | ||
homerun(pkg, [,, 'add', '1', '2']).exec(function(err, stdout, stderr) { | ||
homerun(scripts, [,, 'add', '1', '2']).exec(function(err, stdout, stderr) { | ||
assert.equal(err, null) | ||
@@ -40,3 +38,3 @@ assert.equal('', stderr) | ||
homerun(pkg, [,, 'unknown']).exec(function(err, stdout, stderr) { | ||
homerun(scripts, [,, 'unknown']).exec(function(err, stdout, stderr) { | ||
assert.equal(err, null) | ||
@@ -46,5 +44,13 @@ assert.equal('default\n', stdout) | ||
homerun(pkg, [,,]).exec(function(err, stdout, stderr) { | ||
homerun(scripts, [,,]).exec(function(err, stdout, stderr) { | ||
assert.equal(err, null) | ||
assert.equal('blank\n', stdout) | ||
}) | ||
// Test with environment variable | ||
scripts.env = "echo $HOME $1" | ||
homerun(scripts, [,, 'env', 'foo']).exec(function(err, stdout, stderr) { | ||
assert.equal(err, null) | ||
assert.equal(process.env.HOME + ' foo\n', stdout) | ||
}) |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
7027
6.07%99
3.13%94
16.05%5
25%