New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

homerun

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

homerun - npm Package Compare versions

Comparing version

to
0.2.0

.travis.yml

55

index.js

@@ -11,28 +11,49 @@ var cp = require('child_process')

// Pick script
var name = argv[2] ? argv[2] : 'help'
var name = argv[2]
var scripts = pkg.scripts
var script = scripts[name]
// Extract cmd and rebuild args
var parsed = parse(script + ' ' + args, process.env)
var cmd = parsed.shift()
var args = parsed
var script
if (name) {
if (scripts[name]) {
script = scripts[name]
} else if (scripts['default']) {
script = scripts['default']
}
} else {
if (scripts['blank']) {
script = scripts['blank']
}
}
return {
spawn: function() {
return cp.spawn(cmd, args, {
stdio: 'inherit'
})
.on('error', function(err) {
console.error(err)
process.exit(1)
})
.on('exit', process.exit)
.on('close', process.exit)
if (script) {
// Extract cmd and rebuild args
var parsed = parse(script + ' ' + args, process.env)
var cmd = parsed.shift()
var args = parsed
// Spawn
return cp.spawn(cmd, args, {
stdio: 'inherit'
})
.on('error', function(err) {
console.error(err)
process.exit(1)
})
.on('exit', process.exit)
.on('close', process.exit)
} else {
process.exit(1)
}
},
exec: function(cb) {
return cp.exec([cmd].concat(args).join(' '), cb)
if (script) {
return cp.exec(script + ' ' + args, cb)
} else {
cb({ code: 1 }, '', '')
}
}
}
}
{
"name": "homerun",
"version": "0.1.0",
"description": "homerun",
"version": "0.2.0",
"description": "Turn package scripts into commands",
"bin": "./bin.js",

@@ -16,2 +16,3 @@ "scripts": {

"run",
"package",
"cli",

@@ -18,0 +19,0 @@ "scripts"

@@ -1,2 +0,80 @@

homerun
=======
# Homerun [![](https://badge.fury.io/js/homerun.svg)](http://badge.fury.io/js/homerun) [![](https://travis-ci.org/typicode/homerun.svg?branch=master)](https://travis-ci.org/typicode/homerun)
![](http://i.imgur.com/Bs7wA8v.gif)
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).
## Usage
Let's say you have a script called `add` that you can run this way:
```bash
$ npm run add -- 1 2
3
```
Simply install `homerun` and add it to your `package.json`
`npm install homerun --save`
```javascript
{
"name": "mycli"
"bin": "./node_modules/.bin/homerun"
"scripts": {
"add": "node commands/add"
}
}
```
Now, without any other setup, if you `npm link` or `npm publish` you get a CLI for free:
```bash
$ mycli add 1 2
3
# BOOM!
```
## Options
Homerun will use this scripts in case no command is provided or matches.
```javascript
"scripts": {
"blank": "npm start" // mycli
"default": "echo usage: ..." // mycli unknown command
}
```
## Module
If you need to customize, you can use homerun as a module.
```javascript
var homerun = require('homerun')
var pkg = require('./package.json')
// Do things with pkg.scripts...
homerun(pkg, process.argv).spawn()
```
## Test
Testing is easy too, simply run `homerun.exec()` to test output and exit code.
```javascript
var argv = [,, 'add', '1', '2']
homerun(pkg, argv).exec(function(err, stdout, stderr) {
assert.equal(err, null)
assert.equal('', stderr)
assert.equal('3\n', stdout)
})
```
## License
MIT - [Typicode](https://github.com/typicode)
var assert = require('assert')
var homerun = require('./')
var pkg = require('./package.json')
var homerun = require('../')
var pkg = {
scripts: {
add: 'node test/add'
}
}
// Test without default and blank scripts
homerun(pkg, [,, 'add', '1', '2']).exec(function(err, stdout, stderr) {
assert.equal(err, null)
assert.equal('', stderr)
assert.equal('3\n', stdout)
assert.equal('', stderr)
assert.equal(err, null)
})

@@ -21,11 +27,20 @@

pkg.scripts.help = 'node help'
// Test with default and blank scripts
pkg.scripts.default = 'echo default'
pkg.scripts.blank = "echo blank"
homerun(pkg, [,, 'add', '1', '2']).exec(function(err, stdout, stderr) {
assert.equal(err, null)
assert.equal('', stderr)
assert.equal('3\n', stdout)
})
homerun(pkg, [,, 'unknown']).exec(function(err, stdout, stderr) {
assert.equal('help\n', stdout)
assert.equal(err, null)
assert.equal('default\n', stdout)
})
homerun(pkg, [,,]).exec(function(err, stdout, stderr) {
assert.equal('help\n', stdout)
assert.equal(err, null)
assert.equal('blank\n', stdout)
})