feat: enhance app deployment command with environment selection

- Added environmentId flag to the AppDeploy command for specifying the environment.
- Implemented interactive prompts for selecting project, environment, and application if flags are not provided.
- Updated type definitions to include Application and Environment types for better type safety.
- Improved error handling for cases with no available environments or applications.
This commit is contained in:
Mauricio Siu
2025-10-05 01:33:03 -06:00
parent 82aed684b9
commit d32c3b2be2
3 changed files with 64 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
import { Command, Flags } from "@oclif/core"; import { Command, Flags } from "@oclif/core";
import { readAuthConfig } from "../../utils/utils.js"; import { readAuthConfig } from "../../utils/utils.js";
import chalk from "chalk"; import chalk from "chalk";
import { getProject, getProjects } from "../../utils/shared.js"; import { getProject, getProjects, type Application } from "../../utils/shared.js";
import inquirer from "inquirer"; import inquirer from "inquirer";
import type { Answers } from "./create.js"; import type { Answers } from "./create.js";
import axios from "axios"; import axios from "axios";
@@ -26,6 +26,11 @@ export default class AppDeploy extends Command {
description: 'ID of the project', description: 'ID of the project',
required: false, required: false,
}), }),
environmentId: Flags.string({
char: 'e',
description: 'ID of the environment',
required: false,
}),
skipConfirm: Flags.boolean({ skipConfirm: Flags.boolean({
char: 'y', char: 'y',
description: 'Skip confirmation prompt', description: 'Skip confirmation prompt',
@@ -36,13 +41,17 @@ export default class AppDeploy extends Command {
public async run(): Promise<void> { public async run(): Promise<void> {
const auth = await readAuthConfig(this); const auth = await readAuthConfig(this);
const { flags } = await this.parse(AppDeploy); const { flags } = await this.parse(AppDeploy);
let { projectId, applicationId } = flags; let { projectId, applicationId, environmentId } = flags;
// Modo interactivo si no se proporcionan los flags necesarios // Modo interactivo si no se proporcionan los flags necesarios
if (!projectId || !applicationId) { if (!projectId || !applicationId || !environmentId) {
console.log(chalk.blue.bold("\n Listing all Projects \n")); console.log(chalk.blue.bold("\n Listing all Projects \n"));
const projects = await getProjects(auth, this); const projects = await getProjects(auth, this);
let selectedProject;
let selectedEnvironment;
// 1. Seleccionar proyecto
if (!projectId) { if (!projectId) {
const { project } = await inquirer.prompt<Answers>([ const { project } = await inquirer.prompt<Answers>([
{ {
@@ -55,20 +64,44 @@ export default class AppDeploy extends Command {
type: "list", type: "list",
}, },
]); ]);
selectedProject = project;
projectId = project.projectId; projectId = project.projectId;
} else {
selectedProject = projects.find(p => p.projectId === projectId);
} }
const projectSelected = await getProject(projectId, auth, this); // 2. Seleccionar environment del proyecto
if (!environmentId) {
if (!selectedProject?.environments || selectedProject.environments.length === 0) {
this.error(chalk.yellow("No environments found in this project."));
}
if (projectSelected.applications.length === 0) { const { environment } = await inquirer.prompt([
this.error(chalk.yellow("No applications found in this project.")); {
choices: selectedProject.environments.map((env) => ({
name: `${env.name} (${env.description})`,
value: env,
})),
message: "Select an environment:",
name: "environment",
type: "list",
},
]);
selectedEnvironment = environment;
environmentId = environment.environmentId;
} else {
selectedEnvironment = selectedProject?.environments?.find(e => e.environmentId === environmentId);
} }
// 3. Seleccionar application del environment
if (!applicationId) { if (!applicationId) {
if (!selectedEnvironment?.applications || selectedEnvironment.applications.length === 0) {
this.error(chalk.yellow("No applications found in this environment."));
}
const appAnswers = await inquirer.prompt([ const appAnswers = await inquirer.prompt([
{ {
// @ts-ignore choices: selectedEnvironment.applications.map((app: Application) => ({
choices: projectSelected.applications.map((app) => ({
name: app.name, name: app.name,
value: app.applicationId, value: app.applicationId,
})), })),

View File

@@ -103,7 +103,3 @@ export default class Authenticate extends Command {
} }
} }
} }
// curl -X 'GET' \
// 'https://panel.jinza.app/api/project.all' \
// -H 'accept: application/json' \
// -H 'x-api-key: EawCkTREMhxoAqvCxJFZurgCGoDZPjYHHrLgUPghRjJTpXLaahFdhCOGfABZXTRP'

View File

@@ -5,11 +5,34 @@ import chalk from "chalk";
import type { AuthConfig } from "./utils.js"; import type { AuthConfig } from "./utils.js";
export type Application = {
applicationId: string;
name: string;
// Add other application properties as needed
};
export type Environment = {
name: string;
environmentId: string;
description: string;
createdAt: string;
env: string;
projectId: string;
applications: Application[];
mariadb: any[];
mongo: any[];
mysql: any[];
postgres: any[];
redis: any[];
compose: any[];
};
export type Project = { export type Project = {
adminId: string; adminId: string;
name: string; name: string;
projectId?: string | undefined; projectId?: string | undefined;
description?: string | undefined; description?: string | undefined;
environments?: Environment[];
}; };
export const getProjects = async ( export const getProjects = async (