UNPKG

2.13 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.spawn = spawn;
9exports["default"] = void 0;
10
11var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
12
13var _trimSpaces = _interopRequireDefault(require("@lskjs/utils/trimSpaces"));
14
15var _child_process = require("child_process");
16
17var _excluded = ["trace", "log", "error"];
18
19function spawn(command) {
20 var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
21 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
22 var _options$trace = options.trace,
23 trace = _options$trace === void 0 ? console.log : _options$trace,
24 _options$log = options.log,
25 log = _options$log === void 0 ? console.log : _options$log,
26 _options$error = options.error,
27 error = _options$error === void 0 ? console.error : _options$error,
28 otherOptions = (0, _objectWithoutProperties2["default"])(options, _excluded);
29
30 if (trace) {
31 trace('>>>');
32 trace('>>>', (0, _trimSpaces["default"])(command, args.join(' ')));
33 }
34
35 return new Promise(function (resolve, reject) {
36 var proc = (0, _child_process.spawn)(command, args, otherOptions);
37
38 if (proc.stdout) {
39 proc.stdout.on('data', function (data) {
40 var res = data.toString().trim();
41 if (log) log(res);
42 });
43 }
44
45 if (proc.stderr) {
46 proc.stderr.on('data', function (data) {
47 var res = data.toString().trim();
48 if (error) error(res);
49 });
50 }
51
52 proc.on('exit', function (code) {
53 if (trace) {
54 trace('<<< ', (0, _trimSpaces["default"])(command, args.join(' ')), '<<< finished, code:', code);
55 trace('<<<');
56 }
57
58 if (!code) return resolve(proc); // eslint-disable-next-line prefer-promise-reject-errors
59
60 return reject({
61 proc: proc,
62 code: code
63 });
64 });
65 });
66}
67
68var _default = spawn;
69exports["default"] = _default;
70//# sourceMappingURL=spawn.js.map
\No newline at end of file