From a55f312407658ff4042b0c1bc087dd884651f76f Mon Sep 17 00:00:00 2001 From: Dimitar Kerezov Date: Tue, 27 Feb 2018 19:28:11 +0200 Subject: [PATCH] feat: consume shouldprepare hook --- lib/before-shouldPrepare.js | 24 ++++++++++++++++++++++++ package.json | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 lib/before-shouldPrepare.js diff --git a/lib/before-shouldPrepare.js b/lib/before-shouldPrepare.js new file mode 100644 index 00000000..36ef9ad7 --- /dev/null +++ b/lib/before-shouldPrepare.js @@ -0,0 +1,24 @@ +const { join } = require("path"); +const { readFileSync, existsSync, writeFileSync } = require("fs"); +const envOptionsCacheFileLocation = join(__dirname, "env.cache.json"); + +module.exports = function (hookArgs) { + const platformInfo = hookArgs.shouldPrepareInfo && hookArgs.shouldPrepareInfo.platformInfo; + if (platformInfo && platformInfo.appFilesUpdaterOptions && platformInfo.appFilesUpdaterOptions.bundle) { + return (args, originalMethod) => { + return originalMethod(...args).then(originalShouldPrepare => { + const currentEnvString = JSON.stringify(platformInfo.env || {}); + if (existsSync(envOptionsCacheFileLocation)) { + const oldEnvOptionsString = readFileSync(envOptionsCacheFileLocation).toString(); + if (oldEnvOptionsString === currentEnvString) { + return originalShouldPrepare; + } + } + + writeFileSync(envOptionsCacheFileLocation, currentEnvString); + + return true; + }); + }; + } +} diff --git a/package.json b/package.json index 69b41d5b..78709a08 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,11 @@ "script": "lib/before-watchPatterns.js", "inject": true }, + { + "type": "before-shouldPrepare", + "script": "lib/before-shouldPrepare.js", + "inject": true + }, { "type": "after-prepare", "script": "lib/after-prepare.js",