UNPKG

3.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const execa = require("execa");
5exports.execa = execa;
6const pathLib = require("path");
7const utils_1 = require("./utils");
8const fs_1 = require("./fs");
9const stream_1 = require("stream");
10function exec(commands, options) {
11 return tslib_1.__awaiter(this, void 0, void 0, function* () {
12 if (utils_1.Is.str(commands)) {
13 return execa.shell(commands, options);
14 }
15 let rets = [];
16 for (let cmd of commands) {
17 rets.push(yield execa.shell(cmd, options));
18 }
19 return rets;
20 });
21}
22exports.exec = exec;
23exports.spawn = execa;
24class ShellContext {
25 constructor() {
26 this._cwd = process.cwd();
27 this._env = Object.assign({}, process.env);
28 this.logCommand = false;
29 this.sleep = utils_1.sleep;
30 }
31 cwd(cwd = this._cwd) {
32 return (this._cwd = cwd);
33 }
34 cd(path) {
35 this._cwd = pathLib.resolve(this._cwd, path);
36 return this;
37 }
38 exec(commands, options) {
39 let logger = require('./logger').logger;
40 if (this.logCommand) {
41 logger.info('$', commands);
42 }
43 let p = exec(commands, Object.assign({ cwd: this._cwd, env: this._env, stdio: this.redirectLog ? 'pipe' : 'inherit' }, options));
44 let redirectLogStream = this._getRedirectLogFile();
45 if (redirectLogStream) {
46 p.then(p => {
47 p.stdout && redirectLogStream.write(p.stdout);
48 p.stderr && redirectLogStream.write(p.stderr);
49 redirectLogStream.end();
50 });
51 }
52 p.catch(err => {
53 logger.error('Exec failed: ', commands);
54 redirectLogStream.end();
55 throw err;
56 });
57 return p;
58 }
59 spawn(file, args = [], options) {
60 let logger = require('./logger').logger;
61 const command = file + ' ' + args.map(a => `"${a.replace(/"/g, '\\"')}"`).join(' ');
62 if (this.logCommand) {
63 logger.info('Exec: ', command);
64 }
65 let p = exports.spawn(file, args, Object.assign({ cwd: this._cwd, env: this._env, stdio: this.redirectLog ? 'pipe' : 'inherit' }, options));
66 let redirectLogStream = this._getRedirectLogFile();
67 if (redirectLogStream) {
68 p.then(p => {
69 p.stdout && redirectLogStream.write(p.stdout);
70 p.stderr && redirectLogStream.write(p.stderr);
71 redirectLogStream.end();
72 });
73 }
74 p.catch(err => {
75 logger.error('Exec failed: ', command);
76 redirectLogStream.end();
77 throw err;
78 });
79 return p;
80 }
81 env(key, val) {
82 if (arguments.length === 1) {
83 return this._env[key];
84 }
85 this._env[key] = val;
86 return this;
87 }
88 _getRedirectLogFile() {
89 if (!this.redirectLog)
90 return null;
91 let stream;
92 if (this.redirectLog instanceof stream_1.Writable) {
93 return this.redirectLog;
94 }
95 let logFile = utils_1.Is.str(this.redirectLog) ? this.redirectLog : utils_1.DefaultLogFile;
96 return fs_1.fs.createWriteStream(logFile, { mode: fs_1.fs.constants.O_APPEND });
97 }
98}
99exports.ShellContext = ShellContext;
100function shell(callback) {
101 return tslib_1.__awaiter(this, void 0, void 0, function* () {
102 return callback(new ShellContext());
103 });
104}
105exports.shell = shell;
106//# sourceMappingURL=exec.js.map
\No newline at end of file