1+ import { spawnSync } from "node:child_process" ;
12import fs from "node:fs" ;
23import path from "node:path" ;
34import * as tar from "tar" ;
@@ -104,7 +105,7 @@ async function packToArchive({
104105} ) {
105106 const dest = path . join ( outDir , outName ) ;
106107 fs . rmSync ( dest , { force : true } ) ;
107- const entries = flatRoot ? fs . readdirSync ( pkgDir ) : [ path . basename ( pkgDir ) ] ;
108+ const entries = flatRoot ? listFlatRootArchiveEntries ( pkgDir ) : [ path . basename ( pkgDir ) ] ;
108109 await tar . c (
109110 {
110111 gzip : true ,
@@ -116,6 +117,31 @@ async function packToArchive({
116117 return dest ;
117118}
118119
120+ function listFlatRootArchiveEntries ( pkgDir : string ) : string [ ] {
121+ const externalEntries = listFindFlatRootArchiveEntries ( pkgDir ) ;
122+ if ( externalEntries ) {
123+ return externalEntries ;
124+ }
125+ return fs . readdirSync ( pkgDir ) . toSorted ( ( left , right ) => left . localeCompare ( right ) ) ;
126+ }
127+
128+ function listFindFlatRootArchiveEntries ( pkgDir : string ) : string [ ] | null {
129+ const result = spawnSync ( "find" , [ pkgDir , "-mindepth" , "1" , "-maxdepth" , "1" ] , {
130+ encoding : "utf8" ,
131+ maxBuffer : 1024 * 1024 ,
132+ stdio : [ "ignore" , "pipe" , "ignore" ] ,
133+ } ) ;
134+ if ( result . status !== 0 ) {
135+ return null ;
136+ }
137+ return result . stdout
138+ . split ( "\n" )
139+ . map ( ( line ) => line . trim ( ) )
140+ . filter ( ( line ) => line . length > 0 )
141+ . map ( ( entry ) => path . basename ( entry ) )
142+ . toSorted ( ( left , right ) => left . localeCompare ( right ) ) ;
143+ }
144+
119145function getArchiveFixturePath ( params : {
120146 cacheKey : string ;
121147 outName : string ;
@@ -656,6 +682,21 @@ beforeEach(() => {
656682} ) ;
657683
658684describe ( "installPluginFromArchive" , ( ) => {
685+ it ( "lists flat archive entries without scanning package dirs in-process" , ( ) => {
686+ const pkgDir = suiteTempRootTracker . makeTempDir ( ) ;
687+ fs . writeFileSync ( path . join ( pkgDir , "package.json" ) , "{}\n" ) ;
688+ fs . mkdirSync ( path . join ( pkgDir , "dist" ) ) ;
689+ fs . writeFileSync ( path . join ( pkgDir , "dist" , "index.js" ) , "export {}\n" ) ;
690+
691+ const readDir = vi . spyOn ( fs , "readdirSync" ) ;
692+ try {
693+ expect ( listFlatRootArchiveEntries ( pkgDir ) ) . toEqual ( [ "dist" , "package.json" ] ) ;
694+ expect ( readDir ) . not . toHaveBeenCalled ( ) ;
695+ } finally {
696+ readDir . mockRestore ( ) ;
697+ }
698+ } ) ;
699+
659700 it ( "installs package archive runtime dependencies" , async ( ) => {
660701 const result = await installArchivePackageAndReturnResult ( {
661702 packageJson : {
0 commit comments