From 31b7a9e7599d639ba2d312530d8e788d15bcc269 Mon Sep 17 00:00:00 2001 From: Jesse MacFadyen Date: Thu, 14 Nov 2024 17:06:02 -0800 Subject: [PATCH] do not force login if deploy with --no-publish (#819) --- src/commands/app/deploy.js | 3 +-- src/lib/app-helper.js | 31 +++++++++++++++++++++------- test/commands/lib/app-helper.test.js | 7 +++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index ba3333bf..3b8a2224 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -43,7 +43,6 @@ class Deploy extends BuildCommand { // if there are no extensions, then set publish to false flags.publish = flags.publish && !isStandaloneApp - if ( (!flags.publish && !flags['web-assets'] && !flags.actions) ) { @@ -53,7 +52,7 @@ class Deploy extends BuildCommand { try { const aioConfig = (await this.getFullConfig()).aio - const cliDetails = await getCliInfo() + const cliDetails = await getCliInfo(flags.publish) // 1. update log forwarding configuration // note: it is possible that .aio file does not exist, which means there is no local lg config diff --git a/src/lib/app-helper.js b/src/lib/app-helper.js index a323c2c6..c2b4b3c5 100644 --- a/src/lib/app-helper.js +++ b/src/lib/app-helper.js @@ -180,15 +180,30 @@ function wrapError (err) { return new Error(message) } -/** @private */ -async function getCliInfo () { - await context.setCli({ 'cli.bare-output': true }, false) // set this globally - +/** + * getCliInfo + * + * @private + * + * @param {boolean} useForce - if true, user will be forced to login if not already logged in + * @returns {Promise<{accessToken: string, env: string}>} accessToken and env +*/ +async function getCliInfo (useForce = true) { const env = getCliEnv() - - aioLogger.debug(`Retrieving CLI Token using env=${env}`) - const accessToken = await getToken(CLI) - + let accessToken + await context.setCli({ 'cli.bare-output': true }, false) // set this globally + if (useForce) { + aioLogger.debug('Retrieving CLI Token using force=true') + accessToken = await getToken(CLI) + } else { + aioLogger.debug('Retrieving CLI Token using force=false') + // in this case, the user might be logged in, but we don't want to force them + // we just check the config for the token ( we still use the cli context so we don't need to know + // the inner workings of ims-lib and where it stores access tokens) + // todo: this is a workaround, we should have a better way to check if the user is logged in (in ims-lib) + const contextConfig = await context.getCli() + accessToken = contextConfig?.access_token?.token + } return { accessToken, env } } diff --git a/test/commands/lib/app-helper.test.js b/test/commands/lib/app-helper.test.js index cfc59a99..c6f92ce9 100644 --- a/test/commands/lib/app-helper.test.js +++ b/test/commands/lib/app-helper.test.js @@ -906,6 +906,13 @@ describe('getCliInfo', () => { { accessToken: 'stoken', env: 'stage' } ) }) + test('useForceFalse', async () => { + libEnv.getCliEnv.mockReturnValue('prod') + libIms.getToken.mockResolvedValue('asdasd') + const res = await appHelper.getCliInfo(false) + expect(res).toEqual({ accessToken: undefined, env: 'prod' }) + expect(libIms.getToken).toHaveBeenCalledTimes(0) + }) }) describe('createWebExportFilter', () => {