-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathoptions.js
More file actions
103 lines (92 loc) · 3.27 KB
/
options.js
File metadata and controls
103 lines (92 loc) · 3.27 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @overview Provides functionality for parsing shescape options.
* @license MPL-2.0
*/
import { resolveExecutable } from "./executables.js";
import { hasOwn, isString } from "./reflection.js";
/**
* The identifier for 'no shell' or the absence of a shell.
*
* @constant
* @type {symbol}
*/
export const noShell = Symbol();
/**
* Check if the Node.js version is before v22.0.0.
*
* @param {string} version The `process.version` value.
* @returns {boolean} `true` if version is before v22.0.0, `false` otherwise.
*/
function isBeforeNode22(version) {
const [major] = version.replace("v", "").split(".");
return Number.parseInt(major, 10) < 22; // eslint-disable-line no-magic-numbers
}
/**
* Parses the `flagProtection` option.
*
* @param {object} args The arguments for this function.
* @param {object} args.options The options for escaping.
* @returns {object} The parsed option.
*/
function parseFlagProtection({ options }) {
const flagProtection = hasOwn(options, "flagProtection")
? options.flagProtection
: undefined;
if (flagProtection === undefined) {
return true;
}
return Boolean(flagProtection);
}
/**
* Parses the `shell` option.
*
* @param {object} args The arguments for this function.
* @param {Object<string, string>} args.env The environment variables.
* @param {object} args.options The options for escaping.
* @param {string} args.version The Node.js version.
* @param {object} deps The dependencies for this function.
* @param {function(): string} deps.getDefaultShell Function to get the default shell.
* @param {function(): string} deps.getShellName Function to get the name of a shell.
* @param {function(): boolean} deps.isShellSupported Function to see if a shell is usable.
* @returns {object} The parsed option.
* @throws {Error} The shell is not supported or could not be found.
*/
function parseShell(
{ env, options, version },
{ getDefaultShell, getShellName, isShellSupported },
) {
let shell =
hasOwn(options, "shell") || isBeforeNode22(version)
? options.shell
: undefined;
let shellName = noShell;
if (shell !== false) {
if (!isString(shell)) {
shell = getDefaultShell({ env });
}
shellName = getShellName({ env, shell }, { resolveExecutable });
}
if (!isShellSupported(shellName)) {
throw new Error(`Shescape does not support the shell ${shellName}`);
}
return shellName;
}
/**
* Parses the options provided to shescape.
*
* @param {object} args The arguments for this function.
* @param {Object<string, string>} args.env The environment variables.
* @param {object} args.options The options for escaping.
* @param {string} args.version The Node.js version.
* @param {object} deps The dependencies for this function.
* @param {function(): string} deps.getDefaultShell Function to get the default shell.
* @param {function(): string} deps.getShellName Function to get the name of a shell.
* @param {function(): boolean} deps.isShellSupported Function to see if a shell is usable.
* @returns {object} The parsed options.
* @throws {Error} The shell is not supported or could not be found.
*/
export function parseOptions(args, deps) {
const flagProtection = parseFlagProtection(args);
const shellName = parseShell(args, deps);
return { flagProtection, shellName };
}