11import { createHash } from 'node:crypto' ;
22import { createReadStream } from 'node:fs' ;
33import { cp , lstat , mkdir , readFile , readdir , rm , writeFile } from 'node:fs/promises' ;
4- import { dirname , join , relative } from 'node:path' ;
4+ import { basename , dirname , join , relative } from 'node:path' ;
55import { fileURLToPath , pathToFileURL } from 'node:url' ;
66
77const repoRoot = dirname ( dirname ( fileURLToPath ( import . meta. url ) ) ) ;
@@ -76,7 +76,7 @@ export async function prepareBundledNpm({
7676 ) ;
7777 }
7878
79- await inventoryFiles ( sourceNpmRoot ) ;
79+ await inventoryFiles ( sourceNpmRoot , { ignoreGeneratedBinDirectories : true } ) ;
8080 for ( const patch of BUNDLED_NPM_SECURITY_PATCHES ) {
8181 await inventoryFiles ( join ( patchedPackagesRoot , patch . packageName ) ) ;
8282 }
@@ -98,6 +98,7 @@ export async function prepareBundledNpm({
9898 verbatimSymlinks : true ,
9999 } ) ;
100100 }
101+ await removeGeneratedBinDirectories ( runtimeOutputRoot ) ;
101102 await requireRegularFile ( join ( runtimeOutputRoot , 'LICENSE' ) , 'npm license' ) ;
102103 await requireRegularFile ( join ( runtimeOutputRoot , 'bin' , 'npm-cli.js' ) , 'npm CLI' ) ;
103104 const files = await inventoryFiles ( runtimeOutputRoot ) ;
@@ -172,20 +173,27 @@ async function requirePackageVersion(path, name, version, label) {
172173 }
173174}
174175
175- async function inventoryFiles ( root ) {
176+ async function inventoryFiles ( root , { ignoreGeneratedBinDirectories = false } = { } ) {
176177 const files = [ ] ;
177- await walk ( root , root , files ) ;
178+ await walk ( root , root , files , { ignoreGeneratedBinDirectories } ) ;
178179 files . sort ( ( left , right ) => Buffer . from ( left . path ) . compare ( Buffer . from ( right . path ) ) ) ;
179180 return files ;
180181}
181182
182- async function walk ( root , directory , files ) {
183+ async function walk ( root , directory , files , options ) {
183184 const entries = await readdir ( directory , { withFileTypes : true } ) ;
184185 for ( const entry of entries ) {
185186 const absolutePath = join ( directory , entry . name ) ;
187+ if (
188+ options . ignoreGeneratedBinDirectories &&
189+ entry . name === '.bin' &&
190+ basename ( directory ) === 'node_modules'
191+ ) {
192+ continue ;
193+ }
186194 const info = await lstat ( absolutePath ) ;
187195 if ( entry . isDirectory ( ) && ! info . isSymbolicLink ( ) ) {
188- await walk ( root , absolutePath , files ) ;
196+ await walk ( root , absolutePath , files , options ) ;
189197 continue ;
190198 }
191199 if ( ! entry . isFile ( ) || info . isSymbolicLink ( ) ) {
@@ -199,6 +207,21 @@ async function walk(root, directory, files) {
199207 }
200208}
201209
210+ async function removeGeneratedBinDirectories ( directory ) {
211+ const entries = await readdir ( directory , { withFileTypes : true } ) ;
212+ for ( const entry of entries ) {
213+ const absolutePath = join ( directory , entry . name ) ;
214+ if ( entry . name === '.bin' && basename ( directory ) === 'node_modules' ) {
215+ await rm ( absolutePath , { recursive : true , force : true } ) ;
216+ continue ;
217+ }
218+ const info = await lstat ( absolutePath ) ;
219+ if ( entry . isDirectory ( ) && ! info . isSymbolicLink ( ) ) {
220+ await removeGeneratedBinDirectories ( absolutePath ) ;
221+ }
222+ }
223+ }
224+
202225async function requireRegularFile ( path , label ) {
203226 const info = await lstat ( path ) ;
204227 if ( ! info . isFile ( ) || info . isSymbolicLink ( ) ) {
0 commit comments