fix(nuxi): validate template argument (#1409)

This commit is contained in:
James George 2021-10-24 17:09:17 +05:30 committed by GitHub
parent 9e3dfad172
commit bd010313db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,19 @@ const knownTemplates = {
bridge: 'nuxt/starter#bridge' bridge: 'nuxt/starter#bridge'
} }
const resolveTemplate = (template) => {
if (template in knownTemplates) {
return knownTemplates[template]
}
if (typeof template === 'string' && template.includes('/')) {
return template
}
consola.error(`Invalid template name: \`${template}\``)
process.exit(1)
}
export default defineNuxtCommand({ export default defineNuxtCommand({
meta: { meta: {
name: 'init', name: 'init',
@ -21,8 +34,7 @@ export default defineNuxtCommand({
}, },
async invoke (args) { async invoke (args) {
// Clone template // Clone template
const t = args.template || args.t const src = resolveTemplate(args.template || args.t)
const src = knownTemplates[t] || t || 'nuxt/starter#v3'
const dstDir = resolve(process.cwd(), args._[0] || 'nuxt-app') const dstDir = resolve(process.cwd(), args._[0] || 'nuxt-app')
const tiged = createTiged(src, { cache: false /* TODO: buggy */, verbose: (args.verbose || args.v) }) const tiged = createTiged(src, { cache: false /* TODO: buggy */, verbose: (args.verbose || args.v) })
if (existsSync(dstDir) && readdirSync(dstDir).length) { if (existsSync(dstDir) && readdirSync(dstDir).length) {