@@ -11,7 +11,7 @@ import {
11
11
} from '@hey-api/openapi-ts/internal' ;
12
12
import { logger , workspaceRoot } from '@nx/devkit' ;
13
13
import { compareOpenApi } from 'api-smart-diff' ;
14
- import { format , type Options as PrettierOptions } from 'prettier' ;
14
+ import { format , resolveConfig } from 'prettier' ;
15
15
import { convert } from 'swagger2openapi' ;
16
16
17
17
import { CONSTANTS } from './vars' ;
@@ -335,22 +335,17 @@ export async function makeDir(path: string) {
335
335
await mkdir ( path , { recursive : true } ) ;
336
336
}
337
337
338
- export async function formatFiles (
339
- dir : string ,
340
- prettierOptions ?: PrettierOptions ,
341
- ) {
338
+ /**
339
+ * Formats all files in a directory
340
+ */
341
+ export async function formatFiles ( dir : string ) {
342
342
const files = await readdir ( dir , { withFileTypes : true } ) ;
343
343
const tasks = files . map ( async ( file ) => {
344
344
const filePath = join ( dir , file . name ) ;
345
345
if ( file . isDirectory ( ) ) {
346
- await formatFiles ( filePath , prettierOptions ) ;
347
- } else if ( file . name . endsWith ( '.ts' ) ) {
348
- const content = await readFile ( filePath , 'utf-8' ) ;
349
- const formatted = await format ( content , {
350
- ...prettierOptions ,
351
- filepath : filePath ,
352
- } ) ;
353
- await writeFile ( filePath , formatted ) ;
346
+ await formatFiles ( filePath ) ;
347
+ } else if ( file . isFile ( ) ) {
348
+ await formatFile ( filePath ) ;
354
349
}
355
350
} ) ;
356
351
await Promise . all ( tasks ) ;
@@ -434,3 +429,21 @@ export async function getBaseTsConfigPath({
434
429
logger . error ( message ) ;
435
430
throw new Error ( message ) ;
436
431
}
432
+
433
+ export async function formatFile ( filePath : string ) {
434
+ const content = await readFile ( filePath , 'utf-8' ) ;
435
+ const formatted = await formatStringFromFilePath ( content , filePath ) ;
436
+ await writeFile ( filePath , formatted ) ;
437
+ }
438
+
439
+ export async function formatStringFromFilePath (
440
+ content : string ,
441
+ filePath : string ,
442
+ ) {
443
+ const prettierOptions = await resolveConfig ( filePath ) ;
444
+ const formatted = await format ( content , {
445
+ ...prettierOptions ,
446
+ filepath : filePath ,
447
+ } ) ;
448
+ return formatted ;
449
+ }
0 commit comments