lazy-js-utils
Version:
A collection of lazy-loaded JavaScript utilities for efficient development
126 lines (122 loc) • 3.69 kB
JavaScript
import {
isPlainObject,
parallel
} from "./chunk-LIKJRYBJ.mjs";
import {
isArray,
isStr
} from "./chunk-4N54TZJJ.mjs";
// src/node/jsShell.ts
import child_process from "child_process";
import process from "process";
function jsShell(commander, options) {
let args = [];
let stdio;
let errorExit = true;
let isLog = false;
let cwd;
let _options = {};
if (isPlainObject(options)) {
args = options.args ?? [];
stdio = options.stdio;
errorExit = options.errorExit ?? true;
isLog = options.isLog ?? false;
cwd = options.cwd;
_options = options.options ?? {};
} else if (options) {
stdio = options;
}
return isArray(commander) ? Promise.all(commander.map((command) => executor(command))) : executor(commander);
function executor(commander2) {
return new Promise((resolve, reject) => {
var _a, _b;
const child = child_process.spawn(commander2, args, {
shell: true,
stdio,
cwd,
..._options
});
const pid = child.pid;
let result = "";
let errorOutput = "";
(_a = child.stdout) == null ? void 0 : _a.on("data", (data) => {
result += data.toString();
});
(_b = child.stderr) == null ? void 0 : _b.on("data", (data) => {
if (isLog)
console.error(data.toString());
errorOutput += data.toString();
});
child.on("close", (status) => {
result = result.trim();
if (status === 130) {
if (isLog)
console.log("\u5DF2\u53D6\u6D88...");
resolve({ status, result, pid });
} else if (status !== 0) {
if (isLog)
console.error(`Command failed with status ${status}`);
if (errorExit) {
reject(new Error(result || errorOutput));
process.exit(1);
} else {
resolve({ status, result: result || errorOutput, pid });
}
} else {
resolve({ status, result, pid });
}
});
child.on("error", (error) => {
if (isLog)
console.error(error.message);
if (errorExit) {
reject(new Error(error.message));
process.exit(1);
} else {
resolve({ status: null, result: error.message, pid });
}
});
});
}
}
// src/node/useNodeWorker.ts
import worker_threads from "worker_threads";
import path from "path";
import process2 from "process";
import fs from "fs";
async function useNodeWorker(payload, url) {
if (!url) {
url = path.resolve(
__dirname,
"../node_modules/lazy-js-utils/dist/worker/useNodeWorkerThread.mjs"
);
if (!fs.existsSync(url))
url = path.resolve(__dirname, "./worker/useNodeWorkerThread.mjs");
}
const { params } = isStr(payload) ? payload = { params: payload } : payload;
const commands = isArray(params) ? params : params.split("&&");
const result = await parallel(commands, (params2) => createWorker(
Object.assign(payload, {
params: params2
})
));
setTimeout(process2.exit);
return result.length === 1 ? result[0] : result;
function createWorker(payload2) {
const { Worker } = worker_threads;
return new Promise((resolve) => {
const seprateThread = new Worker(url);
seprateThread.on("message", resolve);
seprateThread.postMessage(payload2);
});
}
}
function useProcressNodeWorker(callback) {
const { parentPort } = worker_threads;
parentPort.on("message", async (data) => parentPort == null ? void 0 : parentPort.postMessage(await (callback == null ? void 0 : callback(data)) || (() => "")));
}
export {
jsShell,
useNodeWorker,
useProcressNodeWorker
};