|
| 1 | +import { sortPackageJson } from "sort-package-json"; |
| 2 | + |
1 | 3 | import { readFileSafeAsJson } from "../../../shared/readFileSafeAsJson.js";
|
2 | 4 | import { Options, PartialPackageData } from "../../../shared/types.js";
|
3 | 5 | import { formatJson } from "./formatters/formatJson.js";
|
@@ -35,81 +37,83 @@ export async function writePackageJson(options: Options) {
|
35 | 37 | "./package.json",
|
36 | 38 | )) as null | PartialPackageData) ?? {};
|
37 | 39 |
|
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", |
41 | 44 |
|
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, |
44 | 47 |
|
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, |
51 | 54 |
|
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), |
54 | 57 |
|
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, |
62 | 65 |
|
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 | + ); |
113 | 117 | }
|
114 | 118 |
|
115 | 119 | function copyDevDependencies(existingPackageJson: object) {
|
|
0 commit comments