Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/parser/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ export async function exec(args: Args): Promise<string> {
* 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,
Expand Down
45 changes: 29 additions & 16 deletions src/parser/process.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
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<SetOfBlocks>[];
processedCount: number;
skippedCount: number;
}

/**
* Processes the given files and returns an array of promises that resolve to TranslationStrings.
*
* @param patterns
* @param {Args} args - The arguments for processing the files.
* @param progressBar - The progress bar element.
* @return {Promise<SetOfBlocks[]>} - An array of promises that resolve to TranslationStrings.
* @return {Promise<ProcessResult>} - The tasks and file counts.
*/
export async function processFiles(
patterns: Patterns,
args: Args,
progressBar?: SingleBar,
): Promise<Promise<SetOfBlocks>[]> {
): Promise<ProcessResult> {
const tasks: Promise<SetOfBlocks>[] = [];
let processedFilesCount = 0;
let processedCount = 0;
let skippedCount = 0;

const files = await getFiles(args, patterns);

Expand All @@ -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<SetOfBlocks>);
}
} 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 };
}
10 changes: 8 additions & 2 deletions src/parser/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
10 changes: 8 additions & 2 deletions src/parser/taskRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down