Skip to content

Commit

Permalink
build: transfer to use scripts package
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhc committed Oct 31, 2023
1 parent ab28733 commit c834f61
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 318 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@vexip-ui/commitlint-config": "^0.3.0",
"@vexip-ui/eslint-config": "^0.9.0",
"@vexip-ui/icons": "^1.2.0",
"@vexip-ui/scripts": "^1.0.0",
"conventional-changelog-angular": "^7.0.0",
"conventional-changelog-cli": "^4.1.0",
"eslint": "^8.52.0",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

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

4 changes: 4 additions & 0 deletions scripts/constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

export const rootDir = resolve(fileURLToPath(import.meta.url), '../..')
50 changes: 7 additions & 43 deletions scripts/publish.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { resolve } from 'node:path'
import { readFile } from 'node:fs/promises'

import minimist from 'minimist'
import { logger, rootDir, run } from './utils'
import { logger, publish } from '@vexip-ui/scripts'
import { rootDir } from './constant'

const args = minimist<{
d?: boolean,
Expand All @@ -14,47 +14,11 @@ const args = minimist<{
const isDryRun = args.dry || args.d
const releaseTag = args.tag || args.t

async function main() {
const pkg = JSON.parse(await readFile(resolve(rootDir, 'package.json'), 'utf-8'))
const currentVersion: string = pkg.version

logger.withStartLn(() => logger.infoText('Publishing package...'))

const publishArgs = [
'publish',
'--access',
'public',
'--registry',
'https://registry.npmjs.org/',
'--no-git-checks'
]

if (isDryRun) {
publishArgs.push('--dry-run')
}

if (releaseTag) {
publishArgs.push('--tag', releaseTag)
} else if (currentVersion.includes('-')) {
const [, preversion] = currentVersion.split('-')
const tag = preversion && preversion.split('.')[0]

tag && publishArgs.push('--tag', tag)
}

try {
await run('pnpm', publishArgs, { stdio: 'pipe', cwd: rootDir })
logger.successText(`Successfully published v${currentVersion}'`)
} catch (error) {
if (error.stderr?.match(/previously published/)) {
logger.errorText(`Skipping already published v'${currentVersion}'`)
} else {
throw error
}
}
}

main().catch(error => {
publish({
pkgDir: resolve(rootDir, 'package.json'),
isDryRun,
releaseTag
}).catch(error => {
logger.error(error)
process.exit(1)
})
143 changes: 15 additions & 128 deletions scripts/release.ts
Original file line number Diff line number Diff line change
@@ -1,142 +1,29 @@
import { join, resolve } from 'node:path'
import { readFile, writeFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { resolve } from 'node:path'

import minimist from 'minimist'
import semver from 'semver'
import prompts from 'prompts'
import { dryRun, logger, run } from './utils'
import { logger, release, run } from '@vexip-ui/scripts'
import { rootDir } from './constant'

const args = minimist<{
d?: boolean,
dry?: boolean,
t?: string,
tag?: string,
p: string,
preid?: string
}>(process.argv.slice(2))

const isDryRun = args.dry || args.d

const rootDir = resolve(fileURLToPath(import.meta.url), '../..')

const runIfNotDry = isDryRun ? dryRun : run
const logStep = (msg: string) => {
logger.ln()
logger.infoText(msg)
}
const logSkipped = (msg = 'Skipped') => {
logger.warningText(`(${msg})`)
}

main()

async function main() {
const pkg = JSON.parse(
await readFile(join(rootDir, 'package.json'), 'utf-8')
)
const currentVersion = pkg.version

const preId =
args.preid ||
args.p ||
semver.prerelease(currentVersion)?.[0]

const versionIncrements = [
'patch',
'minor',
'major',
...(preId ? ['prepatch', 'preminor', 'premajor', 'prerelease'] : [])
]

const inc = (i: any) => semver.inc(currentVersion, i, preId as string)

const { release } = await prompts({
type: 'select',
name: 'release',
message: 'Select release type:',
choices: versionIncrements
.map(i => `${i} (${inc(i)})`)
.concat(['custom'])
.map(i => ({ title: i, value: i }))
})

const version =
release === 'custom'
? (await prompts({
type: 'text',
name: 'version',
message: 'Input custom version:'
})).version
: release.match(/\((.*)\)/)?.[1]

if (!semver.valid(version)) {
throw new Error(`Invalid target version: ${version}`)
}

const { confirm } = await prompts({
type: 'confirm',
name: 'confirm',
message: `Confirm release ${version}?`
})

if (!confirm) return

// 执行单元测试
logStep('Running test...')

if (!isDryRun) {
await run('pnpm', ['test'])
} else {
logSkipped()
}

logStep('Updating version...')

pkg.version = version
await writeFile(resolve(rootDir, 'package.json'), JSON.stringify(pkg, null, 2) + '\n')

// 构建库
logStep('Building package...')

if (!isDryRun) {
release({
pkgDir: resolve(rootDir, 'package.json'),
isDryRun,
preId: args.preid,
runTest: () => run('pnpm', ['test']),
runBuild: async () => {
await run('pnpm', ['dev:prepare'])
await run('pnpm', ['build'])
} else {
logSkipped()
}

// 更新 Change Log
logStep('Updating changelog...')

await run('pnpm', ['changelog'])

// 提交改动
logStep('Comitting changes...')

const { stdout } = await run('git', ['diff'], { stdio: 'pipe' })

if (stdout) {
await runIfNotDry('git', ['add', '-A'])
await runIfNotDry('git', ['commit', '-m', `release: v${version}`])
} else {
logSkipped('No changes to commit')
}

// 推送到远程仓库
logStep('Pushing to Remote Repository...')

await runIfNotDry('git', ['tag', `v${version}`])
await runIfNotDry('git', ['push', 'origin', `refs/tags/v${version}`])
await runIfNotDry('git', ['push'])

logger.ln()

if (isDryRun) {
logger.success('Dry run finished - run git diff to see package changes')
} else {
logger.success('Release successfully')
}

logger.ln()
}
},
runChangelog: () => run('pnpm', ['changelog'])
}).catch(error => {
logger.error(error)
process.exit(1)
})
Loading

0 comments on commit c834f61

Please sign in to comment.