feat(cli): sort package.json

This commit is contained in:
pengzhanbo 2025-10-16 00:31:52 +08:00
parent ca84eaeb13
commit d30325dd96
4 changed files with 54 additions and 4 deletions

View File

@ -36,7 +36,8 @@
"handlebars": "catalog:prod",
"nano-spawn": "catalog:prod",
"os-locale": "catalog:prod",
"picocolors": "catalog:prod"
"picocolors": "catalog:prod",
"sort-package-json": "catalog:prod"
},
"plume-deps": {
"vuepress": "2.0.0-rc.24",

View File

@ -1,9 +1,16 @@
import type { File, ResolvedData } from './types.js'
import { kebabCase } from '@pengzhanbo/utils'
import spawn from 'nano-spawn'
import _sortPackageJson from 'sort-package-json'
import { Mode } from './constants.js'
import { readJsonFile, resolve } from './utils/index.js'
function sortPackageJson(json: Record<any, any>) {
return _sortPackageJson(json, {
sortOrder: ['name', 'type', 'version', 'private', 'description', 'packageManager', 'author', 'license', 'scripts', 'devDependencies', 'dependencies', 'pnpm'],
})
}
export async function createPackageJson(
mode: Mode,
pkg: Record<string, any>,
@ -26,8 +33,9 @@ export async function createPackageJson(
if (packageManager !== 'npm') {
let version = await getPackageManagerVersion(packageManager)
if (version) {
if (packageManager === 'yarn' && version.startsWith('1'))
version = '4.6.0'
if (packageManager === 'yarn' && version.startsWith('1')) {
version = '4.10.3'
}
pkg.packageManager = `${packageManager}@${version}`
// pnpm@10 should add `onlyBuiltDependencies`
@ -84,7 +92,7 @@ export async function createPackageJson(
return {
filepath: 'package.json',
content: JSON.stringify(pkg, null, 2),
content: JSON.stringify(sortPackageJson(pkg), null, 2),
}
}

40
pnpm-lock.yaml generated
View File

@ -517,6 +517,9 @@ importers:
picocolors:
specifier: catalog:prod
version: 1.1.1
sort-package-json:
specifier: ^3.4.0
version: 3.4.0
docs:
dependencies:
@ -3964,11 +3967,19 @@ packages:
resolution: {integrity: sha512-reYkTUJAZb9gUuZ2RvVCNhVHdg62RHnJ7WJl8ftMi4diZ6NWlciOzQN88pUhSELEwflJht4oQDv0F0BMlwaYtA==}
engines: {node: '>=8'}
detect-indent@7.0.2:
resolution: {integrity: sha512-y+8xyqdGLL+6sh0tVeHcfP/QDd8gUgbasolJJpY7NgeQGSZ739bDtSiaiDgtoicy+mtYB81dKLxO9xRhCyIB3A==}
engines: {node: '>=12.20'}
detect-libc@1.0.3:
resolution: {integrity: sha512-pGjwhsmsp4kL2RTz08wcOlGN83otlqHeD/Z5T8GXZB+/YcpQ/dgo+lbU8ZsGxV0HIvqqxo9l7mqYwyYMD9bKDg==}
engines: {node: '>=0.10'}
hasBin: true
detect-newline@4.0.1:
resolution: {integrity: sha512-qE3Veg1YXzGHQhlA6jzebZN2qVf6NX+A7m7qlhCGG30dJixrAQhYOsJjsnBjJkCSmuOPpCk30145fr8FV0bzog==}
engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}
devlop@1.1.0:
resolution: {integrity: sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA==}
@ -4588,6 +4599,9 @@ packages:
giscus@1.6.0:
resolution: {integrity: sha512-Zrsi8r4t1LVW950keaWcsURuZUQwUaMKjvJgTCY125vkW6OiEBkatE7ScJDbpqKHdZwb///7FVC21SE3iFK3PQ==}
git-hooks-list@4.1.1:
resolution: {integrity: sha512-cmP497iLq54AZnv4YRAEMnEyQ1eIn4tGKbmswqwmFV4GBnAqE8NLtWxxdXa++AalfgL5EBH4IxTPyquEuGY/jA==}
git-raw-commits@4.0.0:
resolution: {integrity: sha512-ICsMM1Wk8xSGMowkOmPrzo2Fgmfo4bMHLNX6ytHjajRJUqvHOw/TFapQ+QG75c3X/tTDDhOSRPGC52dDbNM8FQ==}
engines: {node: '>=16'}
@ -6584,6 +6598,14 @@ packages:
resolution: {integrity: sha512-bSiSngZ/jWeX93BqeIAbImyTbEihizcwNjFoRUIY/T1wWQsfsm2Vw1agPKylXvQTU7iASGdHhyqRlqQzfz+Htg==}
engines: {node: '>=18'}
sort-object-keys@1.1.3:
resolution: {integrity: sha512-855pvK+VkU7PaKYPc+Jjnmt4EzejQHyhhF33q31qG8x7maDzkeFhAAThdCYay11CISO+qAMwjOBP+fPZe0IPyg==}
sort-package-json@3.4.0:
resolution: {integrity: sha512-97oFRRMM2/Js4oEA9LJhjyMlde+2ewpZQf53pgue27UkbEXfHJnDzHlUxQ/DWUkzqmp7DFwJp8D+wi/TYeQhpA==}
engines: {node: '>=20'}
hasBin: true
source-map-js@1.2.1:
resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==}
engines: {node: '>=0.10.0'}
@ -10953,9 +10975,13 @@ snapshots:
detect-indent@6.1.0: {}
detect-indent@7.0.2: {}
detect-libc@1.0.3:
optional: true
detect-newline@4.0.1: {}
devlop@1.1.0:
dependencies:
dequal: 2.0.3
@ -11731,6 +11757,8 @@ snapshots:
dependencies:
lit: 3.3.1
git-hooks-list@4.1.1: {}
git-raw-commits@4.0.0:
dependencies:
dargs: 8.1.0
@ -14053,6 +14081,18 @@ snapshots:
ansi-styles: 6.2.1
is-fullwidth-code-point: 5.0.0
sort-object-keys@1.1.3: {}
sort-package-json@3.4.0:
dependencies:
detect-indent: 7.0.2
detect-newline: 4.0.1
git-hooks-list: 4.1.1
is-plain-obj: 4.1.0
semver: 7.7.2
sort-object-keys: 1.1.3
tinyglobby: 0.2.15
source-map-js@1.2.1: {}
source-map@0.6.1: {}

View File

@ -113,6 +113,7 @@ catalogs:
picocolors: ^1.1.1
picomatch: ^4.0.3
shiki: ^3.13.0
sort-package-json: ^3.4.0
tinyglobby: ^0.2.15
tm-grammars: ^1.24.17
tm-themes: ^1.10.9