|
| 1 | +#!/usr/bin/env zx |
| 2 | + |
| 3 | +import fs from 'node:fs/promises'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { $, chalk } from 'zx'; |
| 6 | + |
| 7 | +// Exit when error |
| 8 | +$.verbose = false; |
| 9 | + |
| 10 | +const args = process.argv.slice(2); |
| 11 | +const bumpTypeArgs = args.find((arg) => arg.startsWith('--type=')); |
| 12 | + |
| 13 | +async function getCurrentVersion() { |
| 14 | + const packageJsonPath = path.join( |
| 15 | + process.cwd(), |
| 16 | + 'packages/core/package.json', |
| 17 | + ); |
| 18 | + const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf-8')); |
| 19 | + return packageJson.version; |
| 20 | +} |
| 21 | + |
| 22 | +async function getNextVersion(currentVersion, type) { |
| 23 | + const [major, minor, patch] = currentVersion.split('.').map(Number); |
| 24 | + switch (type) { |
| 25 | + case 'patch': |
| 26 | + return `${major}.${minor}.${patch + 1}`; |
| 27 | + case 'minor': |
| 28 | + return `${major}.${minor + 1}.0`; |
| 29 | + case 'major': |
| 30 | + return `${major + 1}.0.0`; |
| 31 | + default: |
| 32 | + throw new Error('Invalid version type'); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +async function generateChangesetFile(bumpType, nextVersion) { |
| 37 | + const changesetDir = path.join(process.cwd(), '.changeset'); |
| 38 | + const timestamp = Date.now(); |
| 39 | + const filename = `${timestamp}-${bumpType}-release.md`; |
| 40 | + const content = `--- |
| 41 | +"@rslib/core": ${bumpType} |
| 42 | +--- |
| 43 | +
|
| 44 | +Release version ${nextVersion} |
| 45 | +`; |
| 46 | + |
| 47 | + await fs.mkdir(changesetDir, { recursive: true }); |
| 48 | + await fs.writeFile(path.join(changesetDir, filename), content); |
| 49 | + |
| 50 | + console.log(chalk.blue(`Generated changeset file: ${filename}`)); |
| 51 | +} |
| 52 | + |
| 53 | +async function main() { |
| 54 | + try { |
| 55 | + // 1. Read the current version |
| 56 | + const currentVersion = await getCurrentVersion(); |
| 57 | + console.log(chalk.blue(`Current version: ${currentVersion}`)); |
| 58 | + |
| 59 | + // 2. Determine bump type |
| 60 | + const bumpType = bumpTypeArgs ? bumpTypeArgs.split('=')[1] : 'patch'; |
| 61 | + |
| 62 | + if (!['major', 'minor', 'patch'].includes(bumpType)) { |
| 63 | + console.error('Invalid bump type. Please select major, minor, or patch.'); |
| 64 | + process.exit(1); |
| 65 | + } |
| 66 | + |
| 67 | + const nextVersion = await getNextVersion(currentVersion, bumpType); |
| 68 | + const branchName = `release-v${nextVersion}`; |
| 69 | + |
| 70 | + console.log(chalk.blue(`Creating branch: ${branchName}`)); |
| 71 | + |
| 72 | + // 3. Create and switch to new branch |
| 73 | + await $`git checkout -b ${branchName}`; |
| 74 | + |
| 75 | + // 4. Generate changeset file |
| 76 | + await generateChangesetFile(bumpType, nextVersion); |
| 77 | + |
| 78 | + // 5. Run changeset version and pnpm install |
| 79 | + await $`pnpm run changeset version`; |
| 80 | + await $`pnpm install --ignore-scripts`; |
| 81 | + |
| 82 | + // 6. Commit changes |
| 83 | + await $`git add .`; |
| 84 | + await $`git commit -m "Release v${nextVersion}"`; |
| 85 | + |
| 86 | + // 7. Push to remote repo |
| 87 | + await $`git push -u origin ${branchName}`; |
| 88 | + |
| 89 | + console.log(chalk.green(`Successfully created and pushed ${branchName}`)); |
| 90 | + } catch (error) { |
| 91 | + console.error(chalk.red(`Error: ${error.message}`)); |
| 92 | + process.exit(1); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +main(); |
0 commit comments