Skip to content

Commit 97d945f

Browse files
Argeentojs2me
authored andcommitted
Loads prettier config from user's project (#286)
1 parent be7dacf commit 97d945f

File tree

5 files changed

+64
-64
lines changed

5 files changed

+64
-64
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ generateApi({
100100
toJS: false,
101101
extractRequestParams: false,
102102
extractRequestBody: false,
103-
prettier: {
103+
prettier: { // By default prettier config is load from your project
104104
printWidth: 120,
105105
tabWidth: 2,
106106
trailingComma: "all",

package-lock.json

Lines changed: 37 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@types/swagger-schema-official": "2.0.21",
5959
"axios": "^0.21.1",
6060
"commander": "^6.2.1",
61+
"cosmiconfig": "^7.0.0",
6162
"eta": "^1.12.1",
6263
"js-yaml": "^4.0.0",
6364
"lodash": "^4.17.21",

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const { generateOutputFiles } = require("./output");
2121
const formatFileContent = require("./formatFileContent");
2222
const { logger } = require("./logger");
2323
const { ComponentTypeNameResolver } = require("./utils/resolveName");
24+
const { getPrettierOptions } = require("./prettierOptions");
2425

2526
module.exports = {
2627
constants: constants,
@@ -46,7 +47,7 @@ module.exports = {
4647
defaultResponseType = config.defaultResponseType,
4748
unwrapResponseData = config.unwrapResponseData,
4849
singleHttpClient = config.singleHttpClient,
49-
prettier: prettierOptions = constants.PRETTIER_OPTIONS,
50+
prettier: prettierOptions = getPrettierOptions(),
5051
hooks: rawHooks,
5152
extraTemplates,
5253
enumNamesAsValues,

src/prettierOptions.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { cosmiconfigSync } = require("cosmiconfig");
2+
const constants = require("./constants");
3+
4+
/**
5+
* Get prettier options from user's project or return the default one
6+
* @return {import('prettier').Options} Prettier options
7+
*/
8+
function getPrettierOptions() {
9+
const prettier = cosmiconfigSync("prettier").search();
10+
11+
if (prettier) {
12+
return {
13+
...prettier.config,
14+
parser: "typescript",
15+
};
16+
}
17+
18+
return constants.PRETTIER_OPTIONS;
19+
}
20+
21+
module.exports = {
22+
getPrettierOptions,
23+
};

0 commit comments

Comments
 (0)