-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathblockPrettier.ts
118 lines (113 loc) · 2.74 KB
/
blockPrettier.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { z } from "zod";
import { formatIgnoreFile } from "../../steps/writing/creation/formatters/formatIgnoreFile.js";
import { base } from "../base.js";
import { blockCSpell } from "./blockCSpell.js";
import { blockDevelopmentDocs } from "./blockDevelopmentDocs.js";
import { blockGitHubActionsCI } from "./blockGitHubActionsCI.js";
import { blockPackageJson } from "./blockPackageJson.js";
import { blockVSCode } from "./blockVSCode.js";
import { getPackageDependencies } from "./packageData.js";
import { CommandPhase } from "./phases.js";
export const blockPrettier = base.createBlock({
about: {
name: "Prettier",
},
addons: {
ignores: z.array(z.string()).default([]),
overrides: z
.array(
z.object({
files: z.string(),
options: z.object({
parser: z.string(),
}),
}),
)
.default([]),
plugins: z.array(z.string()).default([]),
},
migrate() {
return {
scripts: [
{
commands: ["rm .prettierrc* prettier.config*"],
phase: CommandPhase.Migrations,
},
],
};
},
produce({ addons }) {
const { ignores, overrides, plugins } = addons;
return {
addons: [
blockCSpell({
ignores: [".all-contributorsrc"],
}),
blockDevelopmentDocs({
sections: {
Formatting: {
contents: `
[Prettier](https://prettier.io) is used to format code.
It should be applied automatically when you save files in VS Code or make a Git commit.
To manually reformat all files, you can run:
\`\`\`shell
pnpm format --write
\`\`\`
`,
},
},
}),
blockGitHubActionsCI({
jobs: [
{
name: "Prettier",
steps: [{ run: "pnpm format --list-different" }],
},
],
}),
blockPackageJson({
properties: {
devDependencies: getPackageDependencies(
...plugins,
"husky",
"lint-staged",
"prettier",
),
"lint-staged": {
"*": "prettier --ignore-unknown --write",
},
scripts: {
format: "prettier .",
prepare: "husky",
},
},
}),
blockVSCode({
extensions: ["esbenp.prettier-vscode"],
settings: { "editor.defaultFormatter": "esbenp.prettier-vscode" },
}),
],
files: {
".husky": {
".gitignore": "_\n",
"pre-commit": ["npx lint-staged\n", { mode: 33279 }],
},
".prettierignore": formatIgnoreFile(
["/.husky", "/lib", "/pnpm-lock.yaml", ...ignores].sort(),
),
".prettierrc.json": JSON.stringify({
$schema: "http://json.schemastore.org/prettierrc",
...(overrides.length && { overrides: overrides.sort() }),
...(plugins.length && { plugins: plugins.sort() }),
useTabs: true,
}),
},
scripts: [
{
commands: ["pnpm format --write"],
phase: CommandPhase.Format,
},
],
};
},
});