feat: enhance CLI commands with improved flags and error handling

This commit introduces several improvements across multiple CLI commands:
- Added new flags for more flexible command usage
- Implemented interactive mode for commands when flags are not provided
- Improved error handling and messaging
- Added support for environment variable authentication
- Standardized confirmation prompts and error messages
- Enhanced type safety with TypeScript improvements
This commit is contained in:
Mauricio Siu
2025-02-23 21:29:45 -06:00
parent 7cb5369f15
commit 10cfe23763
26 changed files with 2483 additions and 1621 deletions

View File

@@ -1,4 +1,4 @@
import { Command } from "@oclif/core";
import { Command, Flags } from "@oclif/core";
import { readAuthConfig } from "../../utils/utils.js";
import chalk from "chalk";
import inquirer from "inquirer";
@@ -11,79 +11,109 @@ export default class AppStop extends Command {
static examples = ["$ <%= config.bin %> app stop"];
static flags = {
projectId: Flags.string({
char: 'p',
description: 'ID of the project',
required: false,
}),
applicationId: Flags.string({
char: 'a',
description: 'ID of the application to stop',
required: false,
}),
skipConfirm: Flags.boolean({
char: 'y',
description: 'Skip confirmation prompt',
default: false,
})
};
public async run(): Promise<void> {
const auth = await readAuthConfig(this);
const { flags } = await this.parse(AppStop);
let { projectId, applicationId } = flags;
console.log(chalk.blue.bold("\n Listing all Projects \n"));
// Modo interactivo si no se proporcionan los flags necesarios
if (!projectId || !applicationId) {
console.log(chalk.blue.bold("\n Listing all Projects \n"));
const projects = await getProjects(auth, this);
const projects = await getProjects(auth, this);
if (!projectId) {
const { project } = await inquirer.prompt<Answers>([
{
choices: projects.map((project) => ({
name: project.name,
value: project,
})),
message: "Select a project to stop the application from:",
name: "project",
type: "list",
},
]);
projectId = project.projectId;
}
const { project } = await inquirer.prompt<Answers>([
{
choices: projects.map((project) => ({
name: project.name,
value: project,
})),
message: "Select a project to stop the application in:",
name: "project",
type: "list",
},
]);
const projectSelected = await getProject(projectId, auth, this);
const projectId = project.projectId;
if (projectSelected.applications.length === 0) {
this.error(chalk.yellow("No applications found in this project."));
}
const projectSelected = await getProject(projectId, auth, this);
if (projectSelected.applications.length === 0) {
this.error(chalk.yellow("No applications found in this project."));
if (!applicationId) {
const appAnswers = await inquirer.prompt([
{
choices: projectSelected.applications.map((app: { name: string; applicationId: string }) => ({
name: app.name,
value: app.applicationId,
})),
message: "Select the application to stop:",
name: "selectedApp",
type: "list",
},
]);
applicationId = appAnswers.selectedApp;
}
}
const appAnswers = await inquirer.prompt([
{
// @ts-ignore
choices: projectSelected.applications.map((app) => ({
name: app.name,
value: app.applicationId,
})),
message: "Select the application to stop:",
name: "selectedApp",
type: "list",
},
]);
const applicationId = appAnswers.selectedApp;
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to stop this application?",
name: "confirmDelete",
type: "confirm",
},
]);
if (!confirmAnswers.confirmDelete) {
this.error(chalk.yellow("Application stop cancelled."));
}
const response = await axios.post(
`${auth.url}/api/trpc/application.stop`,
{
json: {
applicationId,
// Confirmar si no se especifica --skipConfirm
if (!flags.skipConfirm) {
const confirmAnswers = await inquirer.prompt([
{
default: false,
message: "Are you sure you want to stop this application?",
name: "confirmDelete",
type: "confirm",
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
]);
if (response.status !== 200) {
this.error(chalk.red("Error stopping application"));
if (!confirmAnswers.confirmDelete) {
this.error(chalk.yellow("Application stop cancelled."));
}
}
try {
const response = await axios.post(
`${auth.url}/api/trpc/application.stop`,
{
json: {
applicationId,
},
},
{
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
},
);
if (response.status !== 200) {
this.error(chalk.red("Error stopping application"));
}
this.log(chalk.green("Application stop successful."));
} catch (error: any) {
this.error(chalk.red(`Error stopping application: ${error.message}`));
}
this.log(chalk.green("Application stop successful."));
}
}