11import { performance } from "node:perf_hooks" ;
22
33import type { CAC } from "cac" ;
4- import {
5- ACTIVATION ,
6- CodeGeniusOptions ,
7- execCommand ,
8- formatGlob ,
9- loggerInfo ,
10- } from "code-genius" ;
4+ import { ACTIVATION , execCommand , loggerInfo } from "code-genius" ;
115
12- const mergeConfig = async ( config : CodeGeniusOptions ) => {
13- const commands = config && config ?. commands ;
14- if ( commands && commands . format ) {
15- const { paths } = commands . format ;
16- return {
17- paths : paths && paths . length > 0 ? paths : formatGlob ,
18- } ;
19- }
20- return {
21- paths : formatGlob ,
22- } ;
23- } ;
6+ import { formatGlob , FormatOptions } from "./common" ;
247
258const prettierFormat = async ( paths : string [ ] ) => {
269 if ( ACTIVATION ) {
@@ -32,24 +15,22 @@ const prettierFormat = async (paths: string[]) => {
3215 } ) ;
3316} ;
3417
35- const prettierFormatInstaller = ( config : CodeGeniusOptions ) => {
18+ const prettierFormatInstaller = ( config : FormatOptions ) => {
19+ const { files } = config ;
3620 return {
3721 name : "prettierFormatInstaller" ,
3822 setup : ( cli : CAC ) => {
3923 cli
4024 . command ( "format" , "运行 prettier 格式化代码风格" )
4125 . option ( "-p, --pattern <pattern>" , "设置匹配规则" )
4226 . action ( async ( options ) => {
43- const { paths } = await mergeConfig ( config ) ;
27+ let paths = files || formatGlob ;
4428 const { pattern } = options ;
45- const start = performance . now ( ) ;
4629 if ( pattern ) {
47- await prettierFormat (
48- typeof pattern === "string" ? [ pattern ] : pattern ,
49- ) ;
50- } else {
51- await prettierFormat ( paths ) ;
30+ paths = typeof pattern === "string" ? [ pattern ] : pattern ;
5231 }
32+ const start = performance . now ( ) ;
33+ await prettierFormat ( paths ) ;
5334 const getTime = ( ) => `${ ( performance . now ( ) - start ) . toFixed ( 2 ) } ms` ;
5435 loggerInfo ( `😁 format 命令执行结束, 共用时: ${ getTime ( ) } ` ) ;
5536 } ) ;
0 commit comments