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 GitHub
parent 6f91f8e9f2
commit 4c2708849f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import consola from 'consola'
import { common } from '../options'
import { normalizeArg } from '../utils'
export default {
name: 'generate',
@ -28,7 +29,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'
}
}

View File

@ -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)
}
}
},

View File

@ -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
}

View File

@ -63,6 +63,22 @@ describe('build', () => {
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')))

View File

@ -64,6 +64,20 @@ describe('generate', () => {
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')))

View File

@ -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: {