Skip to content

Commit 73f6bfd

Browse files
authored
Replace Prettier with Biome as the code formatter to improve performance during the code generation phase (#1145)
Signed-off-by: Sora Morimoto <[email protected]>
1 parent 4835fb4 commit 73f6bfd

File tree

36 files changed

+14910
-4063
lines changed

36 files changed

+14910
-4063
lines changed

Diff for: .changeset/twelve-plants-invent.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"swagger-typescript-api": patch
3+
---
4+
5+
Replace Prettier with Biome as the code formatter to improve performance during the code generation phase.

Diff for: README.md

-7
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,6 @@ generateApi({
108108
extractRequestBody: false,
109109
extractEnums: false,
110110
unwrapResponseData: false,
111-
prettier: {
112-
// By default prettier config is load from your project
113-
printWidth: 120,
114-
tabWidth: 2,
115-
trailingComma: "all",
116-
parser: "typescript",
117-
},
118111
defaultResponseType: "void",
119112
singleHttpClient: true,
120113
cleanOutput: false,

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@
4343
"test": "vitest run"
4444
},
4545
"dependencies": {
46+
"@biomejs/js-api": "^0.7.1",
47+
"@biomejs/wasm-nodejs": "^1.9.4",
4648
"@types/swagger-schema-official": "^2.0.25",
4749
"c12": "^3.0.2",
4850
"citty": "^0.1.6",
4951
"consola": "^3.4.2",
50-
"cosmiconfig": "^9.0.0",
5152
"eta": "^2.2.0",
5253
"js-yaml": "^4.1.0",
5354
"lodash": "^4.17.21",
5455
"nanoid": "^5.1.5",
55-
"prettier": "~3.5.3",
5656
"swagger-schema-official": "2.0.0-bab6bed",
5757
"swagger2openapi": "^7.0.8",
5858
"typescript": "~5.8.2"

Diff for: src/code-formatter.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import * as prettier from "prettier";
1+
import * as path from "node:path";
2+
import { Biome, Distribution } from "@biomejs/js-api";
3+
import * as nanoid from "nanoid";
24
import * as typescript from "typescript";
35
import type { CodeGenConfig } from "./configuration.js";
46

@@ -34,23 +36,27 @@ export class CodeFormatter {
3436
return content;
3537
};
3638

37-
prettierFormat = async (content: string) => {
38-
const formatted = await prettier.format(
39-
content,
40-
this.config.prettierOptions,
41-
);
42-
return formatted;
39+
format = async (content: string) => {
40+
const biome = await Biome.create({ distribution: Distribution.NODE });
41+
biome.applyConfiguration({
42+
files: { maxSize: Number.MAX_SAFE_INTEGER },
43+
formatter: { indentStyle: "space" },
44+
});
45+
const formatted = biome.formatContent(content, {
46+
filePath: path.format({ name: nanoid.nanoid(), ext: "ts" }),
47+
});
48+
return formatted.content;
4349
};
4450

4551
formatCode = async (
4652
code: string,
47-
{ removeUnusedImports = true, prettierFormat = true } = {},
53+
{ removeUnusedImports = true, format = true } = {},
4854
) => {
4955
if (removeUnusedImports) {
5056
code = this.removeUnusedImports(code);
5157
}
52-
if (prettierFormat) {
53-
code = await this.prettierFormat(code);
58+
if (format) {
59+
code = await this.format(code);
5460
}
5561
return code;
5662
};

Diff for: src/configuration.ts

-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as cosmiconfig from "cosmiconfig";
21
import lodash from "lodash";
32
import type { OpenAPI } from "openapi-types";
43
import * as typescript from "typescript";
@@ -87,7 +86,6 @@ export class CodeGenConfig {
8786
outOfModuleApi: "Common",
8887
};
8988
routeNameDuplicatesMap = new Map();
90-
prettierOptions = { ...CONSTANTS.PRETTIER_OPTIONS };
9189
hooks: Hooks = {
9290
onPreBuildRoutePath: (_routePath: unknown) => void 0,
9391
onBuildRoutePath: (_routeData: unknown) => void 0,
@@ -390,7 +388,6 @@ export class CodeGenConfig {
390388
templateExtensions = [".eta", ".ejs"];
391389

392390
constructor({
393-
prettierOptions = getDefaultPrettierOptions(),
394391
codeGenConstructs,
395392
primitiveTypeConstructs,
396393
constants,
@@ -405,10 +402,6 @@ export class CodeGenConfig {
405402

406403
this.update({
407404
...otherConfig,
408-
prettierOptions:
409-
prettierOptions === undefined
410-
? getDefaultPrettierOptions()
411-
: prettierOptions,
412405
hooks: lodash.merge(this.hooks, hooks || {}),
413406
constants: {
414407
...CONSTANTS,
@@ -430,20 +423,3 @@ export class CodeGenConfig {
430423
objectAssign(this, update);
431424
};
432425
}
433-
434-
const getDefaultPrettierOptions = () => {
435-
const prettier = cosmiconfig
436-
.cosmiconfigSync("prettier", {
437-
searchStrategy: "global",
438-
})
439-
.search();
440-
441-
if (prettier) {
442-
return {
443-
...prettier.config,
444-
parser: "typescript",
445-
};
446-
}
447-
448-
return { ...CONSTANTS.PRETTIER_OPTIONS };
449-
};

Diff for: src/constants.ts

-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ export const HTTP_CLIENT = {
2121
AXIOS: "axios",
2222
} as const;
2323

24-
export const PRETTIER_OPTIONS = {
25-
printWidth: 120,
26-
tabWidth: 2,
27-
trailingComma: "all",
28-
parser: "typescript",
29-
} as const;
30-
3124
export const PROJECT_VERSION = packageJson.version;
3225

3326
export const RESERVED_BODY_ARG_NAMES = ["data", "body", "reqBody"];

0 commit comments

Comments
 (0)