Skip to content

Commit

Permalink
feat: use zolt spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
Alcadramin committed Oct 1, 2024
1 parent f7101fa commit b81a720
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
20 changes: 20 additions & 0 deletions .versionrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"bumpFiles": [
"package.json"
],
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"section": "Maintenance",
"hidden": false
}
]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
"express": "^4.21.0",
"prettier": "^3.3.3",
"puppeteer": "^23.4.1",
"readline": "^1.3.0"
"readline": "^1.3.0",
"zolt": "^1.0.0"
},
"husky": {
"hooks": {
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

54 changes: 20 additions & 34 deletions src/utils/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import chalk from 'chalk';
import readline from 'readline';
import { Zolt } from 'zolt';

type MessageType = 'error' | 'success' | 'info';
type RouteInfo = { route: string; size: number };

let spinnerInterval: NodeJS.Timeout | null = null;
let spinnerFrameIndex = 0;
let spinnerStartTime: number | null = null; // To store the time when spinner starts
const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];

/**
* @param {any} msg Message or array of routes with size to print.
Expand All @@ -18,30 +15,26 @@ export const printMessage = (msg: any, type: MessageType, errorSummary?: any): v
switch (type) {
case 'error':
stopSpinner(false);
console.error(`${chalk.red.bold('\u{10102}')} ${msg}\n${errorSummary?.stack || errorSummary}\n`);
console.error(
`${chalk.red.bold('\u{10102}')} ${msg}\n${errorSummary?.stack || errorSummary}\n`,
);
break;
case 'success': {
const routes: RouteInfo[] = msg;
if (routes && routes.length) {
routes.forEach((routeInfo) => {
console.log(`${chalk.green.bold('\u{1F5F8}')} generated ${routeInfo.route} (${routeInfo.size}kb)`);
routes.forEach(routeInfo => {
console.log(
`${chalk.green.bold('\u{1F5F8}')} generated ${routeInfo.route} (${routeInfo.size}kb)`,
);
});
} else {
console.log('No pages were generated.');
}
break;
}
case 'info': {
spinnerFrameIndex = 0;
if (!spinnerInterval) {
spinnerStartTime = Date.now(); // Capture the time when spinner starts
spinnerInterval = setInterval(() => {
const frame = spinnerFrames[spinnerFrameIndex];
readline.cursorTo(process.stdout, 0);
process.stdout.write(`${frame} Processing pages...`);
spinnerFrameIndex = (spinnerFrameIndex + 1) % spinnerFrames.length;
}, 80);
}
Zolt.start('dots', 'green', 'Processing pages...');
spinnerStartTime = Date.now();
break;
}
default:
Expand All @@ -54,22 +47,15 @@ export const printMessage = (msg: any, type: MessageType, errorSummary?: any): v
* @param {boolean} success Whether the processing was successful.
*/
export const stopSpinner = (success: boolean = true): void => {
if (spinnerInterval) {
clearInterval(spinnerInterval);
spinnerInterval = null;

const endTime = Date.now(); // Capture the time when the spinner stops
const elapsedTime = ((endTime - (spinnerStartTime || 0)) / 1000).toFixed(2);

readline.cursorTo(process.stdout, 0); // Move cursor to start of line
readline.clearLine(process.stdout, 0); // Clear the current line

if (success) {
console.log(chalk.green.bold(`All pages are processed. Took ${elapsedTime} seconds.`));
} else {
console.log(chalk.red.bold(`Error processing routes.`));
}

spinnerStartTime = null;
const endTime = Date.now(); // Capture the time when the spinner stops
const elapsedTime = ((endTime - (spinnerStartTime || 0)) / 1000).toFixed(2);
let finishText: string = '';

if (success) {
finishText = chalk.green.bold(`All pages are processed. Took ${elapsedTime} seconds.`);
} else {
finishText = chalk.red.bold(`Error processing routes.`);
}

Zolt.stop(() => console.log(finishText));
};

0 comments on commit b81a720

Please sign in to comment.