1
1
import { setTimeout as sleep } from 'timers/promises'
2
- import { readdir , readFile , rename , stat , writeFile , cp , mkdir } from 'fs/promises'
3
- import path from 'path'
2
+ import { readdir , readFile , rename , stat , writeFile , cp , mkdir , unlink } from 'fs/promises'
3
+ import { join } from 'path'
4
4
import { spawn } from 'child_process'
5
+ import Handlebars from 'handlebars'
6
+ import { registerHandlebarsHelpers } from '@/handlebars/register'
7
+
8
+ registerHandlebarsHelpers ( )
5
9
6
10
async function renameFiles ( currentDir : string ) {
7
11
const renameMap = {
@@ -18,31 +22,39 @@ async function renameFiles(currentDir: string) {
18
22
if ( ! ( item in renameMap ) ) {
19
23
continue
20
24
}
21
- await rename ( path . join ( currentDir , item ) , path . join ( currentDir , renameMap [ item as keyof typeof renameMap ] ) )
25
+ await rename ( join ( currentDir , item ) , join ( currentDir , renameMap [ item as keyof typeof renameMap ] ) )
22
26
}
23
27
}
24
28
25
- async function replaceVariables (
29
+ async function compileTemplateFiles (
26
30
currentDir : string ,
27
- replacements : {
31
+ templateData : {
28
32
projectName : string
33
+ year : string
34
+ transports : string [ ]
35
+ plugins : string [ ]
36
+ components : string [ ]
29
37
} ,
30
38
) {
31
- const variables = {
32
- '{{PROJECT_NAME}}' : replacements . projectName ,
33
- '{{YEAR}}' : new Date ( ) . getFullYear ( ) . toString ( ) ,
34
- }
35
39
const items = await readdir ( currentDir , { recursive : true } )
36
40
for ( const item of items ) {
37
- const itemPath = path . join ( currentDir , item )
41
+ const itemPath = join ( currentDir , item )
38
42
const itemStat = await stat ( itemPath )
39
43
if ( itemStat . isDirectory ( ) ) {
40
44
continue
41
45
}
42
- for ( const [ key , value ] of Object . entries ( variables ) ) {
43
- const content = await readFile ( itemPath , 'utf-8' )
44
- const regex = new RegExp ( key , 'g' )
45
- const newContent = content . replace ( regex , value )
46
+ const content = await readFile ( itemPath , 'utf-8' )
47
+ const template = Handlebars . compile ( content )
48
+ const newContent = template ( templateData )
49
+ if ( newContent . trim ( ) === '' ) {
50
+ await unlink ( itemPath )
51
+ continue
52
+ }
53
+ if ( itemPath . endsWith ( '.hbs' ) ) {
54
+ const newItemPath = itemPath . slice ( 0 , - 4 )
55
+ await writeFile ( newItemPath , newContent , 'utf-8' )
56
+ await unlink ( itemPath )
57
+ } else {
46
58
await writeFile ( itemPath , newContent , 'utf-8' )
47
59
}
48
60
}
@@ -70,14 +82,52 @@ export function installDependencies(currentDir: string) {
70
82
export async function createProject (
71
83
targetPath : string ,
72
84
templatePath : string ,
73
- replacements : {
85
+ templateData : {
74
86
projectName : string
87
+ year : string
88
+ transports : string [ ]
89
+ plugins : string [ ]
90
+ components : string [ ]
75
91
} ,
76
92
) {
77
93
await mkdir ( targetPath , { recursive : true } )
78
- await cp ( templatePath , targetPath , { recursive : true } )
94
+ await cp ( templatePath , targetPath , {
95
+ recursive : true ,
96
+ filter : ( ) => {
97
+ // const relativePath = relative(templatePath, src)
98
+ // const fileName = basename(src)
99
+ // console.log('relativePath', relativePath)
100
+ // console.log('fileName', fileName)
101
+ // if (plugins === null) {
102
+ // return true
103
+ // }
104
+ // if (!plugins.includes('github-action') && relativePath.startsWith('_github')) {
105
+ // return false
106
+ // }
107
+ // if (
108
+ // !plugins.includes('vitest') &&
109
+ // (relativePath.startsWith('tests') ||
110
+ // ['vitest.config.js', 'vitest.setup.js', 'vitest.config.ts', 'vitest.setup.ts'].includes(relativePath))
111
+ // ) {
112
+ // return false
113
+ // }
114
+ // if (
115
+ // !plugins.includes('style') &&
116
+ // ['eslint.config.js', '_prettierrc', 'lint-staged.config.js', '_husky/pre-commit'].includes(relativePath)
117
+ // ) {
118
+ // return false
119
+ // }
120
+ // if (!plugins.includes('commitlint') && ['commitlint.config.js', '_husky/commit-msg'].includes(relativePath)) {
121
+ // return false
122
+ // }
123
+ // if (!plugins.includes('changelog') && ['changelog-option.js'].includes(relativePath)) {
124
+ // return false
125
+ // }
126
+ return true
127
+ } ,
128
+ } )
79
129
await renameFiles ( targetPath )
80
- await replaceVariables ( targetPath , replacements )
130
+ await compileTemplateFiles ( targetPath , templateData )
81
131
}
82
132
83
133
export { sleep }
0 commit comments