Comparing version
51
index.js
'use strict'; | ||
const tag = (spawn, options) => function(arg) { | ||
if (!Array.isArray(arg)) return tag(spawn, arg); | ||
const cp = require('child_process'); | ||
const makeTag = (fn, opts) => function(first) { | ||
if (!Array.isArray(first)) { | ||
return tag(fn, Object.assign({}, opts, first)); | ||
} | ||
const args = []; | ||
arg.forEach((string, index) => { | ||
first.forEach((string, index) => { | ||
string.split(/\s+/).forEach(arg => { | ||
@@ -13,12 +16,35 @@ if (arg) args.push(arg); | ||
const arg = arguments[index + 1]; | ||
if (arg != null) args.push(arg); | ||
if (arg == null) return; | ||
args.push(/\S$/.test(string) | ||
? args.pop() + arg | ||
: arg | ||
); | ||
}); | ||
const cmd = args.shift(); | ||
return fn(args, opts); | ||
}; | ||
return spawn(cmd, args, options); | ||
const spawn = (args, opts) => cp.spawn(args.shift(), args, opts); | ||
const spawnSync = (args, opts) => cp.spawnSync(args.shift(), args, opts); | ||
const promise = (args, opts) => { | ||
let out = ''; | ||
let err = ''; | ||
return new Promise((resolve, reject) => { | ||
const proc = spawn(args, opts); | ||
proc.on('exit', code => { | ||
if (code) reject(err.trim()); | ||
else resolve(out.trim()); | ||
}); | ||
proc.stdout.on('data', data => out += data); | ||
proc.stderr.on('data', data => err += data); | ||
}); | ||
}; | ||
const sync = function() { | ||
const proc = cp.spawnSync.apply(cp, arguments); | ||
const sync = (args, opts) => { | ||
const proc = spawnSync(args, opts); | ||
if (proc.error) throw proc.error; | ||
@@ -30,7 +56,8 @@ if (proc.status) throw new Error(`${proc.stderr}`.trim()); | ||
const cp = require('child_process'); | ||
const sh = tag(sync); | ||
sh.sync = sh; | ||
sh.async = tag(cp.spawn); | ||
const sh = makeTag(promise); | ||
sh.sync = makeTag(sync); | ||
sh.sync.spawn = makeTag(spawnSync); | ||
sh.spawn = makeTag(spawn); | ||
sh.makeTag = makeTag; | ||
module.exports = sh; |
{ | ||
"name": "tag-shell", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Use ES6 template tags for your node.js shell commands.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha -r chai/register-should" | ||
}, | ||
@@ -22,3 +22,7 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/paulmillr/tag-shell#readme" | ||
"homepage": "https://github.com/paulmillr/tag-shell#readme", | ||
"devDependencies": { | ||
"chai": "chaijs/chai", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
@@ -5,8 +5,18 @@ # tag-shell | ||
Using *backticks* is important and the package won't work otherwise. | ||
```javascript | ||
const sh = require('tag-shell'); | ||
const opts = {app: 'App', icon: 'icon.png'}; | ||
// Async. | ||
sh.async`notify-send -a ${opts.app} | ||
-i ${opts.icon} | ||
`; | ||
// Streaming. | ||
sh.spawn`node -v`.stdout.on('data', data => console.log(data.toString())) | ||
// Sync. | ||
const version = sh`node -v`; | ||
``` | ||
@@ -13,0 +23,0 @@ |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
12208
681.56%6
100%91
264%1
-50%26
62.5%2
Infinity%