-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos.js
92 lines (92 loc) · 10.1 KB
/
os.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
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.shell = void 0;
const child_process = require("child_process");
const chalk = require("chalk");
const logging_1 = require("./logging");
/**
* OS helpers
*
* Shell function which both prints to stdout and collects the output into a
* string.
*/
async function shell(command) {
const commandLine = renderCommandLine(command);
(0, logging_1.debug)(`Executing ${chalk.blue(commandLine)}`);
const child = child_process.spawn(command[0], renderArguments(command.slice(1)), {
// Need this for Windows where we want .cmd and .bat to be found as well.
shell: true,
stdio: ['ignore', 'pipe', 'inherit'],
});
return new Promise((resolve, reject) => {
const stdout = new Array();
// Both write to stdout and collect
child.stdout.on('data', chunk => {
process.stdout.write(chunk);
stdout.push(chunk);
});
child.once('error', reject);
child.once('exit', code => {
if (code === 0) {
resolve(Buffer.from(stdout).toString('utf-8'));
}
else {
reject(new Error(`${commandLine} exited with error code ${code}`));
}
});
});
}
exports.shell = shell;
function renderCommandLine(cmd) {
return renderArguments(cmd).join(' ');
}
/**
* Render the arguments to include escape characters for each platform.
*/
function renderArguments(cmd) {
if (process.platform !== 'win32') {
return doRender(cmd, hasAnyChars(' ', '\\', '!', '"', "'", '&', '$'), posixEscape);
}
else {
return doRender(cmd, hasAnyChars(' ', '"', '&', '^', '%'), windowsEscape);
}
}
/**
* Render a UNIX command line
*/
function doRender(cmd, needsEscaping, doEscape) {
return cmd.map(x => needsEscaping(x) ? doEscape(x) : x);
}
/**
* Return a predicate that checks if a string has any of the indicated chars in it
*/
function hasAnyChars(...chars) {
return (str) => {
return chars.some(c => str.indexOf(c) !== -1);
};
}
/**
* Escape a shell argument for POSIX shells
*
* Wrapping in single quotes and escaping single quotes inside will do it for us.
*/
function posixEscape(x) {
// Turn ' -> '"'"'
x = x.replace(/'/g, "'\"'\"'");
return `'${x}'`;
}
/**
* Escape a shell argument for cmd.exe
*
* This is how to do it right, but I'm not following everything:
*
* https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
*/
function windowsEscape(x) {
// First surround by double quotes, ignore the part about backslashes
x = `"${x}"`;
// Now escape all special characters
const shellMeta = new Set(['"', '&', '^', '%']);
return x.split('').map(c => shellMeta.has(x) ? '^' + c : c).join('');
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoib3MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJvcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBQSwrQ0FBK0M7QUFDL0MsK0JBQStCO0FBQy9CLHVDQUFrQztBQUVsQzs7Ozs7R0FLRztBQUNJLEtBQUssVUFBVSxLQUFLLENBQUMsT0FBaUI7SUFDM0MsTUFBTSxXQUFXLEdBQUcsaUJBQWlCLENBQUMsT0FBTyxDQUFDLENBQUM7SUFDL0MsSUFBQSxlQUFLLEVBQUMsYUFBYSxLQUFLLENBQUMsSUFBSSxDQUFDLFdBQVcsQ0FBQyxFQUFFLENBQUMsQ0FBQztJQUM5QyxNQUFNLEtBQUssR0FBRyxhQUFhLENBQUMsS0FBSyxDQUFDLE9BQU8sQ0FBQyxDQUFDLENBQUMsRUFBRSxlQUFlLENBQUMsT0FBTyxDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUMsQ0FBQyxFQUFFO1FBQy9FLHlFQUF5RTtRQUN6RSxLQUFLLEVBQUUsSUFBSTtRQUNYLEtBQUssRUFBRSxDQUFDLFFBQVEsRUFBRSxNQUFNLEVBQUUsU0FBUyxDQUFDO0tBQ3JDLENBQUMsQ0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