var path = require ( "path" ) ; var fs = require ( "fs" ) ; module . exports = function ( $logger , $projectData , hookArgs ) { return new Promise ( function ( resolve , reject ) { var validProdEnvs = [ 'prod' , 'production' ] ; var isProdEnv = false ; var env = ( hookArgs . platformSpecificData || hookArgs . prepareData ) . env ; if ( env ) { Object . keys ( env ) . forEach ( ( key ) => { if ( validProdEnvs . indexOf ( key ) > - 1 ) { isProdEnv = true ; } } ) ; } var buildType = isProdEnv ? 'production' : 'development' ; const platformFromHookArgs = hookArgs && ( hookArgs . platform || ( hookArgs . prepareData && hookArgs . prepareData . platform ) ) ; const platform = ( platformFromHookArgs || '' ) . toLowerCase ( ) ; var npfInfoPath = path . join ( $projectData . platformsDir , platform , ".pluginfirebaseinfo" ) ; var npfInfo = { buildType : buildType , } ; try { fs . writeFileSync ( npfInfoPath , JSON . stringify ( npfInfo ) ) ; } catch ( err ) { $logger . info ( 'nativescript-plugin-firebase: unable to create ' + npfInfoPath + ', prepare will be forced next time!' ) ; } var project = hookArgs . prepareData . env . project ; if ( platform === 'android' ) { var destinationGoogleJson = path . join ( $projectData . platformsDir , "android" , "app" , "google-services.json" ) ; var destinationGoogleJsonAlt = path . join ( $projectData . platformsDir , "android" , "google-services.json" ) ; var sourceGoogleJson = path . join ( $projectData . appResourcesDirectoryPath , "Android" , "google-services.json" ) ; var sourceGoogleJsonProd = path . join ( $projectData . projectDir , 'configurations' , project , platform , `google-services.json.prod` ) ; var sourceGoogleJsonDev = path . join ( $projectData . projectDir , 'configurations' , project , platform , `google-services.json.dev` ) ; if ( fs . existsSync ( sourceGoogleJsonProd ) && fs . existsSync ( sourceGoogleJsonDev ) ) { if ( buildType === 'production' ) { sourceGoogleJson = sourceGoogleJsonProd ; } else { sourceGoogleJson = sourceGoogleJsonDev ; } } if ( fs . existsSync ( sourceGoogleJson ) && fs . existsSync ( path . dirname ( destinationGoogleJson ) ) ) { $logger . info ( "Copy " + sourceGoogleJson + " to " + destinationGoogleJson + "." ) ; fs . writeFileSync ( destinationGoogleJson , fs . readFileSync ( sourceGoogleJson ) ) ; resolve ( ) ; } else if ( fs . existsSync ( sourceGoogleJson ) && fs . existsSync ( path . dirname ( destinationGoogleJsonAlt ) ) ) { $logger . info ( "Copy " + sourceGoogleJson + " to " + destinationGoogleJsonAlt + "." ) ; fs . writeFileSync ( destinationGoogleJsonAlt , fs . readFileSync ( sourceGoogleJson ) ) ; resolve ( ) ; } else { $logger . warn ( "Unable to copy google-services.json." ) ; reject ( ) ; } } else { resolve ( ) ; } } ) ; } ;