@ima/plugin-cli
Version:
IMA.js Plugin CLI tool to build, link, develop IMA.js plugins.
60 lines • 2.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanOutput = cleanOutput;
exports.processOutput = processOutput;
exports.createBatcher = createBatcher;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const logger_1 = require("@ima/dev-utils/logger");
const chalk_1 = __importDefault(require("chalk"));
/**
* Clean output directories
*/
async function cleanOutput(config, cwd = process.cwd()) {
return processOutput(config, async (outputPath) => {
if (fs_1.default.existsSync(outputPath)) {
await fs_1.default.promises.rm(outputPath, { recursive: true });
}
}, cwd);
}
/**
* Run processor over each defined output directory
*/
async function processOutput(config, outputProcessor, cwd = process.cwd()) {
return Promise.all(config.output
.map(output => path_1.default.resolve(cwd, output.dir))
.map(async (outputDir) => await outputProcessor(outputDir)));
}
function createBatcher(title, timeout = 150) {
let batchedQueue = [];
let timeoutId = null;
return (fn, eventName) => {
batchedQueue.push([fn, eventName]);
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
timeoutId = setTimeout(async () => {
const stats = {
added: 0,
deleted: 0,
};
const currentQueue = [...batchedQueue];
batchedQueue = [];
await Promise.all(currentQueue.map(([fn, event]) => {
if (['add', 'change'].includes(event)) {
stats.added++;
}
else if (['unlink'].includes(event)) {
stats.deleted++;
}
return fn();
}));
logger_1.logger.write(`${(0, logger_1.printTime)()} ${title} ${chalk_1.default.green.bold(`✓ ${stats['added']}`)} ${chalk_1.default.black('|')} ${chalk_1.default.red.bold(`⛌ ${stats['deleted']}`)}`);
}, timeout);
};
}
//# sourceMappingURL=utils.js.map