@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
102 lines (100 loc) • 3.29 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../../_virtual/_rolldown/runtime.cjs');
const require_utils_runParallel_spawnPosix = require('./spawnPosix.cjs');
const require_utils_runParallel_spawnWin32 = require('./spawnWin32.cjs');
let node_path = require("node:path");
//#region src/utils/runParallel/index.ts
/**
* Start a cross-platform parallel process using npm-run-all approach.
* Accepts either a single string (e.g., 'next start') or an array of tokens (e.g., ['next', 'start']).
*/
const runParallel = (proc) => {
if (!proc || Array.isArray(proc) && proc.length === 0) throw new Error("Invalid command");
const commandText = Array.isArray(proc) ? proc.join(" ") : proc;
const isArray = Array.isArray(proc);
const command = isArray ? proc[0] : commandText;
const args = isArray ? proc.slice(1) : [];
const cwdBin = (0, node_path.join)(process.cwd(), "node_modules", ".bin");
const PATH_KEY = Object.keys(process.env).find((key) => key.toLowerCase() === "path") ?? "PATH";
const extendedPath = [cwdBin, process.env[PATH_KEY] ?? ""].filter(Boolean).join(node_path.delimiter);
const childEnv = {
...process.env,
[PATH_KEY]: extendedPath
};
const isWin = process.platform === "win32";
const spawnFunc = isWin ? require_utils_runParallel_spawnWin32.spawnWin32 : require_utils_runParallel_spawnPosix.spawnPosix;
const spawnOptions = {
cwd: process.cwd(),
stdio: "inherit",
env: childEnv,
shell: isWin
};
const child = isArray ? args.length === 0 && /\s/.test(command) ? isWin ? spawnFunc(process.env.ComSpec ?? "cmd.exe", [
"/d",
"/s",
"/c",
command
], spawnOptions) : spawnFunc(process.env.SHELL ?? "/bin/sh", ["-c", command], spawnOptions) : spawnFunc(command, args, spawnOptions) : isWin ? spawnFunc(process.env.ComSpec ?? "cmd.exe", [
"/d",
"/s",
"/c",
commandText
], spawnOptions) : spawnFunc(process.env.SHELL ?? "/bin/sh", ["-c", commandText], spawnOptions);
const result = new Promise((resolve, reject) => {
child.on("error", (err) => {
try {
console.error(`[runParallel] Failed to start: ${err?.message ?? String(err)}`);
} catch {}
reject(err);
});
child.on("exit", (code, signal) => {
if (code === 0 || new Set([
129,
130,
131,
143
]).has(code ?? -1) || signal && [
"SIGINT",
"SIGTERM",
"SIGQUIT",
"SIGHUP"
].includes(signal)) resolve();
else reject(Object.assign(/* @__PURE__ */ new Error("Parallel process failed"), {
code,
signal
}));
});
});
const kill = () => {
try {
child.kill("SIGTERM");
} catch {}
};
const handleExit = () => kill();
const handleSigInt = () => {
kill();
process.off("SIGINT", handleSigInt);
process.kill(process.pid, "SIGINT");
};
const handleSigTerm = () => {
kill();
process.off("SIGTERM", handleSigTerm);
process.kill(process.pid, "SIGTERM");
};
process.on("exit", handleExit);
process.on("SIGINT", handleSigInt);
process.on("SIGTERM", handleSigTerm);
child.on("exit", () => {
process.off("exit", handleExit);
process.off("SIGINT", handleSigInt);
process.off("SIGTERM", handleSigTerm);
});
return {
kill,
result,
commandText
};
};
//#endregion
exports.runParallel = runParallel;
//# sourceMappingURL=index.cjs.map