UNPKG

react-native-integrate

Version:

Automate integration of additional code into React Native projects

130 lines (129 loc) 6.07 kB
"use strict"; // noinspection ExceptionCaughtLocallyJS var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.summary = exports.runTask = void 0; exports.shellTask = shellTask; const child_process_1 = require("child_process"); const path_1 = __importDefault(require("path")); const picocolors_1 = __importDefault(require("picocolors")); const constants_1 = require("../constants"); const prompter_1 = require("../prompter"); const checkCondition_1 = require("../utils/checkCondition"); const getErrMessage_1 = require("../utils/getErrMessage"); const getProjectPath_1 = require("../utils/getProjectPath"); const parseArgs_1 = require("../utils/parseArgs"); const setState_1 = require("../utils/setState"); const variables_1 = require("../variables"); async function shellTask(args) { const { task, packageName } = args; for (const action of task.actions) { if (action.when && !(0, checkCondition_1.checkCondition)(action.when)) { (0, setState_1.setState)(action.name, { state: 'skipped', reason: 'when', }); continue; } (0, setState_1.setState)(action.name, { state: 'progress', }); try { let command, args, cwd; if (action.args) { command = (0, variables_1.getText)(action.command); args = (0, variables_1.transformTextInObject)(action.args); } else { const cmdWithArgs = (0, parseArgs_1.parseArgs)((0, variables_1.getText)(action.command)); command = cmdWithArgs[0]; args = cmdWithArgs.slice(1); } if (action.cwd) { const cwdPath = path_1.default.join((0, getProjectPath_1.getProjectPath)(), (0, variables_1.getText)(action.cwd)); // security check if (!cwdPath.startsWith((0, getProjectPath_1.getProjectPath)())) { throw new Error('invalid cwd path'); } cwd = cwdPath; } if (packageName !== constants_1.Constants.UPGRADE_CONFIG_FILE_NAME) { const isAllowed = await (0, prompter_1.confirm)(`requesting permission to run ${picocolors_1.default.yellow(command + ' ' + args.join(' '))}`, { positive: 'allow', negative: 'skip', }); if (!isAllowed) { (0, setState_1.setState)(action.name, { state: 'skipped', reason: 'user denied', }); (0, prompter_1.logMessageGray)(`skipped running ${picocolors_1.default.yellow(command + ' ' + args.join(' '))}`); continue; } } let output = ''; (0, prompter_1.startSpinner)(`running ${picocolors_1.default.yellow(command + ' ' + args.join(' '))}`); let exitCode = undefined; let isDone = false; try { exitCode = await new Promise((resolve, reject) => { try { const child = (0, child_process_1.spawn)(command, args, { cwd, shell: process.platform == 'win32', }); child.stdout.on('data', (chunk) => { if (isDone) return; const partialOutput = chunk.toString('utf8'); (0, prompter_1.updateSpinner)(`running ${picocolors_1.default.yellow(command + ' ' + args.join(' '))} ${picocolors_1.default.gray((0, prompter_1.getLastLine)(partialOutput))}`); output += partialOutput; }); child.stderr.on('data', (chunk) => { if (isDone) return; const partialOutput = chunk.toString('utf8'); (0, prompter_1.updateSpinner)(`running ${picocolors_1.default.yellow(command + ' ' + args.join(' '))} ${picocolors_1.default.gray((0, prompter_1.getLastLine)(partialOutput))}`); output += partialOutput; }); // child.stdout.pipe(process.stdout); // child.stderr.pipe(process.stderr); child.on('close', code => { resolve(code ?? 0); }); } catch (e) { reject(e instanceof Error ? e : new Error(e?.toString() ?? '')); } }); } finally { isDone = true; if (action.name) variables_1.variables.set(`${action.name}.output`, output); if (exitCode == null) { // throwing error (0, prompter_1.stopSpinner)(`run failed using ${picocolors_1.default.yellow(command + ' ' + args.join(' '))}`); } } if (exitCode != 0) { (0, prompter_1.stopSpinner)(`run failed using ${picocolors_1.default.yellow(command + ' ' + args.join(' '))}`); throw new Error(`process exit with non zero exit code (${exitCode})`); } else { (0, prompter_1.stopSpinner)(`run ${picocolors_1.default.yellow(command + ' ' + args.join(' '))}`); } } catch (e) { (0, setState_1.setState)(action.name, { state: 'error', reason: (0, getErrMessage_1.getErrMessage)(e), }); throw e; } } } exports.runTask = shellTask; exports.summary = 'shell execution';