File tree Expand file tree Collapse file tree 2 files changed +15
-4
lines changed
Expand file tree Collapse file tree 2 files changed +15
-4
lines changed Original file line number Diff line number Diff line change 11import { workerMain } from "@better-rules-javascript/bazel-worker" ;
22import { ArgumentParser } from "argparse" ;
3- import { dirname } from "node:path" ;
3+ import * as path from "node:path" ;
44import { pathToFileURL } from "node:url" ;
55import { Options } from "prettier" ;
66import { load , resolve } from "./import" ;
7+ import { readFile , writeFile } from "node:fs/promises" ;
78
89interface Args {
910 config ?: string ;
@@ -23,7 +24,7 @@ workerMain(async (a) => {
2324 : ( await resolveConfig ( args . config , { config : args . config } ) ) ||
2425 undefined ;
2526 if ( options ?. plugins ) {
26- const contextUrl = pathToFileURL ( dirname ( args . config ! ) ) ;
27+ const contextUrl = pathToFileURL ( path . dirname ( args . config ! ) ) ;
2728 options . plugins = await Promise . all (
2829 options . plugins . map ( async ( plugin ) => {
2930 // in theory, should be able to just resolve the path, but for some reason
@@ -37,7 +38,8 @@ workerMain(async (a) => {
3738 } ) ,
3839 ) ;
3940 }
40- const worker = new PrettierWorker ( options ) ;
41+ const fileExtOverrides = ( await require ( path . resolve ( args . config ) ) ) . fileExtOverrides ;
42+ const worker = new PrettierWorker ( options , fileExtOverrides ) ;
4143
4244 return async ( a ) => {
4345 try {
Original file line number Diff line number Diff line change @@ -3,7 +3,10 @@ import { readFile, writeFile } from "node:fs/promises";
33import { Options , format } from "prettier" ;
44
55export class PrettierWorker {
6- constructor ( private readonly options : Options | undefined ) { }
6+ constructor (
7+ readonly options : Options | undefined ,
8+ readonly fileExtOverrides : { [ fileExt : string ] : object } | undefined ,
9+ ) { }
710
811 async run ( a : string [ ] ) {
912 const parser = new ArgumentParser ( ) ;
@@ -13,6 +16,12 @@ export class PrettierWorker {
1316 const input = await readFile ( args . input , "utf8" ) ;
1417 const output = await format ( input , {
1518 ...this . options ,
19+ ...Object . assign (
20+ { } ,
21+ ...Object . entries ( this . fileExtOverrides ?? { } )
22+ . filter ( ( [ fileExt , overrides ] ) => args . input . endsWith ( fileExt ) )
23+ . map ( ( [ fileExt , overrides ] ) => overrides )
24+ ) ,
1625 filepath : args . input ,
1726 } ) ;
1827 await writeFile ( args . output , output , "utf8" ) ;
You can’t perform that action at this time.
0 commit comments