From 93ce793b37ad6da0bfc0578e6d04442b13ff5547 Mon Sep 17 00:00:00 2001 From: Himavanth Date: Wed, 2 Dec 2020 23:19:48 +0530 Subject: [PATCH] Extending build cmd instead of calling it from deploy --- src/commands/app/build.js | 5 +++ src/commands/app/deploy.js | 20 ++--------- test/commands/app/deploy.test.js | 57 +++++++++++++++++--------------- 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/commands/app/build.js b/src/commands/app/build.js index add60e2c..390ddcfc 100644 --- a/src/commands/app/build.js +++ b/src/commands/app/build.js @@ -27,6 +27,11 @@ class Build extends BaseCommand { const config = this.getAppConfig() const spinner = ora() + + await this.build(config, flags, spinner) + } + + async build (config, flags, spinner) { const onProgress = !flags.verbose ? info => { spinner.text = info } : info => { diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index e541e1ad..4acb9e1c 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -16,12 +16,13 @@ const chalk = require('chalk') const { cli } = require('cli-ux') const BaseCommand = require('../../BaseCommand') +const BuildCommand = require('./build') const webLib = require('@adobe/aio-lib-web') const { flags } = require('@oclif/command') const { runPackageScript, wrapError } = require('../../lib/app-helper') const rtLib = require('@adobe/aio-lib-runtime') -class Deploy extends BaseCommand { +class Deploy extends BuildCommand { async run () { // cli input const { flags } = this.parse(Deploy) @@ -40,22 +41,7 @@ class Deploy extends BaseCommand { try { // build phase if (!flags['skip-build']) { - const buildArgs = [] - Object.keys(flags).forEach((flag) => { - if (['skip-static', 'skip-actions', 'verbose', 'version'].includes(flag)) { - buildArgs.push('--' + flag) - } - }) - if (!flags['force-build']) { - buildArgs.push('--no-force-build') - } - if (flags.action) { - flags.action.forEach((actionName) => { - buildArgs.push('-a') - buildArgs.push(actionName) - }) - } - await this.config.runCommand('app:build', buildArgs) + await this.build(config, flags, spinner) } // deploy phase diff --git a/test/commands/app/deploy.test.js b/test/commands/app/deploy.test.js index f2f06179..f5b4d4ba 100644 --- a/test/commands/app/deploy.test.js +++ b/test/commands/app/deploy.test.js @@ -90,7 +90,10 @@ describe('run', () => { command.error = jest.fn() command.log = jest.fn() command.appConfig = mockConfigData + command.appConfig.actions = { dist: 'actions' } + command.appConfig.web.distProd = 'dist' command.config = { runCommand: jest.fn() } + command.build = jest.fn() mockRuntimeLib.deployActions.mockResolvedValue({}) }) @@ -103,7 +106,7 @@ describe('run', () => { await command.run() // expect(command.error).toHaveBeenCalledWith(0) expect(command.error).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledTimes(1) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) }) @@ -114,8 +117,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', expect.arrayContaining(['--no-force-build', '--verbose'])) + expect(command.build).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': false, verbose: true }), expect.anything()) }) test('build & deploy --skip-static', async () => { @@ -124,8 +127,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', expect.arrayContaining(['--skip-static', '--no-force-build'])) + expect(command.build).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': false, 'skip-static': true }), expect.anything()) }) test('build & deploy only some actions using --action', async () => { @@ -134,9 +137,9 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', expect.arrayContaining(['--skip-static', '--no-force-build', '-a', 'a', '-a', 'b', '-a', 'c'])) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': false, 'skip-static': true, action: ['a', 'b', 'c'] }), expect.anything()) expect(mockRuntimeLib.deployActions).toHaveBeenCalledWith(mockConfigData, { filterEntities: { actions: ['a', 'b', 'c'] } }, @@ -150,8 +153,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(0) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', ['--skip-static', '--no-force-build']) + expect(command.build).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': false, 'skip-static': true }), expect.anything()) }) test('build & deploy actions with no actions folder but with a manifest', async () => { @@ -160,7 +163,7 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledTimes(1) }) test('build & deploy with --skip-actions', async () => { @@ -169,8 +172,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(0) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', expect.arrayContaining(['--skip-actions'])) + expect(command.build).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': false, 'skip-actions': true }), expect.anything()) }) test('build & deploy with --skip-actions with no static folder', async () => { @@ -180,8 +183,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(0) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', ['--skip-actions', '--no-force-build']) + expect(command.build).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': false, 'skip-actions': true }), expect.anything()) }) test('build & deploy with no manifest.yml', async () => { @@ -190,8 +193,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(0) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', ['--no-force-build']) + expect(command.build).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': false }), expect.anything()) }) test('--skip-deploy', async () => { @@ -200,7 +203,7 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(0) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', ['--no-force-build']) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': false }), expect.anything()) }) test('--skip-deploy --verbose', async () => { @@ -209,8 +212,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(0) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', ['--verbose', '--no-force-build']) + expect(command.build).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': false, verbose: true }), expect.anything()) }) test('--skip-deploy --skip-static', async () => { @@ -219,7 +222,7 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(0) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledTimes(1) }) test('--skip-build', async () => { @@ -228,7 +231,7 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledTimes(0) + expect(command.build).toHaveBeenCalledTimes(0) }) test('--skip-build --verbose', async () => { @@ -237,7 +240,7 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledTimes(0) + expect(command.build).toHaveBeenCalledTimes(0) }) test('--skip-build --skip-actions', async () => { @@ -246,7 +249,7 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(0) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledTimes(0) + expect(command.build).toHaveBeenCalledTimes(0) }) test('--skip-build --skip-static', async () => { @@ -255,7 +258,7 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(0) - expect(command.config.runCommand).toHaveBeenCalledTimes(0) + expect(command.build).toHaveBeenCalledTimes(0) }) test('--force-build', async () => { @@ -264,8 +267,8 @@ describe('run', () => { expect(command.error).toHaveBeenCalledTimes(0) expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) expect(mockWebLib.deployWeb).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) - expect(command.config.runCommand).toHaveBeenCalledWith('app:build', []) // force-build is true by default for build cmd + expect(command.build).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalledWith(command.appConfig, expect.objectContaining({ 'force-build': true }), expect.anything()) // force-build is true by default for build cmd }) test('deploy should show ui url', async () => { @@ -327,7 +330,7 @@ describe('run', () => { mockRuntimeLib.deployActions.mockRejectedValue(error) await command.run() expect(command.error).toHaveBeenCalledWith(error) - expect(command.config.runCommand).toHaveBeenCalledTimes(1) + expect(command.build).toHaveBeenCalled() expect(mockRuntimeLib.deployActions).toHaveBeenCalledTimes(1) })