Skip to content

Commit ba23003

Browse files
committed
Add fileExtOverrides option handling for Prettier
1 parent 5ff25db commit ba23003

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

prettier/format/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { workerMain } from "@better-rules-javascript/bazel-worker";
22
import { ArgumentParser } from "argparse";
3-
import { dirname } from "node:path";
3+
import * as path from "node:path";
44
import { pathToFileURL } from "node:url";
55
import { Options } from "prettier";
66
import { load, resolve } from "./import";
7+
import { readFile, writeFile } from "node:fs/promises";
78

89
interface 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 {

prettier/format/src/worker.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { readFile, writeFile } from "node:fs/promises";
33
import { Options, format } from "prettier";
44

55
export 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");

0 commit comments

Comments
 (0)