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

46 lines
1.4 KiB
TypeScript
Raw Normal View History

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