2021-10-05 20:35:23 +00:00
|
|
|
import { existsSync, readdirSync } from 'fs'
|
2021-10-18 10:04:06 +00:00
|
|
|
import createTiged from 'tiged'
|
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'
|
|
|
|
|
|
|
|
const rpath = p => relative(process.cwd(), p)
|
|
|
|
|
2021-10-24 11:39:17 +00:00
|
|
|
const resolveTemplate = (template) => {
|
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',
|
2021-11-02 09:50:04 +00:00
|
|
|
usage: 'npx nuxi init|create [--verbose|-v] [--template,-t] [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')
|
2021-10-18 10:04:06 +00:00
|
|
|
const tiged = createTiged(src, { cache: false /* TODO: buggy */, verbose: (args.verbose || args.v) })
|
2021-10-05 20:35:23 +00:00
|
|
|
if (existsSync(dstDir) && readdirSync(dstDir).length) {
|
2021-10-07 10:15:15 +00:00
|
|
|
consola.error(`Directory ${dstDir} is not empty. Please pick another name or remove it first. Aborting.`)
|
2021-10-05 20:35:23 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
const formatArgs = msg => msg.replace('options.', '--')
|
2021-10-18 10:04:06 +00:00
|
|
|
tiged.on('warn', event => consola.warn(formatArgs(event.message)))
|
|
|
|
tiged.on('info', event => consola.info(formatArgs(event.message)))
|
2021-10-13 10:30:27 +00:00
|
|
|
try {
|
2021-10-18 10:04:06 +00:00
|
|
|
await tiged.clone(dstDir)
|
2021-10-13 10:30:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e.toString().includes('could not find commit hash')) {
|
2021-10-13 11:16:15 +00:00
|
|
|
consola.warn('Make sure you have installed `git` correctly')
|
2021-10-13 10:30:27 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
throw e
|
|
|
|
}
|
2021-10-05 20:35:23 +00:00
|
|
|
|
|
|
|
// Show neet steps
|
2021-10-13 10:30:27 +00:00
|
|
|
console.log(`\n 🎉 Another ${superb.random()} Nuxt project just made! Next steps:` + [
|
2021-10-05 20:35:23 +00:00
|
|
|
'',
|
|
|
|
`📁 \`cd ${rpath(dstDir)}\``,
|
|
|
|
'💿 Install dependencies with `npm install` or `yarn install`',
|
|
|
|
'🚀 Start development server with `npm run dev` or `yarn dev`',
|
|
|
|
''
|
|
|
|
].join('\n\n '))
|
|
|
|
}
|
|
|
|
})
|