Skip to content

Commit

Permalink
Pull findUp out of findHerebyfile, fixes a silly bug nobody should hit
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Dec 20, 2023
1 parent c71a57b commit a74c7fb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-turkeys-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hereby": patch
---

Fix the (unlikely) case of a Herebyfile at the FS root
14 changes: 7 additions & 7 deletions src/cli/loadHerebyfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import { pathToFileURL } from "node:url";
import pc from "picocolors";

import { Task } from "../index.js";
import { UserError } from "./utils.js";
import { findUp, UserError } from "./utils.js";

const herebyfileRegExp = /^herebyfile\.m?js$/i;

export function findHerebyfile(dir: string): string {
const root = path.parse(dir).root;

while (true) {
const result = findUp(dir, (dir) => {
const entries = fs.readdirSync(dir);
const matching = entries.filter((e) => herebyfileRegExp.test(e));
if (matching.length > 1) {
Expand All @@ -27,11 +25,13 @@ export function findHerebyfile(dir: string): string {
return candidate;
}
if (entries.includes("package.json")) {
break; // TODO: Is this actually desirable? What about monorepos?
return false; // TODO: Is this actually desirable? What about monorepos?
}
return undefined;
});

if (dir === root) break;
dir = path.dirname(dir);
if (result) {
return result;
}

throw new UserError("Unable to find Herebyfile.");
Expand Down
13 changes: 13 additions & 0 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ export function simplifyPath(p: string) {
return p;
}

export function findUp<T>(p: string, predicate: (dir: string) => T | undefined): T | undefined {
const root = path.parse(p).root;

while (true) {
const result = predicate(p);
if (result !== undefined) return result;
if (p === root) break;
p = path.dirname(p);
}

return undefined;
}

/**
* UserError is a special error that, when caught in the CLI will be printed
* as a message only, without stacktrace. Use this instead of process.exit.
Expand Down

0 comments on commit a74c7fb

Please sign in to comment.