UNPKG

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