From f5231d88020263153d3b10b248f36669fa8d7a45 Mon Sep 17 00:00:00 2001 From: Ben Delaney Date: Fri, 29 Jan 2021 20:57:45 +1100 Subject: [PATCH 1/3] Add check for release build in determining env --- publish/scripts/installer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/publish/scripts/installer.js b/publish/scripts/installer.js index 5aae77f0..359ddb5d 100755 --- a/publish/scripts/installer.js +++ b/publish/scripts/installer.js @@ -727,6 +727,7 @@ return new Promise(function(resolve, reject) { var isProdEnv = false; // building with --env.prod or --env.production flag var isStagingEnv = false; var env = (hookArgs.platformSpecificData || hookArgs.prepareData).env; + var isReleaseBuild = !!(hookArgs.prepareData).release; if (env) { Object.keys(env).forEach((key) => { @@ -739,10 +740,10 @@ return new Promise(function(resolve, reject) { }); } - var buildType = isProdEnv && !isStagingEnv ? "production" : "development"; const platformFromHookArgs = hookArgs && (hookArgs.platform || (hookArgs.prepareData && hookArgs.prepareData.platform)); const platform = (platformFromHookArgs || '').toLowerCase(); + var buildType = (isReleaseBuild && !isStagingEnv) || isProdEnv ? 'production' : 'development'; /* Create info file in platforms dir so we can detect changes in environment and force prepare if needed */ var npfInfoPath = path.join($projectData.platformsDir, platform, ".pluginfirebaseinfo"); From acb4ac6625b99d9f22c3ded5deba0d95b31ee869 Mon Sep 17 00:00:00 2001 From: Ben Delaney Date: Fri, 29 Jan 2021 20:58:34 +1100 Subject: [PATCH 2/3] Tidy up --- publish/scripts/installer.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/publish/scripts/installer.js b/publish/scripts/installer.js index 359ddb5d..6b4dc849 100755 --- a/publish/scripts/installer.js +++ b/publish/scripts/installer.js @@ -744,8 +744,8 @@ return new Promise(function(resolve, reject) { const platform = (platformFromHookArgs || '').toLowerCase(); var buildType = (isReleaseBuild && !isStagingEnv) || isProdEnv ? 'production' : 'development'; + /* Create info file in platforms dir so we can detect changes in environment and force prepare if needed */ - var npfInfoPath = path.join($projectData.platformsDir, platform, ".pluginfirebaseinfo"); var npfInfo = { buildType: buildType, @@ -756,9 +756,7 @@ return new Promise(function(resolve, reject) { $logger.info('nativescript-plugin-firebase: unable to create '+npfInfoPath+', prepare will be forced next time!'); } - /* Handle preparing of Google Services files */ - if (platform === 'android') { var destinationGoogleJson = path.join($projectData.platformsDir, "android", "app", "google-services.json"); var destinationGoogleJsonAlt = path.join($projectData.platformsDir, "android", "google-services.json"); @@ -768,8 +766,7 @@ return new Promise(function(resolve, reject) { // ensure we have both dev/prod versions so we never overwrite singlular google-services.json if (fs.existsSync(sourceGoogleJsonProd) && fs.existsSync(sourceGoogleJsonDev)) { - if (buildType==='production') { sourceGoogleJson = sourceGoogleJsonProd; } // use prod version - else { sourceGoogleJson = sourceGoogleJsonDev; } // use dev version + sourceGoogleJson = buildType === 'production' ? sourceGoogleJsonProd : sourceGoogleJsonDev } // copy correct version to destination @@ -804,6 +801,7 @@ return new Promise(function(resolve, reject) { } }); }; + `; var scriptPath = path.join(appRoot, "hooks", "after-prepare", "firebase-copy-google-services.js"); var afterPrepareDirPath = path.dirname(scriptPath); @@ -835,7 +833,6 @@ module.exports = function($logger, hookArgs) { return new Promise(function(resolve, reject) { /* Decide whether to prepare for dev or prod environment */ - var isReleaseBuild = !!((hookArgs.checkForChangesOpts && hookArgs.checkForChangesOpts.projectChangesOptions) || hookArgs.prepareData).release; var validProdEnvs = ['prod','production']; var validStagingEnvs = ["dev", "development", "staging"]; @@ -873,11 +870,11 @@ module.exports = function($logger, hookArgs) { 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) { + if (npfInfo && npfInfo.hasOwnProperty('buildType') && npfInfo.buildType === buildType) { $logger.info('nativescript-plugin-firebase: building for same environment, not forcing prepare.'); - forcePrepare=false; + forcePrepare = false; } - } else { $logger.info('nativescript-plugin-firebase: '+npfInfoPath+' not found, forcing prepare!'); } + } 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!'); @@ -902,7 +899,7 @@ var copyPlist = function(copyPlistOpts) { // if we have both dev/prod versions, we copy (or overwrite) GoogleService-Info.plist in destination dir if (fs.existsSync(sourceGooglePlistProd) && fs.existsSync(sourceGooglePlistDev)) { - if (copyPlistOpts.buildType==='production') { // use prod version + if (copyPlistOpts.buildType === 'production') { // use prod version copyPlistOpts.$logger.info("nativescript-plugin-firebase: copy " + sourceGooglePlistProd + " to " + destinationGooglePlist + "."); fs.writeFileSync(destinationGooglePlist, fs.readFileSync(sourceGooglePlistProd)); return true; From 775c08b2d773068d002266579725ec5995da6424 Mon Sep 17 00:00:00 2001 From: Ben Delaney Date: Fri, 29 Jan 2021 21:02:22 +1100 Subject: [PATCH 3/3] Refactor reflect style in after-prepare and before-checkForChanges --- publish/scripts/installer.js | 42 ++++++++---------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/publish/scripts/installer.js b/publish/scripts/installer.js index 6b4dc849..288e5af6 100755 --- a/publish/scripts/installer.js +++ b/publish/scripts/installer.js @@ -722,27 +722,14 @@ module.exports = function($logger, $projectData, hookArgs) { return new Promise(function(resolve, reject) { /* Decide whether to prepare for dev or prod environment */ - var validStagingEnvs = ["dev", "development", "staging"]; - var validProdEnvs = ['prod','production']; - var isProdEnv = false; // building with --env.prod or --env.production flag - var isStagingEnv = false; var env = (hookArgs.platformSpecificData || hookArgs.prepareData).env; + const platformFromHookArgs = hookArgs.platform || (hookArgs.prepareData && hookArgs.prepareData.platform); + const platform = (platformFromHookArgs || '').toLowerCase(); var isReleaseBuild = !!(hookArgs.prepareData).release; - if (env) { - Object.keys(env).forEach((key) => { - if (validProdEnvs.indexOf(key)>-1) { - isProdEnv = true; - } - if (validStagingEnvs.indexOf(key) > -1) { - isStagingEnv = true; - } - }); - } - - const platformFromHookArgs = hookArgs && (hookArgs.platform || (hookArgs.prepareData && hookArgs.prepareData.platform)); - const platform = (platformFromHookArgs || '').toLowerCase(); - + /* Decide whether to prepare for dev or prod environment */ + var isStagingEnv = ["dev", "development", "staging"].some(k => k in env); + var isProdEnv = ["prod", "production"].some(k => k in env); // building with --env.prod or --env.production flag var buildType = (isReleaseBuild && !isStagingEnv) || isProdEnv ? 'production' : 'development'; /* Create info file in platforms dir so we can detect changes in environment and force prepare if needed */ @@ -833,22 +820,12 @@ module.exports = function($logger, hookArgs) { return new Promise(function(resolve, reject) { /* Decide whether to prepare for dev or prod environment */ + var env = ((hookArgs.checkForChangesOpts && hookArgs.checkForChangesOpts.projectData && hookArgs.checkForChangesOpts.projectData.$options && hookArgs.checkForChangesOpts.projectData.$options.argv) || hookArgs.prepareData).env || {}; + const platform = (hookArgs.checkForChangesOpts || hookArgs.prepareData).platform.toLowerCase(); var isReleaseBuild = !!((hookArgs.checkForChangesOpts && hookArgs.checkForChangesOpts.projectChangesOptions) || hookArgs.prepareData).release; - var validProdEnvs = ['prod','production']; - var validStagingEnvs = ["dev", "development", "staging"]; - var isStagingEnv = false; - var isProdEnv = false; // building with --env.prod or --env.production flag - - 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; } - if (validStagingEnvs.indexOf(key) > -1) { isStagingEnv = true; } - }); - - - } + var isStagingEnv = ["dev", "development", "staging"].some(k => k in env); + var isProdEnv = ["prod", "production"].some(k => k in env); // building with --env.prod or --env.production flag var buildType = (isReleaseBuild && !isStagingEnv) || isProdEnv ? 'production' : 'development'; /* @@ -856,7 +833,6 @@ module.exports = function($logger, hookArgs) { for which environment {development|prod} the project was prepared. If needed, we delete the NS .nsprepareinfo file so we force a new prepare */ - 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;