UNPKG

2.35 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.AbstractRunner = void 0;
13const chalk = require("chalk");
14const child_process_1 = require("child_process");
15const ui_1 = require("../ui");
16class AbstractRunner {
17 constructor(binary, args = []) {
18 this.binary = binary;
19 this.args = args;
20 }
21 run(command, collect = false, cwd = process.cwd()) {
22 return __awaiter(this, void 0, void 0, function* () {
23 const args = [command];
24 const options = {
25 cwd,
26 stdio: collect ? 'pipe' : 'inherit',
27 shell: true,
28 };
29 return new Promise((resolve, reject) => {
30 const child = (0, child_process_1.spawn)(`${this.binary}`, [...this.args, ...args], options);
31 if (collect) {
32 child.stdout.on('data', (data) => resolve(data.toString().replace(/\r\n|\n/, '')));
33 }
34 child.on('close', (code) => {
35 if (code === 0) {
36 resolve(null);
37 }
38 else {
39 console.error(chalk.red(ui_1.MESSAGES.RUNNER_EXECUTION_ERROR(`${this.binary} ${command}`)));
40 reject();
41 }
42 });
43 });
44 });
45 }
46 /**
47 * @param command
48 * @returns The entire command that will be ran when calling `run(command)`.
49 */
50 rawFullCommand(command) {
51 const commandArgs = [...this.args, command];
52 return `${this.binary} ${commandArgs.join(' ')}`;
53 }
54}
55exports.AbstractRunner = AbstractRunner;