@intlayer/chokidar
Version:
Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.
135 lines (133 loc) • 5.68 kB
JavaScript
import { colorize, getPrefix, spinnerFrames, v, x } from "@intlayer/config/logger";
import { extractErrorMessage } from "@intlayer/config/utils";
import * as ANSIColors from "@intlayer/config/colors";
import defaultConfiguration from "@intlayer/config/built";
//#region src/loadDictionaries/log.ts
var DictionariesLogger = class {
statuses = [];
spinnerTimer = null;
spinnerIndex = 0;
renderedLines = 0;
spinnerFrames = spinnerFrames;
isFinished = false;
prefix;
lastRenderedState = "";
remoteCheckInProgress = false;
expectRemote = false;
remoteError;
pluginTotal = 0;
pluginDone = 0;
pluginError;
constructor() {
this.prefix = getPrefix(defaultConfiguration.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 = 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 = extractErrorMessage(error);
this.render();
}
render() {
const { localTotal, localDone, remoteTotal, remoteDone, pluginTotal, pluginDone } = this.computeProgress();
const frame = this.spinnerFrames[this.spinnerIndex];
const clock = colorize(frame, ANSIColors.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} ${v} Local content: ${colorize(`${localDone}`, ANSIColors.GREEN)}${colorize(`/${localTotal}`, ANSIColors.GREY)}`);
else lines.push(`${this.prefix} ${clock} Local content: ${colorize(`${localDone}`, ANSIColors.BLUE)}${colorize(`/${localTotal}`, ANSIColors.GREY)}`);
if (remoteTotal > 0 || this.remoteCheckInProgress || this.remoteError) if (this.remoteError) lines.push(`${this.prefix} ${x} Remote content: ${colorize(this.remoteError, ANSIColors.RED)}`);
else if (remoteTotal === 0) lines.push(`${this.prefix} ${clock} Remote content: ${colorize("Check server", ANSIColors.BLUE)}`);
else if (isRemoteDone) lines.push(`${this.prefix} ${v} Remote content: ${colorize(`${remoteDone}`, ANSIColors.GREEN)}${colorize(`/${remoteTotal}`, ANSIColors.GREY)}`);
else lines.push(`${this.prefix} ${clock} Remote content: ${colorize(`${remoteDone}`, ANSIColors.BLUE)}${colorize(`/${remoteTotal}`, ANSIColors.GREY)}`);
if (pluginTotal > 0 || this.pluginError) if (this.pluginError) lines.push(`${this.prefix} ${x} Plugin content: ${colorize(this.pluginError, ANSIColors.RED)}`);
else if (isPluginDone) lines.push(`${this.prefix} ${v} Plugin content: ${colorize(`${pluginDone}`, ANSIColors.GREEN)}${colorize(`/${pluginTotal}`, ANSIColors.GREY)}`);
else lines.push(`${this.prefix} ${clock} Plugin content: ${colorize(`${pluginDone}`, ANSIColors.BLUE)}${colorize(`/${pluginTotal}`, ANSIColors.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
export { DictionariesLogger };
//# sourceMappingURL=log.mjs.map