Start Spinner Solution
Follow these steps to integrate and use a spinner in your CLI command:
Import ora
:
Open the src/commands/create.ts
file in your IDE and add the following import statement at the top of the file:typescript
Copy codeimport ora from 'ora';
Create and Start Spinner:
Add the spinner functionality inside your command’s action
method. Here’s how to do it:typescript
Copy codeimport { Command } from 'commander';
import ora from 'ora';
import prompts from 'prompts';
export const create = new Command() .name("create") .description("Create a file with JSON") .argument("fileName", "the file name to create") .action(async (fileName, opts) => { const options = await prompts([ { type: "toggle", name: "create", message: 'Would you like to create a file with JSON content?', initial: true, active: "yes", inactive: "no", } ]);
console.log("options.create", options.create);
if (options.create) {
// Create and start a spinner
const spinner = ora('Starting the file creation process...').start();
// Simulate a task
setTimeout(() => {
spinner.succeed('File creation completed!');
// You can replace the setTimeout with actual logic
}, 2000); // Simulate a 2-second task
}
});
In the above code:
- We created a spinner instance with a message indicating the start of the process.
- Used spinner.start()
to begin the spinner animation.
- Simulated a task with setTimeout
and updated the spinner state using spinner.succeed()
when the task is completed.
This setup allows your CLI to visually communicate ongoing processes to the user, enhancing the overall user experience.
Complete the following steps to finish this lesson.