@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
155 lines (153 loc) • 3.64 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../../_virtual/_rolldown/runtime.cjs');
let node_stream = require("node:stream");
//#region src/utils/runParallel/runTask.ts
/**
* Remove the given value from the array.
*/
const remove = (array, x) => {
const index = array.indexOf(x);
if (index !== -1) array.splice(index, 1);
};
const signals = {
SIGABRT: 6,
SIGALRM: 14,
SIGBUS: 10,
SIGCHLD: 20,
SIGCONT: 19,
SIGFPE: 8,
SIGHUP: 1,
SIGILL: 4,
SIGINT: 2,
SIGKILL: 9,
SIGPIPE: 13,
SIGQUIT: 3,
SIGSEGV: 11,
SIGSTOP: 17,
SIGTERM: 15,
SIGTRAP: 5,
SIGTSTP: 18,
SIGTTIN: 21,
SIGTTOU: 22,
SIGUSR1: 30,
SIGUSR2: 31
};
/**
* Converts a signal name to a number.
*/
const convert = (signal) => {
return signals[signal] || 0;
};
/**
* Simple in-memory writable stream
*/
var MemoryStream = class extends node_stream.Writable {
chunks = [];
_write(chunk, _encoding, callback) {
this.chunks.push(chunk);
callback();
}
toString() {
return Buffer.concat(this.chunks).toString("utf8");
}
};
/**
* Run npm-scripts of given names in parallel.
*
* If a npm-script exited with a non-zero code, this aborts other all npm-scripts.
*
* Note: This is a simplified version for our use case.
* The full implementation would require the actual runTask function from npm-run-all.
*/
const runTasks = (tasks, options) => {
return new Promise((resolve, reject) => {
if (tasks.length === 0) {
resolve([]);
return;
}
const results = tasks.map((task) => ({
name: task,
code: void 0
}));
const queue = tasks.map((task, index) => ({
name: task,
index
}));
const promises = [];
let error = null;
let aborted = false;
/**
* Done.
*/
const done = () => {
if (error == null) resolve(results);
else reject(error);
};
/**
* Aborts all tasks.
*/
const abort = () => {
if (aborted) return;
aborted = true;
if (promises.length === 0) done();
else {
for (const p of promises) p.abort?.();
Promise.all(promises).then(done, reject);
}
};
/**
* Runs a next task.
*/
const next = () => {
if (aborted) return;
if (queue.length === 0) {
if (promises.length === 0) done();
return;
}
const originalOutputStream = options.stdout;
const optionsClone = { ...options };
const writer = new MemoryStream();
if (options.aggregateOutput) optionsClone.stdout = writer;
const task = queue.shift();
const promise = Promise.resolve({
name: task.name,
code: 0,
signal: null
});
promises.push(promise);
promise.then((result) => {
remove(promises, promise);
if (aborted) return;
if (options.aggregateOutput) originalOutputStream.write(writer.toString());
if (result.code === null && result.signal !== null && result.signal !== void 0) result.code = 128 + convert(result.signal);
results[task.index].code = result.code;
if (result.code) {
error = /* @__PURE__ */ new Error(`Task ${result.name} failed with code ${result.code}`);
if (!options.continueOnError) {
abort();
return;
}
}
if (options.race && !result.code) {
abort();
return;
}
next();
}, (thisError) => {
remove(promises, promise);
if (!options.continueOnError || options.race) {
error = thisError;
abort();
return;
}
next();
});
};
const max = options.maxParallel;
const end = typeof max === "number" && max > 0 ? Math.min(tasks.length, max) : tasks.length;
for (let i = 0; i < end; ++i) next();
});
};
//#endregion
exports.runTasks = runTasks;
//# sourceMappingURL=runTask.cjs.map