1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const child_process_1 = require("child_process");
|
4 | const Logger_1 = require("./Logger");
|
5 | const DefaultOptions = {
|
6 | args: [],
|
7 | cwd: process.cwd(),
|
8 | };
|
9 | class Command {
|
10 | constructor(options) {
|
11 | this.log = Logger_1.Logger.extend('command');
|
12 | this.options = Object.assign({}, DefaultOptions, options);
|
13 | }
|
14 | static from(options) {
|
15 | return new Command(options);
|
16 | }
|
17 | exec(collectResult = true) {
|
18 | return new Promise((resolve, reject) => {
|
19 | const options = {
|
20 | cwd: this.options.cwd,
|
21 | shell: this.options.shell || undefined,
|
22 | };
|
23 | const command = this.createCommand();
|
24 | const child = child_process_1.exec(command, options, (error, stdout, stderr) => {
|
25 | if (error && collectResult === false) {
|
26 | reject(error);
|
27 | }
|
28 | else {
|
29 | resolve({ args: this.options.args, code: 0, exe: this.options.exe, messages: stdout, reason: stderr });
|
30 | }
|
31 | });
|
32 | child.on('uncaughtException', (error) => {
|
33 | this.log.error('uncaught-exception', error);
|
34 | if (collectResult === false) {
|
35 | reject(error);
|
36 | }
|
37 | });
|
38 | });
|
39 | }
|
40 | spawn() {
|
41 | const options = {
|
42 | cwd: this.options.cwd,
|
43 | shell: this.options.shell,
|
44 | };
|
45 | const child = child_process_1.spawn(this.options.exe, this.options.args, options);
|
46 | child.on('uncaughtException', (error) => this.log.error(error));
|
47 | return Promise.resolve(child);
|
48 | }
|
49 | createCommand() {
|
50 | return [this.options.exe, ...this.options.args].join(' ');
|
51 | }
|
52 | }
|
53 | exports.Command = Command;
|
54 |
|
\ | No newline at end of file |