Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimize process #1

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"glob": "^10.3.10",
"inquirer": "^9.2.12",
"nanospinner": "^1.1.0",
"rslog": "^1.1.0",
"picocolors": "^1.0.0",
"semver": "^7.5.4",
"tsup": "^7.2.0"
}
}
}
13 changes: 3 additions & 10 deletions pnpm-lock.yaml

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

19 changes: 19 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pico from 'picocolors'

export default {
info(text: string) {
console.log(text)
},
success(text: string) {
console.log(pico.green(text))
},
warning(text: string) {
console.log(pico.yellow(text))
},
error(text: string) {
console.log(pico.red(text))
},
title(text: string) {
console.log(pico.cyan(text))
},
}
91 changes: 74 additions & 17 deletions src/release.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSpinner } from 'nanospinner'
import fse from 'fs-extra'
import { execa } from 'execa'
import { logger } from 'rslog'
import { type ExecaReturnBase, execa } from 'execa'
import logger from './logger'
import semver from 'semver'
import { glob } from 'glob'
import inquirer from 'inquirer'
Expand All @@ -12,7 +12,55 @@ const cwd = process.cwd()
const { writeFileSync, readJSONSync } = fse
const { prompt } = inquirer

const releaseTypes = ['premajor', 'preminor', 'prepatch', 'major', 'minor', 'patch']
const releaseTypes = ['premajor', 'preminor', 'prepatch', 'major', 'minor', 'patch'] as const

async function getRemoteVersion() {
const { name: packageName, version } = readJSONSync(resolve(cwd, 'package.json'))

const s = createSpinner('Get Remote version...').start()
const choices = (
(
await Promise.allSettled([
execa('npm', ['view', `${packageName}@latest`, 'version']),
execa('npm', ['view', `${packageName}@alpha`, 'version']),
])
).filter((res) => res.status === 'fulfilled') as PromiseFulfilledResult<ExecaReturnBase<string>>[]
).map((res) => res.value.stdout)
s.stop()

if (choices.length) {
const name = 'Please select the version'
const ret = await prompt([
{
name,
type: 'list',
choices,
},
])

return ret[name] as string
}

const name = 'Get remote version error, if this is the first release, please select'
const ret = await prompt([
{
name,
type: 'list',
choices: [
{
name: 'Use default version 0.0.0',
value: '0.0.0',
},
{
name: 'Use package.json version',
value: version,
},
],
},
])

return ret[name] as string
}

async function isWorktreeEmpty() {
const ret = await execa('git', ['status', '--porcelain'])
Expand Down Expand Up @@ -106,17 +154,33 @@ async function confirmRefs(remote = 'origin') {
return ret[name]
}

async function getReleaseType() {
async function getExpectVersion(currentVersion: string) {
const name = 'Please select release type'
const choices = releaseTypes.map((type) => {
const version = semver.inc(
currentVersion,
type.startsWith('pre') && currentVersion.includes('alpha') ? 'prerelease' : type,
'alpha',
)
return {
name: `${type}(${version})`,
value: version,
}
})
const ret = await prompt([
{
name,
type: 'list',
choices: releaseTypes,
choices,
},
])

return ret[name]
const expectVersion = ret[name] as string

return {
isPreRelease: expectVersion.includes('alpha'),
expectVersion,
}
}

export interface ReleaseCommandOptions {
Expand All @@ -126,13 +190,6 @@ export interface ReleaseCommandOptions {

export async function release(options: ReleaseCommandOptions) {
try {
const currentVersion = readJSONSync(resolve(cwd, 'package.json')).version

if (!currentVersion) {
logger.error('Your package is missing the version field')
return
}

if (!(await isWorktreeEmpty())) {
logger.error('Git worktree is not empty, please commit changed')
return
Expand All @@ -146,10 +203,10 @@ export async function release(options: ReleaseCommandOptions) {
return
}

const type = await getReleaseType()
const isPreRelease = type.startsWith('pre')
let expectVersion = semver.inc(currentVersion, type, `alpha.${Date.now()}`) as string
expectVersion = isPreRelease ? expectVersion.slice(0, -2) : expectVersion
const currentVersion = await getRemoteVersion()
logger.title(`current version: ${currentVersion}`)

const { expectVersion, isPreRelease } = await getExpectVersion(currentVersion)

if (!(await confirmVersion(currentVersion, expectVersion))) {
return
Expand Down