Skip to content

Commit

Permalink
feat(core): support upgrade --next
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 17, 2024
1 parent 970ea6d commit 5fc9660
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
9 changes: 3 additions & 6 deletions packages/core/src/index.ts
Expand Up @@ -65,21 +65,18 @@ export namespace PackageJson {
export type Exports = string | { [key: string]: Exports }
}

// @ts-ignore
export interface Events<C extends Context = Context> extends cordis.Events<C> {}

// @ts-ignore
export interface Context {
[Context.events]: Events
[Context.events]: Events<this>
yakumo: Yakumo
register(name: string, callback: () => void, options?: Options): void
}

// @ts-ignore
export class Context<T = any> extends cordis.Context<T> {
export class Context extends cordis.Context {
constructor(config: any) {
super(config)
this.plugin(Yakumo)
this.plugin(Yakumo, config)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/publish.ts
Expand Up @@ -22,7 +22,7 @@ function getVersion(name: string, isNext = false) {
if (isNext) {
return latest(name, { version: 'next' }).catch(() => getVersion(name))
} else {
return latest(name).catch(() => '0.0.1')
return latest(name, { version: 'latest' }).catch(() => '0.0.1')
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/plugins/upgrade.ts
Expand Up @@ -41,7 +41,7 @@ export default function apply(ctx: Context, config: Config = {}) {
const oldVersion = oldRange.slice(1)
const [newVersion, lastestVersion] = await Promise.all([
latest(dep, { version: oldRange }),
latest(dep),
latest(dep, { version: ctx.yakumo.argv.next ? '' : 'latest' }),
])
updateProgress()
try {
Expand Down Expand Up @@ -89,5 +89,7 @@ export default function apply(ctx: Context, config: Config = {}) {
}
}
}
}, {
boolean: ['next'],
})
}
39 changes: 23 additions & 16 deletions packages/core/src/plugins/version.ts
Expand Up @@ -4,7 +4,7 @@ import { gt, SemVer } from 'semver'
import kleur from 'kleur'
import Yakumo, { confirm, Context, cwd, PackageJson } from '../index.js'

const bumpTypes = ['major', 'minor', 'patch', 'prerelease', 'version', 'reset'] as const
const bumpTypes = ['major', 'minor', 'patch', 'version', 'reset'] as const
type BumpType = typeof bumpTypes[number]

class Package {
Expand All @@ -18,11 +18,27 @@ class Package {
this.version = this.meta.version
}

bump(flag: BumpType, options: any) {
bump(flag: BumpType, options: any, prerelease: boolean) {
if (this.meta.private) return
let version = new SemVer(this.meta.version)
const reset = flag === 'reset'
if (!flag || reset) {
if (prerelease) {
if (version.prerelease.length) {
version.prerelease = [{
alpha: 'beta',
beta: 'rc',
}[version.prerelease[0]]!, 0]
} else {
flag ??= 'major'
if (flag === 'major') {
version = new SemVer(`${version.major + 1}.0.0-alpha.0`)
} else if (flag === 'minor') {
version = new SemVer(`${version.major}.${version.minor + 1}.0-alpha.0`)
} else if (flag === 'patch') {
version = new SemVer(`${version.major}.${version.minor}.${version.patch + 1}-alpha.0`)
}
}
} else if (!flag || reset) {
if (version.prerelease.length) {
const prerelease = version.prerelease.slice() as [string, number]
prerelease[1] += reset ? -1 : 1
Expand All @@ -38,15 +54,6 @@ class Package {
this.dirty = true
this.version = options.version
return options.version
} else if (flag === 'prerelease') {
if (version.prerelease.length) {
version.prerelease = [{
alpha: 'beta',
beta: 'rc',
}[version.prerelease[0]]!, 0]
} else {
version = new SemVer(`${version.major + 1}.0.0-alpha.0`)
}
} else {
if (version.prerelease.length) {
version.prerelease = []
Expand Down Expand Up @@ -88,8 +95,8 @@ class Graph {
return results
}

bump(node: Package, flag: BumpType) {
const version = node.bump(flag, this.project.argv)
bump(node: Package, flag: BumpType, isPre: boolean) {
const version = node.bump(flag, this.project.argv, isPre)
if (!version) return
const dependents = new Set<Package>()
this.each((target) => {
Expand Down Expand Up @@ -121,7 +128,7 @@ class Graph {
}
})
if (!this.project.argv.recursive) return
dependents.forEach(dep => this.bump(dep, flag))
dependents.forEach(dep => this.bump(dep, flag, isPre))
}

async save() {
Expand Down Expand Up @@ -158,7 +165,7 @@ export default function apply(ctx: Context) {
const graph = new Graph(ctx.yakumo)
const paths = ctx.yakumo.locate(ctx.yakumo.argv._)
for (const path of paths) {
graph.bump(graph.nodes[path], flag)
graph.bump(graph.nodes[path], flag, ctx.yakumo.argv.prerelease)
}

await graph.save()
Expand Down

0 comments on commit 5fc9660

Please sign in to comment.