Skip to content

Commit afadcc8

Browse files
authored
Merge pull request #82 from wp-blocks/process-bar
working process bar
2 parents 0123a7b + b19ecd2 commit afadcc8

4 files changed

Lines changed: 23 additions & 13 deletions

File tree

src/fs/glob.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ export const ignoreFunc = (
6565
* @param {Patterns} pattern - The pattern object containing the included and excluded file patterns.
6666
* @return A promise that resolves to an array of file paths.
6767
*/
68-
export function getFiles(args: Args, pattern: Patterns) {
69-
// Execute the glob search with the built patterns
70-
return new Glob(pattern.include, {
68+
export async function getFiles(args: Args, pattern: Patterns): Promise<string[]> {
69+
// 1. Create the Glob instance
70+
const g = new Glob(pattern.include, {
7171
ignore: {
7272
ignored: (p: Path) => ignoreFunc(p, pattern.exclude),
7373
},
7474
nodir: true,
7575
cwd: args.paths.cwd,
7676
root: args.paths.root ? path.resolve(args.paths.root) : undefined,
7777
});
78+
79+
// 2. Return the walk() promise, which resolves to an array of all file paths
80+
return g.walk();
7881
}

src/parser/exec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ export async function exec(args: Args): Promise<string> {
4545
* Extract the strings from the files
4646
*/
4747
const patterns = getPatterns(args);
48-
const files = await processFiles(patterns, args);
48+
const files = await processFiles(patterns, args, progressBar);
4949

50-
progressBar.update(2, {
50+
progressBar.start(files.length, 0, {
5151
filename: `Found ${files.length} files... `,
5252
});
5353

src/parser/process.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ export async function processFiles(
2424
const tasks: Promise<SetOfBlocks>[] = [];
2525
let processedFilesCount = 0;
2626

27-
const files = getFiles(args, patterns);
27+
const files = await getFiles(args, patterns);
2828

2929
if (progressBar) {
30-
progressBar.setTotal(Object.values(files).length);
30+
progressBar.setTotal(files.length);
3131
progressBar.update(0, {
32-
filename: `Found ${Object.values(files).length} files`,
32+
filename: `Found ${files.length} files`,
3333
});
3434
}
3535

36-
// loop through the files and parse them
37-
for await (const file of files) {
36+
// Loop through the array
37+
for (const file of files) {
3838
processedFilesCount++;
3939
const filename = path.basename(file);
4040
const ext = path.extname(file).replace(/^./, "");
@@ -56,8 +56,9 @@ export async function processFiles(
5656
}
5757

5858
if (progressBar) {
59-
progressBar.update(processedFilesCount, { filename: filename });
60-
progressBar.render();
59+
progressBar.update(processedFilesCount, {
60+
filename: `${path.basename(file)} (Valid: ${tasks.length})`
61+
});
6162
}
6263
}
6364

src/parser/taskRunner.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ export async function taskRunner(
1919
progressBar: SingleBar,
2020
) {
2121
const messages: string[] = [];
22-
await Promise.allSettled(tasks)
22+
// Create a new array of promises that update the bar when they finish.
23+
const tasksWithProgress = tasks.map((task) =>
24+
task.finally(() => {
25+
progressBar.increment();
26+
})
27+
);
28+
await Promise.allSettled(tasksWithProgress)
2329
.then((strings) => {
2430
/**
2531
* Return the strings that are not rejected (they are fulfilled)

0 commit comments

Comments
 (0)