@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
139 lines (137 loc) • 6.9 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
let _intlayer_config_logger = require("@intlayer/config/logger");
let _intlayer_config_utils = require("@intlayer/config/utils");
let _intlayer_config_colors = require("@intlayer/config/colors");
_intlayer_config_colors = require_runtime.__toESM(_intlayer_config_colors);
let _intlayer_config_built = require("@intlayer/config/built");
_intlayer_config_built = require_runtime.__toESM(_intlayer_config_built);
//#region src/loadDictionaries/log.ts
var DictionariesLogger = class {
statuses = [];
spinnerTimer = null;
spinnerIndex = 0;
renderedLines = 0;
spinnerFrames = _intlayer_config_logger.spinnerFrames;
isFinished = false;
prefix;
lastRenderedState = "";
remoteCheckInProgress = false;
expectRemote = false;
remoteError;
pluginTotal = 0;
pluginDone = 0;
pluginError;
constructor() {
this.prefix = (0, _intlayer_config_logger.getPrefix)(_intlayer_config_built.default.log?.prefix) ?? "";
}
setExpectRemote(expect) {
this.expectRemote = expect;
}
startRemoteCheck() {
if (this.isFinished) return;
this.remoteCheckInProgress = true;
this.startSpinner();
this.render();
}
stopRemoteCheck() {
this.remoteCheckInProgress = false;
}
update(newStatuses) {
if (this.isFinished) return;
for (const status of newStatuses) {
const index = this.statuses.findIndex((s) => s.dictionaryKey === status.dictionaryKey && s.type === status.type);
if (index >= 0) this.statuses[index] = status;
else this.statuses.push(status);
}
const { remoteTotal } = this.computeProgress();
if (this.expectRemote && !this.remoteCheckInProgress && remoteTotal === 0) return;
this.startSpinner();
this.render();
}
finish() {
this.isFinished = true;
this.stopSpinner();
this.render();
}
startSpinner() {
if (this.spinnerTimer || this.isFinished) return;
this.spinnerTimer = setInterval(() => {
this.spinnerIndex = (this.spinnerIndex + 1) % this.spinnerFrames.length;
this.render();
}, 100);
}
stopSpinner() {
if (!this.spinnerTimer) return;
clearInterval(this.spinnerTimer);
this.spinnerTimer = null;
}
setRemoteError = (error) => {
this.remoteError = (0, _intlayer_config_utils.extractErrorMessage)(error);
this.stopRemoteCheck();
this.render();
};
setPluginTotal(total) {
if (this.isFinished) return;
this.pluginTotal = total;
if (total > 0) this.startSpinner();
this.render();
}
setPluginDone(done) {
if (this.isFinished) return;
this.pluginDone = done;
this.render();
}
setPluginError(error) {
if (this.isFinished) return;
this.pluginError = (0, _intlayer_config_utils.extractErrorMessage)(error);
this.render();
}
render() {
const { localTotal, localDone, remoteTotal, remoteDone, pluginTotal, pluginDone } = this.computeProgress();
const frame = this.spinnerFrames[this.spinnerIndex];
const clock = (0, _intlayer_config_logger.colorize)(frame, _intlayer_config_colors.BLUE);
const lines = [];
const isLocalDone = localDone === localTotal;
const isRemoteDone = remoteDone === remoteTotal;
const isPluginDone = pluginDone === pluginTotal;
if (!(this.expectRemote && this.remoteCheckInProgress && remoteTotal === 0)) if (isLocalDone) lines.push(`${this.prefix} ${_intlayer_config_logger.v} Local content: ${(0, _intlayer_config_logger.colorize)(`${localDone}`, _intlayer_config_colors.GREEN)}${(0, _intlayer_config_logger.colorize)(`/${localTotal}`, _intlayer_config_colors.GREY)}`);
else lines.push(`${this.prefix} ${clock} Local content: ${(0, _intlayer_config_logger.colorize)(`${localDone}`, _intlayer_config_colors.BLUE)}${(0, _intlayer_config_logger.colorize)(`/${localTotal}`, _intlayer_config_colors.GREY)}`);
if (remoteTotal > 0 || this.remoteCheckInProgress || this.remoteError) if (this.remoteError) lines.push(`${this.prefix} ${_intlayer_config_logger.x} Remote content: ${(0, _intlayer_config_logger.colorize)(this.remoteError, _intlayer_config_colors.RED)}`);
else if (remoteTotal === 0) lines.push(`${this.prefix} ${clock} Remote content: ${(0, _intlayer_config_logger.colorize)("Check server", _intlayer_config_colors.BLUE)}`);
else if (isRemoteDone) lines.push(`${this.prefix} ${_intlayer_config_logger.v} Remote content: ${(0, _intlayer_config_logger.colorize)(`${remoteDone}`, _intlayer_config_colors.GREEN)}${(0, _intlayer_config_logger.colorize)(`/${remoteTotal}`, _intlayer_config_colors.GREY)}`);
else lines.push(`${this.prefix} ${clock} Remote content: ${(0, _intlayer_config_logger.colorize)(`${remoteDone}`, _intlayer_config_colors.BLUE)}${(0, _intlayer_config_logger.colorize)(`/${remoteTotal}`, _intlayer_config_colors.GREY)}`);
if (pluginTotal > 0 || this.pluginError) if (this.pluginError) lines.push(`${this.prefix} ${_intlayer_config_logger.x} Plugin content: ${(0, _intlayer_config_logger.colorize)(this.pluginError, _intlayer_config_colors.RED)}`);
else if (isPluginDone) lines.push(`${this.prefix} ${_intlayer_config_logger.v} Plugin content: ${(0, _intlayer_config_logger.colorize)(`${pluginDone}`, _intlayer_config_colors.GREEN)}${(0, _intlayer_config_logger.colorize)(`/${pluginTotal}`, _intlayer_config_colors.GREY)}`);
else lines.push(`${this.prefix} ${clock} Plugin content: ${(0, _intlayer_config_logger.colorize)(`${pluginDone}`, _intlayer_config_colors.BLUE)}${(0, _intlayer_config_logger.colorize)(`/${pluginTotal}`, _intlayer_config_colors.GREY)}`);
const currentState = lines.join("\n");
if (currentState === this.lastRenderedState) return;
this.lastRenderedState = currentState;
if (this.renderedLines > 0) process.stdout.write(`\x1b[${this.renderedLines}F`);
const totalLinesToClear = Math.max(this.renderedLines, lines.length);
for (let i = 0; i < totalLinesToClear; i++) {
process.stdout.write("\x1B[2K");
const line = lines[i];
if (line !== void 0) process.stdout.write(line);
process.stdout.write("\n");
}
this.renderedLines = lines.length;
}
computeProgress() {
const localKeys = new Set(this.statuses.filter((s) => s.type === "local").map((s) => s.dictionaryKey));
const localDoneKeys = new Set(this.statuses.filter((s) => s.type === "local" && (s.status === "built" || s.status === "error")).map((s) => s.dictionaryKey));
const remoteKeys = new Set(this.statuses.filter((s) => s.type === "remote").map((s) => s.dictionaryKey));
const remoteDoneKeys = new Set(this.statuses.filter((s) => s.type === "remote" && (s.status === "fetched" || s.status === "imported" || s.status === "error")).map((s) => s.dictionaryKey));
return {
localTotal: localKeys.size,
localDone: localDoneKeys.size,
remoteTotal: remoteKeys.size,
remoteDone: remoteDoneKeys.size,
pluginTotal: this.pluginTotal,
pluginDone: this.pluginDone
};
}
};
//#endregion
exports.DictionariesLogger = DictionariesLogger;
//# sourceMappingURL=log.cjs.map