Skip to content

Commit

Permalink
chore: sort package.json data before printing (#1722)
Browse files Browse the repository at this point in the history
## 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`.

💖
  • Loading branch information
JoshuaKGoldberg authored Dec 3, 2024
1 parent a4a99cb commit f5d30ea
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 73 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"prettier": "^3.4.1",
"replace-in-file": "^8.2.0",
"rimraf": "^6.0.1",
"sort-package-json": "^2.12.0",
"title-case": "^4.3.2",
"zod": "^3.23.8",
"zod-validation-error": "^3.4.0"
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/steps/uninstallPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ export async function uninstallPackages(offline: boolean | undefined) {
"get-github-auth-token",
"git-remote-origin-url",
"git-url-parse",
"lazy-value",
"js-yaml",
"lazy-value",
"npm-user",
"parse-author",
"rimraf",
"octokit",
"parse-author",
"prettier",
"replace-in-file",
"rimraf",
"sort-package-json",
"title-case",
"zod",
"zod-validation-error",
Expand Down
1 change: 1 addition & 0 deletions src/steps/writing/creation/writePackageJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ describe("writePackageJson", () => {
excludeLintYml: true,
excludeReleases: true,
excludeRenovate: true,
excludeTests: true,
});

expect(JSON.parse(packageJson)).toMatchInlineSnapshot(`
Expand Down
144 changes: 74 additions & 70 deletions src/steps/writing/creation/writePackageJson.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { sortPackageJson } from "sort-package-json";

import { readFileSafeAsJson } from "../../../shared/readFileSafeAsJson.js";
import { Options, PartialPackageData } from "../../../shared/types.js";
import { formatJson } from "./formatters/formatJson.js";
Expand Down Expand Up @@ -35,81 +37,83 @@ export async function writePackageJson(options: Options) {
"./package.json",
)) as null | PartialPackageData) ?? {};

return await formatJson({
// If we didn't already have a version, set it to 0.0.0
version: "0.0.0",
return sortPackageJson(
await formatJson({
// If we didn't already have a version, set it to 0.0.0
version: "0.0.0",

// To start, copy over all existing package fields (e.g. "dependencies")
...existingPackageJson,
// To start, copy over all existing package fields (e.g. "dependencies")
...existingPackageJson,

author: { email: options.email.npm, name: options.author },
bin: options.bin,
description: options.description,
keywords: options.keywords?.length
? options.keywords.flatMap((keyword) => keyword.split(/ /))
: undefined,
author: { email: options.email.npm, name: options.author },
bin: options.bin,
description: options.description,
keywords: options.keywords?.length
? options.keywords.flatMap((keyword) => keyword.split(/ /))
: undefined,

// We copy all existing dev dependencies except those we know are not used anymore
devDependencies: copyDevDependencies(existingPackageJson),
// We copy all existing dev dependencies except those we know are not used anymore
devDependencies: copyDevDependencies(existingPackageJson),

// Remove fields we know we don't want, such as old or redundant configs
eslintConfig: undefined,
husky: undefined,
jest: undefined,
mocha: undefined,
prettierConfig: undefined,
types: undefined,
// Remove fields we know we don't want, such as old or redundant configs
eslintConfig: undefined,
husky: undefined,
jest: undefined,
mocha: undefined,
prettierConfig: undefined,
types: undefined,

// The rest of the fields are ones we know from our template
engines: {
node: ">=18.3.0",
},
files: [
options.bin?.replace(/^\.\//, ""),
"lib/",
"package.json",
"LICENSE.md",
"README.md",
].filter(Boolean),
license: "MIT",
"lint-staged": {
"*": "prettier --ignore-unknown --write",
},
main: "./lib/index.js",
name: options.repository,
publishConfig: {
provenance: true,
},
repository: {
type: "git",
url: `https://github.com/${options.owner}/${options.repository}`,
},
scripts: {
...existingPackageJson.scripts,
...(!options.excludeBuild && {
build: "tsup",
}),
format: "prettier .",
lint: "eslint . --max-warnings 0",
...(!options.excludeLintKnip && {
"lint:knip": "knip",
}),
...(!options.excludeLintMd && {
"lint:md":
'markdownlint "**/*.md" ".github/**/*.md" --rules sentences-per-line',
}),
...(!options.excludeLintPackages && {
"lint:packages": "pnpm dedupe --check",
}),
...(!options.excludeLintSpelling && {
"lint:spelling": 'cspell "**" ".github/**/*"',
}),
prepare: "husky",
...(!options.excludeReleases && { test: "vitest" }),
tsc: "tsc",
},
type: "module",
});
// The rest of the fields are ones we know from our template
engines: {
node: ">=18.3.0",
},
files: [
options.bin?.replace(/^\.\//, ""),
"lib/",
"package.json",
"LICENSE.md",
"README.md",
].filter(Boolean),
license: "MIT",
"lint-staged": {
"*": "prettier --ignore-unknown --write",
},
main: "./lib/index.js",
name: options.repository,
publishConfig: {
provenance: true,
},
repository: {
type: "git",
url: `https://github.com/${options.owner}/${options.repository}`,
},
scripts: {
...existingPackageJson.scripts,
...(!options.excludeBuild && {
build: "tsup",
}),
format: "prettier .",
lint: "eslint . --max-warnings 0",
...(!options.excludeLintKnip && {
"lint:knip": "knip",
}),
...(!options.excludeLintMd && {
"lint:md":
'markdownlint "**/*.md" ".github/**/*.md" --rules sentences-per-line',
}),
...(!options.excludeLintPackages && {
"lint:packages": "pnpm dedupe --check",
}),
...(!options.excludeLintSpelling && {
"lint:spelling": 'cspell "**" ".github/**/*"',
}),
prepare: "husky",
...(!options.excludeTests && { test: "vitest" }),
tsc: "tsc",
},
type: "module",
}),
);
}

function copyDevDependencies(existingPackageJson: object) {
Expand Down

0 comments on commit f5d30ea

Please sign in to comment.