Skip to content

Commit

Permalink
refactor: release process for error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
a145789 committed Oct 26, 2024
1 parent daf30da commit 93bd80a
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fse from 'fs-extra'
import logger from './logger'
import semver, { type ReleaseType } from 'semver'
import { confirm, select } from '@inquirer/prompts'
import { x } from 'tinyexec'
import { x as exec } from 'tinyexec'
import { createSpinner } from 'nanospinner'
import { glob } from 'glob'
import { resolve } from 'path'
Expand All @@ -16,7 +16,7 @@ const releaseTypes = ['premajor', 'preminor', 'prepatch', 'major', 'minor', 'pat
const BACK_HINT = 'Back to previous step' as const

async function isWorktreeEmpty() {
const ret = await x('git', ['status', '--porcelain'])
const ret = await exec('git', ['status', '--porcelain'])
return !ret.stdout
}

Expand All @@ -28,7 +28,9 @@ export async function isSameVersion(version?: string) {
if (packageJson) {
const { config } = packageJson
try {
await x('npm', ['view', `${config.name}@${version ?? config.version}`, 'version'])
await exec('npm', ['view', `${config.name}@${version ?? config.version}`, 'version'], {
throwOnError: true,
})

s.warn({
text: `The npm package has a same remote version ${config.version}.`,
Expand Down Expand Up @@ -62,7 +64,7 @@ export async function publish({ preRelease, checkRemoteVersion, npmTag }: Publis
args.push('--tag', npmTag)
}

const ret = await x('pnpm', args)
const ret = await exec('pnpm', args)
if (ret.stderr && ret.stderr.includes('npm ERR!')) {
throw new Error('\n' + ret.stderr)
} else {
Expand All @@ -73,15 +75,25 @@ export async function publish({ preRelease, checkRemoteVersion, npmTag }: Publis

async function pushGit(version: string, remote = 'origin', skipGitTag = false) {
const s = createSpinner('Pushing to remote git repository').start()
await x('git', ['add', '.'])
await x('git', ['commit', '-m', `v${version}`])
await exec('git', ['add', '.'], {
throwOnError: true,
})
await exec('git', ['commit', '-m', `v${version}`], {
throwOnError: true,
})

if (!skipGitTag) {
await x('git', ['tag', `v${version}`])
await x('git', ['push', remote, `v${version}`])
await exec('git', ['tag', `v${version}`], {
throwOnError: true,
})
await exec('git', ['push', remote, `v${version}`], {
throwOnError: true,
})
}

const ret = await x('git', ['push'])
const ret = await exec('git', ['push'], {
throwOnError: true,
})
s.success({ text: 'Push remote repository successfully' })
ret.stdout && logger.info(ret.stdout)
}
Expand Down Expand Up @@ -112,7 +124,7 @@ export function updateVersion(version: string) {
}

async function confirmRegistry() {
const registry = (await x('npm', ['config', 'get', 'registry'])).stdout
const registry = (await exec('npm', ['config', 'get', 'registry'])).stdout
const ret = await confirm({
message: `Current registry is: ${registry}`,
})
Expand All @@ -133,10 +145,10 @@ async function confirmVersion(currentVersion: string, expectVersion: string) {
}

async function confirmRefs(remote = 'origin') {
const { stdout } = await x('git', ['remote', '-v'])
const { stdout } = await exec('git', ['remote', '-v'])
const reg = new RegExp(`${remote}\t(.*) \\(push`)
const repo = stdout.match(reg)?.[1]
const { stdout: branch } = await x('git', ['branch', '--show-current'])
const { stdout: branch } = await exec('git', ['branch', '--show-current'])

const ret = await confirm({
message: `Current refs ${repo}:refs/for/${branch}`,
Expand Down Expand Up @@ -229,13 +241,17 @@ export async function release(options: ReleaseCommandOptions) {

if (isPreRelease) {
try {
await x('git', ['restore', '**/package.json'])
await exec('git', ['restore', '**/package.json'], {
throwOnError: true,
})
} catch {
/* empty */
}

try {
await x('git', ['restore', 'package.json'])
await exec('git', ['restore', 'package.json'], {
throwOnError: true,
})
} catch {
/* empty */
}
Expand Down

0 comments on commit 93bd80a

Please sign in to comment.