|
| 1 | +/* global __dirname process require */ |
| 2 | + |
| 3 | +const chalk = require('chalk'); |
| 4 | +const {execSync} = require('child_process'); |
| 5 | +const path = require('path'); |
| 6 | +const {readJsonSync} = require('fs-extra'); |
| 7 | + |
| 8 | +const branch = `master`; |
| 9 | +const commitMsg = `chore(prerelease): %v`; |
| 10 | +const distTag = `nightly`; |
| 11 | +const preId = `nightly`; |
| 12 | +const remote = `origin`; |
| 13 | + |
| 14 | +const cyan = (str) => chalk.cyan(str); |
| 15 | +const execSyncInherit = (cmd) => execSync(cmd, {stdio: 'inherit'}); |
| 16 | +const log = (mark, str, which = 'log') => console[which]( |
| 17 | + mark, str.filter(s => !!s).join(` `) |
| 18 | +); |
| 19 | +const logError = (...str) => log(chalk.red(`✘`), str, 'error'); |
| 20 | +const logInfo = (...str) => log(chalk.blue(`ℹ`), str); |
| 21 | +const logSuccess = (...str) => log(chalk.green(`✔`), str); |
| 22 | +const logWarning = (...str) => log(chalk.yellow('‼︎'), str); |
| 23 | + |
| 24 | +const failMsg = `${chalk.red(`NIGHTLY RELEASE FAILED!`)} Stopping right here.`; |
| 25 | + |
| 26 | +const runCommand = (cmd, inherit = true, display) => { |
| 27 | + logInfo(`Running command ${cyan(display || cmd)}.`); |
| 28 | + let out; |
| 29 | + if (inherit) { |
| 30 | + execSyncInherit(cmd); |
| 31 | + } else { |
| 32 | + out = execSync(cmd); |
| 33 | + } |
| 34 | + return out; |
| 35 | +}; |
| 36 | + |
| 37 | +(async () => { |
| 38 | + if (!process.env.GH_TOKEN) { |
| 39 | + logError( |
| 40 | + `Environment variable ${cyan('GH_TOKEN')} was not defined or falsy. It`, |
| 41 | + `must be defined with a properly scoped GitHub personal access token.` |
| 42 | + ); |
| 43 | + logError(failMsg); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | + |
| 47 | + if (!process.env.IS_GITHUB_ACTIONS_WORKFLOW) { |
| 48 | + logError( |
| 49 | + `This script should only be run in the context of a GitHub Actions`, |
| 50 | + `workflow (that workflow should set the environment variable`, |
| 51 | + `${cyan('IS_GITHUB_ACTIONS_WORKFLOW')} to a truthy value).` |
| 52 | + ); |
| 53 | + logError(failMsg); |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | + |
| 57 | + const lernaJsonPath = path.join(__dirname, '../lerna.json'); |
| 58 | + logInfo(`Reading ${cyan(lernaJsonPath)}...`); |
| 59 | + |
| 60 | + let lernaJson, registry; |
| 61 | + try { |
| 62 | + lernaJson = readJsonSync(lernaJsonPath); |
| 63 | + registry = lernaJson.command.publish.registry; |
| 64 | + if (!registry) throw new Error('missing registry in lerna.json'); |
| 65 | + } catch (e) { |
| 66 | + console.error(e.stack || e); |
| 67 | + logError( |
| 68 | + `Could not read values from ${cyan(lernaJsonPath)}. Please check the`, |
| 69 | + `error above.` |
| 70 | + ); |
| 71 | + logError(failMsg); |
| 72 | + process.exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + logInfo(`Determining the current branch...`); |
| 76 | + |
| 77 | + let currentBranch; |
| 78 | + try { |
| 79 | + currentBranch = runCommand(`git rev-parse --abbrev-ref HEAD`, false) |
| 80 | + .toString() |
| 81 | + .trim(); |
| 82 | + } catch (e) { |
| 83 | + logError(`Could not determine the branch. Please check the error above.`); |
| 84 | + logError(failMsg); |
| 85 | + process.exit(1); |
| 86 | + } |
| 87 | + |
| 88 | + if (currentBranch === branch) { |
| 89 | + logSuccess(`Current branch and release branch are the same.`); |
| 90 | + } else { |
| 91 | + logError( |
| 92 | + `Current branch ${cyan(currentBranch)} is not the same as release branch`, |
| 93 | + `${cyan(branch)}.` |
| 94 | + ); |
| 95 | + logError(failMsg); |
| 96 | + process.exit(1); |
| 97 | + } |
| 98 | + |
| 99 | + logInfo( |
| 100 | + `Fetching commits from ${cyan(remote)} to compare local and remote`, |
| 101 | + `branches...` |
| 102 | + ); |
| 103 | + |
| 104 | + try { |
| 105 | + runCommand(`git fetch ${remote}`, false); |
| 106 | + } catch (e) { |
| 107 | + logError(`Could not fetch latest commits. Please check the error above.`); |
| 108 | + logError(failMsg); |
| 109 | + process.exit(1); |
| 110 | + } |
| 111 | + |
| 112 | + let localRef, remoteRef; |
| 113 | + try { |
| 114 | + localRef = runCommand(`git rev-parse ${branch}`, false).toString().trim(); |
| 115 | + remoteRef = runCommand(`git rev-parse ${remote}/${branch}`, false) |
| 116 | + .toString() |
| 117 | + .trim(); |
| 118 | + } catch (e) { |
| 119 | + logError(`A problem occured. Please check the error above.`); |
| 120 | + logError(failMsg); |
| 121 | + process.exit(1); |
| 122 | + } |
| 123 | + |
| 124 | + if (localRef === remoteRef) { |
| 125 | + logSuccess(`Local branch is in sync with remote branch.`); |
| 126 | + } else { |
| 127 | + logError( |
| 128 | + `Local branch ${cyan(branch)} is not in sync with`, |
| 129 | + `${cyan(`${remote}/${branch}`)}.` |
| 130 | + ); |
| 131 | + logError(failMsg); |
| 132 | + process.exit(1); |
| 133 | + } |
| 134 | + |
| 135 | + const lernaPublish = [ |
| 136 | + `lerna`, |
| 137 | + `publish`, |
| 138 | + `--conventional-commits`, |
| 139 | + `--conventional-prerelease`, |
| 140 | + `--create-release github`, |
| 141 | + `--dist-tag ${distTag}`, |
| 142 | + `--git-remote ${remote}`, |
| 143 | + `--message "${commitMsg}"`, |
| 144 | + `--preid ${preId}`, |
| 145 | + `--registry ${registry}`, |
| 146 | + `--yes` |
| 147 | + ].filter(str => !!str).join(` `); |
| 148 | + |
| 149 | + try { |
| 150 | + runCommand(lernaPublish); |
| 151 | + if (localRef === |
| 152 | + runCommand(`git rev-parse ${branch}`, false).toString().trim()) { |
| 153 | + logWarning( |
| 154 | + chalk.yellow(`NIGHTLY RELEASE STOPPED!`), |
| 155 | + `No commit or tag was created. No packages were published. This is`, |
| 156 | + `most likely due to no qualifying changes having been made to the`, |
| 157 | + `${cyan(branch)} branch since the previous release. Please check the`, |
| 158 | + `output above.` |
| 159 | + ); |
| 160 | + process.exit(0); |
| 161 | + } |
| 162 | + } catch (e) { |
| 163 | + logError(`A problem occured. Please check the error above.`); |
| 164 | + const lernaDocsUrl = 'https://github.com/lerna/lerna/tree/master/commands/publish#bump-from-package'; |
| 165 | + logError( |
| 166 | + failMsg, |
| 167 | + `Make sure to clean up commits, tags, and releases as necessary. Check`, |
| 168 | + `the package registry to verify if any packages were published. If so`, |
| 169 | + `they may need to be flagged as deprecated since the`, |
| 170 | + `${cyan('lerna publish')} command exited with error. In some cases it`, |
| 171 | + `may be possible to salvage an imcomplete release by using the`, |
| 172 | + `${cyan('from-package')} keyword with the ${cyan('lerna publish')}`, |
| 173 | + `command. See: ${lernaDocsUrl}` |
| 174 | + ); |
| 175 | + process.exit(1); |
| 176 | + } |
| 177 | + |
| 178 | + logSuccess(`${chalk.green(`NIGHTLY RELEASE SUCCEEDED!`)} Woohoo! Done.`); |
| 179 | +})().then(() => { |
| 180 | + process.exit(0); |
| 181 | +}).catch(e => { |
| 182 | + console.error(e.stack || e); |
| 183 | + logError(`A problem occured. Please check the error above.`); |
| 184 | + logError(failMsg); |
| 185 | + process.exit(1); |
| 186 | +}); |
0 commit comments