-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·47 lines (34 loc) · 928 Bytes
/
index.js
File metadata and controls
executable file
·47 lines (34 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env node
'use strict';
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 false;
if (process.stdin.isTTY) return false;
const BUFSIZE = 65536;
let nbytes = 0;
let chunks = [];
let buffer = '';
while(true) {
try {
buffer = Buffer.alloc(BUFSIZE);
nbytes = fs.readSync(0, buffer, 0, BUFSIZE, null);
}
catch (e) {
if (e.code != 'EAGAIN') throw e;
};
if (nbytes === 0) break;
chunks.push(buffer.slice(0, nbytes));
};
const stdin = Buffer.concat(chunks).toString();
if (stdin) {
process.argv.push(stdin.trim());
return true;
}
};