cl-generate
Version:
A cross-platform CLI tool to generate NestJS clean architecture modules
49 lines (43 loc) • 1.14 kB
JavaScript
const { COLORS } = require("../constants/index");
class LoadingSpinner {
constructor() {
this.spinner = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
this.index = 0;
this.interval = null;
}
start(message) {
process.stdout.write(
`${COLORS.BLUE}${message} ${this.spinner[this.index]}${COLORS.NC}`
);
this.interval = setInterval(() => {
this.index = (this.index + 1) % this.spinner.length;
process.stdout.write(
`\r${COLORS.BLUE}${message} ${this.spinner[this.index]}${COLORS.NC}`
);
}, 80);
return this; // Return instance for chaining
}
stop() {
if (this.interval) {
clearInterval(this.interval);
process.stdout.write("\r");
}
}
}
// Global function to handle loading with async operations
async function withLoading(message, operation) {
const spinner = new LoadingSpinner();
try {
spinner.start(message);
const result = await operation();
spinner.stop();
return result;
} catch (error) {
spinner.stop();
throw error;
}
}
module.exports = {
LoadingSpinner,
withLoading,
};