diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index aca2ecc0..743af8be 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -50,65 +50,60 @@ class Undeploy extends BaseCommand { } const spinner = ora() - try { - const { aio: aioConfig, packagejson: packageJson } = await this.getFullConfig({}, flags) - const cliDetails = await getAccessToken({ useCachedToken: !flags.unpublish }) - const appInfo = { - name: packageJson.name, - version: packageJson.version, - project: aioConfig?.project, - runtimeNamespace: aioConfig?.runtime?.namespace + + const { aio: aioConfig, packagejson: packageJson } = await this.getFullConfig({}, flags) + const cliDetails = await getAccessToken({ useCachedToken: !flags.unpublish }) + const appInfo = { + name: packageJson.name, + version: packageJson.version, + project: aioConfig?.project, + runtimeNamespace: aioConfig?.runtime?.namespace + } + + if (cliDetails?.accessToken) { + try { + // send audit log at start (don't wait for deployment to finish) + await sendAppUndeployAuditLog({ + accessToken: cliDetails?.accessToken, + cliCommandFlags: flags, + appInfo, + env: cliDetails.env + }) + } catch (error) { + if (flags.verbose) { + this.warn('Error: Audit Log Service Error: Failed to send audit log event for deployment.') + this.warn(error.message) + } } + } + for (let i = 0; i < keys.length; ++i) { + const k = keys[i] + // TODO: remove this check once the deploy service is enabled by default + const v = setRuntimeApiHostAndAuthHandler(values[i]) + + await this.undeployOneExt(k, v, flags, spinner) if (cliDetails?.accessToken) { + // send logs for case of web-assets undeployment try { - // send audit log at start (don't wait for deployment to finish) - await sendAppUndeployAuditLog({ + await sendAppAssetsUndeployedAuditLog({ accessToken: cliDetails?.accessToken, cliCommandFlags: flags, appInfo, env: cliDetails.env }) } catch (error) { - if (flags.verbose) { - this.warn('Error: Audit Log Service Error: Failed to send audit log event for deployment.') - this.warn(error.message) - } - } - } - - for (let i = 0; i < keys.length; ++i) { - const k = keys[i] - // TODO: remove this check once the deploy service is enabled by default - const v = setRuntimeApiHostAndAuthHandler(values[i]) - - await this.undeployOneExt(k, v, flags, spinner) - if (cliDetails?.accessToken) { - // send logs for case of web-assets undeployment - try { - await sendAppAssetsUndeployedAuditLog({ - accessToken: cliDetails?.accessToken, - cliCommandFlags: flags, - appInfo, - env: cliDetails.env - }) - } catch (error) { - this.warn('Warning: Audit Log Service Error: Failed to send audit log event for un-deployment.') - } + this.warn('Warning: Audit Log Service Error: Failed to send audit log event for un-deployment.') } } + } - // 1.2. unpublish extension manifest - if (flags.unpublish) { - const payload = await this.unpublishExtensionPoints(libConsoleCLI, undeployConfigs, aioConfig, flags['force-unpublish']) - this.log(chalk.blue(chalk.bold(`New Extension Point(s) in Workspace '${aioConfig.project.workspace.name}': '${Object.keys(payload.endpoints)}'`))) - } else { - this.log('skipping unpublish phase...') - } - } catch (error) { - spinner.stop() - // delegate to top handler - throw error + // 1.2. unpublish extension manifest + if (flags.unpublish) { + const payload = await this.unpublishExtensionPoints(libConsoleCLI, undeployConfigs, aioConfig, flags['force-unpublish']) + this.log(chalk.blue(chalk.bold(`New Extension Point(s) in Workspace '${aioConfig.project.workspace.name}': '${Object.keys(payload.endpoints)}'`))) + } else { + this.log('skipping unpublish phase...') } const command = await this.config.findCommand('app:clean') @@ -151,8 +146,7 @@ class Undeploy extends BaseCommand { } spinner.succeed(chalk.green(`Un-deploying actions for ${extName}`)) } catch (err) { - spinner.fail(chalk.green(`Un-deploying actions for ${extName}`)) - throw err + spinner.warn(chalk.yellow(`Error when un-deploying actions for ${extName}: ${err.message}`)) } } else { this.log('no manifest file, skipping action undeploy') @@ -168,8 +162,7 @@ class Undeploy extends BaseCommand { spinner.succeed(chalk.green(`Un-Deploying web assets for ${extName}`)) } catch (err) { - spinner.fail(chalk.green(`Un-Deploying web assets for ${extName}`)) - throw err + spinner.warn(chalk.yellow(`Error when un-deploying web assets for ${extName}: ${err.message}`)) } } else { this.log('no frontend, skipping frontend undeploy') diff --git a/test/commands/app/undeploy.test.js b/test/commands/app/undeploy.test.js index 68a96e23..c7076d21 100644 --- a/test/commands/app/undeploy.test.js +++ b/test/commands/app/undeploy.test.js @@ -158,6 +158,7 @@ describe('run', () => { mockFS.existsSync.mockReset() command = new TheCommand([]) command.error = jest.fn() + command.warn = jest.fn() command.log = jest.fn() command.appConfig = cloneDeep(mockConfigData) command.appConfig.actions = { dist: 'actions' } @@ -325,27 +326,34 @@ describe('run', () => { expect(command.log).toHaveBeenCalledWith('no frontend, skipping frontend undeploy') }) - test('should fail if scripts.undeployActions fails', async () => { + test('should not fail if scripts.undeployActions fails', async () => { command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig()) const error = new Error('mock failure Actions') mockRuntimeLib.undeployActions.mockRejectedValue(error) - await expect(command.run()).rejects.toThrow(error) + await expect(command.run()).resolves.toBeUndefined() + + // multiple check lines because error wraps text + expect(stdout.output).toContain('Error when un-deploying actions for application: mock failure Actions') expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.undeployActions).toHaveBeenCalledTimes(1) + expect(mockWebLib.undeployWeb).toHaveBeenCalledTimes(1) }) - test('should fail if scripts.undeployWeb fails', async () => { + test('should not fail if scripts.undeployWeb fails', async () => { command.getAppExtConfigs.mockResolvedValueOnce(createAppConfig()) const error = new Error('mock failure UI') mockWebLib.undeployWeb.mockRejectedValue(error) - await expect(command.run()).rejects.toThrow(error) + await expect(command.run()).resolves.toBeUndefined() + + expect(stdout.output).toContain('Error when un-deploying web assets for application: mock failure UI') expect(command.error).toHaveBeenCalledTimes(0) + expect(mockRuntimeLib.undeployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.undeployWeb).toHaveBeenCalledTimes(1) })