Skip to content

Commit f5d30ea

Browse files
chore: sort package.json data before printing (#1722)
## PR Checklist - [x] Addresses an existing open issue: fixes #1717 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Adds a dependency on `sort-package-json`, which is already used by `prettier-plugin-packagejson`. 💖
1 parent a4a99cb commit f5d30ea

File tree

5 files changed

+83
-73
lines changed

5 files changed

+83
-73
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"prettier": "^3.4.1",
5757
"replace-in-file": "^8.2.0",
5858
"rimraf": "^6.0.1",
59+
"sort-package-json": "^2.12.0",
5960
"title-case": "^4.3.2",
6061
"zod": "^3.23.8",
6162
"zod-validation-error": "^3.4.0"

pnpm-lock.yaml

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

src/steps/uninstallPackages.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ export async function uninstallPackages(offline: boolean | undefined) {
1414
"get-github-auth-token",
1515
"git-remote-origin-url",
1616
"git-url-parse",
17-
"lazy-value",
1817
"js-yaml",
18+
"lazy-value",
1919
"npm-user",
20-
"parse-author",
21-
"rimraf",
2220
"octokit",
21+
"parse-author",
2322
"prettier",
2423
"replace-in-file",
24+
"rimraf",
25+
"sort-package-json",
2526
"title-case",
2627
"zod",
2728
"zod-validation-error",

src/steps/writing/creation/writePackageJson.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ describe("writePackageJson", () => {
135135
excludeLintYml: true,
136136
excludeReleases: true,
137137
excludeRenovate: true,
138+
excludeTests: true,
138139
});
139140

140141
expect(JSON.parse(packageJson)).toMatchInlineSnapshot(`

src/steps/writing/creation/writePackageJson.ts

Lines changed: 74 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { sortPackageJson } from "sort-package-json";
2+
13
import { readFileSafeAsJson } from "../../../shared/readFileSafeAsJson.js";
24
import { Options, PartialPackageData } from "../../../shared/types.js";
35
import { formatJson } from "./formatters/formatJson.js";
@@ -35,81 +37,83 @@ export async function writePackageJson(options: Options) {
3537
"./package.json",
3638
)) as null | PartialPackageData) ?? {};
3739

38-
return await formatJson({
39-
// If we didn't already have a version, set it to 0.0.0
40-
version: "0.0.0",
40+
return sortPackageJson(
41+
await formatJson({
42+
// If we didn't already have a version, set it to 0.0.0
43+
version: "0.0.0",
4144

42-
// To start, copy over all existing package fields (e.g. "dependencies")
43-
...existingPackageJson,
45+
// To start, copy over all existing package fields (e.g. "dependencies")
46+
...existingPackageJson,
4447

45-
author: { email: options.email.npm, name: options.author },
46-
bin: options.bin,
47-
description: options.description,
48-
keywords: options.keywords?.length
49-
? options.keywords.flatMap((keyword) => keyword.split(/ /))
50-
: undefined,
48+
author: { email: options.email.npm, name: options.author },
49+
bin: options.bin,
50+
description: options.description,
51+
keywords: options.keywords?.length
52+
? options.keywords.flatMap((keyword) => keyword.split(/ /))
53+
: undefined,
5154

52-
// We copy all existing dev dependencies except those we know are not used anymore
53-
devDependencies: copyDevDependencies(existingPackageJson),
55+
// We copy all existing dev dependencies except those we know are not used anymore
56+
devDependencies: copyDevDependencies(existingPackageJson),
5457

55-
// Remove fields we know we don't want, such as old or redundant configs
56-
eslintConfig: undefined,
57-
husky: undefined,
58-
jest: undefined,
59-
mocha: undefined,
60-
prettierConfig: undefined,
61-
types: undefined,
58+
// Remove fields we know we don't want, such as old or redundant configs
59+
eslintConfig: undefined,
60+
husky: undefined,
61+
jest: undefined,
62+
mocha: undefined,
63+
prettierConfig: undefined,
64+
types: undefined,
6265

63-
// The rest of the fields are ones we know from our template
64-
engines: {
65-
node: ">=18.3.0",
66-
},
67-
files: [
68-
options.bin?.replace(/^\.\//, ""),
69-
"lib/",
70-
"package.json",
71-
"LICENSE.md",
72-
"README.md",
73-
].filter(Boolean),
74-
license: "MIT",
75-
"lint-staged": {
76-
"*": "prettier --ignore-unknown --write",
77-
},
78-
main: "./lib/index.js",
79-
name: options.repository,
80-
publishConfig: {
81-
provenance: true,
82-
},
83-
repository: {
84-
type: "git",
85-
url: `https://github.com/${options.owner}/${options.repository}`,
86-
},
87-
scripts: {
88-
...existingPackageJson.scripts,
89-
...(!options.excludeBuild && {
90-
build: "tsup",
91-
}),
92-
format: "prettier .",
93-
lint: "eslint . --max-warnings 0",
94-
...(!options.excludeLintKnip && {
95-
"lint:knip": "knip",
96-
}),
97-
...(!options.excludeLintMd && {
98-
"lint:md":
99-
'markdownlint "**/*.md" ".github/**/*.md" --rules sentences-per-line',
100-
}),
101-
...(!options.excludeLintPackages && {
102-
"lint:packages": "pnpm dedupe --check",
103-
}),
104-
...(!options.excludeLintSpelling && {
105-
"lint:spelling": 'cspell "**" ".github/**/*"',
106-
}),
107-
prepare: "husky",
108-
...(!options.excludeReleases && { test: "vitest" }),
109-
tsc: "tsc",
110-
},
111-
type: "module",
112-
});
66+
// The rest of the fields are ones we know from our template
67+
engines: {
68+
node: ">=18.3.0",
69+
},
70+
files: [
71+
options.bin?.replace(/^\.\//, ""),
72+
"lib/",
73+
"package.json",
74+
"LICENSE.md",
75+
"README.md",
76+
].filter(Boolean),
77+
license: "MIT",
78+
"lint-staged": {
79+
"*": "prettier --ignore-unknown --write",
80+
},
81+
main: "./lib/index.js",
82+
name: options.repository,
83+
publishConfig: {
84+
provenance: true,
85+
},
86+
repository: {
87+
type: "git",
88+
url: `https://github.com/${options.owner}/${options.repository}`,
89+
},
90+
scripts: {
91+
...existingPackageJson.scripts,
92+
...(!options.excludeBuild && {
93+
build: "tsup",
94+
}),
95+
format: "prettier .",
96+
lint: "eslint . --max-warnings 0",
97+
...(!options.excludeLintKnip && {
98+
"lint:knip": "knip",
99+
}),
100+
...(!options.excludeLintMd && {
101+
"lint:md":
102+
'markdownlint "**/*.md" ".github/**/*.md" --rules sentences-per-line',
103+
}),
104+
...(!options.excludeLintPackages && {
105+
"lint:packages": "pnpm dedupe --check",
106+
}),
107+
...(!options.excludeLintSpelling && {
108+
"lint:spelling": 'cspell "**" ".github/**/*"',
109+
}),
110+
prepare: "husky",
111+
...(!options.excludeTests && { test: "vitest" }),
112+
tsc: "tsc",
113+
},
114+
type: "module",
115+
}),
116+
);
113117
}
114118

115119
function copyDevDependencies(existingPackageJson: object) {

0 commit comments

Comments
 (0)