Nuxt/packages/nuxi/src/commands/init.ts

63 lines
1.8 KiB
TypeScript
Raw Normal View History

import { downloadRepo, startShell } from 'giget'
2021-10-05 20:35:23 +00:00
import { relative, resolve } from 'pathe'
import superb from 'superb'
import consola from 'consola'
2021-10-05 20:35:23 +00:00
import { defineNuxtCommand } from './index'
const rpath = (p: string) => relative(process.cwd(), p)
2021-10-05 20:35:23 +00:00
const resolveTemplate = (template: string | boolean) => {
if (typeof template === 'boolean') {
consola.error('Please specify a template!')
process.exit(1)
}
if (!template) {
template = 'v3'
}
if (template.includes('/')) {
return template
}
return `nuxt/starter#${template}`
}
2021-10-05 20:35:23 +00:00
export default defineNuxtCommand({
meta: {
name: 'init',
usage: 'npx nuxi init|create [--template,-t] [--force] [--offline] [--prefer-offline] [--shell] [dir]',
2021-10-05 20:35:23 +00:00
description: 'Initialize a fresh project'
},
async invoke (args) {
// Clone template
const src = resolveTemplate(args.template || args.t)
2021-10-05 20:35:23 +00:00
const dstDir = resolve(process.cwd(), args._[0] || 'nuxt-app')
if (args.verbose || args.v) {
process.env.DEBUG = process.env.DEBUG || 'true'
}
await downloadRepo(src, dstDir, {
force: args.force,
offline: args.offline,
preferOffline: args['prefer-offline']
})
2021-10-05 20:35:23 +00:00
// Show next steps
const relativeDist = rpath(dstDir)
const nextSteps = [
relativeDist.length > 1 && `📁 \`cd ${relativeDist}\``,
'💿 Install dependencies with `npm install` or `yarn install` or `pnpm install --shamefully-hoist`',
'🚀 Start development server with `npm run dev` or `yarn dev` or `pnpm run dev`'
].filter(Boolean)
consola.log(`\n ✨ Your ${superb.random()} Nuxt project is just created! Next steps:\n`)
for (const step of nextSteps) {
consola.log(` ${step}\n`)
}
if (args.shell) {
startShell(dstDir)
}
2021-10-05 20:35:23 +00:00
}
})