UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

67 lines (66 loc) 2.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.runCommand = runCommand; const child_process_1 = require("child_process"); const picocolors_1 = __importDefault(require("picocolors")); const prompter_1 = require("../prompter"); const getProjectPath_1 = require("./getProjectPath"); async function runCommand(...args) { const options = (args.length === 3 ? args[2] : args[1]) || {}; options.silent = options.silent ?? true; options.cwd = options.cwd ?? (0, getProjectPath_1.getProjectPath)(); const commandStr = args.length === 3 ? `${args[0]} ${args[1].join(' ')}` : args[0]; const progressText = options.progressText || 'running command'; const completeText = options.completeText || `run ${picocolors_1.default.yellow(commandStr)} successfully`; const errorText = options.errorText || `running ${picocolors_1.default.yellow(commandStr)} was failed`; if (!options.silent) (0, prompter_1.startSpinner)(progressText); let isDone = false; let output = ''; const isShell = process.platform == 'win32'; const parts = commandStr.split(' '); let spawnArgs = args.length === 3 ? args[1] : parts.slice(1); if (isShell) spawnArgs = spawnArgs.map(arg => (arg.includes(' ') ? `"${arg}"` : arg)); const exitCode = await new Promise(resolve => { const command = args.length === 3 ? args[0] : parts[0]; const child = (0, child_process_1.spawn)(command, spawnArgs, { shell: isShell, cwd: options.cwd, }); child.stdout.on('data', (chunk) => { if (isDone) return; const partialOutput = chunk.toString('utf8'); if (!options.silent) (0, prompter_1.updateSpinner)(`${progressText} ${picocolors_1.default.gray((0, prompter_1.getLastLine)(partialOutput))}`); output += partialOutput; }); child.stderr.on('data', (chunk) => { if (isDone) return; const partialOutput = chunk.toString('utf8'); if (!options.silent) (0, prompter_1.updateSpinner)(`${progressText} ${picocolors_1.default.gray((0, prompter_1.getLastLine)(partialOutput))}`); output += partialOutput; }); child.on('close', code => { resolve(code ?? 0); }); }); isDone = true; if (!options.silent) { if (exitCode !== 0) { (0, prompter_1.stopSpinner)(`${picocolors_1.default.red(`${errorText}, command exit with `)}${picocolors_1.default.yellow(exitCode?.toString())}`); } else (0, prompter_1.stopSpinner)(completeText); } return { exitCode, output, }; }