Skip to content

Commit

Permalink
sort-package-json
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Mar 3, 2024
1 parent 949f795 commit 45ca8c5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"listr2": "^8.0.2",
"process": "^0.11.10",
"semver": "^7.6.0",
"sort-package-json": "^2.8.0",
"tsx": "^4.7.1",
"turbo": "^1.12.3",
"type-fest": "^4.10.3",
Expand Down
44 changes: 44 additions & 0 deletions pnpm-lock.yaml

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

19 changes: 10 additions & 9 deletions scripts/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {Listr, ListrTaskWrapper} from 'listr2'
import * as fs from 'fs'
import * as path from 'path'
import {ListrEnquirerPromptAdapter} from '@listr2/prompt-adapter-enquirer'
import * as assert from 'assert'
import * as semver from 'semver'
import {inspect} from 'util'
import sortPackageJson from 'sort-package-json'

const main = async () => {
const packageJsonFilepath = (pkg: PkgMeta, type: 'local' | 'registry') =>
Expand Down Expand Up @@ -139,14 +139,17 @@ const main = async () => {
...ctx.packages.map(pkg => pkg.version),
...(ctx.packages.map(pkg => loadRegistryPackageJson(pkg)?.version).filter(Boolean) as string[]),
]
const maxVersion = allVersions.sort(semver.compare).at(-1)
const maxVersion = allVersions.sort(semver.compare).at(-1) || '0.0.0'
if (!maxVersion) throw new Error(`No versions found`)

const releaseTypes: semver.ReleaseType[] = ['patch', 'minor', 'major', 'prepatch', 'preminor', 'premajor']
semver.prerelease(maxVersion) ? releaseTypes.unshift('prerelease') : releaseTypes.push('prerelease')

let bumpedVersion = await task.prompt(ListrEnquirerPromptAdapter).run<string>({
type: 'Select',
message: `Select semver increment or specify new version (current latest is ${maxVersion})`,
choices: [
...(['patch', 'minor', 'major', 'prepatch', 'preminor', 'premajor', 'prerelease'] as const).map(type => {
...releaseTypes.map(type => {
const result = semver.inc(maxVersion, type)!
return {
message: `${type} ${result}`,
Expand All @@ -168,11 +171,6 @@ const main = async () => {
})
}

if (Math.random()) {
console.error({newBumpedVersion: bumpedVersion})
throw new Error('random error')
}

// todo: use enquirer to ask
ctx.versionStrategy = {type: 'fixed', version: bumpedVersion}
task.output = inspect(ctx.versionStrategy)
Expand Down Expand Up @@ -227,7 +225,10 @@ const main = async () => {
}
})

fs.writeFileSync(packageJsonFilepath(pkg, 'local'), JSON.stringify(packageJson, null, 2))
fs.writeFileSync(
packageJsonFilepath(pkg, 'local'),
sortPackageJson(JSON.stringify(packageJson, null, 2)),
)
},
})),
)
Expand Down

0 comments on commit 45ca8c5

Please sign in to comment.