Skip to content

Commit 979c24d

Browse files
committed
Use prettier's resolveConfig to get widget prettier config
1 parent ce004d6 commit 979c24d

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

packages/pluggable-widgets-tools/src/typings-generator/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parseStringPromise } from "xml2js";
44
import { PackageXml } from "./PackageXml";
55
import { WidgetXml } from "./WidgetXml";
66
import { generateForWidget } from "./generate";
7-
import { formatTypescript } from "../utils/formatting";
7+
import { formatTypeScript } from "../utils/formatting";
88

99
const { mkdir, readFile, stat, writeFile } = promises;
1010

@@ -40,7 +40,7 @@ export async function transformPackage(content: string, basePath: string) {
4040
);
4141
}
4242

43-
const formattedContent = await formatTypescript(generatedContent);
43+
const formattedContent = await formatTypeScript(generatedContent);
4444
const resultPath = sourcePath.replace(/(\.xml)?$/, "Props.d.ts");
4545
await writeFile(join(resultBasePath, resultPath), formattedContent);
4646
}
Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
import { readFileSync, existsSync } from "fs";
1+
import { readFile } from "fs/promises";
22
import { join } from "path";
3+
import { resolveConfig, format, Options } from "prettier";
34
import { cwd } from "process";
4-
import { format as prettierFormat, Options } from "prettier";
55

6-
const prettierConfigWidgetPath = join(cwd(), "prettier.config.js");
76
const prettierConfigBasePath = join(__dirname, "../../configs/prettier.base.json");
87

9-
let prettierConfigBase: object;
10-
if (existsSync(prettierConfigWidgetPath)) {
11-
prettierConfigBase = require(prettierConfigWidgetPath);
12-
} else {
13-
prettierConfigBase = JSON.parse(readFileSync(prettierConfigBasePath, "utf-8"));
14-
}
8+
let prettierTypescriptConfig: Options | undefined;
9+
10+
/**
11+
* Uses prettier to format the given TypeScript sourcecode.
12+
* @param source The TypeScript snippet that needs to be formatted
13+
*/
14+
export async function formatTypeScript(source: string): Promise<string> {
15+
if (prettierTypescriptConfig === undefined) {
16+
const fakeFilename = join(cwd(), "./src/widget.ts");
17+
// If the widget does not have a prettier config, fall back to packaged base config
18+
const prettierConfig =
19+
(await resolveConfig(fakeFilename)) ?? JSON.parse(await readFile(prettierConfigBasePath, "utf-8"));
1520

16-
const prettierConfig: Options = {
17-
...prettierConfigBase,
18-
parser: "babel-ts"
19-
};
21+
prettierTypescriptConfig = {
22+
...prettierConfig,
23+
parser: "babel-ts"
24+
};
25+
}
2026

21-
export function formatTypescript(source: string): Promise<string> {
22-
return prettierFormat(source, prettierConfig);
27+
return await format(source, prettierTypescriptConfig);
2328
}

0 commit comments

Comments
 (0)