Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/create. Command-line for creating projects from templates.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150import path from 'node:path'import process from 'node:process'import { objectKeys, objectPick } from '@antfu/utils'import { intro, isCancel, select, text, type CANCEL_SYMBOL,} from '@clack/prompts'import ansis from 'ansis'import consola from 'consola'import { downloadTemplate } from 'giget'import { editConfig, getConfig, normalizeTemplate, type ConfigNormalized, type TemplateNormalized,} from './config.ts'import { command } from './features/command.ts'import { git } from './features/git.ts'import { replace } from './features/replace.ts'import { variable } from './features/variable.ts'import { CliError, getColor } from './utils.ts'import type { Config, ConfigTemplate, Context, ProjectInfo } from './types.ts'
export type { Config }export function defineConfig(config: Config): Config { return config}
export async function edit(): Promise<void> { const { init, file } = await getConfig() if (!init) { consola.info(`Open config file in ${ansis.blueBright(file)}`) editConfig(file) }}
export async function run({ projectPath, config,}: { projectPath?: string; config?: ConfigNormalized } = {}): Promise<void> { try { config ||= (await getConfig()).config
intro(ansis.bgBlueBright(' @sxzz/create ')) const templates = await chooseTemplate(config) const template = normalizeTemplate(templates) await create({ projectPath, template }) } catch (error) { if (error instanceof CliError) { consola.error(error.message) } else consola.error(error) }}
async function chooseTemplate(config: ConfigNormalized) { let currentTemplate: ConfigTemplate = { name: '', children: config.templates, ...objectPick( config, objectKeys(config).filter((k) => k !== 'templates'), ), }
const templateStacks: ConfigTemplate[] = [currentTemplate] do { const templateName = await select({ message: 'Pick a template', options: currentTemplate.children!.map(({ name, color }) => { return { value: name, label: getColor(color)(name), } }), }) if (isCancel(templateName)) { templateStacks.pop() if (templateStacks.length === 0) { throw new CliError('No template selected.') } currentTemplate = templateStacks.at(-1)! continue } const template = currentTemplate.children!.find( ({ name }) => name === templateName, )! if (template.url) { return [...templateStacks, template] } else if (template.children) { templateStacks.push(template) currentTemplate = template } else { throw new Error(`Bad template: ${JSON.stringify(template)}`) } // eslint-disable-next-line no-constant-condition } while (true)}
async function create({ template, projectPath: _relativePath,}: { template: TemplateNormalized projectPath?: string}) { let relativePath: string | typeof CANCEL_SYMBOL | undefined = _relativePath if (!relativePath) { relativePath = await text({ message: 'Folder name of the project', validate: (v) => (v?.length ? undefined : 'folder name cannot be empty.'), }) } if (!relativePath || isCancel(relativePath)) throw new CliError('No folder name provided.')
const projectPath = path.resolve(template.cwd, relativePath) const folderName = path.basename(projectPath) const url = template.url! const project: ProjectInfo = { url, folderName, path: projectPath, variables: {}, } const ctx: Context = { template, project }
await variable(ctx)
consola.info(`Creating project in ${ansis.blueBright(projectPath)}...`) await downloadTemplate(url, { provider: 'github', dir: projectPath, ...template.giget, })
await git(ctx) await replace(ctx) await command(ctx)
consola.success( `${ansis.green.bold(`Done. Now run:`)}\n\n ${ansis.blueBright( `cd ${path.relative(process.cwd(), projectPath)}`, )}\n`, )}