|
| 1 | +import { join, relative } from 'node:path' |
| 2 | +import { rm } from 'node:fs/promises' |
| 3 | +import process from 'node:process' |
| 4 | +import { intro, log, outro, spinner } from '@clack/prompts' |
| 5 | +import { Root, defineCommand } from 'clerc' |
| 6 | +import { bold, green } from 'yoctocolors' |
| 7 | +import { cancel, confirm, exists, formatPmCommand, getPmCommand, getTemplateDirectory, gradientBanner, runPmCommand, text, validateProjectName } from '@/utils' |
| 8 | +import { creaateProjectOptions, createProject } from '@/create' |
| 9 | +import { useFlags } from '@/flags' |
| 10 | + |
| 11 | +const defaultProjectName = 'vue-vine-project' |
| 12 | + |
| 13 | +const { flags, executeFlags } = useFlags() |
| 14 | + |
| 15 | +export const createCommand = defineCommand({ |
| 16 | + name: Root, |
| 17 | + description: 'Create a Vue Vine project', |
| 18 | + parameters: [ |
| 19 | + '[projectName]', |
| 20 | + ], |
| 21 | + flags: { |
| 22 | + force: { |
| 23 | + type: Boolean, |
| 24 | + description: 'Delete existing folder', |
| 25 | + alias: 'f', |
| 26 | + default: false, |
| 27 | + }, |
| 28 | + install: { |
| 29 | + type: Boolean, |
| 30 | + description: 'Install dependencies', |
| 31 | + alias: 'i', |
| 32 | + }, |
| 33 | + ...flags, |
| 34 | + }, |
| 35 | + alias: 'create', |
| 36 | +}, async (ctx) => { |
| 37 | + intro(gradientBanner) |
| 38 | + const cwd = process.cwd() |
| 39 | + if (!ctx.parameters.projectName) { |
| 40 | + ctx.parameters.projectName = await text({ |
| 41 | + message: 'Project name:', |
| 42 | + placeholder: defaultProjectName, |
| 43 | + defaultValue: defaultProjectName, |
| 44 | + validate: (value) => { |
| 45 | + if (!validateProjectName(value)) { |
| 46 | + return 'Invalid project name' |
| 47 | + } |
| 48 | + }, |
| 49 | + }) |
| 50 | + } |
| 51 | + const projectPath = join(cwd, ctx.parameters.projectName) |
| 52 | + if (await exists(projectPath)) { |
| 53 | + if (!ctx.flags.force) { |
| 54 | + ctx.flags.force = await confirm({ |
| 55 | + message: `Folder ${ctx.parameters.projectName} already exists. Delete?`, |
| 56 | + initialValue: false, |
| 57 | + }) |
| 58 | + } |
| 59 | + if (!ctx.flags.force) { |
| 60 | + cancel(`Folder ${ctx.parameters.projectName} already exists. Goodbye!`) |
| 61 | + } |
| 62 | + else { |
| 63 | + log.info(`Folder ${ctx.parameters.projectName} will be deleted.`) |
| 64 | + await rm(projectPath, { recursive: true }) |
| 65 | + } |
| 66 | + } |
| 67 | + const templateDir = await getTemplateDirectory() |
| 68 | + if (!templateDir) { |
| 69 | + cancel('Unable to find template directory') |
| 70 | + } |
| 71 | + |
| 72 | + const projectOptions = creaateProjectOptions({ |
| 73 | + path: projectPath, |
| 74 | + name: ctx.parameters.projectName, |
| 75 | + templateDir, |
| 76 | + }) |
| 77 | + |
| 78 | + await executeFlags(ctx.flags, projectOptions) |
| 79 | + |
| 80 | + const s = spinner() |
| 81 | + s.start(`Creating project ${ctx.parameters.projectName}`) |
| 82 | + await createProject(projectOptions) |
| 83 | + s.stop(`Project created at: ${projectPath}`) |
| 84 | + if (ctx.flags.install === undefined) { |
| 85 | + ctx.flags.install = await confirm({ |
| 86 | + message: 'Install dependencies?', |
| 87 | + initialValue: true, |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + if (ctx.flags.install) { |
| 92 | + s.start('Installing dependencies') |
| 93 | + await runPmCommand('install', projectPath) |
| 94 | + s.stop('Dependencies installed!') |
| 95 | + } |
| 96 | + const cdProjectPath = relative(cwd, projectPath) |
| 97 | + const helpText = [ |
| 98 | + 'You\'re all set! Now run:', |
| 99 | + '', |
| 100 | + ` cd ${bold(green(cdProjectPath.includes(' ') ? `"${cdProjectPath}"` : cdProjectPath))}`, |
| 101 | + ctx.flags.install ? undefined : ` ${bold(green(formatPmCommand(getPmCommand('install'))))}`, |
| 102 | + ` ${bold(green(formatPmCommand(getPmCommand('dev'))))}`, |
| 103 | + '', |
| 104 | + ' Happy hacking!', |
| 105 | + ].filter(s => s !== undefined).join('\n') |
| 106 | + outro(helpText) |
| 107 | + process.exit() // Ugh, why |
| 108 | +}) |
0 commit comments