Skip to content

Commit 1e24536

Browse files
committedJun 25, 2023
feat: extended prettier-config, with overrides for .eslintrc, .prettierrc, .stylelintrc, package*.json, *.yml,*.yaml
Signed-off-by: prisis <d.bannert@anolilab.de>
1 parent 5f867d7 commit 1e24536

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed
 

‎packages/prettier-config/src/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Options } from "prettier";
1+
import type { Config } from "prettier";
22

3-
const config: Options = {
3+
const config: Config = {
44
// max 160 characters per line
55
printWidth: 160,
66
// use 4 spaces for indentation
@@ -38,6 +38,14 @@ const config: Options = {
3838
endOfLine: "lf",
3939
// formats quoted code embedded
4040
embeddedLanguageFormatting: "auto",
41+
overrides: [
42+
{ files: ".eslintrc", options: { parser: "json" } },
43+
{ files: ".prettierrc", options: { parser: "json" } },
44+
{ files: ".stylelintrc", options: { parser: "json" } },
45+
{ files: "package*.json", options: { printWidth: 1000 } },
46+
{ files: "*.yml", options: { singleQuote: false } },
47+
{ files: "*.yaml", options: { singleQuote: false } },
48+
],
4149
};
4250

4351
export default config;

‎packages/prettier-config/src/postinstall.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,43 @@ const writeFileAsync = promisify(writeFile);
1414

1515
console.log("Configuring @anolilab/prettier-config", projectPath, "\n");
1616

17+
const configFile = ".prettierrc";
18+
1719
/**
1820
* Writes .prettierrc.${m|c}js if it doesn't exist. Warns if it exists.
1921
*/
2022
const writePrettierRc = () => {
21-
const prettierPath = join(projectPath, ".prettierrc.js");
22-
23-
if (
24-
existsSync(prettierPath)
25-
|| existsSync(prettierPath.replace(".js", ""))
26-
|| existsSync(prettierPath.replace(".js", `.${packageIsTypeModule ? "m" : "c"}js`))
27-
) {
28-
console.warn(`⚠️ .prettierrc.{m|c}js already exists;
23+
// eslint-disable-next-line no-restricted-syntax
24+
for (const filename of [
25+
configFile,
26+
`${configFile}.js`,
27+
`${configFile}.cjs`,
28+
`${configFile}.json`,
29+
`${configFile}.json5`,
30+
`${configFile}.yaml`,
31+
`${configFile}.yml`,
32+
`${configFile}.toml`,
33+
"prettier.config.js",
34+
"prettier.config.cjs",
35+
]) {
36+
if (existsSync(join(projectPath, filename))) {
37+
console.warn(`⚠️ ${filename} already exists;
2938
Make sure that it includes the following for @anolilab/prettier-config to work as it should:
3039
${JSON.stringify(content, undefined, 4)}\n`);
3140

32-
return Promise.resolve();
41+
return Promise.resolve();
42+
}
3343
}
3444

45+
const prettierPath = join(projectPath, ".prettierrc.js");
46+
3547
return writeFileAsync(
3648
prettierPath,
3749
`${packageIsTypeModule ? "export default" : "module.exports ="} ${JSON.stringify(content, undefined, 2).replace(
3850
"rangeEnd: null,",
3951
"rangeEnd: Number.POSITIVE_INFINITY,",
4052
)}\n`,
41-
"utf-8",
53+
"utf8",
4254
);
4355
};
4456

@@ -54,7 +66,11 @@ const writePrettierIgnore = () => {
5466
return Promise.resolve();
5567
}
5668

57-
return writeFileAsync(prettierPath, "", "utf-8");
69+
return writeFileAsync(
70+
prettierPath,
71+
`${["*.md", "*.sh", "*.yml", "*.svg", "*.gif", "*.log", ".DS_Store", "CNAME", "AUTHORS", "LICENSE", "es/", "lib/", "dist/", "coverage/"].join("\n")}\n`,
72+
"utf8",
73+
);
5874
};
5975

6076
// eslint-disable-next-line unicorn/prefer-top-level-await

0 commit comments

Comments
 (0)
Please sign in to comment.