pipe-args
Advanced tools
Comparing version 1.0.1 to 1.2.0
42
index.js
@@ -5,17 +5,41 @@ #!/usr/bin/env node | ||
module.exports.load = function () { | ||
let stdin; | ||
const _ = require('ramda'); | ||
const fs = require('fs'); | ||
const validOpts = opts => { | ||
if(!opts || !opts.commands) return true; | ||
// if there are command constraints, copy stdin->process.argv only if | ||
// command is valid | ||
return _.contains(process.argv[2], opts.commands) ? true : false; | ||
}; | ||
module.exports.load = opts => { | ||
if(!validOpts(opts)) return; | ||
if (process.stdin.isTTY) return; | ||
const BUFSIZE = 65536; | ||
let nbytes = 0; | ||
let chunks = []; | ||
let buffer = ''; | ||
try { | ||
stdin = require('fs').readFileSync('/dev/stdin').toString(); | ||
while(true) { | ||
try { | ||
buffer = Buffer.alloc(BUFSIZE); | ||
nbytes = fs.readSync(0, buffer, 0, BUFSIZE, null); | ||
} | ||
catch (e) { | ||
if (e.code != 'EAGAIN') throw e; | ||
}; | ||
} catch(e) { | ||
if (e.code != 'EAGAIN') throw e; | ||
else return; | ||
if (nbytes === 0) break; | ||
chunks.push(buffer.slice(0, nbytes)); | ||
}; | ||
const stdin = Buffer.concat(chunks).toString(); | ||
if (stdin) process.argv.push(stdin.trim()); | ||
if(stdin) process.argv.push(stdin.trim()); | ||
}; | ||
{ | ||
"name": "pipe-args", | ||
"version": "1.0.1", | ||
"description": "Adds piped values from stdin to process argv in a transparent way", | ||
"version": "1.2.0", | ||
"description": " POSIX compliant pipe argument parser for node CLI apps ", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"dependencies": { | ||
"ramda": "^0.23.0" | ||
}, | ||
"devDependencies": { | ||
@@ -11,3 +13,3 @@ "tap": "^10.3.0" | ||
"scripts": { | ||
"test": "echo 'piped_arg' | node test/index.js" | ||
"test": "echo 'piped_arg' | node test/test_load.js && echo 'piped_arg' | node test/test_opts_valid.js command && echo 'piped_arg' | node test/test_opts_invalid.js command" | ||
}, | ||
@@ -14,0 +16,0 @@ "keywords": [ |
@@ -36,2 +36,29 @@ # pipe-args | ||
#### Options | ||
You can define which commands allow the stdin to be copied to process.argv by | ||
passing them in the `options` object: | ||
``` | ||
const pipe = require('pipe-args').load(['command']); | ||
console.log(`The piped arg is ${process.argv[2]}`); | ||
``` | ||
```bash | ||
$ echo OK | node command index.js | ||
$ The piped arg is OK | ||
``` | ||
```bash | ||
$ echo OK | node commandNotAllowed index.js | ||
$ The piped arg is null // stdin was not parsed into process.argv | ||
``` | ||
#### Integrations | ||
pipe-args plays along with optstrings parsers such as yarg. The following yargs | ||
@@ -59,2 +86,3 @@ parser code: | ||
### License: | ||
@@ -61,0 +89,0 @@ |
Sorry, the diff of this file is not supported yet
24905
10
57
92
1
+ Addedramda@^0.23.0
+ Addedramda@0.23.0(transitive)