fix(cli): a file given as the project folder names the folder to pass (#6476)

This commit is contained in:
Miguel Ángel
2026-10-26 20:51:08 +00:00
committed by GitHub
parent 933ae3eb5b
commit 1df003dfb7
2 changed files with 27 additions and 1 deletions
+19
View File
@@ -23,6 +23,25 @@ describe("resolveProjectOrThrow", () => {
expect(() => resolveProjectOrThrow(missing)).toThrowError(/Not a directory/);
});
it("names the project folder when given a file inside it", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-file-arg-project-"));
try {
mkdirSync(join(dir, "assets"));
writeFileSync(join(dir, "index.html"), "<html></html>");
writeFileSync(join(dir, "assets", "music.mp3"), "x");
const file = join(dir, "assets", "music.mp3");
expect(() => resolveProjectOrThrow(file)).toThrowError(
expect.objectContaining({
title: "Not a directory: " + file,
hint: "music.mp3 is a file; this command takes the project folder.",
suggestion: "Pass the folder that holds index.html: " + dir,
}),
);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("rejects a directory without index.html", () => {
const dir = mkdtempSync(join(tmpdir(), "hf-empty-project-"));
try {
+8 -1
View File
@@ -45,7 +45,14 @@ export function resolveProjectOrThrow(
const name = basename(dir);
const indexPath = resolve(dir, "index.html");
if (!existsSync(dir) || !statSync(dir).isDirectory()) {
if (existsSync(dir) && !statSync(dir).isDirectory()) {
throw new InvalidProjectError(
"Not a directory: " + dir,
basename(dir) + " is a file; this command takes the project folder.",
"Pass the folder that holds index.html: " + projectHolding(dir),
);
}
if (!existsSync(dir)) {
throw new InvalidProjectError("Not a directory: " + dir);
}
if (options.requireIndex !== false && !existsSync(indexPath)) {