Skip to content

Commit 84a4a1b

Browse files
Add a new config option to opt-out from auto-formatting using Prettier (#1453)
* feat: opt out of prettier * style: prettier * fix: typecheck * fix: update calling code with defaults * tweaks * add changeset * Apply suggestions from code review * fmt * fix tests --------- Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
1 parent 01c037c commit 84a4a1b

File tree

12 files changed

+168
-18
lines changed

12 files changed

+168
-18
lines changed

‎.changeset/thick-countries-kiss.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@changesets/types": minor
3+
"@changesets/config": minor
4+
"@changesets/cli": minor
5+
---
6+
7+
Added a new config option to opt-out from formatting with Prettier using `prettier: false`.

‎.changeset/thick-countries-lick.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/write": minor
3+
---
4+
5+
Added a new option to opt-out from formatting with Prettier using `prettier: false`.

‎packages/apply-release-plan/src/index.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class FakeReleasePlan {
5151
baseBranch: "main",
5252
updateInternalDependencies: "patch",
5353
ignore: [],
54+
prettier: true,
5455
privatePackages: { version: true, tag: false },
5556
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
5657
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -94,6 +95,7 @@ async function testSetup(
9495
baseBranch: "main",
9596
updateInternalDependencies: "patch",
9697
ignore: [],
98+
prettier: true,
9799
privatePackages: { version: true, tag: false },
98100
snapshot: {
99101
useCalculatedVersion: false,
@@ -665,6 +667,7 @@ describe("apply release plan", () => {
665667
baseBranch: "main",
666668
changedFilePatterns: ["**"],
667669
updateInternalDependencies: "patch",
670+
prettier: true,
668671
privatePackages: { version: true, tag: false },
669672
ignore: [],
670673
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
@@ -741,6 +744,7 @@ describe("apply release plan", () => {
741744
baseBranch: "main",
742745
changedFilePatterns: ["**"],
743746
updateInternalDependencies: "patch",
747+
prettier: true,
744748
privatePackages: { version: true, tag: false },
745749
ignore: [],
746750
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
@@ -1000,6 +1004,7 @@ describe("apply release plan", () => {
10001004
baseBranch: "main",
10011005
updateInternalDependencies,
10021006
ignore: [],
1007+
prettier: true,
10031008
privatePackages: { version: true, tag: false },
10041009
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
10051010
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -1116,6 +1121,7 @@ describe("apply release plan", () => {
11161121
baseBranch: "main",
11171122
updateInternalDependencies,
11181123
ignore: [],
1124+
prettier: true,
11191125
privatePackages: { version: true, tag: false },
11201126
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
11211127
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -1217,6 +1223,7 @@ describe("apply release plan", () => {
12171223
baseBranch: "main",
12181224
updateInternalDependencies,
12191225
ignore: [],
1226+
prettier: true,
12201227
privatePackages: { version: true, tag: false },
12211228
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
12221229
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -1317,6 +1324,7 @@ describe("apply release plan", () => {
13171324
baseBranch: "main",
13181325
updateInternalDependencies,
13191326
ignore: [],
1327+
prettier: true,
13201328
privatePackages: { version: true, tag: false },
13211329
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
13221330
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -1417,6 +1425,7 @@ describe("apply release plan", () => {
14171425
baseBranch: "main",
14181426
updateInternalDependencies,
14191427
ignore: [],
1428+
prettier: true,
14201429
privatePackages: { version: true, tag: false },
14211430
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
14221431
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -1520,6 +1529,7 @@ describe("apply release plan", () => {
15201529
baseBranch: "main",
15211530
updateInternalDependencies,
15221531
ignore: [],
1532+
prettier: true,
15231533
privatePackages: { version: true, tag: false },
15241534
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
15251535
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -1636,6 +1646,7 @@ describe("apply release plan", () => {
16361646
baseBranch: "main",
16371647
updateInternalDependencies,
16381648
ignore: [],
1649+
prettier: true,
16391650
privatePackages: { version: true, tag: false },
16401651
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
16411652
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -1745,6 +1756,7 @@ describe("apply release plan", () => {
17451756
baseBranch: "main",
17461757
updateInternalDependencies,
17471758
ignore: [],
1759+
prettier: true,
17481760
privatePackages: { version: true, tag: false },
17491761
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
17501762
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -1845,6 +1857,7 @@ describe("apply release plan", () => {
18451857
baseBranch: "main",
18461858
updateInternalDependencies,
18471859
ignore: [],
1860+
prettier: true,
18481861
privatePackages: { version: true, tag: false },
18491862
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
18501863
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -1946,6 +1959,7 @@ describe("apply release plan", () => {
19461959
baseBranch: "main",
19471960
updateInternalDependencies: "patch",
19481961
ignore: [],
1962+
prettier: true,
19491963
privatePackages: { version: true, tag: false },
19501964
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
19511965
onlyUpdatePeerDependentsWhenOutOfRange: true,
@@ -2178,6 +2192,7 @@ describe("apply release plan", () => {
21782192
],
21792193
updateInternalDependencies: "patch",
21802194
ignore: [],
2195+
prettier: true,
21812196
privatePackages: { version: true, tag: false },
21822197
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
21832198
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -2316,6 +2331,7 @@ describe("apply release plan", () => {
23162331
baseBranch: "main",
23172332
updateInternalDependencies: "patch",
23182333
ignore: [],
2334+
prettier: true,
23192335
privatePackages: { version: true, tag: false },
23202336
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
23212337
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -2425,6 +2441,7 @@ describe("apply release plan", () => {
24252441
baseBranch: "main",
24262442
updateInternalDependencies: "minor",
24272443
ignore: [],
2444+
prettier: true,
24282445
privatePackages: { version: true, tag: false },
24292446
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
24302447
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -2546,6 +2563,7 @@ describe("apply release plan", () => {
25462563
baseBranch: "main",
25472564
updateInternalDependencies: "minor",
25482565
ignore: [],
2566+
prettier: true,
25492567
privatePackages: { version: true, tag: false },
25502568
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
25512569
onlyUpdatePeerDependentsWhenOutOfRange: false,
@@ -2681,6 +2699,7 @@ describe("apply release plan", () => {
26812699
baseBranch: "main",
26822700
updateInternalDependencies: "minor",
26832701
ignore: [],
2702+
prettier: true,
26842703
privatePackages: { version: true, tag: false },
26852704
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
26862705
onlyUpdatePeerDependentsWhenOutOfRange: false,

‎packages/apply-release-plan/src/index.ts‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ export default async function applyReleasePlan(
132132
});
133133
});
134134

135-
let prettierInstance = getPrettierInstance(cwd);
135+
let prettierInstance =
136+
config.prettier !== false ? getPrettierInstance(cwd) : undefined;
136137

137138
for (let release of finalisedRelease) {
138139
let { changelog, packageJson, dir, name } = release;
@@ -276,7 +277,7 @@ async function updateChangelog(
276277
changelogPath: string,
277278
changelog: string,
278279
name: string,
279-
prettierInstance: typeof prettier
280+
prettierInstance: typeof prettier | undefined
280281
) {
281282
let templateString = `\n\n${changelog.trim()}\n`;
282283

@@ -311,7 +312,7 @@ async function prependFile(
311312
filePath: string,
312313
data: string,
313314
name: string,
314-
prettierInstance: typeof prettier
315+
prettierInstance: typeof prettier | undefined
315316
) {
316317
const fileData = fs.readFileSync(filePath).toString();
317318
// if the file exists but doesn't have the header, we'll add it in
@@ -332,15 +333,17 @@ async function prependFile(
332333
async function writeFormattedMarkdownFile(
333334
filePath: string,
334335
content: string,
335-
prettierInstance: typeof prettier
336+
prettierInstance: typeof prettier | undefined
336337
) {
337338
await fs.writeFile(
338339
filePath,
339-
// Prettier v3 returns a promise
340-
await prettierInstance.format(content, {
341-
...(await prettierInstance.resolveConfig(filePath)),
342-
filepath: filePath,
343-
parser: "markdown",
344-
})
340+
prettierInstance
341+
? // Prettier v3 returns a promise
342+
await prettierInstance.format(content, {
343+
...(await prettierInstance.resolveConfig(filePath)),
344+
filepath: filePath,
345+
parser: "markdown",
346+
})
347+
: content
345348
);
346349
}

‎packages/cli/src/commands/add/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default async function add(
8585
}
8686

8787
if (newChangeset.confirmed) {
88-
const changesetID = await writeChangeset(newChangeset, cwd);
88+
const changesetID = await writeChangeset(newChangeset, cwd, config);
8989
const [{ getAddMessage }, commitOpts] = getCommitFunctions(
9090
config.commit,
9191
cwd

‎packages/cli/src/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type CliOptions = {
88
ignore?: string | string[];
99
snapshot?: string | boolean;
1010
snapshotPrereleaseTemplate?: string;
11+
prettier?: boolean;
1112
tag?: string;
1213
gitTag?: boolean;
1314
open?: boolean;

‎packages/config/schema.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@
7575
"description": "Determines whether Changesets should commit the results of the add and version command.",
7676
"default": false
7777
},
78+
"prettier": {
79+
"type": "boolean",
80+
"description": "When false, Changesets won't format with Prettier",
81+
"default": true
82+
},
7883
"privatePackages": {
7984
"anyOf": [
8085
{

‎packages/config/src/index.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ test("read reads the config", async () => {
5050
updateInternalDependencies: "patch",
5151
ignore: [],
5252
bumpVersionsWithWorkspaceProtocolOnly: false,
53+
prettier: true,
5354
privatePackages: {
5455
tag: false,
5556
version: true,
@@ -89,6 +90,7 @@ test("read can read config based on the passed in `cwd`", async () => {
8990
updateInternalDependencies: "patch",
9091
ignore: [],
9192
bumpVersionsWithWorkspaceProtocolOnly: false,
93+
prettier: true,
9294
privatePackages: {
9395
tag: false,
9496
version: true,
@@ -146,6 +148,7 @@ let defaults: Config = {
146148
changedFilePatterns: ["**"],
147149
updateInternalDependencies: "patch",
148150
ignore: [],
151+
prettier: true,
149152
privatePackages: { version: true, tag: false },
150153
___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH: {
151154
onlyUpdatePeerDependentsWhenOutOfRange: false,

‎packages/config/src/index.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,16 @@ export let parse = (json: WrittenConfig, packages: Packages): Config => {
331331
}
332332
}
333333

334+
if (json.prettier !== undefined) {
335+
messages.push(
336+
`The \`prettier\` option is set as ${JSON.stringify(
337+
json.prettier,
338+
null,
339+
2
340+
)} when the only valid values are undefined or a boolean`
341+
);
342+
}
343+
334344
const { snapshot } = json;
335345

336346
if (snapshot !== undefined) {
@@ -480,6 +490,8 @@ export let parse = (json: WrittenConfig, packages: Packages): Config => {
480490
?.updateInternalDependents ?? "out-of-range",
481491
},
482492

493+
prettier: typeof json.prettier === "boolean" ? json.prettier : true,
494+
483495
// TODO consider enabling this by default in the next major version
484496
privatePackages:
485497
json.privatePackages === false

‎packages/types/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export type Config = {
7474
access: AccessType;
7575
baseBranch: string;
7676
changedFilePatterns: readonly string[];
77+
/** When false, Changesets won't format with Prettier */
78+
prettier: boolean;
7779
/** Features enabled for Private packages */
7880
privatePackages: PrivatePackages;
7981
/** The minimum bump type to trigger automatic update of internal dependencies that are part of the same release */
@@ -99,6 +101,7 @@ export type WrittenConfig = {
99101
access?: AccessType;
100102
baseBranch?: string;
101103
changedFilePatterns?: readonly string[];
104+
prettier?: boolean;
102105
/** Opt in to tracking non-npm / private packages */
103106
privatePackages?:
104107
| false

0 commit comments

Comments
 (0)