UNPKG

unbag

Version:

一个专门用来开发npm工具的包

119 lines (118 loc) 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeWaitResFile = exports.readWaitResFile = exports.genWaitResAbsoluteFilePath = exports.genWaitCommand = exports.checkWaitFile = exports.WaitDefaultConfig = exports.WaitCmdName = void 0; var _nodePath = _interopRequireDefault(require("node:path")); var _fs = require("./../../utils/fs.cjs"); var _common = require("../../utils/common.cjs"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const WaitDefaultConfig = exports.WaitDefaultConfig = { timeout: 1e4, interval: 500 }; const genWaitResAbsoluteFilePath = params => { const { absoluteTempDir, tag } = params; const absoluteFilePath = _nodePath.default.resolve(absoluteTempDir, `${tag}.json`); return absoluteFilePath; }; exports.genWaitResAbsoluteFilePath = genWaitResAbsoluteFilePath; const writeWaitResFile = async params => { const fs = (0, _fs.useFs)(); const { absoluteFilePath, content, merge } = params; await fs.modifyJson(absoluteFilePath, oldJson => { if (merge) { return { ...oldJson, ...content }; } else { return content; } }); }; exports.writeWaitResFile = writeWaitResFile; const readWaitResFile = async absoluteFilePath => { const fs = (0, _fs.useFs)(); try { return fs.readJson(absoluteFilePath); } catch (error) {} }; exports.readWaitResFile = readWaitResFile; const WaitCmdName = exports.WaitCmdName = "parallel-wait"; const checkWaitFile = async params => { const startTime = Date.now(); const { absoluteFilePath, name } = params; const check = async () => { const thisCheckTime = Date.now(); const currentContent = await readWaitResFile(absoluteFilePath); if (!currentContent) { throw Error(); } await writeWaitResFile({ absoluteFilePath, merge: true, content: { lastCheckTime: thisCheckTime } }); const { timeout = WaitDefaultConfig.timeout, interval = WaitDefaultConfig.interval, finish } = currentContent; if (finish) { return { checkTime: thisCheckTime, content: currentContent }; } const timeSpan = thisCheckTime - startTime; if (timeSpan > timeout) { console.log(`${name} wait timeout`); const timeoutContent = { finish: true, result: false, message: "timeout" }; await writeWaitResFile({ absoluteFilePath, merge: true, content: { ...timeoutContent } }); return { checkTime: thisCheckTime, content: { ...currentContent, ...timeoutContent } }; } console.log(`${name} wait ...`); await (0, _common.sleep)(interval); return await check(); }; return await check(); }; exports.checkWaitFile = checkWaitFile; const genWaitCommand = params => { const { tag, name } = params || {}; let cmd = `unbag ${WaitCmdName} -n ${name} -tg ${tag}`; return cmd; }; exports.genWaitCommand = genWaitCommand;