Skip to content

Commit

Permalink
Add drenv setup command
Browse files Browse the repository at this point in the history
Various minor fixes.
  • Loading branch information
Nitemaeric committed Oct 17, 2024
1 parent 1958198 commit 21fb38e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readVersion } from "../utils/read-version.ts";

import global from "./global.ts";

export default async function newCommand(name: string | undefined = undefined) {
export default async function newCommand(name: string) {
return copy(
`${Deno.env.get("HOME")}/.drenv/versions/${await global()}`,
name,
Expand Down
4 changes: 3 additions & 1 deletion commands/register.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { move } from "jsr:@std/fs";

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

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

const version = await readVersion(path + "/CHANGELOG-CURR.txt");
Expand Down
25 changes: 25 additions & 0 deletions commands/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { exists, move } from "jsr:@std/fs";

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

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"
}

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

console.log(`Installed at ${binPath}\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`);
} else if (shell?.includes("bash")) {
console.log(`echo 'export PATH="$HOME/.drenv/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc`);
}
}
}
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"tasks": {
"dev": "deno run --watch main.ts"
"dev": "deno run --watch main.ts",
"compile": "deno compile -A --output=builds/drenv main.ts"
},
"imports": {
"@std/assert": "jsr:@std/assert@1",
Expand Down
9 changes: 7 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import global from "./commands/global.ts";
import local from "./commands/local.ts";
import newCommand from "./commands/new.ts";
import register from "./commands/register.ts";
import setup from "./commands/setup.ts";
import versions from "./commands/versions.ts";

const program = new Command();
Expand All @@ -24,7 +25,11 @@ const actionRunner = (fn: (...args: any[]) => Promise<any>) => {
program
.name("drenv")
.description("CLI to manage DragonRuby environments.")
.version("0.2.0");
.version("0.2.1");

program.command("setup")
.description("Setup your shell profile to use drenv.")
.action(actionRunner(setup));

program.command("new")
.argument("<name>", "Name of the new project")
Expand All @@ -44,7 +49,7 @@ program.command("global")
.action(actionRunner(global));

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

Expand Down
4 changes: 3 additions & 1 deletion utils/read-version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { readFirstLine } from "./read-first-line.ts";

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

try {
Expand Down

0 comments on commit 21fb38e

Please sign in to comment.