-
Notifications
You must be signed in to change notification settings - Fork 44
/
config.js
59 lines (50 loc) · 1.54 KB
/
config.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
import minimist from 'minimist';
export const EXIT_CODES = {
SHX_ERROR: 27, // https://xkcd.com/221/
CMD_FAILED: 1, // TODO: Once shelljs/shelljs#269 lands, use `error()`
SUCCESS: 0,
};
export const CMD_BLOCKLIST = [
'cd',
'pushd',
'popd',
'dirs',
'set',
'exit',
'exec',
'ShellString',
];
export const OPTION_BLOCKLIST = [
'globOptions', // we don't have good control over globbing in the shell
'execPath', // we don't currently support exec
'bufLength', // we don't use buffers in shx
'maxdepth', // this option is undocumented, add this back if it makes sense
];
export const CONFIG_FILE = '.shxrc.json';
export const SHELLJS_PIPE_INFO = {
cat: { minArgs: 1 },
grep: { minArgs: 2 },
head: { minArgs: 1 },
sed: { minArgs: 2 },
sort: { minArgs: 1 },
tail: { minArgs: 1 },
uniq: { minArgs: 1 },
};
// All valid options
const allOptionsList = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
.split('');
export const shouldReadStdin = (args) => {
const cmd = args[0];
const cmdInfo = SHELLJS_PIPE_INFO[cmd];
const parsedArgs = minimist(args.slice(1), {
stopEarly: true,
boolean: allOptionsList, // treat all short options as booleans
});
let requiredNumArgs = cmdInfo ? cmdInfo.minArgs : -1;
// If a non-boolean option is passed in, increment the required argument
// count (this is the case for `-n` for `head` and `tail`)
if (parsedArgs.n && (cmd === 'head' || cmd === 'tail')) {
requiredNumArgs++;
}
return Boolean(!process.stdin.isTTY && parsedArgs._.length < requiredNumArgs);
};