@@ -197,12 +197,36 @@ function findPackageJson(nodeFile: string) {
197197 return dir ;
198198}
199199
200+ function getPrebuildEnvPrefix ( pkgName : string ) : string {
201+ return `npm_config_${ ( pkgName || '' )
202+ . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '_' )
203+ . replace ( / ^ _ / , '' ) } `;
204+ }
205+
200206function nativePrebuildInstall ( target : Target , nodeFile : string ) {
201207 const prebuildInstall = path . join (
202208 __dirname ,
203209 '../node_modules/.bin/prebuild-install' ,
204210 ) ;
205211 const dir = findPackageJson ( nodeFile ) ;
212+ const packageJson = JSON . parse (
213+ fs . readFileSync ( path . join ( dir , 'package.json' ) , { encoding : 'utf-8' } ) ,
214+ ) ;
215+
216+ // only try prebuild-install for packages that actually use it or if
217+ // explicitly configured via environment variables
218+ const envPrefix = getPrebuildEnvPrefix ( packageJson . name ) ;
219+ if (
220+ packageJson . dependencies ?. [ 'prebuild-install' ] == null &&
221+ ! [
222+ `${ envPrefix } _binary_host` ,
223+ `${ envPrefix } _binary_host_mirror` ,
224+ `${ envPrefix } _local_prebuilds` ,
225+ ] . some ( ( i ) => i in process . env )
226+ ) {
227+ return ;
228+ }
229+
206230 // parse the target node version from the binaryPath
207231 const nodeVersion = path . basename ( target . binaryPath ) . split ( '-' ) [ 1 ] ;
208232
@@ -221,9 +245,7 @@ function nativePrebuildInstall(target: Target, nodeFile: string) {
221245 fs . copyFileSync ( nodeFile , `${ nodeFile } .bak` ) ;
222246 }
223247
224- const napiVersions = JSON . parse (
225- fs . readFileSync ( path . join ( dir , 'package.json' ) , { encoding : 'utf-8' } ) ,
226- ) ?. binary ?. napi_versions ;
248+ const napiVersions = packageJson ?. binary ?. napi_versions ;
227249
228250 const options = [
229251 '--platform' ,
@@ -461,7 +483,7 @@ export default function producer({
461483 try {
462484 const platformFile = nativePrebuildInstall ( target , stripe . file ) ;
463485
464- if ( fs . existsSync ( platformFile ) ) {
486+ if ( platformFile && fs . existsSync ( platformFile ) ) {
465487 return cb (
466488 null ,
467489 pipeMayCompressToNewMeter (
0 commit comments