@@ -2,6 +2,7 @@ import * as path from 'path';
22import * as fs from 'fs' ;
33import chalk from 'chalk' ;
44import * as yaml from 'js-yaml' ;
5+ import glob from 'fast-glob' ;
56
67interface FormatOptions {
78 dir ?: string ;
@@ -20,7 +21,6 @@ export async function format(options: FormatOptions) {
2021 let errorCount = 0 ;
2122
2223 try {
23- const glob = require ( 'fast-glob' ) ;
2424 const files = await glob ( [ '**/*.yml' , '**/*.yaml' ] , {
2525 cwd : rootDir ,
2626 ignore : [ 'node_modules/**' , 'dist/**' , 'build/**' ]
@@ -37,29 +37,34 @@ export async function format(options: FormatOptions) {
3737 // Parse to validate YAML
3838 yaml . load ( content ) ;
3939
40- // Format with Prettier (using require instead of import for better compatibility)
41- const prettierFormat = require ( 'prettier' ) . format ;
42- const formatted = await prettierFormat ( content , {
43- parser : 'yaml' ,
44- printWidth : 80 ,
45- tabWidth : 2 ,
46- singleQuote : true
47- } ) ;
48-
49- if ( content !== formatted ) {
50- if ( options . check ) {
51- console . log ( chalk . yellow ( ` ⚠️ ${ file } needs formatting` ) ) ;
52- formattedCount ++ ;
40+ // Format with Prettier - use dynamic import for better compatibility
41+ try {
42+ const prettier = await import ( 'prettier' ) ;
43+ const formatted = await prettier . format ( content , {
44+ parser : 'yaml' ,
45+ printWidth : 80 ,
46+ tabWidth : 2 ,
47+ singleQuote : true
48+ } ) ;
49+
50+ if ( content !== formatted ) {
51+ if ( options . check ) {
52+ console . log ( chalk . yellow ( ` ⚠️ ${ file } needs formatting` ) ) ;
53+ formattedCount ++ ;
54+ } else {
55+ fs . writeFileSync ( filePath , formatted , 'utf-8' ) ;
56+ console . log ( chalk . green ( ` ✅ ${ file } ` ) ) ;
57+ formattedCount ++ ;
58+ }
5359 } else {
54- fs . writeFileSync ( filePath , formatted , 'utf-8' ) ;
55- console . log ( chalk . green ( ` ✅ ${ file } ` ) ) ;
56- formattedCount ++ ;
57- }
58- } else {
59- unchangedCount ++ ;
60- if ( ! options . check ) {
61- console . log ( chalk . gray ( ` ✓ ${ file } ` ) ) ;
60+ unchangedCount ++ ;
61+ if ( ! options . check ) {
62+ console . log ( chalk . gray ( ` ✓ ${ file } ` ) ) ;
63+ }
6264 }
65+ } catch ( prettierError : any ) {
66+ console . error ( chalk . red ( ` ❌ ${ file } : Prettier error - ${ prettierError . message } ` ) ) ;
67+ errorCount ++ ;
6368 }
6469 } catch ( e : any ) {
6570 console . error ( chalk . red ( ` ❌ ${ file } : ${ e . message } ` ) ) ;
0 commit comments