Skip to content

Commit

Permalink
Add options to modify project
Browse files Browse the repository at this point in the history
  • Loading branch information
natesawant committed Jun 19, 2024
1 parent c39bd7d commit 6ff513b
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 34 deletions.
1 change: 1 addition & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@types/inquirer": "^9.0.7",
"axios": "^1.7.2",
"bun": "^1.1.8",
"dotenv": "^16.4.5",
"execa": "^9.1.0",
"inquirer": "^9.2.21",
"install": "^0.13.0",
Expand Down
8 changes: 7 additions & 1 deletion cli/src/endpoints/authAxios.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import axios from "axios";
import { GetCredentials } from "../utils/getCredentials";

export const axiosInstance = axios.create({
withCredentials: true, // Ensure credentials are sent with every request
});

export const config = {
baseUrl: "https://routes-orcin.vercel.app",
baseUrl: process.env.DEVDB_URL ?? "https://routes-orcin.vercel.app",
};

export function SetCookie(credentials: string) {
Expand All @@ -23,3 +24,8 @@ export function SetCookie(credentials: string) {
},
);
}

if (process.env.DEVDB_TOKEN && process.env.DEVDB_URL) {
config.baseUrl = process.env.DEVDB_URL;
SetCookie(GetCredentials(process.env.DEVDB_TOKEN));
}
14 changes: 0 additions & 14 deletions cli/src/endpoints/getProjects.ts

This file was deleted.

90 changes: 90 additions & 0 deletions cli/src/endpoints/projectEndpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import type { Project } from "../types";
import { axiosInstance, config } from "./authAxios";

export async function GetProjects(): Promise<Project[]> {
const fullUrl = `${config.baseUrl}/api/trpc/database.get?input=${encodeURIComponent(JSON.stringify({ json: { searchTerms: "" } }))}`;

const response = await axiosInstance.get(fullUrl);

const { result } = response.data as { result: { data: { json: Project[] } } };

const projects = result.data.json;

return projects;
}

export async function CreateProject(repoUrl: string): Promise<boolean> {
const { status } = await axiosInstance.post(
`${config.baseUrl}/api/trpc/database.create`,
{
json: {
repoUrl: repoUrl,
provider: "postgres",
},
},
);

return status === 200;
}

export async function DeleteProject(repoUrl: string): Promise<boolean> {
const { status } = await axiosInstance.post(
`${config.baseUrl}/api/trpc/database.delete`,
{
json: {
repoUrl: repoUrl,
},
},
);

return status === 200;
}

export async function StartProject(repoUrl: string): Promise<boolean> {
const { status } = await axiosInstance.post(
`${config.baseUrl}/api/trpc/database.start`,
{
json: {
repoUrl: repoUrl,
},
},
);

return status === 200;
}

export async function StopProject(repoUrl: string): Promise<boolean> {
const { status } = await axiosInstance.post(
`${config.baseUrl}/api/trpc/database.stop`,
{
json: {
repoUrl: repoUrl,
},
},
);

return status === 200;
}

export async function GetEndpoint(
repoUrl: string,
): Promise<string | undefined> {
const response = await axiosInstance.post(
`${config.baseUrl}/api/trpc/database.endpoint`,
{
json: {
repoUrl: repoUrl,
},
},
);

const { status } = response;

const { result } = response.data as { result: { data: { json: string } } };

if (status === 200) {
return result.data.json;
}

return;
}
26 changes: 16 additions & 10 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import SetupCLI from "./menu/setup";
import ViewProjects from "./menu/projects";
import inquirer from "inquirer";
import "dotenv/config";

type MainAnswers = {
selectedMenu: "setup" | "projects" | "exit";
Expand Down Expand Up @@ -35,17 +36,22 @@ async function GetMainAnswers(): Promise<MainAnswers> {
* c. Project 3
*/
async function main() {
const { selectedMenu } = await GetMainAnswers();
console.log(process.env);

switch (selectedMenu) {
case "setup":
await SetupCLI();
break;
case "projects":
await ViewProjects();
break;
default:
process.exit(1);
if (!(process.env.DEVDB_TOKEN && process.env.DEVDB_URL)) {
await SetupCLI();
} else {
const { selectedMenu } = await GetMainAnswers();
switch (selectedMenu) {
case "setup":
await SetupCLI();
break;
case "projects":
await ViewProjects();
break;
default:
process.exit(0);
}
}

await main();
Expand Down
45 changes: 38 additions & 7 deletions cli/src/menu/projects.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import inquirer from "inquirer";
import GetProjects from "../endpoints/getProjects";
import {
CreateProject,
DeleteProject,
GetEndpoint,
GetProjects,
StartProject,
StopProject,
} from "../endpoints/projectEndpoints";
import type { Project } from "../types";
import updateExistingEnvVariable from "../utils/updateEnv";

type ProjectAnswers = {
selectedProject: string;
Expand All @@ -20,7 +28,7 @@ async function SelectProject(projectNames: string[]): Promise<ProjectAnswers> {
}

type ProjectOptionsAnswers = {
selectedOption: string;
selectedOption: "start" | "stop" | "create" | "delete" | "endpoint" | "back";
};

async function SelectProjectOption(
Expand All @@ -29,8 +37,8 @@ async function SelectProjectOption(
return (await inquirer.prompt([
{
type: "list",
name: "selectedProject",
message: "Select a project",
name: "selectedOption",
message: "Select an option",
choices: validOptions,
},
])) as ProjectOptionsAnswers;
Expand All @@ -56,27 +64,50 @@ export default async function ViewProjects() {
}
}

function LogSuccess(success: boolean, pastTense: string, presentTense: string) {
console.log(
success
? `✅ Successfully ${pastTense} project`
: `❌ Failed ${presentTense} project`,
);
}

async function ViewProjectOptions(project: Project) {
console.log(project);
const { status } = project;
const { status, repository } = project;

const validOptions =
status === "available"
? ["stop", "delete", "back"]
: ["start", "delete", "back"];
? ["stop", "endpoint", "delete", "back"]
: ["start", "endpoint", "delete", "back"];

const { selectedOption } = await SelectProjectOption(validOptions);

console.log(`selected option: ${selectedOption}`);

switch (selectedOption) {
case "start":
const startSuccess = await StartProject(repository);
LogSuccess(startSuccess, "started", "starting");
break;
case "stop":
const stopSuccess = await StopProject(repository);
LogSuccess(stopSuccess, "stopped", "stopping");
break;
case "create":
const createSuccess = await CreateProject(repository);
LogSuccess(createSuccess, "created", "creating");
break;
case "delete":
const deleteSuccess = await DeleteProject(repository);
LogSuccess(deleteSuccess, "deleted", "deleting");
break;
case "endpoint":
const endpoint = await GetEndpoint(repository);
if (endpoint) {
await updateExistingEnvVariable("DATABASE_URL", endpoint);
}
LogSuccess(endpoint !== undefined, "set", "setting");
break;
case "back":
// Do nothing
Expand Down
4 changes: 3 additions & 1 deletion cli/src/menu/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default async function SetupCLI() {
process.exit(1);
} else {
await updateExistingEnvVariable("DEVDB_URL", backendUrl);
process.env.DEVDB_URL = backendUrl;
config.baseUrl = backendUrl;
console.log("✅ Successfully reached the backend");
}
Expand All @@ -56,7 +57,7 @@ export default async function SetupCLI() {
// Verify session token is valid
try {
console.log(`Attempting to authenticated token`);
const credentials = await GetCredentials(config.baseUrl, sessionToken);
const credentials = GetCredentials(sessionToken);
SetCookie(credentials);

const { status } = await axiosInstance.get(
Expand All @@ -67,6 +68,7 @@ export default async function SetupCLI() {
process.exit(1);
} else {
await updateExistingEnvVariable("DEVDB_TOKEN", sessionToken);
process.env.DEVDB_TOKEN = sessionToken;
console.log("✅ Successfully authenticated the token");
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/utils/getCredentials.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export async function GetCredentials(baseUrl: string, sessionToken: string) {
export function GetCredentials(sessionToken: string) {
return `next-auth.session-token=${sessionToken}`;
}

0 comments on commit 6ff513b

Please sign in to comment.