Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(progress-bar): setup basic #98

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
42 changes: 42 additions & 0 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 @@ -27,6 +27,7 @@
"prettier": "3.3.3"
},
"dependencies": {
"cli-progress": "^3.12.0",
"commander": "^12.1.0",
"github-slugger": "^2.0.0",
"glob": "^11.0.0",
Expand Down
14 changes: 14 additions & 0 deletions src/generators/legacy-html/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import tableOfContents from './utils/tableOfContents.mjs';

import { groupNodesByModule } from '../../utils/generators.mjs';
import { getRemarkRehype } from '../../utils/remark.mjs';
import {
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
createProgressBar,
startProgressBar,
updateProgressBar,
stopProgressBar,
} from '../../utils/progressBar.mjs';

/**
* @typedef {{
Expand Down Expand Up @@ -147,6 +153,10 @@ export default {
return replaceTemplateValues(generatedTemplate);
};

// Creates a progress bar to show the progress of the generation process
const progressBar = createProgressBar('Generating HTML files');
startProgressBar(progressBar, headNodes.length);

for (const node of headNodes) {
const result = processModuleNodes(node);

Expand All @@ -157,8 +167,12 @@ export default {
});

await writeFile(join(output, `${node.api}.html`), minified);
updateProgressBar(progressBar);
}

// Stops the progress bar and clears the line
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
stopProgressBar(progressBar);

// Define the output folder for API docs assets
const assetsFolder = join(output, 'assets');

Expand Down
17 changes: 17 additions & 0 deletions src/loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { extname } from 'node:path';
import { globSync } from 'glob';
import { VFile } from 'vfile';

import {
createProgressBar,
startProgressBar,
updateProgressBar,
stopProgressBar,
} from './utils/progressBar.mjs';

/**
* This method creates a simple abstract "Loader", which technically
* could be used for different things, but here we want to use it to load
Expand All @@ -26,8 +33,18 @@ const createLoader = () => {
filePath => extname(filePath) === '.md'
);

const progressBar = createProgressBar('Loading files');

startProgressBar(progressBar, resolvedFiles.length);
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved

return resolvedFiles.map(async filePath => {
const fileContents = await readFile(filePath, 'utf-8');
updateProgressBar(progressBar);
// normally we stop the progress bar when the loop is done
// but here we return the loop so we need to stop it when the last file is loaded
if (progressBar.value === progressBar.total) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the .stop() call needed? what happens if we simply stop incrementing when value === total?

stopProgressBar(progressBar);
}

return new VFile({ path: filePath, value: fileContents });
});
Expand Down
43 changes: 43 additions & 0 deletions src/utils/progressBar.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { styleText } from 'node:util';

import cliProgress from 'cli-progress';

/**
*
AugustinMauroy marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} label
* @returns {import('cli-progress').SingleBar}
*/
export function createProgressBar(label = '') {
const format = ` ${styleText(['bold', 'green'], '{bar}')} | ${label} {percentage}% | {value}/{total}`;

return new cliProgress.SingleBar({
format,
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: true,
});
}

/**
*
* @param {import('cli-progress').SingleBar} bar
* @param {number} total
*/
export function startProgressBar(bar, total) {
bar.start(total, 0);
}

/**
* @param {import('cli-progress').SingleBar} bar
* @param {number} value
*/
export function updateProgressBar(bar, value = 1) {
bar.update(value);
}

/**
* @param {import('cli-progress').SingleBar} bar
*/
export function stopProgressBar(bar) {
bar.stop();
}
30 changes: 30 additions & 0 deletions src/utils/tests/progressBar.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';

import cliProgress from 'cli-progress';

import {
createProgressBar,
startProgressBar,
stopProgressBar,
} from '../progressBar.mjs';

describe('progressBar', () => {
it('createProgressBar returns an instance of SingleBar', () => {
const bar = createProgressBar();
assert.ok(bar instanceof cliProgress.SingleBar);
});

it('startProgressBar sets the correct total value', () => {
const bar = createProgressBar();
startProgressBar(bar, 100);
assert.strictEqual(bar.getTotal(), 100);
});

it('stopProgressBar stops the progress bar', () => {
const bar = createProgressBar();
startProgressBar(bar, 100);
stopProgressBar(bar);
assert.strictEqual(bar.isActive, false);
});
});
Loading