UNPKG

lazy-js-utils

Version:

A collection of lazy-loaded JavaScript utilities for efficient development

126 lines (112 loc) 4.59 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _chunk53V2MMRCjs = require('./chunk-53V2MMRC.js'); var _chunk3KT5TQ4Gjs = require('./chunk-3KT5TQ4G.js'); // src/node/jsShell.ts var _child_process = require('child_process'); var _child_process2 = _interopRequireDefault(_child_process); var _process = require('process'); var _process2 = _interopRequireDefault(_process); function jsShell(commander, options) { let args = []; let stdio; let errorExit = true; let isLog = false; let cwd; let _options = {}; if (_chunk53V2MMRCjs.isPlainObject.call(void 0, options)) { args = _nullishCoalesce(options.args, () => ( [])); stdio = options.stdio; errorExit = _nullishCoalesce(options.errorExit, () => ( true)); isLog = _nullishCoalesce(options.isLog, () => ( false)); cwd = options.cwd; _options = _nullishCoalesce(options.options, () => ( {})); } else if (options) { stdio = options; } return _chunk3KT5TQ4Gjs.isArray.call(void 0, commander) ? Promise.all(commander.map((command) => executor(command))) : executor(commander); function executor(commander2) { return new Promise((resolve, reject) => { var _a, _b; const child = _child_process2.default.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)); _process2.default.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)); _process2.default.exit(1); } else { resolve({ status: null, result: error.message, pid }); } }); }); } } // src/node/useNodeWorker.ts var _worker_threads = require('worker_threads'); var _worker_threads2 = _interopRequireDefault(_worker_threads); var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs); async function useNodeWorker(payload, url) { if (!url) { url = _path2.default.resolve( __dirname, "../node_modules/lazy-js-utils/dist/worker/useNodeWorkerThread.mjs" ); if (!_fs2.default.existsSync(url)) url = _path2.default.resolve(__dirname, "./worker/useNodeWorkerThread.mjs"); } const { params } = _chunk3KT5TQ4Gjs.isStr.call(void 0, payload) ? payload = { params: payload } : payload; const commands = _chunk3KT5TQ4Gjs.isArray.call(void 0, params) ? params : params.split("&&"); const result = await _chunk53V2MMRCjs.parallel.call(void 0, commands, (params2) => createWorker( Object.assign(payload, { params: params2 }) )); setTimeout(_process2.default.exit); return result.length === 1 ? result[0] : result; function createWorker(payload2) { const { Worker } = _worker_threads2.default; return new Promise((resolve) => { const seprateThread = new Worker(url); seprateThread.on("message", resolve); seprateThread.postMessage(payload2); }); } } function useProcressNodeWorker(callback) { const { parentPort } = _worker_threads2.default; parentPort.on("message", async (data) => parentPort == null ? void 0 : parentPort.postMessage(await (callback == null ? void 0 : callback(data)) || (() => ""))); } exports.jsShell = jsShell; exports.useNodeWorker = useNodeWorker; exports.useProcressNodeWorker = useProcressNodeWorker;