var path = require ( "path" ) ; var fs = require ( "fs" ) ; module . exports = function ( $logger , hookArgs ) { return new Promise ( function ( resolve , reject ) { var validProdEnvs = [ 'prod' , 'production' ] ; var isProdEnv = false ; var env = ( ( hookArgs . checkForChangesOpts && hookArgs . checkForChangesOpts . projectData && hookArgs . checkForChangesOpts . projectData . $options && hookArgs . checkForChangesOpts . projectData . $options . argv ) || hookArgs . prepareData ) . env ; if ( env ) { Object . keys ( env ) . forEach ( ( key ) => { if ( validProdEnvs . indexOf ( key ) > - 1 ) { isProdEnv = true ; } } ) ; } var buildType = isProdEnv ? 'production' : 'development' ; var platform = ( hookArgs . checkForChangesOpts || hookArgs . prepareData ) . platform . toLowerCase ( ) ; var projectData = ( hookArgs . checkForChangesOpts && hookArgs . checkForChangesOpts . projectData ) || hookArgs . projectData ; var platformsDir = projectData . platformsDir ; var appResourcesDirectoryPath = projectData . appResourcesDirectoryPath ; var forcePrepare = true ; var npfInfoPath = path . join ( platformsDir , platform , ".pluginfirebaseinfo" ) ; var nsPrepareInfoPath = path . join ( platformsDir , platform , ".nsprepareinfo" ) ; var copyPlistOpts = { platform , appResourcesDirectoryPath , buildType , $logger } ; if ( fs . existsSync ( npfInfoPath ) ) { var npfInfo = undefined ; try { npfInfo = JSON . parse ( fs . readFileSync ( npfInfoPath , 'utf8' ) ) ; } catch ( e ) { $logger . info ( 'nativescript-plugin-firebase: error reading ' + npfInfoPath ) ; } if ( npfInfo && npfInfo . hasOwnProperty ( 'buildType' ) && npfInfo . buildType === buildType ) { $logger . info ( 'nativescript-plugin-firebase: building for same environment, not forcing prepare.' ) ; forcePrepare = false ; } } else { $logger . info ( 'nativescript-plugin-firebase: ' + npfInfoPath + ' not found, forcing prepare!' ) ; } if ( forcePrepare ) { $logger . info ( 'nativescript-plugin-firebase: running release build or change in environment detected, forcing prepare!' ) ; if ( fs . existsSync ( npfInfoPath ) ) { fs . unlinkSync ( npfInfoPath ) ; } if ( fs . existsSync ( nsPrepareInfoPath ) ) { fs . unlinkSync ( nsPrepareInfoPath ) ; } if ( copyPlist ( copyPlistOpts ) ) { resolve ( ) ; } else { reject ( ) ; } } else { resolve ( ) ; } } ) ; } ; var copyPlist = function ( copyPlistOpts ) { if ( copyPlistOpts . platform === 'android' ) { return true ; } else if ( copyPlistOpts . platform === 'ios' ) { var sourceGooglePlistProd = path . join ( copyPlistOpts . appResourcesDirectoryPath , "iOS" , "GoogleService-Info.plist.prod" ) ; var sourceGooglePlistDev = path . join ( copyPlistOpts . appResourcesDirectoryPath , "iOS" , "GoogleService-Info.plist.dev" ) ; var destinationGooglePlist = path . join ( copyPlistOpts . appResourcesDirectoryPath , "iOS" , "GoogleService-Info.plist" ) ; if ( fs . existsSync ( sourceGooglePlistProd ) && fs . existsSync ( sourceGooglePlistDev ) ) { if ( copyPlistOpts . buildType === 'production' ) { copyPlistOpts . $logger . info ( "nativescript-plugin-firebase: copy " + sourceGooglePlistProd + " to " + destinationGooglePlist + "." ) ; fs . writeFileSync ( destinationGooglePlist , fs . readFileSync ( sourceGooglePlistProd ) ) ; return true ; } else { copyPlistOpts . $logger . info ( "nativescript-plugin-firebase: copy " + sourceGooglePlistDev + " to " + destinationGooglePlist + "." ) ; fs . writeFileSync ( destinationGooglePlist , fs . readFileSync ( sourceGooglePlistDev ) ) ; return true ; } } else if ( ! fs . existsSync ( destinationGooglePlist ) ) { copyPlistOpts . $logger . warn ( "nativescript-plugin-firebase: " + destinationGooglePlist + " does not exist. Please follow the installation instructions from the documentation" ) ; return false ; } else { return true ; } } else { return true ; } } ;