-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use package.json location as project root
- Loading branch information
1 parent
1429711
commit 0bcfe0b
Showing
4 changed files
with
77 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { chdir, cwd } from "node:process"; | ||
|
||
import tmp from "tmp"; | ||
|
||
import { Config } from "../config"; | ||
import { basename, join } from "node:path"; | ||
import { mkdirSync, writeFileSync } from "node:fs"; | ||
import { PackageJson } from "type-fest"; | ||
|
||
tmp.setGracefulCleanup(); | ||
|
||
describe(Config, () => { | ||
it("respects environment variables", () => { | ||
process.env.APPMAP_ROOT = "/test/app"; | ||
expect(new Config()).toMatchObject({ | ||
root: "/test/app", | ||
appmapDir: "/test/app/tmp/appmap", | ||
appName: "app", | ||
}); | ||
}); | ||
|
||
it("uses the current directory for root", () => { | ||
expect(new Config()).toMatchObject({ | ||
root: dir, | ||
appmapDir: join(dir, "tmp", "appmap"), | ||
appName: basename(dir), | ||
}); | ||
}); | ||
|
||
it("searches for package.json and uses package name from it", () => { | ||
writeFileSync("package.json", JSON.stringify({ name: "test-package" } as PackageJson)); | ||
|
||
mkdirSync("subdirectory"); | ||
chdir("subdirectory"); | ||
expect(new Config()).toMatchObject({ | ||
root: dir, | ||
appmapDir: join(dir, "tmp", "appmap"), | ||
appName: "test-package", | ||
}); | ||
}); | ||
}); | ||
|
||
let dir: string; | ||
beforeEach(() => { | ||
chdir((dir = tmp.dirSync().name)); | ||
jest.replaceProperty(process, "env", {}); | ||
}); | ||
|
||
const origCwd = cwd(); | ||
afterEach(() => chdir(origCwd)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { existsSync } from "node:fs"; | ||
import { dirname, join } from "node:path"; | ||
import { cwd } from "node:process"; | ||
|
||
export default function locateFileUp(filename: string, dir = cwd()): string | undefined { | ||
if (existsSync(join(dir, filename))) return dir; | ||
const parent = dirname(dir); | ||
if (parent === dir) return; | ||
return locateFileUp(filename, parent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import { readFileSync } from "node:fs"; | ||
import { dirname, join } from "node:path"; | ||
import { join } from "node:path"; | ||
|
||
import type { PackageJson } from "type-fest"; | ||
import findFileUp from "./findFileUp"; | ||
|
||
export function readPkgUp(dir: string): PackageJson | undefined { | ||
try { | ||
return JSON.parse(readFileSync(join(dir, "package.json"), "utf-8")) as PackageJson; | ||
} catch { | ||
const parent = dirname(dir); | ||
if (parent === dir) return; | ||
else return readPkgUp(parent); | ||
} | ||
const path = findFileUp("package.json", dir); | ||
if (path) return JSON.parse(readFileSync(join(path, "package.json"), "utf-8")) as PackageJson; | ||
} |