2022-09-08 15:43:04 +00:00
|
|
|
import { downloadRepo, startShell } from 'giget'
|
2021-10-05 20:35:23 +00:00
|
|
|
import { relative, resolve } from 'pathe'
|
|
|
|
import superb from 'superb'
|
2021-10-07 10:15:15 +00:00
|
|
|
import consola from 'consola'
|
2021-10-05 20:35:23 +00:00
|
|
|
import { defineNuxtCommand } from './index'
|
|
|
|
|
2022-08-26 15:47:29 +00:00
|
|
|
const rpath = (p: string) => relative(process.cwd(), p)
|
2021-10-05 20:35:23 +00:00
|
|
|
|
2022-08-26 15:47:29 +00:00
|
|
|
const resolveTemplate = (template: string | boolean) => {
|
2021-10-24 14:48:02 +00:00
|
|
|
if (typeof template === 'boolean') {
|
2022-01-17 21:29:32 +00:00
|
|
|
consola.error('Please specify a template!')
|
2021-10-24 14:48:02 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:29:32 +00:00
|
|
|
if (!template) {
|
|
|
|
template = 'v3'
|
2021-10-24 11:39:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-24 14:48:02 +00:00
|
|
|
if (template.includes('/')) {
|
2021-10-24 11:39:17 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2022-01-17 21:29:32 +00:00
|
|
|
return `nuxt/starter#${template}`
|
2021-10-24 11:39:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 20:35:23 +00:00
|
|
|
export default defineNuxtCommand({
|
|
|
|
meta: {
|
|
|
|
name: 'init',
|
2022-09-08 15:43:04 +00:00
|
|
|
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
|
2021-10-24 11:39:17 +00:00
|
|
|
const src = resolveTemplate(args.template || args.t)
|
2021-10-05 20:35:23 +00:00
|
|
|
const dstDir = resolve(process.cwd(), args._[0] || 'nuxt-app')
|
2022-09-08 15:43:04 +00:00
|
|
|
if (args.verbose || args.v) {
|
|
|
|
process.env.DEBUG = process.env.DEBUG || 'true'
|
2021-10-13 10:30:27 +00:00
|
|
|
}
|
2022-09-08 15:43:04 +00:00
|
|
|
await downloadRepo(src, dstDir, {
|
|
|
|
force: args.force,
|
|
|
|
offline: args.offline,
|
|
|
|
preferOffline: args['prefer-offline']
|
|
|
|
})
|
2021-10-05 20:35:23 +00:00
|
|
|
|
2022-04-29 19:03:46 +00:00
|
|
|
// Show next steps
|
|
|
|
const relativeDist = rpath(dstDir)
|
|
|
|
const nextSteps = [
|
|
|
|
relativeDist.length > 1 && `📁 \`cd ${relativeDist}\``,
|
2022-03-22 09:35:07 +00:00
|
|
|
'💿 Install dependencies with `npm install` or `yarn install` or `pnpm install --shamefully-hoist`',
|
2022-04-29 19:03:46 +00:00
|
|
|
'🚀 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`)
|
|
|
|
}
|
2022-09-08 15:43:04 +00:00
|
|
|
|
|
|
|
if (args.shell) {
|
|
|
|
startShell(dstDir)
|
|
|
|
}
|
2021-10-05 20:35:23 +00:00
|
|
|
}
|
|
|
|
})
|