diff --git a/src/args/args.d.ts b/src/args/args.d.ts index facd7d3..b82ebd1 100644 --- a/src/args/args.d.ts +++ b/src/args/args.d.ts @@ -3,10 +3,12 @@ import type { bugArgs } from '@/args/bug.ts' import type { componentArgs } from '@/args/component.ts' import type { defaultArgs } from '@/args/default.ts' import type { demoArgs } from '@/args/demo.ts' +import type { initArgs } from '@/args/init.ts' export type OptionsArgs = ParsedArgs & Partial< ParsedArgs & ParsedArgs & ParsedArgs + & ParsedArgs > diff --git a/src/args/init.ts b/src/args/init.ts new file mode 100644 index 0000000..ecd60e3 --- /dev/null +++ b/src/args/init.ts @@ -0,0 +1,19 @@ +import type { ArgsDef } from 'citty' + +export const initArgs = { + name: { + type: 'positional', + description: 'Project Name', + default: 'antdv-next-project', + }, + nuxt: { + type: 'boolean', + description: 'Whether to initialize the Nuxt project template', + default: false, + }, + git: { + type: 'boolean', + description: 'Whether or not to initialize the Git repository', + default: true, + }, +} satisfies ArgsDef diff --git a/src/commands/init.ts b/src/commands/init.ts new file mode 100644 index 0000000..5d18fd5 --- /dev/null +++ b/src/commands/init.ts @@ -0,0 +1,62 @@ +import { resolve } from 'node:path' +import { box, intro, outro } from '@clack/prompts' +import { defineCommand } from 'citty' +import { downloadTemplate } from 'giget' +import { cyan, green } from 'picocolors' +import { x } from 'tinyexec' +import { defaultArgs } from '@/args/default.ts' +import { initArgs } from '@/args/init.ts' +import { resolveConfig } from '@/config.ts' + +export default defineCommand({ + meta: { + name: 'init', + description: 'Initialize a fresh project', + }, + args: { + ...defaultArgs, + ...initArgs, + }, + async run({ args }) { + intro('Welcome to Antdv!') + + const config = resolveConfig(args) + + const dir = resolve(config.cwd, args.name) + const template = `https://codeload.github.com/antdv-next/${args.nuxt ? 'nuxt-template' : 'starter-template'}/tar.gz/refs/heads/main` + + await downloadTemplate(template, { + cwd: config.cwd, + dir, + force: true, + forceClean: true, + }) + + if (args.git) { + await x('git', ['init'], { + nodeOptions: { + cwd: dir, + stdio: 'pipe', + }, + }) + } + + outro(`🚀 Successfully created project ${green(args.name)}`) + + const nextStep = [ + `cd ${args.name}`, + 'pnpm install', + 'pnpm run dev', + ] + + box(`\n${nextStep.map(step => ` › ${cyan(step)}`).join('\n')}\n`, ' 👉 Next steps ', { + contentAlign: 'left', + titleAlign: 'left', + width: 'auto', + rounded: true, + titlePadding: 2, + contentPadding: 2, + withGuide: false, + }) + }, +}) diff --git a/src/index.ts b/src/index.ts index c5acc04..df14ea1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { description, name, version } from '../package.json' with { type: 'json' const CLI_VERSION = __CLI_VERSION__ const subCommands = { + 'init': () => import('./commands/init.ts').then(r => r.default), 'bug': () => import('./commands/bug.ts').then(r => r.default), 'bug-cli': () => import('./commands/bug-cli.ts').then(r => r.default), 'changelog': () => import('./commands/changelog.ts').then(r => r.default),