|
| 1 | +const path = require('path'); |
| 2 | + |
| 3 | +const semver = require('semver'); |
| 4 | + |
| 5 | +const { |
| 6 | + askForConfirmation, |
| 7 | + askForText, |
| 8 | + checkGitStatus, |
| 9 | + checkNpmAuth, |
| 10 | + runExec, |
| 11 | + revertCommit, |
| 12 | +} = require('./release.helpers'); |
| 13 | + |
| 14 | +const packageJsonPath = path.resolve(__dirname, '../next/package.json'); |
| 15 | +const packageJson = require(packageJsonPath); |
| 16 | + |
| 17 | +async function checkGitBranchAndStatus() { |
| 18 | + console.log('Checking your branch...'); |
| 19 | + const resultBranch = await runExec('git branch --show-current', { |
| 20 | + silent: true, |
| 21 | + }); |
| 22 | + const branchName = resultBranch.stdout.toString().trim(); |
| 23 | + if (branchName === 'main') { |
| 24 | + console.error(`🟠 You are at "main". Are you sure you wanna release a next version here?`); |
| 25 | + process.exit(1); |
| 26 | + } |
| 27 | + |
| 28 | + await checkGitStatus(); |
| 29 | +} |
| 30 | + |
| 31 | +async function getNewVersion() { |
| 32 | + const currentVersion = packageJson.version; |
| 33 | + const versionType = process.argv.slice(2)[0]; |
| 34 | + |
| 35 | + if (!versionType) { |
| 36 | + console.log('🟠 version type is missing. Make sure to run the script from package.json'); |
| 37 | + process.exit(1); |
| 38 | + } |
| 39 | + |
| 40 | + // Keep alpha tag for next versions |
| 41 | + const versionBase = semver.coerce(currentVersion); |
| 42 | + return semver.inc(versionBase, versionType) + '-alpha.0'; |
| 43 | +} |
| 44 | + |
| 45 | +async function bumpVersion({ newVersion }) { |
| 46 | + const cmd = `cd next && npm version --no-git-tag-version ${newVersion}`; |
| 47 | + await runExec(cmd); |
| 48 | +} |
| 49 | + |
| 50 | +async function build() { |
| 51 | + console.log('Building next version...'); |
| 52 | + const cmd = 'cd next && npm run build'; |
| 53 | + await runExec(cmd); |
| 54 | +} |
| 55 | + |
| 56 | +async function gitCommit({ newVersion }) { |
| 57 | + console.log('Committing published version...'); |
| 58 | + const cmd = `git add next/package.json && git commit -m "Release next ${newVersion}" && git tag next-v${newVersion} && git push && git push origin --tags`; |
| 59 | + await runExec(cmd); |
| 60 | +} |
| 61 | + |
| 62 | +async function publish({ newVersion, otp }) { |
| 63 | + console.log('Publishing new version...'); |
| 64 | + |
| 65 | + // Use --tag=next to mark this as the "next" version |
| 66 | + const cmd = `cd next && npm publish --access=public --tag=next --otp=${otp}`; |
| 67 | + try { |
| 68 | + await runExec(cmd); |
| 69 | + console.log(`🎉 Next version ${newVersion} published!`); |
| 70 | + } catch { |
| 71 | + console.log('🚨 Publish failed! Perhaps the OTP is wrong.'); |
| 72 | + await revertCommit({ newVersion }); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function init() { |
| 77 | + await checkGitBranchAndStatus(); |
| 78 | + const newVersion = await getNewVersion(); |
| 79 | + |
| 80 | + console.log(':: Current version:', packageJson.version); |
| 81 | + console.log(':::::: New version:', newVersion); |
| 82 | + |
| 83 | + const answer = await askForConfirmation('Ready to commit and publish it?'); |
| 84 | + |
| 85 | + if (answer === 'no') { |
| 86 | + process.exit(1); |
| 87 | + } |
| 88 | + |
| 89 | + await checkNpmAuth(); |
| 90 | + const otp = await askForText('🔐 What is the NPM Auth OTP? (Check 1PW) '); |
| 91 | + |
| 92 | + await bumpVersion({ newVersion }); |
| 93 | + await build(); |
| 94 | + await gitCommit({ newVersion }); |
| 95 | + await publish({ newVersion, otp }); |
| 96 | +} |
| 97 | + |
| 98 | +init(); |
0 commit comments