Skip to content

Commit cbfe0de

Browse files
authored
Continue searching for package.json if one is found with no name (#176)
1 parent fa4caf9 commit cbfe0de

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

.changeset/dull-oranges-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/integration': patch
3+
---
4+
5+
Continue searching for package.json if one is found with no name

packages/integration/src/packageInfo.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,22 @@ interface PackageInfo {
88
dirname: string;
99
}
1010

11+
function getClosestPackageInfo(directory: string) {
12+
const packageJsonPath = findUp.sync('package.json', {
13+
cwd: directory,
14+
});
15+
16+
if (packageJsonPath) {
17+
const { name } = require(packageJsonPath);
18+
19+
return {
20+
name,
21+
path: packageJsonPath,
22+
dirname: path.dirname(packageJsonPath),
23+
};
24+
}
25+
}
26+
1127
const packageInfoCache = new Map<string, PackageInfo>();
1228

1329
export function getPackageInfo(cwd?: string | null): PackageInfo {
@@ -18,21 +34,19 @@ export function getPackageInfo(cwd?: string | null): PackageInfo {
1834
return cachedValue;
1935
}
2036

21-
const packageJsonPath = findUp.sync('package.json', {
22-
cwd: resolvedCwd,
23-
});
37+
let packageInfo = getClosestPackageInfo(resolvedCwd);
2438

25-
if (!packageJsonPath) {
26-
throw new Error(`Can't find package.json`);
39+
while (packageInfo && !packageInfo.name) {
40+
packageInfo = getClosestPackageInfo(
41+
path.resolve(packageInfo.dirname, '..'),
42+
);
2743
}
2844

29-
const { name } = require(packageJsonPath);
30-
31-
const packageInfo = {
32-
name,
33-
path: packageJsonPath,
34-
dirname: path.dirname(packageJsonPath),
35-
};
45+
if (!packageInfo || !packageInfo.name) {
46+
throw new Error(
47+
`Couldn't find parent package.json with a name field from '${resolvedCwd}'`,
48+
);
49+
}
3650

3751
packageInfoCache.set(resolvedCwd, packageInfo);
3852

0 commit comments

Comments
 (0)