UNPKG

@harves/nx-node-esm-plugin

Version:

Node executor including ESM module resolution for buildable libraries within Nx workspaces.

203 lines 6.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createProcess = exports.calculateCwd = exports.LARGE_BUFFER = void 0; const tslib_1 = require("tslib"); const chalk = require("chalk"); const child_process_1 = require("child_process"); const npm_run_path_1 = require("npm-run-path"); const path = require("path"); /** * @see https://github.com/nrwl/nx/blob/316dcb948cf172e4faef8866bc66a9002ed4c4e3/packages/nx/src/executors/run-commands/run-commands.impl.ts#L14 */ exports.LARGE_BUFFER = 1024 * 1000000; /** * @see https://github.com/nrwl/nx/blob/316dcb948cf172e4faef8866bc66a9002ed4c4e3/packages/nx/src/executors/run-commands/run-commands.impl.ts#L16C1-L17C1 */ const childProcesses = new Set(); /** * * @param out * @param config * @returns * * @see https://github.com/nrwl/nx/blob/316dcb948cf172e4faef8866bc66a9002ed4c4e3/packages/nx/src/executors/run-commands/run-commands.impl.ts#L417-L440 */ function addColorAndPrefix(out, config) { if (config.prefix) { out = out .split('\n') .map((l) => l.trim().length > 0 ? `${chalk.bold(config.prefix)} ${l}` : l) .join('\n'); } if (config.color && chalk[config.color]) { out = chalk[config.color](out); } if (config.bgColor && chalk[config.bgColor]) { out = chalk[config.bgColor](out); } return out; } /** * * @param cwd * @param context * @returns * * @see https://github.com/nrwl/nx/blob/316dcb948cf172e4faef8866bc66a9002ed4c4e3/packages/nx/src/executors/run-commands/run-commands.impl.ts#L442-L449 */ function calculateCwd(cwd, context) { if (!cwd) return context.root; if (path.isAbsolute(cwd)) return cwd; return path.join(context.root, cwd); } exports.calculateCwd = calculateCwd; /** * * @param color * @param cwd * @param env * @returns * * @see https://github.com/nrwl/nx/blob/316dcb948cf172e4faef8866bc66a9002ed4c4e3/packages/nx/src/executors/run-commands/run-commands.impl.ts#L451-L466 */ function processEnv(color, cwd, env) { const localEnv = (0, npm_run_path_1.env)({ cwd: cwd !== null && cwd !== void 0 ? cwd : process.cwd() }); const res = Object.assign(Object.assign(Object.assign({}, process.env), localEnv), env); // need to override PATH to make sure we are using the local node_modules if (localEnv.PATH) res.PATH = localEnv.PATH; // UNIX-like if (localEnv.Path) res.Path = localEnv.Path; // Windows if (color) { res.FORCE_COLOR = `${color}`; } return res; } /** * * @param commandConfig * @param readyWhen * @param color * @param cwd * @param env * @param isParallel * @param usePty * @param streamOutput * @param tty * @returns * * @see https://github.com/nrwl/nx/blob/316dcb948cf172e4faef8866bc66a9002ed4c4e3/packages/nx/src/executors/run-commands/run-commands.impl.ts#L293C1-L353C2 */ function createProcess(commandConfig_1, color_1, cwd_1, env_1) { return tslib_1.__awaiter(this, arguments, void 0, function* ( // pseudoTerminal: PseudoTerminal | null, commandConfig, // readyWhen: string, color, cwd, env, // isParallel: boolean, // usePty: boolean = true, streamOutput = true) { env = processEnv(color, cwd, env); // The rust runCommand is always a tty, so it will not look nice in parallel and if we need prefixes // currently does not work properly in windows // if ( // pseudoTerminal && // process.env.NX_NATIVE_COMMAND_RUNNER !== 'false' && // !commandConfig.prefix && // !isParallel && // usePty // ) { // let terminalOutput = chalk.dim('> ') + commandConfig.command + '\r\n\r\n'; // if (streamOutput) { // process.stdout.write(terminalOutput); // } // const cp = pseudoTerminal.runCommand(commandConfig.command, { // cwd, // jsEnv: env, // quiet: !streamOutput, // tty, // }); // childProcesses.add(cp); // return new Promise((res) => { // cp.onOutput((output) => { // terminalOutput += output; // if (readyWhen && output.indexOf(readyWhen) > -1) { // res({ success: true, terminalOutput }); // } // }); // cp.onExit((code) => { // if (code >= 128) { // process.exit(code); // } else { // res({ success: code === 0, terminalOutput }); // } // }); // }); // } return nodeProcess(commandConfig, cwd, env, '', streamOutput); }); } exports.createProcess = createProcess; /** * * @param commandConfig * @param cwd * @param env * @param readyWhen * @param streamOutput * @returns * * @see https://github.com/nrwl/nx/blob/316dcb948cf172e4faef8866bc66a9002ed4c4e3/packages/nx/src/executors/run-commands/run-commands.impl.ts#L355-L415 */ function nodeProcess(commandConfig, cwd, env, readyWhen, streamOutput = true) { let terminalOutput = chalk.dim('> ') + commandConfig.command + '\r\n\r\n'; if (streamOutput) { process.stdout.write(terminalOutput); } return new Promise((res) => { const childProcess = (0, child_process_1.fork)(commandConfig.command, commandConfig.args, { // maxBuffer: LARGE_BUFFER, env, cwd, stdio: ['pipe', 'pipe', 'pipe', 'ipc'], }); childProcesses.add(childProcess); childProcess.stdout.on('data', (data) => { const output = addColorAndPrefix(data.toString(), commandConfig); terminalOutput += output; if (streamOutput) { process.stdout.write(output); } if (readyWhen && data.toString().indexOf(readyWhen) > -1) { res({ success: true, terminalOutput }); } }); childProcess.stderr.on('data', (err) => { const output = addColorAndPrefix(err.toString(), commandConfig); terminalOutput += output; if (streamOutput) { process.stderr.write(output); } if (readyWhen && err.toString().indexOf(readyWhen) > -1) { res({ success: true, terminalOutput }); } }); childProcess.on('error', (err) => { const ouptput = addColorAndPrefix(err.toString(), commandConfig); terminalOutput += ouptput; if (streamOutput) { process.stderr.write(ouptput); } res({ success: false, terminalOutput }); }); childProcess.on('exit', (code) => { childProcesses.delete(childProcess); if (!readyWhen) { res({ success: code === 0, terminalOutput }); } }); }); } //# sourceMappingURL=run-commands-utils.js.map