unbag
Version:
一个专门用来开发npm工具的包
111 lines (110 loc) • 3.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.parallel = exports.ParallelDefaultConfig = void 0;
var _uuid = require("uuid");
var _wait = require("./wait.cjs");
var _concurrently = _interopRequireDefault(require("concurrently"));
var _path = require("../../utils/path.cjs");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const ParallelDefaultConfig = exports.ParallelDefaultConfig = {
wait: _wait.WaitDefaultConfig,
commands: [],
groups: {}
};
const parallel = async config => {
const needWaitCmdMap = /* @__PURE__ */new Map();
const {
parallel: parallel2,
root,
tempDir
} = config;
const {
commands,
groupName,
groups
} = parallel2;
const path = (0, _path.usePath)();
const parallelTempDir = path.resolve(root, tempDir, "parallel");
const parallelWaitTempDir = path.resolve(parallelTempDir, "wait");
const finalCommands = (groupName ? groups[groupName] : commands) || [];
const commandList = finalCommands.map(e => {
let command = `${e.npmScript}`;
if (e.wait?.func) {
const waitTag = `${e.name}_wait_${(0, _uuid.v4)()}`;
const waitCmd = (0, _wait.genWaitCommand)({
tag: waitTag,
name: e.name
});
command = `${waitCmd} && ${command}`;
needWaitCmdMap.set(waitTag, e);
}
return {
name: e.name,
command
};
});
const waitFuncObj = Object.fromEntries(needWaitCmdMap.entries());
await Promise.all(Object.keys(waitFuncObj).map(async tag => {
const cmd = waitFuncObj[tag];
if (!cmd?.wait?.func) {
return;
}
const waitResAbsoluteFilePath = (0, _wait.genWaitResAbsoluteFilePath)({
absoluteTempDir: parallelWaitTempDir,
tag
});
await (0, _wait.writeWaitResFile)({
absoluteFilePath: waitResAbsoluteFilePath,
content: {
tag,
lastUpdateTime: Date.now(),
finish: false,
interval: cmd.wait.interval || _wait.WaitDefaultConfig.interval,
timeout: cmd.wait.interval || _wait.WaitDefaultConfig.timeout
}
});
}));
(0, _concurrently.default)(commandList, {
prefixColors: "auto",
prefix: "[{time}]-[{name}]",
timestampFormat: "HH:mm:ss",
killOthers: ["failure"]
});
Object.keys(waitFuncObj).map(async tag => {
const cmd = waitFuncObj[tag];
if (!cmd?.wait?.func) {
return;
}
const waitResAbsoluteFilePath = (0, _wait.genWaitResAbsoluteFilePath)({
absoluteTempDir: parallelWaitTempDir,
tag
});
try {
const res = await cmd.wait.func();
await (0, _wait.writeWaitResFile)({
absoluteFilePath: waitResAbsoluteFilePath,
merge: true,
content: {
lastUpdateTime: Date.now(),
finish: true,
result: res,
message: res ? "success" : "false"
}
});
} catch (error) {
await (0, _wait.writeWaitResFile)({
absoluteFilePath: waitResAbsoluteFilePath,
merge: true,
content: {
finish: true,
lastUpdateTime: Date.now(),
result: false,
message: error.message || "unknown error"
}
});
}
});
};
exports.parallel = parallel;