@@ -134,96 +134,6 @@ export const printError = (content: string | unknown) => {
134
134
) ;
135
135
} ;
136
136
137
- /**
138
- * 根据后缀列表过滤获取合法的文件列表
139
- * @param fileList
140
- * @param suffixes
141
- * @returns
142
- */
143
- export function getFiilesBySuffixes (
144
- fileList : string [ ] ,
145
- suffixes : string [ ] ,
146
- ) : string [ ] {
147
- const paths : string [ ] = [ ] ;
148
-
149
- for ( let i = 0 ; i < fileList . length ; i ++ ) {
150
- const file = fileList [ i ] ;
151
-
152
- for ( let j = 0 ; j < suffixes . length ; j ++ ) {
153
- const extension = suffixes [ j ] ;
154
-
155
- if ( file . endsWith ( extension ) ) {
156
- paths . push ( file ) ;
157
- break ;
158
- }
159
- }
160
- }
161
- return paths ;
162
- }
163
-
164
- /**
165
- * 获取目录列表下所有的文件列表
166
- *
167
- * @param paths
168
- * @returns
169
- */
170
- export function getEveryFiles ( paths : string [ ] ) : string [ ] {
171
- const fileList : string [ ] = [ ] ;
172
- function traverseDirectory ( dirPath : string ) {
173
- const files = fs . readdirSync ( dirPath ) ;
174
-
175
- files . forEach ( ( file ) => {
176
- const filePath = path . join ( dirPath , file ) ;
177
- const stat = fs . statSync ( filePath ) ;
178
-
179
- if ( stat . isDirectory ( ) ) {
180
- traverseDirectory ( filePath ) ;
181
- } else {
182
- fileList . push ( filePath ) ;
183
- }
184
- } ) ;
185
- }
186
-
187
- paths . forEach ( ( dirPath ) => {
188
- traverseDirectory ( dirPath ) ;
189
- } ) ;
190
-
191
- return Array . from ( new Set ( fileList ) ) ;
192
- }
193
-
194
- /**
195
- * 获取指定目录后暂存区所有符合给定后缀的文件列表
196
- * @param cwd
197
- * @param staged
198
- * @param paths
199
- * @param suffix
200
- * @returns
201
- */
202
- export const getEveryFilesBySuffixes = async (
203
- cwd : string ,
204
- staged : boolean ,
205
- paths : string [ ] ,
206
- suffix : string [ ] ,
207
- ) => {
208
- let files : string [ ] = [ ] ;
209
- if ( staged ) {
210
- const result = await execCommand ( "git" , [
211
- "diff" ,
212
- "--name-only" ,
213
- "--diff-filter=d" ,
214
- "--cached" ,
215
- ] ) ;
216
- files = result ?. split ( "\n" ) . map ( ( path : string ) => `${ cwd } /${ path } ` ) || [ ] ;
217
- } else {
218
- files = getEveryFiles ( paths . map ( ( path ) => `${ cwd } /${ path } ` ) ) ;
219
- }
220
- return getFiilesBySuffixes ( files , suffix ) ;
221
- } ;
222
-
223
- export function formatTargetDir ( targetDir : string | undefined ) {
224
- return targetDir ?. trim ( ) . replace ( / \/ + $ / g, "" ) ;
225
- }
226
-
227
137
export function isValidPackageName ( projectName : string ) {
228
138
return / ^ (?: @ [ a - z 0 - 9 - * ~ ] [ a -z 0 -9 -* ._ ~ ] * \/ ) ? [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * $ / . test (
229
139
projectName ,
@@ -284,6 +194,11 @@ export function isValidVariant(framework: string) {
284
194
return variants . length > 0 ;
285
195
}
286
196
197
+ /**
198
+ * 生成随机串
199
+ * @param length
200
+ * @returns
201
+ */
287
202
export function generateRandom ( length : number ) {
288
203
let result = "" ;
289
204
const characters =
@@ -295,9 +210,19 @@ export function generateRandom(length: number) {
295
210
return result ;
296
211
}
297
212
213
+ /**
214
+ * 用户配置工厂函数
215
+ * @param config
216
+ * @returns
217
+ */
298
218
export const defineConfig = ( config : CodeGeniusOptions ) : CodeGeniusOptions =>
299
219
config ;
300
220
221
+ /**
222
+ * 用于循环注册指令
223
+ * @param cli
224
+ * @param config
225
+ */
301
226
export async function cmdInstaller ( cli : CAC , config : CodeGeniusOptions ) {
302
227
const { plugins } = config ;
303
228
if ( plugins ) {
@@ -307,6 +232,11 @@ export async function cmdInstaller(cli: CAC, config: CodeGeniusOptions) {
307
232
}
308
233
}
309
234
235
+ /**
236
+ * 用于转换 scripts 结构
237
+ * @param scripts
238
+ * @returns
239
+ */
310
240
export function genScriptConfig ( scripts : { [ key : string ] : string } ) {
311
241
return Object . keys ( scripts ) . map ( ( key ) => {
312
242
return {
@@ -317,6 +247,12 @@ export function genScriptConfig(scripts: { [key: string]: string }) {
317
247
} ) ;
318
248
}
319
249
250
+ /**
251
+ * 用于合并两份 scripts.config.json 数据
252
+ * @param pkgScripts
253
+ * @param configScripts
254
+ * @returns
255
+ */
320
256
export function syncScripts (
321
257
pkgScripts : Array < CommandOptions > ,
322
258
configScripts : Array < CommandOptions > ,
@@ -337,13 +273,16 @@ export function syncScripts(
337
273
}
338
274
}
339
275
340
- const syncScripts = mergedScripts . filter ( ( configScript ) => {
276
+ const scripts = mergedScripts . filter ( ( configScript ) => {
341
277
return pkgScripts . find ( ( i ) => i . cmd === configScript . cmd ) ;
342
278
} ) ;
343
279
344
- return syncScripts ;
280
+ return scripts ;
345
281
}
346
282
283
+ /**
284
+ * 读取 package.json 解析并生成 scripts.config.json 配置文件
285
+ */
347
286
export const generateScripts = async ( ) => {
348
287
const pkg = await fsExtra . readJSONSync (
349
288
path . join ( process . cwd ( ) , "package.json" ) ,
@@ -367,6 +306,9 @@ export const generateScripts = async () => {
367
306
printInfo ( "代理脚本 scripts.config.json 已完成同步" ) ;
368
307
} ;
369
308
309
+ /**
310
+ * 动态加载用户配置文件
311
+ */
370
312
export async function loadConfigModule ( ) : Promise <
371
313
CodeGeniusOptions | undefined
372
314
> {
0 commit comments