diff --git a/src/parser/exec.ts b/src/parser/exec.ts index 338e231..54a0bd4 100644 --- a/src/parser/exec.ts +++ b/src/parser/exec.ts @@ -45,14 +45,14 @@ export async function exec(args: Args): Promise { * Extract the strings from the files */ const patterns = getPatterns(args); - const files = await processFiles(patterns, args, progressBar); + const { tasks, processedCount, skippedCount } = await processFiles(patterns, args, progressBar); - progressBar.start(files.length, 0, { - filename: `Found ${files.length} files... `, + progressBar.start(tasks.length, 0, { + filename: `Processing ${processedCount} files (${skippedCount} skipped)... `, }); translationsUnion = await taskRunner( - files, + tasks, translationsUnion, args, progressBar, diff --git a/src/parser/process.ts b/src/parser/process.ts index fc7fcf0..5d1e437 100644 --- a/src/parser/process.ts +++ b/src/parser/process.ts @@ -1,12 +1,21 @@ -import path from "node:path"; -import type { SingleBar } from "cli-progress"; -import type { SetOfBlocks } from "gettext-merger"; -import { allowedFormats } from "../const.js"; -import { parseJsonCallback } from "../extractors/json.js"; -import { readFileAsync } from "../fs/fs.js"; -import { getFiles } from "../fs/glob.js"; -import type { Args, Patterns } from "../types.js"; -import { doTree } from "./tree.js"; +import path from 'node:path' +import type { SingleBar } from 'cli-progress' +import type { SetOfBlocks } from 'gettext-merger' +import { parseJsonCallback } from '../extractors/json.js' +import { readFileAsync } from '../fs/fs.js' +import { getFiles } from '../fs/glob.js' +import type { Args, Patterns } from '../types.js' +import { doTree } from './tree.js' +import { allowedFormats } from '../const' + +/** + * The result of processing files. + */ +export interface ProcessResult { + tasks: Promise[]; + processedCount: number; + skippedCount: number; +} /** * Processes the given files and returns an array of promises that resolve to TranslationStrings. @@ -14,15 +23,16 @@ import { doTree } from "./tree.js"; * @param patterns * @param {Args} args - The arguments for processing the files. * @param progressBar - The progress bar element. - * @return {Promise} - An array of promises that resolve to TranslationStrings. + * @return {Promise} - The tasks and file counts. */ export async function processFiles( patterns: Patterns, args: Args, progressBar?: SingleBar, -): Promise[]> { +): Promise { const tasks: Promise[] = []; - let processedFilesCount = 0; + let processedCount = 0; + let skippedCount = 0; const files = await getFiles(args, patterns); @@ -35,32 +45,35 @@ export async function processFiles( // Loop through the array for (const file of files) { - processedFilesCount++; const filename = path.basename(file); const ext = path.extname(file).replace(/^./, ""); const fileRealPath = path.resolve(args.paths.cwd, file); if (filename === "theme.json" || filename === "block.json") { + processedCount++; tasks.push( readFileAsync(fileRealPath).then((sourceCode) => parseJsonCallback(sourceCode, args.paths.cwd, filename), ), ); } else if (allowedFormats.includes(ext)) { + processedCount++; const fileTree = readFileAsync(fileRealPath).then((content) => doTree(content, file, args.debug, args), ); if (fileTree) { tasks.push(fileTree as Promise); } + } else { + skippedCount++; } if (progressBar) { - progressBar.update(processedFilesCount, { - filename: `${path.basename(file)} (Valid: ${tasks.length})` + progressBar.update(processedCount + skippedCount, { + filename: `${path.basename(file)} (Processed: ${processedCount} | Skipped: ${skippedCount})`, }); } } - return tasks; + return { tasks, processedCount, skippedCount }; } diff --git a/src/parser/progress.ts b/src/parser/progress.ts index 9b995f7..f323b58 100644 --- a/src/parser/progress.ts +++ b/src/parser/progress.ts @@ -9,14 +9,20 @@ import type { Args } from "../types.js"; * @return {cliProgress.SingleBar} The progress bar element. */ export function initProgress(_args: Args, _filesCount: number): SingleBar { + const FILENAME_WIDTH = 40; // Set up the progress bar return new cliProgress.SingleBar( { clearOnComplete: true, etaBuffer: 1000, hideCursor: true, - format: - " {bar} {percentage}% | ETA: {eta}s | {filename} | {value}/{total}", + format: (options, params, payload) => { + const bar = options.barCompleteString?.substring(0, Math.round(params.progress * (options.barsize ?? 40))) ?? ""; + const emptyBar = options.barIncompleteString?.substring(0, (options.barsize ?? 40) - bar.length) ?? ""; + const pct = Math.round(params.progress * 100); + const filename = (payload.filename || "").substring(0, FILENAME_WIDTH).padEnd(FILENAME_WIDTH); + return ` ${bar}${emptyBar} ${pct}% | ETA: ${params.eta}s | ${filename} | ${params.value}/${params.total}`; + }, }, cliProgress.Presets.shades_classic, ); diff --git a/src/parser/taskRunner.ts b/src/parser/taskRunner.ts index 9c4d202..cfe46e1 100644 --- a/src/parser/taskRunner.ts +++ b/src/parser/taskRunner.ts @@ -21,8 +21,14 @@ export async function taskRunner( const messages: string[] = []; // Create a new array of promises that update the bar when they finish. const tasksWithProgress = tasks.map((task) => - task.finally(() => { - progressBar.increment(); + task.then((result) => { + progressBar.increment({ + filename: result.path ? path.basename(result.path) : "", + }); + return result; + }).catch((err) => { + progressBar.increment({ filename: "error" }); + throw err; }) ); await Promise.allSettled(tasksWithProgress)