fix: modern=true or false not work as expected (#4378)

This commit is contained in:
Clark Du 2018-11-22 15:48:26 +00:00 committed by Pooya Parsa
parent b12141b99a
commit ff7c083dda
6 changed files with 100 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import consola from 'consola' import consola from 'consola'
import { common } from '../options' import { common } from '../options'
import { normalizeArg } from '../utils'
export default { export default {
name: 'generate', name: 'generate',
@ -16,7 +17,7 @@ export default {
...common.modern, ...common.modern,
description: 'Generate app in modern build (modern mode can be only client)', description: 'Generate app in modern build (modern mode can be only client)',
prepare(cmd, options, argv) { prepare(cmd, options, argv) {
if (argv.modern !== undefined) { if (normalizeArg(argv.modern)) {
options.modern = 'client' options.modern = 'client'
} }
} }

View File

@ -1,3 +1,5 @@
import { normalizeArg } from '../utils'
export default { export default {
spa: { spa: {
alias: 's', alias: 's',
@ -21,7 +23,7 @@ export default {
description: 'Build/Start app for modern browsers, e.g. server, client and false', description: 'Build/Start app for modern browsers, e.g. server, client and false',
prepare(cmd, options, argv) { prepare(cmd, options, argv) {
if (argv.modern !== undefined) { if (argv.modern !== undefined) {
options.modern = argv.modern || true options.modern = normalizeArg(argv.modern)
} }
} }
}, },

View File

@ -110,3 +110,21 @@ export function showBanner(nuxt) {
process.stdout.write(box + '\n') 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
}

View File

@ -44,6 +44,41 @@ describe('build', () => {
expect(process.exit).toHaveBeenCalled() 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 () => { test('catches error', async () => {
mockGetNuxt({ mode: 'universal' }) mockGetNuxt({ mode: 'universal' })
mockGetBuilder(Promise.reject(new Error('Builder Error'))) mockGetBuilder(Promise.reject(new Error('Builder Error')))

View File

@ -46,6 +46,38 @@ describe('generate', () => {
Command.prototype.getArgv = getArgv 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 () => { test('catches error', async () => {
mockGetNuxt() mockGetNuxt()
mockGetGenerator(Promise.reject(new Error('Generator Error'))) mockGetGenerator(Promise.reject(new Error('Generator Error')))

View File

@ -81,6 +81,16 @@ describe('cli/utils', () => {
expect(consola.fatal).toHaveBeenCalledWith('Error while fetching async configuration') 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', () => { test('nuxtServerConfig: server env', () => {
const options = getDefaultNuxtConfig({ const options = getDefaultNuxtConfig({
env: { env: {