zhangjian
2023-06-05 0976d2d0f90cff460cedfdc8bd74e98c2c31a58c
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
class ShellError extends Error {
    constructor(message) {
        message = message && message.split('\n')[0]; // assign only first line
        super(message);
    }
}
exports.ShellError = ShellError;
function shellAsync(command, options = {}) {
    return new Promise((resolve, reject) => {
        const nextOptions = Object.assign({}, options, { shell: true, stdio: options.stdio || 'inherit' });
        const asyncProcess = child_process_1.spawn(command, nextOptions);
        let output = null;
        asyncProcess.on('error', (error) => {
            reject(new ShellError(`Failed to start command: ${command}; ${error.toString()}`));
        });
        asyncProcess.on('close', (exitCode) => {
            if (exitCode === 0) {
                resolve(output);
            }
            else {
                reject(new ShellError(`Command failed: ${command} with exit code ${exitCode}`));
            }
        });
        if (nextOptions.stdio === 'pipe') {
            asyncProcess.stdout.on('data', (buffer) => {
                output = buffer.toString();
            });
        }
        if (nextOptions.timeout) {
            setTimeout(() => {
                asyncProcess.kill();
                reject(new ShellError(`Command timeout: ${command}`));
            }, nextOptions.timeout);
        }
    });
}
function shellSync(command, options = {}) {
    try {
        const nextOptions = Object.assign({}, options, { stdio: options.stdio || 'inherit' });
        const buffer = child_process_1.execSync(command, nextOptions);
        if (buffer) {
            return buffer.toString();
        }
        return null;
    }
    catch (error) {
        throw new ShellError(error.message);
    }
}
function shell(command, options) {
    return options && options.async
        ? shellAsync(command, options)
        : shellSync(command, options);
}
exports.default = shell;
//# sourceMappingURL=shell.js.map