1+ import { spawnSync } from "node:child_process" ;
12import fs from "node:fs" ;
23import path from "node:path" ;
34
@@ -19,7 +20,46 @@ function normalizePackageRelativePath(value) {
1920 return normalized ;
2021}
2122
23+ function listTrackedExtensionPackageDirs ( rootDir , fsImpl ) {
24+ if ( fsImpl !== fs ) {
25+ return null ;
26+ }
27+ const result = spawnSync ( "git" , [ "ls-files" , "--" , ":(glob)extensions/*/package.json" ] , {
28+ cwd : rootDir ,
29+ encoding : "utf8" ,
30+ stdio : [ "ignore" , "pipe" , "ignore" ] ,
31+ } ) ;
32+ if ( result . status !== 0 ) {
33+ return null ;
34+ }
35+ return result . stdout
36+ . split ( "\n" )
37+ . map ( ( line ) => toPosixPath ( line . trim ( ) ) )
38+ . filter ( ( line ) => line . length > 0 )
39+ . flatMap ( ( line ) => {
40+ const match = / ^ e x t e n s i o n s \/ ( [ ^ / ] + ) \/ p a c k a g e \. j s o n $ / u. exec ( line ) ;
41+ if ( ! match ?. [ 1 ] ) {
42+ return [ ] ;
43+ }
44+ const packageDir = path . join ( rootDir , "extensions" , match [ 1 ] ) ;
45+ return [
46+ {
47+ dirName : match [ 1 ] ,
48+ hasPackageJson : true ,
49+ packageDir,
50+ packageJsonPath : path . join ( packageDir , "package.json" ) ,
51+ } ,
52+ ] ;
53+ } )
54+ . toSorted ( ( left , right ) => left . dirName . localeCompare ( right . dirName ) ) ;
55+ }
56+
2257function listExtensionPackageDirs ( rootDir , fsImpl ) {
58+ const trackedDirs = listTrackedExtensionPackageDirs ( rootDir , fsImpl ) ;
59+ if ( trackedDirs ) {
60+ return trackedDirs ;
61+ }
62+
2363 const extensionsRoot = path . join ( rootDir , "extensions" ) ;
2464 if ( ! fsImpl . existsSync ( extensionsRoot ) ) {
2565 return [ ] ;
@@ -29,7 +69,9 @@ function listExtensionPackageDirs(rootDir, fsImpl) {
2969 . filter ( ( entry ) => entry . isDirectory ( ) )
3070 . map ( ( entry ) => ( {
3171 dirName : entry . name ,
72+ hasPackageJson : undefined ,
3273 packageDir : path . join ( extensionsRoot , entry . name ) ,
74+ packageJsonPath : path . join ( extensionsRoot , entry . name , "package.json" ) ,
3375 } ) )
3476 . toSorted ( ( left , right ) => left . dirName . localeCompare ( right . dirName ) ) ;
3577}
@@ -58,9 +100,11 @@ export function discoverStaticExtensionAssets(params = {}) {
58100 const rootDir = params . rootDir ?? process . cwd ( ) ;
59101 const fsImpl = params . fs ?? fs ;
60102 const assets = [ ] ;
61- for ( const { dirName, packageDir } of listExtensionPackageDirs ( rootDir , fsImpl ) ) {
62- const packageJsonPath = path . join ( packageDir , "package.json" ) ;
63- if ( ! fsImpl . existsSync ( packageJsonPath ) ) {
103+ for ( const { dirName, hasPackageJson, packageJsonPath } of listExtensionPackageDirs (
104+ rootDir ,
105+ fsImpl ,
106+ ) ) {
107+ if ( ! ( hasPackageJson ?? fsImpl . existsSync ( packageJsonPath ) ) ) {
64108 continue ;
65109 }
66110 const packageJson = readJsonFile ( packageJsonPath , fsImpl ) ;
0 commit comments