Skip to content

Commit

Permalink
feat: env create, remove, and rename
Browse files Browse the repository at this point in the history
  • Loading branch information
SlayerAnsh committed Oct 11, 2024
1 parent b1fc57c commit 1ea734e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
15 changes: 15 additions & 0 deletions packages/cli/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,22 @@ export function createEnv(env: string, data?: Partial<ReturnType<typeof envConfi
* Rename env with its folder
*/
export function renameEnv(env: string, newName: string) {
const config = JSON.parse(loadStorageFile(env, "env.json").toString()) as ReturnType<typeof envConfig.getProperties>;
fs.renameSync(path.join(CONFIG_DIRECTORY, env), path.join(CONFIG_DIRECTORY, newName));
config.name = newName;
writeStorageFile(
newName,
"env.json",
JSON.stringify(config)
);
}

/**
* Rename env with its folder
*/
export function removeEnv(env: string) {
if (env === envConfig.get("name")) throw new Error("Cannot remove current env");
fs.rmSync(path.join(CONFIG_DIRECTORY, env), { recursive: true });
}


Expand Down
69 changes: 54 additions & 15 deletions packages/cli/src/handlers/env/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pc from "picocolors";
import { Commands } from "../../types";
import { createEnv, envConfig, getAllEnvs, loadEnv, renameEnv } from "../../config";
import { createEnv, DEFAULT_ENVS, envConfig, getAllEnvs, loadEnv, removeEnv, renameEnv } from "../../config";
import state from "../../state/State";
import { title } from "cmd";
import { promptWithExit, title } from "cmd";
import { loadStorageFile, writeStorageFile } from "config/storage";
import Table from "cli-table";
import { logTableConfig } from "common";
Expand All @@ -22,7 +22,7 @@ const commands: Commands = {
{
requestMessage: "Select env to use: ",
options: async () => {
return getAllEnvs();
return getAllEnvs().filter((env) => env !== envConfig.get("name"));
},
},
],
Expand Down Expand Up @@ -55,10 +55,34 @@ const commands: Commands = {
color: pc.yellow,
description: "Rename env",
inputs: [
{
requestMessage: "Select env: ",
options: getAllEnvs().filter((env) => !Object.values(DEFAULT_ENVS).includes(env as DEFAULT_ENVS)),
},
{
requestMessage: "Enter new name: ",
validate: (input: string) => {
const exists = getAllEnvs().includes(input);
if (exists) {
console.log(pc.red("Env already exists!"));
return false;
}
return true;
}
}
],
},
remove: {
handler: removeHandler,
usage: "env remove <name>",
color: pc.yellow,
description: "Remove env",
inputs: [
{
requestMessage: "Enter env to remove: ",
options: getAllEnvs().filter((env) => env !== envConfig.get("name") && !Object.values(DEFAULT_ENVS).includes(env as DEFAULT_ENVS)),
},
]
],
},
create: {
handler: createHandler,
Expand Down Expand Up @@ -172,20 +196,35 @@ async function updateHandler(input: string[]) {
* @param input - The key and value to update
*/
async function renameHandler(input: string[]) {
const [env, newName] = input;
renameEnv(env, newName);
if (envConfig.get("name") === env) {
await loadEnv(newName);
state.refresh();
await state.connectClient();
await title();
}
}

/**
* Updates the current environment
* @param input - The key and value to update
*/
async function removeHandler(input: string[]) {
const [name] = input;
renameEnv(envConfig.get("name"), name);
envConfig.set("name", name);
writeStorageFile(
envConfig.get("name"),
"env.json",
JSON.stringify(envConfig.getProperties())
);
await loadEnv(envConfig.get("name"));
state.refresh();
await state.connectClient();
await title();

const confirm = await promptWithExit({
type: "confirm",
name: "confirmremove",
message: `This action is irreversible! Are you sure you want to remove env ${name}?`,
});

if (!confirm.confirmremove) return;
removeEnv(name);
console.log(pc.green("Env removed!"));
}


/**
* Updates the current environment
* @param input - The key and value to update
Expand Down

0 comments on commit 1ea734e

Please sign in to comment.