From ff7c083dda3158ff8b1e0bc4868e64dede9f0313 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Thu, 22 Nov 2018 15:48:26 +0000 Subject: [PATCH] fix: modern=true or false not work as expected (#4378) --- packages/cli/src/commands/generate.js | 3 ++- packages/cli/src/options/common.js | 4 ++- packages/cli/src/utils/index.js | 18 +++++++++++++ packages/cli/test/unit/build.test.js | 35 +++++++++++++++++++++++++ packages/cli/test/unit/generate.test.js | 32 ++++++++++++++++++++++ packages/cli/test/unit/utils.test.js | 10 +++++++ 6 files changed, 100 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands/generate.js b/packages/cli/src/commands/generate.js index d727174e74..2632b59434 100644 --- a/packages/cli/src/commands/generate.js +++ b/packages/cli/src/commands/generate.js @@ -1,5 +1,6 @@ import consola from 'consola' import { common } from '../options' +import { normalizeArg } from '../utils' export default { name: 'generate', @@ -16,7 +17,7 @@ export default { ...common.modern, description: 'Generate app in modern build (modern mode can be only client)', prepare(cmd, options, argv) { - if (argv.modern !== undefined) { + if (normalizeArg(argv.modern)) { options.modern = 'client' } } diff --git a/packages/cli/src/options/common.js b/packages/cli/src/options/common.js index 28f66061b1..bab7c20c24 100644 --- a/packages/cli/src/options/common.js +++ b/packages/cli/src/options/common.js @@ -1,3 +1,5 @@ +import { normalizeArg } from '../utils' + export default { spa: { alias: 's', @@ -21,7 +23,7 @@ export default { description: 'Build/Start app for modern browsers, e.g. server, client and false', prepare(cmd, options, argv) { if (argv.modern !== undefined) { - options.modern = argv.modern || true + options.modern = normalizeArg(argv.modern) } } }, diff --git a/packages/cli/src/utils/index.js b/packages/cli/src/utils/index.js index b3786a6f14..5a1eca0f5e 100644 --- a/packages/cli/src/utils/index.js +++ b/packages/cli/src/utils/index.js @@ -110,3 +110,21 @@ export function showBanner(nuxt) { process.stdout.write(box + '\n') } + +/** + * Normalize string argument in command + * + * @export + * @param {String} argument + * @param {*} defaultValue + * @returns formatted argument + */ +export function normalizeArg(arg, defaultValue) { + switch (arg) { + case 'true': arg = true; break + case '': arg = true; break + case 'false': arg = false; break + case undefined: arg = defaultValue; break + } + return arg +} diff --git a/packages/cli/test/unit/build.test.js b/packages/cli/test/unit/build.test.js index 1ebc3ea7b4..924fb32016 100644 --- a/packages/cli/test/unit/build.test.js +++ b/packages/cli/test/unit/build.test.js @@ -44,6 +44,41 @@ describe('build', () => { expect(process.exit).toHaveBeenCalled() }) + test('build with devtools', async () => { + mockGetNuxt({ + mode: 'universal' + }) + const builder = mockGetBuilder(Promise.resolve()) + + const cmd = NuxtCommand.from(build) + const args = ['build', '.', '--devtools'] + const argv = cmd.getArgv(args) + argv._ = ['.'] + + const options = await cmd.getNuxtConfig(argv) + + await cmd.run() + + expect(options.vue.config.devtools).toBe(true) + expect(builder).toHaveBeenCalled() + }) + + test('build with modern mode', async () => { + mockGetNuxt({ + mode: 'universal' + }) + mockGetBuilder(Promise.resolve()) + + const cmd = NuxtCommand.from(build) + const args = ['build', '.', '--m'] + + const options = await cmd.getNuxtConfig(cmd.getArgv(args)) + + await cmd.run() + + expect(options.modern).toBe(true) + }) + test('catches error', async () => { mockGetNuxt({ mode: 'universal' }) mockGetBuilder(Promise.reject(new Error('Builder Error'))) diff --git a/packages/cli/test/unit/generate.test.js b/packages/cli/test/unit/generate.test.js index 4e4cd9bfee..9c17b35c78 100644 --- a/packages/cli/test/unit/generate.test.js +++ b/packages/cli/test/unit/generate.test.js @@ -46,6 +46,38 @@ describe('generate', () => { Command.prototype.getArgv = getArgv }) + test('build with devtools', async () => { + mockGetNuxt() + const generator = mockGetGenerator(Promise.resolve()) + + const cmd = NuxtCommand.from(generate) + const args = ['generate', '.', '--devtools'] + const argv = cmd.getArgv(args) + argv._ = ['.'] + + const options = await cmd.getNuxtConfig(argv) + + await cmd.run() + + expect(options.vue.config.devtools).toBe(true) + expect(generator).toHaveBeenCalled() + expect(generator.mock.calls[0][0].build).toBe(true) + }) + + test('generate with modern mode', async () => { + mockGetNuxt() + mockGetGenerator(Promise.resolve()) + + const cmd = NuxtCommand.from(generate) + const args = ['generate', '.', '--m'] + + const options = await cmd.getNuxtConfig(cmd.getArgv(args)) + + await cmd.run() + + expect(options.modern).toBe('client') + }) + test('catches error', async () => { mockGetNuxt() mockGetGenerator(Promise.reject(new Error('Generator Error'))) diff --git a/packages/cli/test/unit/utils.test.js b/packages/cli/test/unit/utils.test.js index 630add5bad..2e5476727d 100644 --- a/packages/cli/test/unit/utils.test.js +++ b/packages/cli/test/unit/utils.test.js @@ -81,6 +81,16 @@ describe('cli/utils', () => { expect(consola.fatal).toHaveBeenCalledWith('Error while fetching async configuration') }) + test('normalizeArg: normalize string argument in command', () => { + expect(utils.normalizeArg('true')).toBe(true) + expect(utils.normalizeArg('false')).toBe(false) + expect(utils.normalizeArg(true)).toBe(true) + expect(utils.normalizeArg(false)).toBe(false) + expect(utils.normalizeArg('')).toBe(true) + expect(utils.normalizeArg(undefined, 'default')).toBe('default') + expect(utils.normalizeArg('text')).toBe('text') + }) + test('nuxtServerConfig: server env', () => { const options = getDefaultNuxtConfig({ env: {