|
1 | 1 | import { join } from "path"; |
2 | | -import { format, resolveConfig, resolveConfigFile } from "prettier"; |
| 2 | +import { format, Options, resolveConfig, resolveConfigFile } from "prettier"; |
3 | 3 | import { |
4 | 4 | Project, |
5 | 5 | SourceFile, |
6 | 6 | SyntaxKind, |
7 | 7 | ObjectLiteralExpression, |
8 | 8 | Node, |
9 | 9 | } from "ts-morph"; |
| 10 | +import { pathExists } from "fs-extra/esm"; |
10 | 11 |
|
11 | 12 | export class InjectConfig { |
12 | 13 | private project: Project; |
@@ -146,13 +147,16 @@ export class InjectConfig { |
146 | 147 | const updatedText = objects.getText().replace(/,\s*,/g, ","); |
147 | 148 | objects.replaceWithText(updatedText); |
148 | 149 |
|
149 | | - const prettierConfig = await resolveConfig(this.projectRoot, { |
150 | | - config: join(this.projectRoot, ".prettierrc"), |
151 | | - }); |
| 150 | + let prettierConfig = null; |
| 151 | + if (await pathExists(join(this.projectRoot, ".prettierrc"))) { |
| 152 | + prettierConfig = await resolveConfig(this.projectRoot, { |
| 153 | + config: join(this.projectRoot, ".prettierrc"), |
| 154 | + }); |
| 155 | + } |
152 | 156 |
|
153 | 157 | const formatted = await format(this.sourceFile.getFullText(), { |
154 | 158 | parser: "typescript", |
155 | | - ...prettierConfig, |
| 159 | + ...(prettierConfig || {}), |
156 | 160 | }); |
157 | 161 |
|
158 | 162 | this.sourceFile.replaceWithText(formatted); |
|
0 commit comments