From 0e323af9cf3f2564406f649f4acc2dee9be5937a Mon Sep 17 00:00:00 2001 From: Endeavorance Date: Wed, 28 May 2025 17:19:11 -0400 Subject: [PATCH 1/2] 0.2.0 --- package.json | 2 +- src/cli.ts | 55 +++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index d62bb74..1ecad94 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@endeavorance/rundir", "type": "module", "module": "src/cli.ts", - "version": "0.1.1", + "version": "0.2.0", "bin": { "rundir": "build/cli.js" }, diff --git a/src/cli.ts b/src/cli.ts index 481dab2..191fd58 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,5 +1,4 @@ import { existsSync } from "node:fs"; -import { homedir } from "node:os"; import path from "node:path"; import { parseArgs } from "node:util"; import { $, Glob } from "bun"; @@ -20,13 +19,20 @@ class CLIError extends Error { } } +class NoRundirError extends CLIError { + name = "NoRundirError"; + + constructor() { + super("No rundir found in current directory tree"); + } +} + const TEMPLATE_FILE_PATH = process.env["RUNDIR_TEMPLATE"] ?? null; const DEFAULT_TEMPLATE = ` #!/usr/bin/env bash # Description... `.trim(); const VERSION = pkg.version; -const RUNDIR_PATH = path.join(process.cwd(), ".run"); const COMMENT_STARTERS = ["#", "//", "--"]; const COMMANDS = ["ls", "help", "new", "create", "which", "x", "run"] as const; type Command = (typeof COMMANDS)[number]; @@ -77,7 +83,7 @@ function makeScriptPath(name: string): string { const rundir = findNearestRundir(); if (!rundir) { - throw new CLIError("No rundir found in the current directory tree."); + throw new NoRundirError(); } return path.join(rundir, name); @@ -94,22 +100,23 @@ function makeScriptPath(name: string): string { * @param name The name of the script to find the path of */ async function findScriptPath(name: string): Promise { - let currentDir = process.cwd(); + const currentRundir = findNearestRundir(); - while (currentDir !== path.dirname(currentDir)) { - const potentialPath = path.join(currentDir, ".run", name); - if (await Bun.file(potentialPath).exists()) { - return potentialPath; - } - currentDir = path.dirname(currentDir); + if (!currentRundir) { + throw new NoRundirError(); } - const homeDirPath = path.join(homedir(), ".run", name); - if (await Bun.file(homeDirPath).exists()) { - return homeDirPath; + const expectedPath = path.join(currentRundir, name); + + const checkFile = Bun.file(expectedPath); + + const exists = await checkFile.exists(); + + if (!exists) { + return null; } - return null; + return expectedPath; } /** @@ -245,7 +252,7 @@ async function newScript(name: string) { await Bun.write(scriptFile, template); await $`chmod +x ${scriptPath}`; - console.log(name); + console.log(scriptFile); } /** @@ -260,7 +267,23 @@ async function runScript(name: string) { process.exit(1); } - await $`${scriptPath}`; + const rundir = path.dirname(scriptPath); + const cwd = path.resolve(rundir, ".."); + + const proc = Bun.spawn([scriptPath], { + cwd, + env: { + ...process.env, + RUNDIR_PATH: rundir, + RUNDIR_CWD: cwd, + RUNDIR_SCRIPT_PATH: scriptPath, + }, + stdout: Bun.stdout, + stderr: Bun.stderr, + stdin: Bun.stdin, + }); + + await proc.exited; } /** From 48a673dc09037ab20f1ef012f302c131142d0ed1 Mon Sep 17 00:00:00 2001 From: Endeavorance Date: Thu, 29 May 2025 08:52:39 -0400 Subject: [PATCH 2/2] Cleanup --- src/cli.ts | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 191fd58..d27544a 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -58,19 +58,19 @@ function parseCommand(command: string): Command { * @param fromPath The path to start searching from * @returns The path to the rundir, or null if not found */ -function findNearestRundir(fromPath?: string): string | null { +function getRundirPath(fromPath?: string): string { const currentDir = fromPath ?? process.cwd(); - const potentialPath = path.join(currentDir, ".run"); + const potentialPath = path.resolve(currentDir, ".run"); if (currentDir === path.dirname(currentDir)) { - return null; + throw new NoRundirError(); } if (existsSync(potentialPath)) { return potentialPath; } - return findNearestRundir(path.dirname(currentDir)); + return getRundirPath(path.dirname(currentDir)); } /** @@ -79,14 +79,12 @@ function findNearestRundir(fromPath?: string): string | null { * @param name The name of the script * @returns The absolute path to the script */ -function makeScriptPath(name: string): string { - const rundir = findNearestRundir(); +function getScriptPath(name: string): string { + return path.resolve(getRundirPath(), name); +} - if (!rundir) { - throw new NoRundirError(); - } - - return path.join(rundir, name); +async function scriptExists(name: string): Promise { + return Bun.file(getScriptPath(name)).exists(); } /** @@ -100,13 +98,13 @@ function makeScriptPath(name: string): string { * @param name The name of the script to find the path of */ async function findScriptPath(name: string): Promise { - const currentRundir = findNearestRundir(); + const currentRundir = getRundirPath(); if (!currentRundir) { throw new NoRundirError(); } - const expectedPath = path.join(currentRundir, name); + const expectedPath = path.resolve(currentRundir, name); const checkFile = Bun.file(expectedPath); @@ -184,8 +182,9 @@ async function readScriptData(scriptName: string): Promise { */ async function ls(scriptName?: string) { if (scriptName) { - const pathname = await findScriptPath(scriptName); - if (pathname === null) { + const pathname = getScriptPath(scriptName); + const exists = await scriptExists(pathname); + if (!exists) { throw new CLIError(`Script "${scriptName}" not found.`); } @@ -193,7 +192,7 @@ async function ls(scriptName?: string) { return; } - const currentRundir = findNearestRundir(); + const currentRundir = getRundirPath(); const filesInDir = new Glob(`${currentRundir}/*`).scan(); const scriptLines: string[] = []; @@ -216,14 +215,7 @@ async function ls(scriptName?: string) { * @command */ async function which() { - const nearest = findNearestRundir(); - - if (!nearest) { - console.error("No rundir found in the current directory tree."); - process.exit(1); - } - - console.log(path.resolve(nearest)); + console.log(getRundirPath()); } /** @@ -231,7 +223,7 @@ async function which() { * @command */ async function newScript(name: string) { - const scriptPath = makeScriptPath(name); + const scriptPath = getScriptPath(name); const scriptFile = Bun.file(scriptPath); let template = DEFAULT_TEMPLATE;