Skip to content

Commit

Permalink
Move all commands into commands directory. Add the local command.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitemaeric committed Oct 17, 2024
1 parent ca80abf commit 1958198
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 40 deletions.
46 changes: 46 additions & 0 deletions commands/local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { copy, exists } from "jsr:@std/fs";

import { readVersion } from "../utils/read-version.ts";

export class NotInstalled extends Error {
version: string;

constructor(version: string) {
super(`drenv: version '${version}' not installed`);

this.name = "NotInstalled";
this.version = version;
}
}

export default async function local(version: string | undefined = undefined) {
if (version) {
return setLocalVersion(version);
} else {
return getLocalVersion();
}
}

const setLocalVersion = async (version: string) => {
const sourceDirectory = `${Deno.env.get("HOME")}/.drenv/versions/${version}`;

if (!await exists(sourceDirectory)) {
throw new NotInstalled(version);
}

const items = await Deno.readDir(sourceDirectory);

for await (const item of items) {
if (item.name == "mygame") {
continue;
}

await copy(sourceDirectory + "/" + item.name, "./" + item.name, {
overwrite: true,
});
}
};

const getLocalVersion = async () => {
return readVersion("./CHANGELOG-CURR.txt");
};
12 changes: 12 additions & 0 deletions commands/new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { copy } from "jsr:@std/fs";

import { readVersion } from "../utils/read-version.ts";

import global from "./global.ts";

export default async function newCommand(name: string | undefined = undefined) {
return copy(
`${Deno.env.get("HOME")}/.drenv/versions/${await global()}`,
name,
);
}
13 changes: 13 additions & 0 deletions commands/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { readVersion } from "../utils/read-version.ts";

export default async function register(path: string | undefined = undefined) {
// TODO: Validate that directory is a DragonRuby installation

const version = await readVersion(path + "/CHANGELOG-CURR.txt");

if (!version) {
throw new Error("drenv: DragonRuby installation is missing version");
}

return move(path, `${Deno.env.get("HOME")}/.drenv/versions/${version}`);
}
17 changes: 17 additions & 0 deletions commands/versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { readVersion } from "../utils/read-version.ts";

export default async function versions() {
const directories = await Deno.readDir(
`${Deno.env.get("HOME")}/.drenv/versions/`,
);

const currentVersion = await readVersion("./CHANGELOG-CURR.txt");

for await (const directory of directories) {
if (directory.name == currentVersion) {
console.log("* " + directory.name);
} else {
console.log(" " + directory.name);
}
}
}
58 changes: 18 additions & 40 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Command } from "npm:commander";
import { copy, move } from "jsr:@std/fs";

import global from "./commands/global.ts";

import { readFirstLine } from "./utils/read-first-line.ts";
import local from "./commands/local.ts";
import newCommand from "./commands/new.ts";
import register from "./commands/register.ts";
import versions from "./commands/versions.ts";

const program = new Command();

Expand All @@ -12,8 +13,8 @@ const actionRunner = (fn: (...args: any[]) => Promise<any>) => {
fn(...args)
.then(
(value) =>
typeof value === "string" ||
typeof value === "number" && console.log(value),
(typeof value === "string" ||
typeof value === "number") && console.log(value),
)
.catch(
(error) => console.error(error.message),
Expand All @@ -23,55 +24,32 @@ const actionRunner = (fn: (...args: any[]) => Promise<any>) => {
program
.name("drenv")
.description("CLI to manage DragonRuby environments.")
.version("0.1.1");
.version("0.2.0");

program.command("new")
.argument("<name>", "Name of the new project")
.description("Create a new DragonRuby project.")
.action(async (name) => {
await copy(
`${Deno.env.get("HOME")}/.drenv/versions/${await global()}`,
name,
);
});
.action(actionRunner(newCommand));

program.command("register")
.argument("<path>", "Path to a fresh DragonRuby directory")
.description("Register a DragonRuby installation. This moves the installation to the $HOME/.drenv directory.")
.action(async (path) => {
const content = await readFirstLine(path + "/CHANGELOG-CURR.txt");
const version = content.match(/[0-9\.]+/)?.[0];

await move(path, `${Deno.env.get("HOME")}/.drenv/versions/${version}`);
});
.description(
"Register a DragonRuby installation. This moves the installation to the $HOME/.drenv directory.",
)
.action(actionRunner(register));

program.command("global")
.argument("[version]", "Version of DragonRuby to use")
.description("Get or set the global version of DragonRuby.")
.action(actionRunner(global));

program.command("local")
.argument("[version]", "Version of DragonRuby to use")
.description("Get or set the local version of DragonRuby.")
.action(actionRunner(local));

program.command("versions")
.description("List out all locally installed versions of DragonRuby.")
.action(async (options) => {
const directories = await Deno.readDir(
`${Deno.env.get("HOME")}/.drenv/versions/`,
);

let currentVersion;

try {
const content = await readFirstLine("./CHANGELOG-CURR.txt");

currentVersion = content.match(/[0-9\.]+/)?.[0];
} catch (error) {}

for await (const directory of directories) {
if (directory.name == currentVersion) {
console.log("* " + directory.name);
} else {
console.log(" " + directory.name);
}
}
});
.action(actionRunner(versions));

program.parse();
13 changes: 13 additions & 0 deletions utils/read-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { readFirstLine } from "./read-first-line.ts";

export const readVersion = async (path: string): Promise<string> => {
let currentVersion;

try {
const content = await readFirstLine(path);

currentVersion = content.match(/[0-9\.]+/)?.[0];
} catch (error) {}

return currentVersion;
};

0 comments on commit 1958198

Please sign in to comment.