2022-09-10 13:37:25 +00:00
|
|
|
|
import { downloadTemplate, startShell } from 'giget'
|
|
|
|
|
import { relative } from 'pathe'
|
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-09-10 13:37:25 +00:00
|
|
|
|
const DEFAULT_REGISTRY = 'https://raw.githubusercontent.com/nuxt/starter/templates/templates'
|
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
|
2022-09-10 13:37:25 +00:00
|
|
|
|
const template = args.template || args.t || 'v3'
|
|
|
|
|
|
|
|
|
|
const t = await downloadTemplate(template, {
|
|
|
|
|
dir: args._[0] as string,
|
2022-09-08 15:43:04 +00:00
|
|
|
|
force: args.force,
|
|
|
|
|
offline: args.offline,
|
2022-09-10 13:37:25 +00:00
|
|
|
|
preferOffline: args['prefer-offline'],
|
|
|
|
|
registry: process.env.NUXI_INIT_REGISTRY || DEFAULT_REGISTRY
|
2022-09-08 15:43:04 +00:00
|
|
|
|
})
|
2021-10-05 20:35:23 +00:00
|
|
|
|
|
2022-04-29 19:03:46 +00:00
|
|
|
|
// Show next steps
|
2022-09-10 13:37:25 +00:00
|
|
|
|
const relativeDist = rpath(t.dir)
|
2022-04-29 19:03:46 +00:00
|
|
|
|
const nextSteps = [
|
2022-09-10 13:37:25 +00:00
|
|
|
|
!args.shell && 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`'
|
2022-04-29 19:03:46 +00:00
|
|
|
|
].filter(Boolean)
|
|
|
|
|
|
2022-09-10 13:37:25 +00:00
|
|
|
|
consola.log(`✨ Nuxt project is created with \`${t.name}\` template. Next steps:`)
|
2022-04-29 19:03:46 +00:00
|
|
|
|
for (const step of nextSteps) {
|
2022-09-10 13:37:25 +00:00
|
|
|
|
consola.log(` › ${step}`)
|
2022-04-29 19:03:46 +00:00
|
|
|
|
}
|
2022-09-08 15:43:04 +00:00
|
|
|
|
|
|
|
|
|
if (args.shell) {
|
2022-09-10 13:37:25 +00:00
|
|
|
|
startShell(t.dir)
|
2022-09-08 15:43:04 +00:00
|
|
|
|
}
|
2021-10-05 20:35:23 +00:00
|
|
|
|
}
|
|
|
|
|
})
|