From b9e87ad7c3fca2af9a5d2f2f67c8ecd9b026e77c Mon Sep 17 00:00:00 2001 From: Ivan Porto Wigner Date: Tue, 29 Sep 2026 12:54:53 +0200 Subject: [PATCH 1/2] fix: reload config after pre-app hooks and default --web-optimize from NODE_ENV --- src/commands/app/build.js | 8 +++--- src/commands/app/deploy.js | 5 ++-- src/commands/app/run.js | 1 + src/commands/app/undeploy.js | 2 ++ src/lib/build-actions.js | 2 ++ src/lib/deploy-actions.js | 2 ++ test/commands/app/build.test.js | 32 ++++++++++++++++++++++++ test/commands/app/deploy.test.js | 13 +++++----- test/commands/app/run.test.js | 17 +++++++++++++ test/commands/app/undeploy.test.js | 10 ++++++++ test/commands/lib/build-actions.test.js | 10 ++++++++ test/commands/lib/deploy-actions.test.js | 9 +++++++ 12 files changed, 100 insertions(+), 11 deletions(-) diff --git a/src/commands/app/build.js b/src/commands/app/build.js index 3e07c0d9c..68462e979 100644 --- a/src/commands/app/build.js +++ b/src/commands/app/build.js @@ -18,6 +18,7 @@ const { Flags } = require('@oclif/core') const { runInProcess, writeConfig } = require('../../lib/app-helper') const RuntimeLib = require('@adobe/aio-lib-runtime') const { bundle } = require('@adobe/aio-lib-web') +const coreConfig = require('@adobe/aio-lib-core-config') const fs = require('fs-extra') const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:build', { provider: 'debug' }) @@ -68,6 +69,7 @@ class Build extends BaseCommand { } catch (err) { this.log(err) } + coreConfig.reload() if (flags.actions) { // removed flags['force-build'] || as it is always forced at this point, we need to check to decide what to build @@ -117,7 +119,7 @@ class Build extends BaseCommand { const bundleOptions = { shouldDisableCache: true, shouldContentHash: flags['content-hash'], - shouldOptimize: flags['web-optimize'], + shouldOptimize: flags['web-optimize'] ?? process.env.NODE_ENV === 'production', logLevel: flags.verbose ? 'verbose' : 'warn' } // empty the dist folder to prevent an S3 explosion @@ -179,8 +181,8 @@ Build.flags = { allowNo: true }), 'web-optimize': Flags.boolean({ - description: '[default: false] Enable optimization (minification) of js/css/html', - default: false + description: '[default: true if NODE_ENV=production, false otherwise] Enable optimization (minification) of js/css/html', + allowNo: true }), extension: Flags.string({ description: 'Build only a specific extension point, the flags can be specified multiple times', diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index bc275c4d4..1df8eadbc 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -206,6 +206,7 @@ class Deploy extends BuildCommand { } catch (err) { this.error(err) } + aioConfigLoader.reload() // provision database if configured if (config.manifest?.full?.database?.['auto-provision'] === true) { @@ -487,8 +488,8 @@ Deploy.flags = { exclusive: ['action', 'publish'] // no-publish is excluded }), 'web-optimize': Flags.boolean({ - description: '[default: false] Enable optimization (minification) of web js/css/html', - default: false + description: '[default: true if NODE_ENV=production, false otherwise] Enable optimization (minification) of web js/css/html', + allowNo: true }), 'log-forwarding-update': Flags.boolean({ description: '[default: true] Update log forwarding configuration on server', diff --git a/src/commands/app/run.js b/src/commands/app/run.js index bc5c907ef..02ab0fbcd 100644 --- a/src/commands/app/run.js +++ b/src/commands/app/run.js @@ -84,6 +84,7 @@ class Run extends BaseCommand { } catch (err) { this.log(err) } + coreConfig.reload() // check if there are certificates available, and generate them if not ... // only care about certificates if the application has a UI diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index 743af8bec..8f1a287c6 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -19,6 +19,7 @@ const BaseCommand = require('../../BaseCommand') const webLib = require('@adobe/aio-lib-web') const { runInProcess, buildExtensionPointPayloadWoMetadata } = require('../../lib/app-helper') const rtLib = require('@adobe/aio-lib-runtime') +const coreConfig = require('@adobe/aio-lib-core-config') const { sendAppAssetsUndeployedAuditLog, sendAppUndeployAuditLog } = require('../../lib/audit-logger') const { setRuntimeApiHostAndAuthHandler, getAccessToken } = require('../../lib/auth-helper') @@ -136,6 +137,7 @@ class Undeploy extends BaseCommand { } catch (err) { this.log(err) } + coreConfig.reload() if (flags.actions) { if (config.app.hasBackend) { diff --git a/src/lib/build-actions.js b/src/lib/build-actions.js index 547b84cd1..ed2f52cd8 100644 --- a/src/lib/build-actions.js +++ b/src/lib/build-actions.js @@ -12,6 +12,7 @@ governing permissions and limitations under the License. const { runInProcess } = require('./app-helper') const { buildActions } = require('@adobe/aio-lib-runtime') +const coreConfig = require('@adobe/aio-lib-core-config') /** * Builds actions. @@ -22,6 +23,7 @@ const { buildActions } = require('@adobe/aio-lib-runtime') */ module.exports = async (config, filterActions, forceBuild = false) => { await runInProcess(config.hooks['pre-app-build'], config) + coreConfig.reload() const script = await runInProcess(config.hooks['build-actions'], { config, options: { filterActions, forceBuild } }) if (!script) { await buildActions(config, filterActions, forceBuild) diff --git a/src/lib/deploy-actions.js b/src/lib/deploy-actions.js index fca533b5c..5498ddf1c 100644 --- a/src/lib/deploy-actions.js +++ b/src/lib/deploy-actions.js @@ -13,6 +13,7 @@ governing permissions and limitations under the License. const { runInProcess } = require('./app-helper') const { deployActions } = require('@adobe/aio-lib-runtime') const logActions = require('./log-actions') +const coreConfig = require('@adobe/aio-lib-core-config') /** * Deploys actions. @@ -31,6 +32,7 @@ module.exports = async ({ inprocHook }) => { await runInProcess(config.hooks['pre-app-deploy'], config) + coreConfig.reload() const hookFilterEntities = Array.isArray(deployConfig.filterEntities?.actions) ? deployConfig.filterEntities.actions : [] const hookData = { diff --git a/test/commands/app/build.test.js b/test/commands/app/build.test.js index d6ad110dd..7f6dd865c 100644 --- a/test/commands/app/build.test.js +++ b/test/commands/app/build.test.js @@ -255,6 +255,38 @@ describe('run', () => { expect(mockBundleFunc).toHaveBeenCalledTimes(1) }) + test('reloads config after pre-app-build and before building', async () => { + const mockConfig = require('@adobe/aio-lib-core-config') + mockConfig.reload.mockClear() + command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig(command.appConfig)) + + await command.run() + expect(mockConfig.reload).toHaveBeenCalledTimes(1) + expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(mockConfig.reload.mock.invocationCallOrder[0]) + expect(mockConfig.reload.mock.invocationCallOrder[0]).toBeLessThan(mockRuntimeLib.buildActions.mock.invocationCallOrder[0]) + }) + + test.each([ + [[], 'production', true], + [[], 'development', false], + [[], undefined, false], + [['--web-optimize'], undefined, true], + [['--no-web-optimize'], 'production', false] + ])('web-optimize %j with NODE_ENV=%s optimizes: %s', async (argv, nodeEnv, expected) => { + const originalNodeEnv = process.env.NODE_ENV + if (nodeEnv) process.env.NODE_ENV = nodeEnv + else delete process.env.NODE_ENV + try { + command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig(command.appConfig)) + command.argv = argv + await command.run() + expect(mockWebLib.bundle).toHaveBeenCalledWith(expect.any(String), expect.any(String), + expect.objectContaining({ shouldOptimize: expected }), expect.any(Function)) + } finally { + process.env.NODE_ENV = originalNodeEnv + } + }) + test('build & deploy an App with --no-content-hash', async () => { command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig(command.appConfig)) diff --git a/test/commands/app/deploy.test.js b/test/commands/app/deploy.test.js index 40bb0d976..e92dcc931 100644 --- a/test/commands/app/deploy.test.js +++ b/test/commands/app/deploy.test.js @@ -880,9 +880,12 @@ describe('run', () => { command.argv = ['--no-web-assets'] await command.run() - expect(mockConfig.reload).toHaveBeenCalledTimes(1) + const reloadOrder = mockConfig.reload.mock.invocationCallOrder expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) - expect(mockConfig.reload.mock.invocationCallOrder[0]).toBeLessThan( + // reloaded after pre-app-deploy and again just before deploying actions + expect(reloadOrder.length).toBeGreaterThanOrEqual(2) + expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(reloadOrder[0]) + expect(reloadOrder[reloadOrder.length - 1]).toBeLessThan( mockRuntimeLib.deployActions.mock.invocationCallOrder[0] ) }) @@ -1368,8 +1371,7 @@ describe('run', () => { 'log-forwarding-update': true, open: false, publish: false, - 'web-assets': true, - 'web-optimize': false + 'web-assets': true }, env: mockEnv, opItems: [ @@ -1492,8 +1494,7 @@ describe('run', () => { 'log-forwarding-update': true, open: false, publish: false, - 'web-assets': true, - 'web-optimize': false + 'web-assets': true }, env: mockEnv, opItems: [ diff --git a/test/commands/app/run.test.js b/test/commands/app/run.test.js index 1cf3784f8..69841951b 100644 --- a/test/commands/app/run.test.js +++ b/test/commands/app/run.test.js @@ -701,4 +701,21 @@ describe('run', () => { expect(scriptSequence[0]).toEqual('pre-app-run') expect(scriptSequence[1]).toEqual('post-app-run') }) + + test('reloads config after pre-app-run and before starting the dev environment', async () => { + mockFSExists([PRIVATE_KEY_PATH, PUB_CERT_PATH]) + command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig(command.appConfig)) + + command.argv = [] + await command.run() + + expect(mockConfig.reload).toHaveBeenCalledTimes(1) + expect(mockRunDev).toHaveBeenCalledTimes(1) + expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan( + mockConfig.reload.mock.invocationCallOrder[0] + ) + expect(mockConfig.reload.mock.invocationCallOrder[0]).toBeLessThan( + mockRunDev.mock.invocationCallOrder[0] + ) + }) }) diff --git a/test/commands/app/undeploy.test.js b/test/commands/app/undeploy.test.js index 788d1f6f3..7dccc3510 100644 --- a/test/commands/app/undeploy.test.js +++ b/test/commands/app/undeploy.test.js @@ -198,6 +198,16 @@ describe('run', () => { expect(mockWebLib.undeployWeb).toHaveBeenCalledTimes(1) }) + test('reloads config after pre-app-undeploy and before undeploying', async () => { + const mockConfig = require('@adobe/aio-lib-core-config') + command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig()) + + await command.run() + expect(mockConfig.reload).toHaveBeenCalledTimes(1) + expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(mockConfig.reload.mock.invocationCallOrder[0]) + expect(mockConfig.reload.mock.invocationCallOrder[0]).toBeLessThan(mockRuntimeLib.undeployActions.mock.invocationCallOrder[0]) + }) + test('undeploy an App with no flags with hooks', async () => { command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig()) __setupMockHooks() diff --git a/test/commands/lib/build-actions.test.js b/test/commands/lib/build-actions.test.js index a4fa171e3..d3deb28c6 100644 --- a/test/commands/lib/build-actions.test.js +++ b/test/commands/lib/build-actions.test.js @@ -28,6 +28,16 @@ beforeEach(() => { utils.runInProcess.mockReset() }) +test('reloads config after pre-app-build', async () => { + const coreConfig = require('@adobe/aio-lib-core-config') + coreConfig.reload.mockClear() + utils.runInProcess.mockImplementation(script => script === 'build-actions' ? {} : undefined) + await buildActions(extensionConfig) + expect(coreConfig.reload).toHaveBeenCalledTimes(1) + expect(utils.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(coreConfig.reload.mock.invocationCallOrder[0]) + expect(coreConfig.reload.mock.invocationCallOrder[0]).toBeLessThan(utils.runInProcess.mock.invocationCallOrder[1]) +}) + test('exports', () => { expect(typeof buildActions).toEqual('function') }) diff --git a/test/commands/lib/deploy-actions.test.js b/test/commands/lib/deploy-actions.test.js index e1ad539c7..53976331d 100644 --- a/test/commands/lib/deploy-actions.test.js +++ b/test/commands/lib/deploy-actions.test.js @@ -35,6 +35,15 @@ test('exports', () => { expect(typeof deployActions).toEqual('function') }) +test('reloads config after pre-app-deploy', async () => { + const coreConfig = require('@adobe/aio-lib-core-config') + coreConfig.reload.mockClear() + await deployActions({ config: { hooks: {} } }) + expect(coreConfig.reload).toHaveBeenCalledTimes(1) + expect(utils.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(coreConfig.reload.mock.invocationCallOrder[0]) + expect(coreConfig.reload.mock.invocationCallOrder[0]).toBeLessThan(rtDeployActions.mock.invocationCallOrder[0]) +}) + test('deploy-actions app hook available', async () => { utils.runInProcess.mockImplementation(script => { if (script === 'deploy-actions') { From d9b91a0a02505e15f54ddc553404942bf82a71be Mon Sep 17 00:00:00 2001 From: Ivan Porto Wigner Date: Tue, 13 Oct 2026 15:38:23 +0200 Subject: [PATCH 2/2] fix: gate the post-hook config reload behind AIO_RELOAD_ENV_ON_HOOKS The reloads added after pre-app-build, pre-app-deploy, pre-app-run and pre-app-undeploy now run only when AIO_RELOAD_ENV_ON_HOOKS is set to a truthy value. Unset, empty, 0 and false skip the reload. The reload before rtLib.deployActions predates this branch and stays unconditional. --- src/commands/app/build.js | 5 ++-- src/commands/app/deploy.js | 4 +-- src/commands/app/run.js | 4 +-- src/commands/app/undeploy.js | 5 ++-- src/lib/app-helper.js | 18 ++++++++++++++ src/lib/build-actions.js | 5 ++-- src/lib/deploy-actions.js | 5 ++-- test/commands/app/build.test.js | 11 ++++----- test/commands/app/deploy.test.js | 15 +++++++----- test/commands/app/run.test.js | 9 ++++--- test/commands/app/undeploy.test.js | 10 ++++---- test/commands/lib/app-helper.test.js | 31 ++++++++++++++++++++++++ test/commands/lib/build-actions.test.js | 11 ++++----- test/commands/lib/deploy-actions.test.js | 11 ++++----- 14 files changed, 95 insertions(+), 49 deletions(-) diff --git a/src/commands/app/build.js b/src/commands/app/build.js index 68462e979..ad5fa4d30 100644 --- a/src/commands/app/build.js +++ b/src/commands/app/build.js @@ -15,10 +15,9 @@ const chalk = require('chalk') const BaseCommand = require('../../BaseCommand') const { Flags } = require('@oclif/core') -const { runInProcess, writeConfig } = require('../../lib/app-helper') +const { runInProcess, writeConfig, reloadEnvOnHooks } = require('../../lib/app-helper') const RuntimeLib = require('@adobe/aio-lib-runtime') const { bundle } = require('@adobe/aio-lib-web') -const coreConfig = require('@adobe/aio-lib-core-config') const fs = require('fs-extra') const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:build', { provider: 'debug' }) @@ -69,7 +68,7 @@ class Build extends BaseCommand { } catch (err) { this.log(err) } - coreConfig.reload() + reloadEnvOnHooks() if (flags.actions) { // removed flags['force-build'] || as it is always forced at this point, we need to check to decide what to build diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index 1df8eadbc..dfb7efc9c 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -19,7 +19,7 @@ const BuildCommand = require('./build') const webLib = require('@adobe/aio-lib-web') const { Flags } = require('@oclif/core') const { - rewriteActionUrlInEntities, runInProcess, + rewriteActionUrlInEntities, runInProcess, reloadEnvOnHooks, buildExtensionPointPayloadWoMetadata, buildExcShellViewExtensionMetadata, getFilesCountWithExtension } = require('../../lib/app-helper') @@ -206,7 +206,7 @@ class Deploy extends BuildCommand { } catch (err) { this.error(err) } - aioConfigLoader.reload() + reloadEnvOnHooks() // provision database if configured if (config.manifest?.full?.database?.['auto-provision'] === true) { diff --git a/src/commands/app/run.js b/src/commands/app/run.js index 02ab0fbcd..ffe809c9e 100644 --- a/src/commands/app/run.js +++ b/src/commands/app/run.js @@ -23,7 +23,7 @@ const coreConfig = require('@adobe/aio-lib-core-config') const BaseCommand = require('../../BaseCommand') const runDev = require('../../lib/run-dev') const { defaultHttpServerPort: SERVER_DEFAULT_PORT } = require('../../lib/defaults') -const { runInProcess } = require('../../lib/app-helper') +const { runInProcess, reloadEnvOnHooks } = require('../../lib/app-helper') const DEV_KEYS_DIR = 'dist/dev-keys/' const PRIVATE_KEY_PATH = DEV_KEYS_DIR + 'private.key' @@ -84,7 +84,7 @@ class Run extends BaseCommand { } catch (err) { this.log(err) } - coreConfig.reload() + reloadEnvOnHooks() // check if there are certificates available, and generate them if not ... // only care about certificates if the application has a UI diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index 8f1a287c6..206ee65b5 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -17,9 +17,8 @@ const { Flags } = require('@oclif/core') const BaseCommand = require('../../BaseCommand') const webLib = require('@adobe/aio-lib-web') -const { runInProcess, buildExtensionPointPayloadWoMetadata } = require('../../lib/app-helper') +const { runInProcess, buildExtensionPointPayloadWoMetadata, reloadEnvOnHooks } = require('../../lib/app-helper') const rtLib = require('@adobe/aio-lib-runtime') -const coreConfig = require('@adobe/aio-lib-core-config') const { sendAppAssetsUndeployedAuditLog, sendAppUndeployAuditLog } = require('../../lib/audit-logger') const { setRuntimeApiHostAndAuthHandler, getAccessToken } = require('../../lib/auth-helper') @@ -137,7 +136,7 @@ class Undeploy extends BaseCommand { } catch (err) { this.log(err) } - coreConfig.reload() + reloadEnvOnHooks() if (flags.actions) { if (config.app.hasBackend) { diff --git a/src/lib/app-helper.js b/src/lib/app-helper.js index ae3bebc2b..a423d40a8 100644 --- a/src/lib/app-helper.js +++ b/src/lib/app-helper.js @@ -81,6 +81,23 @@ async function runPackageScript (scriptName, dir, cmdArgs = []) { } } +/** + * Reloads the aio config so that changes made by a hook to .env or .aio are picked up. + * Opt-in: it only reloads when the AIO_RELOAD_ENV_ON_HOOKS env var is set to a truthy value. + * + * @returns {boolean} true if the config was reloaded + */ +function reloadEnvOnHooks () { + const value = (process.env.AIO_RELOAD_ENV_ON_HOOKS || '').trim().toLowerCase() + if (value === '' || value === '0' || value === 'false') { + aioLogger.debug('reloadEnvOnHooks: AIO_RELOAD_ENV_ON_HOOKS is not set, skipping config reload') + return false + } + aioLogger.debug('reloadEnvOnHooks: AIO_RELOAD_ENV_ON_HOOKS is set, reloading config') + aioConfig.reload() + return true +} + /** * @param {string} hookPath to be require()'d and run. Should export an async function that takes a config object as its only argument * @param {object} config which will be passed to the hook @@ -557,6 +574,7 @@ module.exports = { installPackages, runScript, runInProcess, + reloadEnvOnHooks, runPackageScript, wrapError, removeProtocolFromURL, diff --git a/src/lib/build-actions.js b/src/lib/build-actions.js index ed2f52cd8..428aa6d45 100644 --- a/src/lib/build-actions.js +++ b/src/lib/build-actions.js @@ -10,9 +10,8 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -const { runInProcess } = require('./app-helper') +const { runInProcess, reloadEnvOnHooks } = require('./app-helper') const { buildActions } = require('@adobe/aio-lib-runtime') -const coreConfig = require('@adobe/aio-lib-core-config') /** * Builds actions. @@ -23,7 +22,7 @@ const coreConfig = require('@adobe/aio-lib-core-config') */ module.exports = async (config, filterActions, forceBuild = false) => { await runInProcess(config.hooks['pre-app-build'], config) - coreConfig.reload() + reloadEnvOnHooks() const script = await runInProcess(config.hooks['build-actions'], { config, options: { filterActions, forceBuild } }) if (!script) { await buildActions(config, filterActions, forceBuild) diff --git a/src/lib/deploy-actions.js b/src/lib/deploy-actions.js index 5498ddf1c..75d9fcaa4 100644 --- a/src/lib/deploy-actions.js +++ b/src/lib/deploy-actions.js @@ -10,10 +10,9 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -const { runInProcess } = require('./app-helper') +const { runInProcess, reloadEnvOnHooks } = require('./app-helper') const { deployActions } = require('@adobe/aio-lib-runtime') const logActions = require('./log-actions') -const coreConfig = require('@adobe/aio-lib-core-config') /** * Deploys actions. @@ -32,7 +31,7 @@ module.exports = async ({ inprocHook }) => { await runInProcess(config.hooks['pre-app-deploy'], config) - coreConfig.reload() + reloadEnvOnHooks() const hookFilterEntities = Array.isArray(deployConfig.filterEntities?.actions) ? deployConfig.filterEntities.actions : [] const hookData = { diff --git a/test/commands/app/build.test.js b/test/commands/app/build.test.js index 7f6dd865c..f35ba3071 100644 --- a/test/commands/app/build.test.js +++ b/test/commands/app/build.test.js @@ -255,15 +255,14 @@ describe('run', () => { expect(mockBundleFunc).toHaveBeenCalledTimes(1) }) - test('reloads config after pre-app-build and before building', async () => { - const mockConfig = require('@adobe/aio-lib-core-config') - mockConfig.reload.mockClear() + test('reloads env after pre-app-build and before building', async () => { + helpers.reloadEnvOnHooks.mockClear() command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig(command.appConfig)) await command.run() - expect(mockConfig.reload).toHaveBeenCalledTimes(1) - expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(mockConfig.reload.mock.invocationCallOrder[0]) - expect(mockConfig.reload.mock.invocationCallOrder[0]).toBeLessThan(mockRuntimeLib.buildActions.mock.invocationCallOrder[0]) + expect(helpers.reloadEnvOnHooks).toHaveBeenCalledTimes(1) + expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(helpers.reloadEnvOnHooks.mock.invocationCallOrder[0]) + expect(helpers.reloadEnvOnHooks.mock.invocationCallOrder[0]).toBeLessThan(mockRuntimeLib.buildActions.mock.invocationCallOrder[0]) }) test.each([ diff --git a/test/commands/app/deploy.test.js b/test/commands/app/deploy.test.js index e92dcc931..dd9f842c6 100644 --- a/test/commands/app/deploy.test.js +++ b/test/commands/app/deploy.test.js @@ -869,8 +869,9 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) }) - test('deploy reloads config before deploying actions', async () => { + test('deploy reloads env after pre-app-deploy and reloads config before deploying actions', async () => { command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig(command.appConfig)) + helpers.reloadEnvOnHooks.mockClear() const noScriptFound = undefined helpers.runInProcess .mockResolvedValueOnce(noScriptFound) // pre-app-deploy @@ -880,12 +881,14 @@ describe('run', () => { command.argv = ['--no-web-assets'] await command.run() - const reloadOrder = mockConfig.reload.mock.invocationCallOrder expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) - // reloaded after pre-app-deploy and again just before deploying actions - expect(reloadOrder.length).toBeGreaterThanOrEqual(2) - expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(reloadOrder[0]) - expect(reloadOrder[reloadOrder.length - 1]).toBeLessThan( + // env reload is gated, the config reload just before deploying actions is not + expect(helpers.reloadEnvOnHooks).toHaveBeenCalledTimes(1) + expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan( + helpers.reloadEnvOnHooks.mock.invocationCallOrder[0] + ) + expect(mockConfig.reload).toHaveBeenCalledTimes(1) + expect(mockConfig.reload.mock.invocationCallOrder[0]).toBeLessThan( mockRuntimeLib.deployActions.mock.invocationCallOrder[0] ) }) diff --git a/test/commands/app/run.test.js b/test/commands/app/run.test.js index 69841951b..07c055855 100644 --- a/test/commands/app/run.test.js +++ b/test/commands/app/run.test.js @@ -702,19 +702,20 @@ describe('run', () => { expect(scriptSequence[1]).toEqual('post-app-run') }) - test('reloads config after pre-app-run and before starting the dev environment', async () => { + test('reloads env after pre-app-run and before starting the dev environment', async () => { mockFSExists([PRIVATE_KEY_PATH, PUB_CERT_PATH]) + helpers.reloadEnvOnHooks.mockClear() command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig(command.appConfig)) command.argv = [] await command.run() - expect(mockConfig.reload).toHaveBeenCalledTimes(1) + expect(helpers.reloadEnvOnHooks).toHaveBeenCalledTimes(1) expect(mockRunDev).toHaveBeenCalledTimes(1) expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan( - mockConfig.reload.mock.invocationCallOrder[0] + helpers.reloadEnvOnHooks.mock.invocationCallOrder[0] ) - expect(mockConfig.reload.mock.invocationCallOrder[0]).toBeLessThan( + expect(helpers.reloadEnvOnHooks.mock.invocationCallOrder[0]).toBeLessThan( mockRunDev.mock.invocationCallOrder[0] ) }) diff --git a/test/commands/app/undeploy.test.js b/test/commands/app/undeploy.test.js index 7dccc3510..bf230dfac 100644 --- a/test/commands/app/undeploy.test.js +++ b/test/commands/app/undeploy.test.js @@ -198,14 +198,14 @@ describe('run', () => { expect(mockWebLib.undeployWeb).toHaveBeenCalledTimes(1) }) - test('reloads config after pre-app-undeploy and before undeploying', async () => { - const mockConfig = require('@adobe/aio-lib-core-config') + test('reloads env after pre-app-undeploy and before undeploying', async () => { + helpers.reloadEnvOnHooks.mockClear() command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig()) await command.run() - expect(mockConfig.reload).toHaveBeenCalledTimes(1) - expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(mockConfig.reload.mock.invocationCallOrder[0]) - expect(mockConfig.reload.mock.invocationCallOrder[0]).toBeLessThan(mockRuntimeLib.undeployActions.mock.invocationCallOrder[0]) + expect(helpers.reloadEnvOnHooks).toHaveBeenCalledTimes(1) + expect(helpers.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(helpers.reloadEnvOnHooks.mock.invocationCallOrder[0]) + expect(helpers.reloadEnvOnHooks.mock.invocationCallOrder[0]).toBeLessThan(mockRuntimeLib.undeployActions.mock.invocationCallOrder[0]) }) test('undeploy an App with no flags with hooks', async () => { diff --git a/test/commands/lib/app-helper.test.js b/test/commands/lib/app-helper.test.js index 28ed4655d..ffa97bf60 100644 --- a/test/commands/lib/app-helper.test.js +++ b/test/commands/lib/app-helper.test.js @@ -42,6 +42,7 @@ beforeEach(() => { execa.command.mockReset() aioConfig.get.mockReset() aioConfig.set.mockReset() + aioConfig.reload.mockReset() libEnv.getCliEnv.mockReset() libIms.getToken.mockReset() mockFetch.mockReset() @@ -248,6 +249,36 @@ test('runInProcess fails with no hook-path', async () => { expect(mockLogger.debug).toHaveBeenCalledWith('runInProcess: undefined hookPath') }) +describe('reloadEnvOnHooks', () => { + const OLD_VALUE = process.env.AIO_RELOAD_ENV_ON_HOOKS + + afterEach(() => { + if (OLD_VALUE === undefined) { + delete process.env.AIO_RELOAD_ENV_ON_HOOKS + } else { + process.env.AIO_RELOAD_ENV_ON_HOOKS = OLD_VALUE + } + }) + + test('does not reload when AIO_RELOAD_ENV_ON_HOOKS is not set', () => { + delete process.env.AIO_RELOAD_ENV_ON_HOOKS + expect(appHelper.reloadEnvOnHooks()).toBe(false) + expect(aioConfig.reload).not.toHaveBeenCalled() + }) + + test.each(['', ' ', '0', 'false', 'FALSE'])('does not reload when AIO_RELOAD_ENV_ON_HOOKS is %p', (value) => { + process.env.AIO_RELOAD_ENV_ON_HOOKS = value + expect(appHelper.reloadEnvOnHooks()).toBe(false) + expect(aioConfig.reload).not.toHaveBeenCalled() + }) + + test.each(['1', 'true', 'TRUE', 'yes'])('reloads when AIO_RELOAD_ENV_ON_HOOKS is %p', (value) => { + process.env.AIO_RELOAD_ENV_ON_HOOKS = value + expect(appHelper.reloadEnvOnHooks()).toBe(true) + expect(aioConfig.reload).toHaveBeenCalledTimes(1) + }) +}) + test('runScript with empty command', async () => { await appHelper.runScript(undefined, 'dir') expect(execa.command).not.toHaveBeenCalled() diff --git a/test/commands/lib/build-actions.test.js b/test/commands/lib/build-actions.test.js index d3deb28c6..d64ba69ed 100644 --- a/test/commands/lib/build-actions.test.js +++ b/test/commands/lib/build-actions.test.js @@ -28,14 +28,13 @@ beforeEach(() => { utils.runInProcess.mockReset() }) -test('reloads config after pre-app-build', async () => { - const coreConfig = require('@adobe/aio-lib-core-config') - coreConfig.reload.mockClear() +test('reloads env after pre-app-build', async () => { + utils.reloadEnvOnHooks.mockClear() utils.runInProcess.mockImplementation(script => script === 'build-actions' ? {} : undefined) await buildActions(extensionConfig) - expect(coreConfig.reload).toHaveBeenCalledTimes(1) - expect(utils.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(coreConfig.reload.mock.invocationCallOrder[0]) - expect(coreConfig.reload.mock.invocationCallOrder[0]).toBeLessThan(utils.runInProcess.mock.invocationCallOrder[1]) + expect(utils.reloadEnvOnHooks).toHaveBeenCalledTimes(1) + expect(utils.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(utils.reloadEnvOnHooks.mock.invocationCallOrder[0]) + expect(utils.reloadEnvOnHooks.mock.invocationCallOrder[0]).toBeLessThan(utils.runInProcess.mock.invocationCallOrder[1]) }) test('exports', () => { diff --git a/test/commands/lib/deploy-actions.test.js b/test/commands/lib/deploy-actions.test.js index 53976331d..2457388ec 100644 --- a/test/commands/lib/deploy-actions.test.js +++ b/test/commands/lib/deploy-actions.test.js @@ -35,13 +35,12 @@ test('exports', () => { expect(typeof deployActions).toEqual('function') }) -test('reloads config after pre-app-deploy', async () => { - const coreConfig = require('@adobe/aio-lib-core-config') - coreConfig.reload.mockClear() +test('reloads env after pre-app-deploy', async () => { + utils.reloadEnvOnHooks.mockClear() await deployActions({ config: { hooks: {} } }) - expect(coreConfig.reload).toHaveBeenCalledTimes(1) - expect(utils.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(coreConfig.reload.mock.invocationCallOrder[0]) - expect(coreConfig.reload.mock.invocationCallOrder[0]).toBeLessThan(rtDeployActions.mock.invocationCallOrder[0]) + expect(utils.reloadEnvOnHooks).toHaveBeenCalledTimes(1) + expect(utils.runInProcess.mock.invocationCallOrder[0]).toBeLessThan(utils.reloadEnvOnHooks.mock.invocationCallOrder[0]) + expect(utils.reloadEnvOnHooks.mock.invocationCallOrder[0]).toBeLessThan(rtDeployActions.mock.invocationCallOrder[0]) }) test('deploy-actions app hook available', async () => {