Skip to content

Commit

Permalink
refactor(release): canary version structure
Browse files Browse the repository at this point in the history
  • Loading branch information
chnliquan committed Nov 4, 2024
1 parent 171f5d3 commit 272c1a5
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Preid } from '@/types'
import { chalk, logger, pascalCase, prompts, type PkgJSON } from '@eljs/utils'
import semver, { RELEASE_TYPES, type ReleaseType } from 'semver'
import {
getCanaryVersion,
getReferenceVersion,
getReleaseVersion,
getRemoteDistTag,
isVersionExist,
} from './version'
} from '@/utils'
import { chalk, logger, pascalCase, prompts, type PkgJSON } from '@eljs/utils'
import semver, { RELEASE_TYPES, type ReleaseType } from 'semver'

function getPreVersionPromptQuestions(
referenceVersion: string,
Expand Down Expand Up @@ -35,13 +35,16 @@ function getPreVersionPromptQuestions(

export async function getBumpVersion(opts: {
cwd: string
canary: boolean
pkgJSON: Required<PkgJSON>
publishPkgNames: string[]
preid?: Preid
releaseTypeOrVersion?: ReleaseType | string
}): Promise<string> {
const { cwd, pkgJSON, publishPkgNames, preid, releaseTypeOrVersion } = opts
const { cwd, canary, pkgJSON, publishPkgNames, preid, releaseTypeOrVersion } =
opts

// #region 用户传入版本
if (
releaseTypeOrVersion &&
!RELEASE_TYPES.includes(releaseTypeOrVersion as ReleaseType)
Expand Down Expand Up @@ -69,58 +72,73 @@ export async function getBumpVersion(opts: {
}

const localVersion = pkgJSON.version
const { latest, alpha, beta, rc } = await getRemoteDistTag(
publishPkgNames,
cwd,
)
const {
latest: remoteLatestVersion,
alpha: remoteAlphaVersion,
beta: remoteBetaVersion,
rc: remoteRcVersion,
} = await getRemoteDistTag(publishPkgNames, cwd)

const referenceVersionMap = {
latest: getReferenceVersion(localVersion, latest),
alpha: getReferenceVersion(localVersion, alpha || latest),
beta: getReferenceVersion(localVersion, beta || latest),
rc: getReferenceVersion(localVersion, rc || latest),
latest: getReferenceVersion(localVersion, remoteLatestVersion, 'latest'),
alpha: getReferenceVersion(
localVersion,
remoteAlphaVersion || remoteLatestVersion,
'alpha',
),
beta: getReferenceVersion(
localVersion,
remoteBetaVersion || remoteLatestVersion,
'beta',
),
rc: getReferenceVersion(
localVersion,
remoteRcVersion || remoteLatestVersion,
'rc',
),
}

if (RELEASE_TYPES.includes(releaseTypeOrVersion as ReleaseType)) {
return getReleaseVersion({
releaseType: releaseTypeOrVersion as ReleaseType,
referenceVersion: preid
? referenceVersionMap[preid as keyof typeof referenceVersionMap]
? referenceVersionMap[preid]
: referenceVersionMap.latest,
preid,
})
}
// #endregion

if (preid === 'canary') {
if (canary) {
return getCanaryVersion(referenceVersionMap.latest, cwd)
} else {
logger.info(`- Local version: ${chalk.cyanBright.bold(localVersion)}`)

if (referenceVersionMap.latest) {
if (remoteLatestVersion) {
logger.info(
`- Remote latest version: ${chalk.cyanBright.bold(
referenceVersionMap.latest,
)}`,
)
}

if (referenceVersionMap.alpha && (!preid || preid === 'alpha')) {
if (remoteAlphaVersion && (!preid || preid === 'alpha')) {
logger.info(
`- Remote alpha version: ${chalk.cyanBright.bold(
referenceVersionMap.alpha,
)}`,
)
}

if (referenceVersionMap.beta && (!preid || preid === 'beta')) {
if (remoteBetaVersion && (!preid || preid === 'beta')) {
logger.info(
`- Remote beta version: ${chalk.cyanBright.bold(
referenceVersionMap.beta,
)}`,
)
}

if (referenceVersionMap.rc && (!preid || preid === 'rc')) {
if (remoteRcVersion && (!preid || preid === 'rc')) {
logger.info(
`- Remote rc version: ${chalk.cyanBright.bold(referenceVersionMap.rc)}`,
)
Expand Down Expand Up @@ -178,7 +196,12 @@ export async function getBumpVersion(opts: {
{
title: `Canary`,
value: 'canary',
description: chalk.grey(`Canary Version`),
description: chalk.grey(`Canary Deployment Version`),
},
{
title: `Custom`,
value: 'custom',
description: chalk.grey(`Custom version`),
},
]

Expand All @@ -190,9 +213,9 @@ export async function getBumpVersion(opts: {
process.exit(1)
}

let resolvedPreid: Preid | undefined = preid
let releaseType = ''

if (!resolvedPreid) {
if (!preid) {
answer = await prompts(
[
{
Expand All @@ -207,21 +230,35 @@ export async function getBumpVersion(opts: {
},
)

resolvedPreid = answer.value
releaseType = answer.value

if (resolvedPreid === 'canary') {
if (releaseType === 'canary') {
return getCanaryVersion(referenceVersionMap.latest, cwd)
}

if (!['alpha', 'beta', 'rc'].includes(resolvedPreid as Preid)) {
if (releaseType === 'custom') {
answer = await prompts({
name: 'value',
type: 'text',
message: 'Input custom version:',
initial: pkgJSON.version,
})

if (!semver.valid(answer.value)) {
logger.printErrorAndExit(
`Invalid semantic version ${chalk.bold(answer.value)}.`,
)
}
}

if (!['alpha', 'beta', 'rc'].includes(releaseType as Preid)) {
return answer.value
}
}

const referenceVersion =
referenceVersionMap[answer.value as keyof typeof referenceVersionMap]
const referenceVersion = referenceVersionMap[answer.value as Preid]
answer = await prompts(
getPreVersionPromptQuestions(referenceVersion, resolvedPreid as Preid),
getPreVersionPromptQuestions(referenceVersion, releaseType as Preid),
{
onCancel,
},
Expand Down
7 changes: 5 additions & 2 deletions packages/release/src/core/reconfirm.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Preid } from '@/types'
import { getBumpVersion } from '@/utils'
import { chalk, confirm, type PkgJSON } from '@eljs/utils'
import { getBumpVersion } from './bump'

export interface ReconfirmOpts {
cwd: string
canary: boolean
bumpVersion: string
publishPkgNames: string[]
pkgJSON: Required<PkgJSON>
Expand All @@ -12,7 +13,8 @@ export interface ReconfirmOpts {
}

export async function reconfirm(opts: ReconfirmOpts): Promise<string> {
const { cwd, bumpVersion, publishPkgNames, pkgJSON, preid, verbose } = opts
const { cwd, preid, canary, bumpVersion, publishPkgNames, pkgJSON, verbose } =
opts
let confirmMessage = ''

if (publishPkgNames.length === 1 || !verbose) {
Expand All @@ -34,6 +36,7 @@ export async function reconfirm(opts: ReconfirmOpts): Promise<string> {
} else {
const version = await getBumpVersion({
cwd,
canary,
pkgJSON,
publishPkgNames,
preid,
Expand Down
9 changes: 6 additions & 3 deletions packages/release/src/core/release.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Options } from '@/types'
import { step } from '@/utils'
import {
chalk,
isGitBehindRemote,
Expand All @@ -6,9 +8,7 @@ import {
logger,
} from '@eljs/utils'
import path from 'path'

import type { Options } from '../types'
import { getBumpVersion, step } from '../utils'
import { getBumpVersion } from './bump'
import { generateChangelog } from './changelog'
import { commit } from './commit'
import { init } from './init'
Expand All @@ -35,6 +35,7 @@ export async function release(opts: Options): Promise<void> {
const {
cwd = process.cwd(),
preid,
canary = false,
independent = false,
dry = false,
verbose = false,
Expand Down Expand Up @@ -137,6 +138,7 @@ export async function release(opts: Options): Promise<void> {
bumpVersion = await getBumpVersion({
cwd,
preid,
canary,
pkgJSON: rootPkgJSON,
publishPkgNames,
releaseTypeOrVersion: version,
Expand All @@ -146,6 +148,7 @@ export async function release(opts: Options): Promise<void> {
bumpVersion = await reconfirm({
cwd,
preid,
canary,
bumpVersion,
publishPkgNames,
pkgJSON: rootPkgJSON,
Expand Down
22 changes: 17 additions & 5 deletions packages/release/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/**
* 预发布 ID
* 预发布版本
*/
export type Preid = 'alpha' | 'beta' | 'rc' | 'canary'
export type Preid = 'alpha' | 'beta' | 'rc'

/**
* NPM dist tag
*/
export type DistTag = Preid | 'latest'

/**
* 仓库类型
Expand All @@ -14,12 +19,19 @@ export type RepoType = 'github' | 'gitlab'
export interface Options {
/**
* 工作目录
* @default process.cwd()
*/
cwd?: string
/**
* 预发布 ID
* 预发布版本
* @default undefined
*/
preid?: Preid
/**
* 是否发布金丝雀版本
* @default false
*/
canary?: boolean
/**
* 是否生成独立的 git tag
* @default false
Expand Down Expand Up @@ -51,8 +63,8 @@ export interface Options {
*/
syncCnpm?: boolean
/**
* 是否需要再次确认
* @default false
* 是否需要确认发布内容
* @default true
*/
confirm?: boolean
/**
Expand Down
1 change: 0 additions & 1 deletion packages/release/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './prompt'
export * from './step'
export * from './version'
Loading

0 comments on commit 272c1a5

Please # to comment.