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 12 commits into
base: main
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions src/loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { extname } from 'node:path';
import { globSync } from 'glob';
import { VFile } from 'vfile';

import createProgressBar 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 +28,19 @@ const createLoader = () => {
filePath => extname(filePath) === '.md'
);

const progressBar = createProgressBar('Loading files');

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

return resolvedFiles.map(async filePath => {
const fileContents = await readFile(filePath, 'utf-8');
progressBar.increment();

// 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?

Copy link
Member Author

Choose a reason for hiding this comment

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

sometimes it's let the node process running for any reasons. So yeah I think it's needed to call .stop() method.
But it's can change if we use clack

progressBar.stop();
}

return new VFile({ path: filePath, value: fileContents });
});
Expand Down
15 changes: 14 additions & 1 deletion src/parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import createQueries from './queries.mjs';

import { getRemark } from './utils/remark.mjs';
import { createNodeSlugger } from './utils/slugger.mjs';
import createProgressBar from './utils/progressBar.mjs';

/**
* Creates an API doc parser for a given Markdown API doc file
Expand Down Expand Up @@ -177,7 +178,19 @@ const createParser = () => {
const parseApiDocs = async apiDocs => {
// We do a Promise.all, to ensure that each API doc is resolved asynchronously
// but all need to be resolved first before we return the result to the caller
const resolvedApiDocEntries = await Promise.all(apiDocs.map(parseApiDoc));

const progressBar = createProgressBar('Parsing API Docs');
progressBar.start(apiDocs.length, 0);

const resolvedApiDocEntries = await Promise.all(
apiDocs.map(apiDoc => {
progressBar.increment();

return parseApiDoc(apiDoc);
})
);

progressBar.stop();

return resolvedApiDocEntries.flat();
};
Expand Down
8 changes: 8 additions & 0 deletions src/utils/generators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { coerce } from 'semver';

import createProgressBar from './progressBar.mjs';

/**
* Groups all the API metadata nodes by module (`api` property) so that we can process each different file
* based on the module it belongs to.
Expand All @@ -12,14 +14,20 @@ export const groupNodesByModule = nodes => {
/** @type {Map<string, Array<ApiDocMetadataEntry>>} */
const groupedNodes = new Map();

const progressBar = createProgressBar(groupNodesByModule.name);
progressBar.start(nodes.length, 0);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this should be inside this util :)

Copy link
Member

Choose a reason for hiding this comment

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

To clarify the only thing this util should do is to group nodes by module; This is breaking the single responsibility pattern.

Copy link
Member Author

Choose a reason for hiding this comment

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

Humm yeah


for (const node of nodes) {
if (!groupedNodes.has(node.api)) {
groupedNodes.set(node.api, []);
}

groupedNodes.get(node.api).push(node);
progressBar.increment();
}

progressBar.stop();

return groupedNodes;
};

Expand Down
24 changes: 24 additions & 0 deletions src/utils/progressBar.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { styleText } from 'node:util';

import cliProgress from 'cli-progress';

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

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

export default createProgressBar;
15 changes: 15 additions & 0 deletions src/utils/tests/progressBar.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from 'node:assert';
import { test } from 'node:test';

import cliProgress from 'cli-progress';

import createProgressBar from '../progressBar.mjs';

/**
* Simple test to unsure that the progress bar is created
* and we assume that it's work with ou style
*/
test('createProgressBar returns an instance of SingleBar', () => {
const bar = createProgressBar();
assert.ok(bar instanceof cliProgress.SingleBar);
});
Loading