@@ -6,11 +6,12 @@ import boxen from "boxen";
6
6
import { CAC } from "cac" ;
7
7
import type { Options } from "execa" ;
8
8
import execa from "execa" ;
9
+ import fsExtra from "fs-extra" ;
9
10
import { gray , green , red , yellow } from "kolorist" ;
10
11
11
12
import { ACTIVATION , FRAMEWORKS , TEMPLATES } from "@/config" ;
12
13
13
- import { CodeGeniusOptions } from "./types" ;
14
+ import { CodeGeniusOptions , CommandOptions } from "./types" ;
14
15
15
16
const boxenBorderStyle = {
16
17
padding : 1 ,
@@ -291,9 +292,69 @@ export function generateRandom(length: number) {
291
292
export const defineConfig = ( config : CodeGeniusOptions ) : CodeGeniusOptions =>
292
293
config ;
293
294
294
- export function cmdInstaller ( cli : CAC , config : CodeGeniusOptions ) {
295
+ export async function cmdInstaller ( cli : CAC , config : CodeGeniusOptions ) {
295
296
const { plugins } = config ;
296
297
for ( const plugin of plugins ) {
297
298
plugin ( cli ) . setup ( ) ;
298
299
}
299
300
}
301
+
302
+ export function genScriptConfig ( scripts : { [ key : string ] : string } ) {
303
+ return Object . keys ( scripts ) . map ( ( key ) => {
304
+ return {
305
+ cmd : key ,
306
+ script : scripts [ key ] ,
307
+ desc : "describe the function of this cmd command" ,
308
+ } ;
309
+ } ) ;
310
+ }
311
+
312
+ export function syncScripts (
313
+ pkgScripts : Array < CommandOptions > ,
314
+ configScripts : Array < CommandOptions > ,
315
+ ) {
316
+ const mergedScripts = [ ...configScripts ] ;
317
+
318
+ for ( const pkgScript of pkgScripts ) {
319
+ const configScript = mergedScripts . find (
320
+ ( itemB ) => itemB . cmd === pkgScript . cmd ,
321
+ ) ;
322
+
323
+ if ( configScript ) {
324
+ if ( configScript . script !== pkgScript . script ) {
325
+ configScript . script = pkgScript . script ;
326
+ }
327
+ } else {
328
+ mergedScripts . push ( pkgScript ) ;
329
+ }
330
+ }
331
+
332
+ const syncScripts = mergedScripts . filter ( ( configScript ) => {
333
+ return pkgScripts . find ( ( i ) => i . cmd === configScript . cmd ) ;
334
+ } ) ;
335
+
336
+ return syncScripts ;
337
+ }
338
+
339
+ export const generateScripts = async ( ) => {
340
+ const pkg = await fsExtra . readJSONSync (
341
+ path . join ( process . cwd ( ) , "package.json" ) ,
342
+ ) ;
343
+ let configContent = genScriptConfig ( pkg . scripts ) ;
344
+ const configfile = path . join ( process . cwd ( ) , "scripts.config.json" ) ;
345
+ if ( fs . existsSync ( configfile ) ) {
346
+ const { scripts } = await fsExtra . readJSONSync ( configfile ) ;
347
+ configContent = syncScripts ( configContent , scripts ) ;
348
+ }
349
+ await fsExtra . outputFileSync (
350
+ configfile ,
351
+ JSON . stringify (
352
+ {
353
+ scripts : configContent ,
354
+ } ,
355
+ null ,
356
+ 2 ,
357
+ ) ,
358
+ ) ;
359
+ printInfo ( "代理脚本 scripts.config.json 已完成同步" ) ;
360
+ } ;
0 commit comments