Skip to content

Commit

Permalink
feat: if package root can't be found, a .nrg folder is created in the…
Browse files Browse the repository at this point in the history
… users home directory
  • Loading branch information
AllanOricil committed Sep 28, 2024
1 parent 23702bd commit d11f89c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import os from "os";
import path from "path";
import { packageDirectorySync } from "pkg-dir";

function getProjectRoot() {
const packageDirectory = packageDirectorySync();
if (!packageDirectory) throw new Error("could not locate project's root");
if (!packageDirectory) {
const homeDir = os.homedir();
return path.resolve(homeDir);
}

return packageDirectory;
}
Expand Down
9 changes: 7 additions & 2 deletions test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os from "os";
import path from "path";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { getProjectRoot } from "../src/utils";
import { packageDirectorySync } from "pkg-dir";
Expand All @@ -19,9 +21,12 @@ describe("getProjectRoot", () => {
expect(result).toBe("/mock/project/root");
});

it("should throw an error if the project root cannot be located", () => {
it("should default to user's home directory if package root can't be found", () => {
(packageDirectorySync as vi.Mock).mockReturnValue(null);

expect(() => getProjectRoot()).toThrow("could not locate project's root");
const homeDir = os.homedir();
const projectRoot = getProjectRoot();

expect(projectRoot).toBe(path.resolve(homeDir));
});
});

0 comments on commit d11f89c

Please sign in to comment.