1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var fsPath = require("path");
|
5 | var rxjs_1 = require("rxjs");
|
6 | var childProcess = require("child_process");
|
7 | var libs_1 = require("../libs");
|
8 | var ExecutionError = (function (_super) {
|
9 | tslib_1.__extends(ExecutionError, _super);
|
10 | function ExecutionError(error, code) {
|
11 | if (error === void 0) { error = 'Error: Command failed'; }
|
12 | var _this = _super.call(this, error) || this;
|
13 | _this.code = 1;
|
14 | if (code) {
|
15 | _this.code = code;
|
16 | }
|
17 | return _this;
|
18 | }
|
19 | ExecutionError.prototype.setCode = function (code) {
|
20 | this.code = code;
|
21 | return this;
|
22 | };
|
23 | return ExecutionError;
|
24 | }(Error));
|
25 | exports.ExecutionError = ExecutionError;
|
26 | function exec(cmd, options) {
|
27 | if (options === void 0) { options = {}; }
|
28 | var silent = options.silent || false;
|
29 | var stdout = '';
|
30 | var code = 0;
|
31 | try {
|
32 | stdout = childProcess.execSync(cmd).toString();
|
33 | if (!silent) {
|
34 | libs_1.log.info(stdout);
|
35 | }
|
36 | }
|
37 | catch (err) {
|
38 | var stderr = err.toString();
|
39 | code = err.status || 1;
|
40 | throw new ExecutionError(stderr).setCode(code);
|
41 | }
|
42 | return {
|
43 | code: code,
|
44 | cmd: cmd,
|
45 | stdout: stdout,
|
46 | };
|
47 | }
|
48 | exports.exec = exec;
|
49 | function execWithin(path, cmd, options) {
|
50 | path = fsPath.resolve(path);
|
51 | return exec("cd " + path + " && " + cmd, options);
|
52 | }
|
53 | exports.execWithin = execWithin;
|
54 | function execAsync(cmd, options) {
|
55 | if (options === void 0) { options = {}; }
|
56 | return new Promise(function (resolve, reject) {
|
57 | var silent = options.silent || false;
|
58 | var child = childProcess.exec(cmd, function (err, stdout, stderr) {
|
59 | if (err) {
|
60 | return reject({
|
61 | error: err,
|
62 | code: err.status || 1,
|
63 | });
|
64 | }
|
65 | resolve({
|
66 | code: 0,
|
67 | cmd: cmd,
|
68 | stdout: stdout,
|
69 | stderr: stderr === '' ? undefined : stderr,
|
70 | });
|
71 | });
|
72 | if (!silent) {
|
73 | child.stdout.pipe(process.stdout);
|
74 | child.stderr.pipe(process.stderr);
|
75 | }
|
76 | process.on('exit', function () {
|
77 | if (options.onExit) {
|
78 | options.onExit();
|
79 | }
|
80 | child.kill();
|
81 | });
|
82 | });
|
83 | }
|
84 | exports.execAsync = execAsync;
|
85 | function execWithinAsync(path, cmd, options) {
|
86 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
87 | return tslib_1.__generator(this, function (_a) {
|
88 | path = fsPath.resolve(path);
|
89 | return [2, execAsync("cd " + path + " && " + cmd, options)];
|
90 | });
|
91 | });
|
92 | }
|
93 | exports.execWithinAsync = execWithinAsync;
|
94 | function exec$(cmd) {
|
95 | var code = 0;
|
96 | var result = new rxjs_1.Subject();
|
97 | var child = childProcess.exec(cmd, function (err, stdout, stderr) {
|
98 | if (err) {
|
99 | code = err.status || 1;
|
100 | result.error(err);
|
101 | }
|
102 | else {
|
103 | result.complete();
|
104 | }
|
105 | });
|
106 | var next = function (stdout, stderr) {
|
107 | if (stdout) {
|
108 | stdout = stdout.replace(/\n$/, '');
|
109 | }
|
110 | result.next({
|
111 | code: code,
|
112 | cmd: cmd,
|
113 | stdout: stdout,
|
114 | stderr: stderr,
|
115 | });
|
116 | };
|
117 | child.stdout.on('data', function (chunk) { return next(chunk.toString()); });
|
118 | child.stderr.on('data', function (chunk) { return next(undefined, chunk.toString()); });
|
119 | return result;
|
120 | }
|
121 | exports.exec$ = exec$;
|
122 |
|
\ | No newline at end of file |