Skip to content
Open
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
2 changes: 2 additions & 0 deletions bin/commands/generate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const { runGenerators } = createGenerator();
* @property {string} index
* @property {boolean} minify
* @property {string} typeMap
* @property {boolean} progress
*/

export default new Command('generate')
Expand Down Expand Up @@ -58,6 +59,7 @@ export default new Command('generate')
.addOption(new Option('--index <url>', 'index.md URL or path'))
.addOption(new Option('--minify', 'Minify?'))
.addOption(new Option('--type-map <url>', 'Type map URL or path'))
.addOption(new Option('--no-progress', 'Disable the progress bar'))

.action(
errorWrap(async opts => {
Expand Down
142 changes: 63 additions & 79 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@rollup/plugin-virtual": "^3.0.2",
"@swc/html-wasm": "^1.15.18",
"acorn": "^8.16.0",
"cli-progress": "^3.12.0",
"commander": "^14.0.3",
"dedent": "^1.7.1",
"estree-util-to-js": "^2.0.0",
Expand Down
19 changes: 13 additions & 6 deletions src/generators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import logger from './logger/index.mjs';
import { isAsyncGenerator, createStreamingCache } from './streaming.mjs';
import createWorkerPool from './threading/index.mjs';
import createParallelWorker from './threading/parallel.mjs';
import createProgressBar from './utils/progressBar.mjs';

const generatorsLogger = logger.child('generators');

Expand Down Expand Up @@ -92,7 +93,7 @@ const createGenerator = () => {
/**
* Runs all requested generators with their dependencies.
*
* @param {import('./utils/configuration/types').Configuration} options - Runtime options
* @param {import('./utils/configuration/types').Configuration} configuration - Runtime options
* @returns {Promise<unknown[]>} Results of all requested generators
*/
const runGenerators = async configuration => {
Expand All @@ -111,6 +112,9 @@ const createGenerator = () => {
await scheduleGenerator(name, configuration);
}

const progress = createProgressBar({ enabled: configuration.progress });
progress?.start(generators.length, 0, { phase: 'Starting...' });

// Start all collections in parallel (don't await sequentially)
const resultPromises = generators.map(async name => {
let result = await cachedGenerators[name];
Expand All @@ -119,14 +123,17 @@ const createGenerator = () => {
result = await streamingCache.getOrCollect(name, result);
}

progress?.increment({ phase: name });
return result;
});

const results = await Promise.all(resultPromises);

await pool.destroy();

return results;
try {
const results = await Promise.all(resultPromises);
return results;
} finally {
await pool.destroy();
progress?.stop();
}
};

return { runGenerators };
Expand Down
2 changes: 2 additions & 0 deletions src/utils/configuration/__tests__/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe('config.mjs', () => {
target: 'json',
threads: 4,
chunkSize: 5,
progress: true,
};

const config = createConfigFromCLIOptions(options);
Expand All @@ -112,6 +113,7 @@ describe('config.mjs', () => {
target: 'json',
threads: 4,
chunkSize: 5,
progress: true,
});
});

Expand Down
2 changes: 2 additions & 0 deletions src/utils/configuration/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const getDefaultConfig = lazy(() =>

threads: cpus().length,
chunkSize: 10,
progress: true,
})
)
);
Expand Down Expand Up @@ -96,6 +97,7 @@ export const createConfigFromCLIOptions = options => ({
target: options.target,
threads: options.threads,
chunkSize: options.chunkSize,
progress: options.progress,
});

/**
Expand Down
1 change: 1 addition & 0 deletions src/utils/configuration/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type Configuration = {

// Number of items to process per worker thread
chunkSize: number;
progress: boolean;
} & {
[K in keyof AllGenerators]: GlobalConfiguration &
AllGenerators[K]['defaultConfiguration'];
Expand Down
30 changes: 30 additions & 0 deletions src/utils/progressBar.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

import { SingleBar, Presets } from 'cli-progress';

/**
* Creates a progress bar for the generation pipeline.
* Writes to stderr to avoid conflicts with the logger (stdout).
* Returns a no-op object if disabled or if stderr is not a TTY (e.g. CI).
*
* @param {object} [options]
* @param {boolean} [options.enabled=true] Whether to render the progress bar
* @returns {SingleBar | null}
*/
const createProgressBar = ({ enabled = true } = {}) => {
if (!enabled || !process.stderr.isTTY) {
return null;
}

return new SingleBar(
{
stream: process.stderr,
format: ' {phase} [{bar}] {percentage}% | {value}/{total}',
hideCursor: true,
clearOnComplete: false,
},
Presets.shades_grey
);
};

export default createProgressBar;
Loading