@@ -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+
1127const packageInfoCache = new Map < string , PackageInfo > ( ) ;
1228
1329export 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