@@ -28,6 +28,7 @@ import { HostingRewrites } from "../firebaseConfig";
2828import * as experiments from "../experiments" ;
2929import { ensureTargeted } from "../functions/ensureTargeted" ;
3030import { implicitInit } from "../hosting/implicitInit" ;
31+ import { fileExistsSync } from "../fsutils" ;
3132
3233// Use "true &&"" to keep typescript from compiling this file and rewriting
3334// the import statement into a require
@@ -124,8 +125,6 @@ const DEFAULT_FIND_DEP_OPTIONS: FindDepOptions = {
124125 omitDev : true ,
125126} ;
126127
127- const NPM_COMMAND = process . platform === "win32" ? "npm.cmd" : "npm" ;
128-
129128export const WebFrameworks : Record < string , Framework > = Object . fromEntries (
130129 readdirSync ( __dirname )
131130 . filter ( ( path ) => statSync ( join ( __dirname , path ) ) . isDirectory ( ) )
@@ -233,15 +232,30 @@ function scanDependencyTree(searchingFor: string, dependencies = {}): any {
233232 return ;
234233}
235234
235+ export function getNodeModuleBin ( name : string , cwd : string ) {
236+ const cantFindExecutable = new FirebaseError ( `Could not find the ${ name } executable.` ) ;
237+ const npmBin = spawnSync ( "npm" , [ "bin" ] , { cwd } ) . stdout ?. toString ( ) . trim ( ) ;
238+ if ( ! npmBin ) {
239+ throw cantFindExecutable ;
240+ }
241+ const path = join ( npmBin , name ) ;
242+ if ( ! fileExistsSync ( path ) ) {
243+ throw cantFindExecutable ;
244+ }
245+ return path ;
246+ }
247+
236248/**
237249 *
238250 */
239251export function findDependency ( name : string , options : Partial < FindDepOptions > = { } ) {
240- const { cwd, depth, omitDev } = { ...DEFAULT_FIND_DEP_OPTIONS , ...options } ;
252+ const { cwd : dir , depth, omitDev } = { ...DEFAULT_FIND_DEP_OPTIONS , ...options } ;
253+ const cwd = spawnSync ( "npm" , [ "root" ] , { cwd : dir } ) . stdout ?. toString ( ) . trim ( ) ;
254+ if ( ! cwd ) return ;
241255 const env : any = Object . assign ( { } , process . env ) ;
242256 delete env . NODE_ENV ;
243257 const result = spawnSync (
244- NPM_COMMAND ,
258+ "npm" ,
245259 [
246260 "list" ,
247261 name ,
@@ -395,7 +409,10 @@ export async function prepareFrameworks(
395409 process . env . __FIREBASE_DEFAULTS__ = JSON . stringify ( firebaseDefaults ) ;
396410 }
397411 const results = await discover ( getProjectPath ( ) ) ;
398- if ( ! results ) throw new Error ( "Epic fail." ) ;
412+ if ( ! results )
413+ throw new FirebaseError (
414+ "Unable to detect the web framework in use, check firebase-debug.log for more info."
415+ ) ;
399416 const { framework, mayWantBackend, publicDirectory } = results ;
400417 const {
401418 build,
@@ -569,7 +586,7 @@ ${firebaseDefaults ? `__FIREBASE_DEFAULTS__=${JSON.stringify(firebaseDefaults)}\
569586
570587 await Promise . all ( envs . map ( ( path ) => copyFile ( path , join ( functionsDist , basename ( path ) ) ) ) ) ;
571588
572- execSync ( `${ NPM_COMMAND } i --omit dev --no-audit` , {
589+ execSync ( `npm i --omit dev --no-audit` , {
573590 cwd : functionsDist ,
574591 stdio : "inherit" ,
575592 } ) ;
0 commit comments