Skip to content

Commit

Permalink
feat(core): support version -P, --stable
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 23, 2024
1 parent 280f967 commit dc2c94f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
19 changes: 11 additions & 8 deletions packages/core/src/plugins/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { writeFile } from 'fs/promises'
import { readFileSync } from 'fs'
import { gt, SemVer } from 'semver'
import kleur from 'kleur'
import Yakumo, { confirm, Context, cwd, PackageJson } from '../index.js'
import Yakumo, { Arguments, confirm, Context, cwd, PackageJson } from '../index.js'

const bumpTypes = ['major', 'minor', 'patch', 'version', 'reset'] as const
type BumpType = typeof bumpTypes[number]
Expand All @@ -18,11 +18,13 @@ class Package {
this.version = this.meta.version
}

bump(flag: BumpType, options: any, prerelease: boolean) {
bump(flag: BumpType, options: any, args: Arguments) {
if (this.meta.private) return
let version = new SemVer(this.meta.version)
const reset = flag === 'reset'
if (prerelease) {
if (args.stable) {
version.prerelease = []
} else if (args.prerelease) {
if (version.prerelease.length) {
version.prerelease = [{
alpha: 'beta',
Expand Down Expand Up @@ -95,8 +97,8 @@ class Graph {
return results
}

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

async save() {
Expand Down Expand Up @@ -165,7 +167,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, ctx.yakumo.argv.prerelease)
graph.bump(graph.nodes[path], flag, ctx.yakumo.argv)
}

await graph.save()
Expand All @@ -176,9 +178,10 @@ export default function apply(ctx: Context) {
patch: ['3'],
reset: ['0'],
prerelease: ['p'],
stable: ['P'],
version: ['v'],
recursive: ['r'],
},
boolean: ['major', 'minor', 'patch', 'reset', 'prerelease', 'recursive'],
boolean: ['major', 'minor', 'patch', 'reset', 'prerelease', 'stable', 'recursive'],
})
}
8 changes: 4 additions & 4 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ let registryTask: Promise<string>

export namespace latest {
export interface Options {
version?: string
version: string
}
}

export async function latest(name: string, options: latest.Options = {}) {
export async function latest(name: string, options: latest.Options) {
const registry = await (registryTask ||= getRegistry())
const packageUrl = new URL(encodeURIComponent(name).replace(/^%40/, '@'), registry)
const response = await fetch(packageUrl, {
headers: {
'Accept': 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*',
},
})
const { version = 'latest' } = options
const { version } = options
const data = await response.json()
if (data['dist-tags'][version]) {
return data['dist-tags'][version]
} else if (data.versions[version]) {
return version
} else {
const versions = Object.keys(data.versions)
return semver.maxSatisfying(versions, version)
return semver.maxSatisfying(versions, version, { includePrerelease: true })
}
}

0 comments on commit dc2c94f

Please sign in to comment.