0.2.0
This commit is contained in:
parent
8970bd2895
commit
0e323af9cf
2 changed files with 40 additions and 17 deletions
|
@ -2,7 +2,7 @@
|
||||||
"name": "@endeavorance/rundir",
|
"name": "@endeavorance/rundir",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"module": "src/cli.ts",
|
"module": "src/cli.ts",
|
||||||
"version": "0.1.1",
|
"version": "0.2.0",
|
||||||
"bin": {
|
"bin": {
|
||||||
"rundir": "build/cli.js"
|
"rundir": "build/cli.js"
|
||||||
},
|
},
|
||||||
|
|
55
src/cli.ts
55
src/cli.ts
|
@ -1,5 +1,4 @@
|
||||||
import { existsSync } from "node:fs";
|
import { existsSync } from "node:fs";
|
||||||
import { homedir } from "node:os";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { parseArgs } from "node:util";
|
import { parseArgs } from "node:util";
|
||||||
import { $, Glob } from "bun";
|
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 TEMPLATE_FILE_PATH = process.env["RUNDIR_TEMPLATE"] ?? null;
|
||||||
const DEFAULT_TEMPLATE = `
|
const DEFAULT_TEMPLATE = `
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Description...
|
# Description...
|
||||||
`.trim();
|
`.trim();
|
||||||
const VERSION = pkg.version;
|
const VERSION = pkg.version;
|
||||||
const RUNDIR_PATH = path.join(process.cwd(), ".run");
|
|
||||||
const COMMENT_STARTERS = ["#", "//", "--"];
|
const COMMENT_STARTERS = ["#", "//", "--"];
|
||||||
const COMMANDS = ["ls", "help", "new", "create", "which", "x", "run"] as const;
|
const COMMANDS = ["ls", "help", "new", "create", "which", "x", "run"] as const;
|
||||||
type Command = (typeof COMMANDS)[number];
|
type Command = (typeof COMMANDS)[number];
|
||||||
|
@ -77,7 +83,7 @@ function makeScriptPath(name: string): string {
|
||||||
const rundir = findNearestRundir();
|
const rundir = findNearestRundir();
|
||||||
|
|
||||||
if (!rundir) {
|
if (!rundir) {
|
||||||
throw new CLIError("No rundir found in the current directory tree.");
|
throw new NoRundirError();
|
||||||
}
|
}
|
||||||
|
|
||||||
return path.join(rundir, name);
|
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
|
* @param name The name of the script to find the path of
|
||||||
*/
|
*/
|
||||||
async function findScriptPath(name: string): Promise<string | null> {
|
async function findScriptPath(name: string): Promise<string | null> {
|
||||||
let currentDir = process.cwd();
|
const currentRundir = findNearestRundir();
|
||||||
|
|
||||||
while (currentDir !== path.dirname(currentDir)) {
|
if (!currentRundir) {
|
||||||
const potentialPath = path.join(currentDir, ".run", name);
|
throw new NoRundirError();
|
||||||
if (await Bun.file(potentialPath).exists()) {
|
|
||||||
return potentialPath;
|
|
||||||
}
|
|
||||||
currentDir = path.dirname(currentDir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const homeDirPath = path.join(homedir(), ".run", name);
|
const expectedPath = path.join(currentRundir, name);
|
||||||
if (await Bun.file(homeDirPath).exists()) {
|
|
||||||
return homeDirPath;
|
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 Bun.write(scriptFile, template);
|
||||||
await $`chmod +x ${scriptPath}`;
|
await $`chmod +x ${scriptPath}`;
|
||||||
console.log(name);
|
console.log(scriptFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -260,7 +267,23 @@ async function runScript(name: string) {
|
||||||
process.exit(1);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue