UNPKG

@plugjs/plug

Version:
114 lines (112 loc) 4.8 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // utils/exec.ts var exec_exports = {}; __export(exec_exports, { execChild: () => execChild }); module.exports = __toCommonJS(exec_exports); var import_node_child_process = require("node:child_process"); var import_node_path = __toESM(require("node:path"), 1); var import_node_readline = __toESM(require("node:readline"), 1); var import_asserts = require("../asserts.cjs"); var import_logging = require("../logging.cjs"); var import_paths = require("../paths.cjs"); async function execChild(cmd, args, options = {}, context) { const { env = {}, // default empty environment shell = false, // by default do not use a shell cwd = void 0, // by default use "process.cwd()" coverageDir, // default "undefined" (pass throug from env) ...extraOptions } = options; const childCwd = cwd ? context.resolve(cwd) : (0, import_paths.getCurrentWorkingDirectory)(); (0, import_asserts.assert)((0, import_paths.resolveDirectory)(childCwd), `Current working directory ${(0, import_logging.$p)(childCwd)} does not exist`); const childPaths = []; const baseNodePath = context.resolve("@node_modules", ".bin"); if ((0, import_paths.resolveDirectory)(baseNodePath)) childPaths.push(baseNodePath); const buildNodePath = context.resolve("./node_modules", ".bin"); if ((0, import_paths.resolveDirectory)(buildNodePath)) childPaths.push(buildNodePath); const extraPath = env.PATH || process.env.PATH; if (extraPath) childPaths.push(extraPath); const PATH = childPaths.join(import_node_path.default.delimiter); const childEnv = { ...process.env, // environment from current running process ...env, // environment configured from "execChild" arguments ...import_logging.logOptions.forkEnv(), // forked log options for child plugjs PATH // path with all ".../node_modules/.bin" directories }; if (coverageDir) childEnv.NODE_V8_COVERAGE = context.resolve(coverageDir); const childOptions = { ...extraOptions, stdio: ["ignore", "pipe", "pipe"], cwd: childCwd, env: childEnv, shell }; context.log.info("Executing", [cmd, ...args]); context.log.debug("Child process options", childOptions); const child = (0, import_node_child_process.spawn)(cmd, args, childOptions); try { context.log.info("Child process PID", child.pid); if (child.stdout) { const out = import_node_readline.default.createInterface(child.stdout); out.on("line", (line) => context.log.notice(line || "\xA0")); } if (child.stderr) { const err = import_node_readline.default.createInterface(child.stderr); err.on("line", (line) => context.log.warn(line || "\xA0")); } } catch (error) { child.kill(); throw error; } return new Promise((resolve, reject) => { child.on("error", (error) => reject(error)); child.on("exit", (code, signal) => { if (code === 0) return resolve(); if (signal) return reject(import_asserts.BuildFailure.withMessage(`Child process exited with signal ${signal}`)); if (code) return reject(import_asserts.BuildFailure.withMessage(`Child process exited with code ${code}`)); reject(import_asserts.BuildFailure.withMessage("Child process failed for an unknown reason")); }); }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { execChild }); //# sourceMappingURL=exec.cjs.map