22import pkgUp from 'pkg-up' ;
33import detectIndent from 'detect-indent' ;
44import parseJson from 'parse-json' ;
5+ import multimatch from 'multimatch' ;
56import * as path from 'path' ;
67import * as fs from './fs' ;
78import type { Config } from '../types' ;
@@ -23,20 +24,48 @@ export async function writeConfigFile(filePath: string, config: Config) {
2324 await fs . writeFile ( filePath , contents ) ;
2425}
2526
26- export async function getProjectConfig ( cwd : string ) {
27+ export async function getPackageStack ( cwd : string ) {
28+ let stack = [ ] ;
2729 let searching = cwd ;
2830
2931 while ( searching ) {
3032 let filePath = await findConfigFile ( searching ) ;
3133
3234 if ( filePath ) {
3335 let config = await readConfigFile ( filePath ) ;
34- if ( config && config . pworkspaces ) {
35- return filePath ;
36- }
36+ stack . unshift ( { filePath, config } ) ;
3737 searching = path . dirname ( path . dirname ( filePath ) ) ;
3838 } else {
39- return null ;
39+ searching = null ;
4040 }
4141 }
42+
43+ return stack ;
44+ }
45+
46+ export async function getProjectConfig ( cwd : string ) {
47+ let stack = await getPackageStack ( cwd ) ;
48+
49+ let highest = stack . pop ( ) ;
50+ let matches = [ highest ] ;
51+
52+ while ( stack . length ) {
53+ let current = stack . pop ( ) ;
54+
55+ if ( current . config . pworkspaces ) {
56+ let patterns = current . config . pworkspaces ;
57+ let filePaths = matches . map ( match => {
58+ return path . relative ( path . dirname ( current . filePath ) , path . dirname ( match . filePath ) ) ;
59+ } ) ;
60+
61+ let found = multimatch ( filePaths , patterns ) ;
62+
63+ if ( found . length ) {
64+ matches . push ( current ) ;
65+ highest = current ;
66+ }
67+ }
68+ }
69+
70+ return highest . filePath ;
4271}
0 commit comments