Cleanup
This commit is contained in:
parent
0e323af9cf
commit
48a673dc09
1 changed files with 17 additions and 25 deletions
42
src/cli.ts
42
src/cli.ts
|
@ -58,19 +58,19 @@ function parseCommand(command: string): Command {
|
||||||
* @param fromPath The path to start searching from
|
* @param fromPath The path to start searching from
|
||||||
* @returns The path to the rundir, or null if not found
|
* @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 currentDir = fromPath ?? process.cwd();
|
||||||
const potentialPath = path.join(currentDir, ".run");
|
const potentialPath = path.resolve(currentDir, ".run");
|
||||||
|
|
||||||
if (currentDir === path.dirname(currentDir)) {
|
if (currentDir === path.dirname(currentDir)) {
|
||||||
return null;
|
throw new NoRundirError();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (existsSync(potentialPath)) {
|
if (existsSync(potentialPath)) {
|
||||||
return 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
|
* @param name The name of the script
|
||||||
* @returns The absolute path to the script
|
* @returns The absolute path to the script
|
||||||
*/
|
*/
|
||||||
function makeScriptPath(name: string): string {
|
function getScriptPath(name: string): string {
|
||||||
const rundir = findNearestRundir();
|
return path.resolve(getRundirPath(), name);
|
||||||
|
}
|
||||||
|
|
||||||
if (!rundir) {
|
async function scriptExists(name: string): Promise<boolean> {
|
||||||
throw new NoRundirError();
|
return Bun.file(getScriptPath(name)).exists();
|
||||||
}
|
|
||||||
|
|
||||||
return path.join(rundir, name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,13 +98,13 @@ 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> {
|
||||||
const currentRundir = findNearestRundir();
|
const currentRundir = getRundirPath();
|
||||||
|
|
||||||
if (!currentRundir) {
|
if (!currentRundir) {
|
||||||
throw new NoRundirError();
|
throw new NoRundirError();
|
||||||
}
|
}
|
||||||
|
|
||||||
const expectedPath = path.join(currentRundir, name);
|
const expectedPath = path.resolve(currentRundir, name);
|
||||||
|
|
||||||
const checkFile = Bun.file(expectedPath);
|
const checkFile = Bun.file(expectedPath);
|
||||||
|
|
||||||
|
@ -184,8 +182,9 @@ async function readScriptData(scriptName: string): Promise<ScriptData> {
|
||||||
*/
|
*/
|
||||||
async function ls(scriptName?: string) {
|
async function ls(scriptName?: string) {
|
||||||
if (scriptName) {
|
if (scriptName) {
|
||||||
const pathname = await findScriptPath(scriptName);
|
const pathname = getScriptPath(scriptName);
|
||||||
if (pathname === null) {
|
const exists = await scriptExists(pathname);
|
||||||
|
if (!exists) {
|
||||||
throw new CLIError(`Script "${scriptName}" not found.`);
|
throw new CLIError(`Script "${scriptName}" not found.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,7 +192,7 @@ async function ls(scriptName?: string) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentRundir = findNearestRundir();
|
const currentRundir = getRundirPath();
|
||||||
const filesInDir = new Glob(`${currentRundir}/*`).scan();
|
const filesInDir = new Glob(`${currentRundir}/*`).scan();
|
||||||
const scriptLines: string[] = [];
|
const scriptLines: string[] = [];
|
||||||
|
|
||||||
|
@ -216,14 +215,7 @@ async function ls(scriptName?: string) {
|
||||||
* @command
|
* @command
|
||||||
*/
|
*/
|
||||||
async function which() {
|
async function which() {
|
||||||
const nearest = findNearestRundir();
|
console.log(getRundirPath());
|
||||||
|
|
||||||
if (!nearest) {
|
|
||||||
console.error("No rundir found in the current directory tree.");
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(path.resolve(nearest));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -231,7 +223,7 @@ async function which() {
|
||||||
* @command
|
* @command
|
||||||
*/
|
*/
|
||||||
async function newScript(name: string) {
|
async function newScript(name: string) {
|
||||||
const scriptPath = makeScriptPath(name);
|
const scriptPath = getScriptPath(name);
|
||||||
const scriptFile = Bun.file(scriptPath);
|
const scriptFile = Bun.file(scriptPath);
|
||||||
let template = DEFAULT_TEMPLATE;
|
let template = DEFAULT_TEMPLATE;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue