Skip to content

Commit

Permalink
Ensure ~/.drenv subdirectories are present
Browse files Browse the repository at this point in the history
Also introduce a constants file to keep reference of cross-command constants, such as important drenv paths.
  • Loading branch information
Nitemaeric committed Oct 17, 2024
1 parent 2073278 commit 519f313
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 32 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Commands:

### `drenv setup`

This command will move the executable to your home directory and show instructions on how to add it to your `$PATH`.
This command will move the executable to your home directory and show
instructions on how to add it to your `$PATH`.

This allows you to call `drenv` from anywhere in your terminal.

Expand All @@ -73,10 +74,12 @@ installation into the new project directory.

### `drenv local [version]`

This command will update your current directory's DragonRuby version to the specified version.
This command will update your current directory's DragonRuby version to the
specified version.

Under the hood, all **drenv** does is copy the contents of the global DragonRuby
installation into the current project directory, excluding the `mygame` directory.
installation into the current project directory, excluding the `mygame`
directory.

### `drenv versions`

Expand Down
15 changes: 6 additions & 9 deletions commands/global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { exists } from "jsr:@std/fs";

import { homePath, versionsPath } from "../constants.ts";

export class NotInstalled extends Error {
version: string;

Expand Down Expand Up @@ -27,22 +29,17 @@ export default async function global(version: string | undefined = undefined) {
}

const setGlobalVersion = async (version: string) => {
if (!await exists(`${Deno.env.get("HOME")}/.drenv/versions/${version}`)) {
if (!await exists(`${versionsPath}/${version}`)) {
throw new NotInstalled(version);
}

return Deno.writeTextFile(
`${Deno.env.get("HOME")}/.drenv/.dragonruby-version`,
version,
);
return Deno.writeTextFile(`${homePath}/.dragonruby-version`, version);
};

const getGlobalVersion = async () => {
if (!await exists(`${Deno.env.get("HOME")}/.drenv/.dragonruby-version`)) {
if (!await exists(`${homePath}/.dragonruby-version`)) {
throw new NoGlobalVersion();
}

return Deno.readTextFile(
`${Deno.env.get("HOME")}/.drenv/.dragonruby-version`,
);
return Deno.readTextFile(`${homePath}/.dragonruby-version`);
};
3 changes: 2 additions & 1 deletion commands/local.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { copy, exists } from "jsr:@std/fs";

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

export class NotInstalled extends Error {
version: string;
Expand All @@ -22,7 +23,7 @@ export default async function local(version: string | undefined = undefined) {
}

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

if (!await exists(sourceDirectory)) {
throw new NotInstalled(version);
Expand Down
7 changes: 2 additions & 5 deletions commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { copy } from "jsr:@std/fs";

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

import global from "./global.ts";

export default async function newCommand(name: string) {
return copy(
`${Deno.env.get("HOME")}/.drenv/versions/${await global()}`,
name,
);
return copy(`${versionsPath}/${await global()}`, name);
}
7 changes: 5 additions & 2 deletions commands/register.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { move } from "jsr:@std/fs";
import { ensureDir, move } from "jsr:@std/fs";

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

export default async function register(path: string) {
// TODO: Validate that directory is a DragonRuby installation
Expand All @@ -11,5 +12,7 @@ export default async function register(path: string) {
throw new Error("drenv: DragonRuby installation is missing version");
}

return move(path, `${Deno.env.get("HOME")}/.drenv/versions/${version}`);
await ensureDir(versionsPath);

return move(path, `${versionsPath}/${version}`);
}
22 changes: 14 additions & 8 deletions commands/setup.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { exists, move } from "jsr:@std/fs";
import { ensureDir, exists, move } from "jsr:@std/fs";

import { binPath, shell } from "../constants.ts";

export default async function setup() {
const binPath = `${Deno.env.get("HOME")}/.drenv/bin/drenv`;
const shell = Deno.env.get("SHELL");
const drenvPath = `${binPath}/drenv`;

if (await exists(binPath)) {
return "drenv: already installed";
} else {
if (!Deno.execPath().includes("drenv")) {
return "drenv: this command must be run from the drenv executable"
return "drenv: this command must be run from the drenv executable";
}

await move(Deno.execPath(), binPath);
await ensureDir(binPath);
await move(Deno.execPath(), drenvPath);

console.log(`Installed at ${binPath}\n`);
console.log(`Installed at ${drenvPath}\n`);
console.log(`Run the following to add drenv to your shell profile:\n`);

if (shell?.includes("zsh")) {
console.log(`echo 'export PATH="$HOME/.drenv/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc`);
console.log(
`echo 'export PATH="$HOME/.drenv/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc`,
);
} else if (shell?.includes("bash")) {
console.log(`echo 'export PATH="$HOME/.drenv/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc`);
console.log(
`echo 'export PATH="$HOME/.drenv/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc`,
);
}
}
}
5 changes: 2 additions & 3 deletions commands/versions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { readVersion } from "../utils/read-version.ts";
import { versionsPath } from "../constants.ts";

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

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

Expand Down
4 changes: 4 additions & 0 deletions constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const homePath = `${Deno.env.get("HOME")}/.drenv`;
export const versionsPath = `${homePath}/versions`;
export const binPath = `${homePath}/bin`;
export const shell = Deno.env.get("SHELL");
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const actionRunner = (fn: (...args: any[]) => Promise<any>) => {
program
.name("drenv")
.description("CLI to manage DragonRuby environments.")
.version("0.2.2");
.version("0.2.3");

program.command("setup")
.description("Setup your shell profile to use drenv.")
Expand Down

0 comments on commit 519f313

Please sign in to comment.