refactor: add commands

This commit is contained in:
Mauricio Siu
2024-06-22 00:52:17 -06:00
parent fbf6b7f5cc
commit d610c967d9
19 changed files with 779 additions and 1154 deletions

81
src/utils/shared.ts Normal file
View File

@@ -0,0 +1,81 @@
import type { Command } from "@oclif/core";
import axios from "axios";
import chalk from "chalk";
import type { AuthConfig } from "./utils.js";
export type Project = {
adminId: string;
name: string;
projectId?: string | undefined;
description?: string | undefined;
};
export const getProjects = async (
auth: AuthConfig,
command: Command,
): Promise<Project[]> => {
try {
const response = await axios.get(`${auth.url}/api/trpc/project.all`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
});
if (!response.data.result.data.json) {
command.error(chalk.red("Error fetching projects"));
}
const projects = response.data.result.data.json;
if (projects.length === 0) {
command.log(chalk.yellow("No projects found."));
return [];
}
return projects;
} catch {
// @ts-expect-error TODO: Fix this
command.error(chalk.red(`Failed to fetch project list: ${error.message}`));
}
};
export const getProject = async (
projectId: string | undefined,
auth: AuthConfig,
command: Command,
) => {
try {
if (!projectId) {
command.error(chalk.red("Project ID is required"));
}
const response = await axios.get(`${auth.url}/api/trpc/project.one`, {
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
params: {
input: JSON.stringify({
json: { projectId },
}),
},
});
if (!response.data.result.data.json) {
command.error(chalk.red("Error fetching project"));
}
const project = response.data.result.data.json;
if (!project) {
command.error(chalk.red("Error fetching project"));
}
return project;
} catch {
// @ts-expect-error TODO: Fix this
command.error(chalk.red(`Failed to fetch project: ${error.message}`));
}
};

View File

@@ -9,9 +9,12 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const configPath = path.join(__dirname, "..", "..", "config.json");
export const readAuthConfig = async (
command: Command,
): Promise<{ token: string; url: string }> => {
export type AuthConfig = {
token: string;
url: string;
};
export const readAuthConfig = async (command: Command): Promise<AuthConfig> => {
if (!fs.existsSync(configPath)) {
command.error(
chalk.red(