Skip to content

Commit

Permalink
feat: list / delete created env
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Jul 21, 2022
1 parent 4d38b0d commit 5fefeca
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/commands/env/index.ts
@@ -0,0 +1,27 @@
import path from "node:path";
import fs from "node:fs";
import os from "node:os";
import { OptionValues, Command } from "commander";

export default async function env(opts: OptionValues, cmd: Command) {
const envs = fs.readdirSync(os.tmpdir()).filter((x) => x.startsWith("ncaic-"));

if (envs.length) {
const padding = Math.log10(envs.length) + 1;
for (let i = 0; i < envs.length; i++) {
const dir = path.resolve(os.tmpdir(), envs[i]);
console.log(`${(i + 1).toString().padStart(padding)}. ${dir}`);
}

if (opts.remove) {
console.log("---");
for (let i = 0; i < envs.length; i++) {
const dir = path.resolve(os.tmpdir(), envs[i]);
fs.rmSync(dir, { recursive: true });
console.log(`Removed ${dir}`);
}
}
} else {
console.log("No environments found");
}
}
5 changes: 4 additions & 1 deletion src/compile.ts
Expand Up @@ -83,7 +83,10 @@ export function compile(source: string) {
* @returns The absolute path of the temporary directory.
*/
export function create_env(source: string) {
const dir = path.resolve(tmpdir(), Math.random().toString().slice(2));
const dir = path.resolve(
tmpdir(),
"ncaic-" + Math.random().toString().slice(2, 8).padEnd(6, "0"),
);
if (fs.existsSync(dir)) {
fs.rmSync(dir, { recursive: true });
}
Expand Down
7 changes: 7 additions & 0 deletions src/index.ts
Expand Up @@ -7,6 +7,7 @@ import init from "./commands/init";
import verify from "./commands/verify";
import test from "./commands/test";
import run from "./commands/run";
import env from "./commands/env";

const package_info = JSON.parse(
fs.readFileSync(path.join(__dirname, "..", "package.json"), "utf8"),
Expand All @@ -16,6 +17,12 @@ const program = new commander.Command().version(`${package_info.name} ${package_

program.command("check").description("check for language support").action(check);

program
.command("env")
.description("list created all environments")
.option("-r, --remove", "remove all environments after listing")
.action(env);

program
.command("init")
.description("create a config file")
Expand Down

0 comments on commit 5fefeca

Please sign in to comment.