Skip to content

Commit

Permalink
feat: support next as release type (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <github@antfu.me>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people authored Mar 4, 2024
1 parent d9660fe commit 81b7e35
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"upgrade": "npm-check -u && npm audit fix",
"bumpp": "esno src/cli/run.ts",
"prepublishOnly": "npm run clean && npm run build",
"release": "npm run bumpp && npm publish"
"release": "npm run bumpp && npm publish",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@jsdevtools/ez-spawn": "^3.0.4",
Expand Down
17 changes: 9 additions & 8 deletions src/get-new-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export async function getNewVersion(operation: Operation): Promise<Operation> {
*/
function getNextVersion(oldVersion: string, bump: BumpRelease): string {
const oldSemVer = new SemVer(oldVersion)
const newSemVer = oldSemVer.inc(bump.type as any, bump.preid)

const type = bump.type === 'next'
? oldSemVer.prerelease.length ? 'prerelease' : 'patch'
: bump.type

const newSemVer = oldSemVer.inc(type, bump.preid)

if (
isPrerelease(bump.type)
Expand All @@ -58,19 +63,15 @@ function getNextVersion(oldVersion: string, bump: BumpRelease): string {
/**
* Returns the next version number for all release types.
*/
function getNextVersions(oldVersion: string, preid: string): Record<ReleaseType | 'next', string> {
function getNextVersions(oldVersion: string, preid: string): Record<ReleaseType, string> {
const next: Record<string, string> = {}

const parse = semver.parse(oldVersion)
if (typeof parse?.prerelease[0] === 'string')
preid = parse?.prerelease[0] || 'preid'

for (const type of releaseTypes)
next[type] = semver.inc(oldVersion, type, preid)!

next.next = parse?.prerelease?.length
? semver.inc(oldVersion, 'prerelease', preid)!
: semver.inc(oldVersion, 'patch')!
next[type] = getNextVersion(oldVersion, { type, preid })

return next
}
Expand Down Expand Up @@ -121,7 +122,7 @@ async function promptForNewVersion(operation: Operation): Promise<Operation> {
},
},
]) as {
release: ReleaseType | 'next' | 'none' | 'custom' | 'config'
release: ReleaseType | 'none' | 'custom' | 'config'
custom?: string
}

Expand Down
7 changes: 2 additions & 5 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ export function isManifest(obj: any): obj is Manifest {
* Determines whether the specified manifest is package-lock.json
*/
export function isPackageLockManifest(
manifest: Manifest
manifest: Manifest,
): manifest is PackageLockManifest {
return (
typeof (manifest as PackageLockManifest).packages?.['']?.version ===
'string'
)
return (typeof (manifest as PackageLockManifest).packages?.['']?.version === 'string')
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function normalizeOptions(raw: VersionBumpOptions): Promise<Normali
if (!raw.release || raw.release === 'prompt')
release = { type: 'prompt', preid }

else if (isReleaseType(raw.release))
else if (isReleaseType(raw.release) || raw.release === 'next')
release = { type: raw.release, preid }

else
Expand Down
6 changes: 3 additions & 3 deletions src/release-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ReleaseType } from 'semver'
import type { ReleaseType as SemverReleaseType } from 'semver'

export type { ReleaseType }
export type ReleaseType = SemverReleaseType | 'next'

/**
* The different types of pre-releases.
Expand All @@ -10,7 +10,7 @@ export const prereleaseTypes: ReleaseType[] = ['premajor', 'preminor', 'prepatch
/**
* All possible release types.
*/
export const releaseTypes: ReleaseType[] = prereleaseTypes.concat(['major', 'minor', 'patch'])
export const releaseTypes: ReleaseType[] = prereleaseTypes.concat(['major', 'minor', 'patch', 'next'])

/**
* Determines whether the specified value is a pre-release.
Expand Down
2 changes: 1 addition & 1 deletion src/types/version-bump-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export interface VersionBumpOptions {
/**
* A callback that is provides information about the progress of the `versionBump()` function.
*/
progress?(progress: VersionBumpProgress): void
progress?: (progress: VersionBumpProgress) => void

/**
* Excute additional command after bumping and before commiting
Expand Down
4 changes: 2 additions & 2 deletions src/update-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ async function updateManifestFile(relPath: string, operation: Operation): Promis

if (isManifest(file.data) && file.data.version !== newVersion) {
file.data.version = newVersion
if (isPackageLockManifest(file.data)) {
if (isPackageLockManifest(file.data))
file.data.packages[''].version = newVersion
}

await writeJsonFile(file)
modified = true
}
Expand Down

0 comments on commit 81b7e35

Please sign in to comment.