@@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url'
33import defu from 'defu'
44import fs from 'fs-extra'
55import path from 'pathe'
6+ import pc from 'picocolors'
67import set from 'set-value'
78import { logger } from './logger'
89
@@ -17,32 +18,38 @@ export interface CreateNewProjectOptions {
1718 name ?: string
1819 cwd ?: string
1920 renameJson ?: boolean
21+ type ?: 'tsup' | 'unbuild'
22+ }
23+
24+ const defaultTemplate = 'tsup'
25+
26+ const fromMap = {
27+ tsup : 'bar' ,
28+ unbuild : 'foo' ,
2029}
2130
2231export async function createNewProject ( options ?: CreateNewProjectOptions ) {
23- const defaultTemplate = 'bar'
24- const { name, renameJson, cwd } = defu < Required < CreateNewProjectOptions > , CreateNewProjectOptions [ ] > ( options , {
32+ const { name : targetName , renameJson, cwd, type } = defu < Required < CreateNewProjectOptions > , CreateNewProjectOptions [ ] > ( options , {
2533 cwd : process . cwd ( ) ,
26- name : defaultTemplate ,
34+ name : fromMap [ defaultTemplate ] ,
2735 renameJson : false ,
2836 } )
29-
30- const targetTemplate = name
31- const from = path . join ( templatesDir , defaultTemplate )
32- const to = path . join ( cwd , targetTemplate )
37+ const bundlerName = type ?? defaultTemplate
38+ const from = path . join ( templatesDir , fromMap [ bundlerName ] )
39+ const to = path . join ( cwd , targetName )
3340 const filelist = await fs . readdir ( from )
3441 for ( const filename of filelist ) {
3542 if ( filename === 'package.json' ) {
3643 const sourceJsonPath = path . resolve ( from , filename )
3744 const sourceJson = await fs . readJson ( sourceJsonPath )
3845 set ( sourceJson , 'version' , '0.0.0' )
39- set ( sourceJson , 'name' , path . basename ( targetTemplate ) )
46+ set ( sourceJson , 'name' , path . basename ( targetName ) )
4047 await fs . outputJson ( path . resolve ( to , renameJson ? 'package.mock.json' : filename ) , sourceJson , { spaces : 2 } )
4148 }
4249 else {
4350 await fs . copy ( path . resolve ( from , filename ) , path . resolve ( to , filename ) )
4451 }
4552 }
4653
47- logger . success ( `${ targetTemplate } 项目创建成功!` )
54+ logger . success ( `${ pc . bgGreenBright ( pc . white ( `[ ${ bundlerName } ]` ) ) } ${ targetName } 项目创建成功!` )
4855}
0 commit comments