|
| 1 | +const path = require('path'); |
| 2 | +const { EOL } = require('os'); |
| 3 | +const { existsSync } = require('fs'); |
| 4 | +const { spawn } = require('child_process'); |
| 5 | + |
| 6 | +const workspace = process.env.GITHUB_WORKSPACE; |
| 7 | +const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'; |
| 8 | + |
| 9 | +(async () => { |
| 10 | + const event = process.env.GITHUB_EVENT_PATH ? require(process.env.GITHUB_EVENT_PATH) : {}; |
| 11 | + |
| 12 | + console.log('release event action:', event.action); |
| 13 | + |
| 14 | + if (event.action !== 'published' || !event.release) { |
| 15 | + exitSuccess('This action is only run when a release is published.'); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + console.log('release tag name:', event.release.tag_name); |
| 20 | + |
| 21 | + if (!event.release.tag_name) { |
| 22 | + exitSuccess('This action is only run when a release is published with a tag name.'); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const releaseTargetBranch = event.release.target_commitish; |
| 27 | + |
| 28 | + console.log('release target branch:', releaseTargetBranch); |
| 29 | + |
| 30 | + if (releaseTargetBranch !== 'main') { |
| 31 | + exitSuccess('This action is only run when a release is published from the main branch.'); |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const releaseVersion = event.release.tag_name.startsWith('v') |
| 36 | + ? event.release.tag_name.slice(1) |
| 37 | + : event.release.tag_name; |
| 38 | + |
| 39 | + console.log('release version:', releaseVersion); |
| 40 | + |
| 41 | + const pkg = getPackageJson(); |
| 42 | + const packageVersion = pkg.version.toString(); |
| 43 | + |
| 44 | + console.log('package.json version:', packageVersion); |
| 45 | + |
| 46 | + if (packageVersion !== releaseVersion) { |
| 47 | + exitFailure('The version in package.json and the release tag version do not match.'); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + console.log('publishing package...'); |
| 53 | + |
| 54 | + await runInWorkspace(npmCommand, ['publish']); |
| 55 | + |
| 56 | + exitSuccess(`Package version v${releaseVersion} published!`); |
| 57 | + } catch (e) { |
| 58 | + logError(e); |
| 59 | + exitFailure('Failed to publish package'); |
| 60 | + return; |
| 61 | + } |
| 62 | +})(); |
| 63 | + |
| 64 | +function getPackageJson() { |
| 65 | + const pathToPackage = path.join(workspace, 'package.json'); |
| 66 | + if (!existsSync(pathToPackage)) |
| 67 | + throw new Error("package.json could not be found in your project's root."); |
| 68 | + return require(pathToPackage); |
| 69 | +} |
| 70 | + |
| 71 | +function exitSuccess(message) { |
| 72 | + console.info(`✔ success ${message}`); |
| 73 | + process.exit(0); |
| 74 | +} |
| 75 | + |
| 76 | +function exitFailure(message) { |
| 77 | + logError(message); |
| 78 | + process.exit(1); |
| 79 | +} |
| 80 | + |
| 81 | +function logError(error) { |
| 82 | + console.error(`✖ fatal ${error.stack || error}`); |
| 83 | +} |
| 84 | + |
| 85 | +function runInWorkspace(command, args) { |
| 86 | + return new Promise((resolve, reject) => { |
| 87 | + const child = spawn(command, args, { cwd: workspace }); |
| 88 | + |
| 89 | + const errorMessages = []; |
| 90 | + let isDone = false; |
| 91 | + |
| 92 | + child.on('error', (error) => { |
| 93 | + if (!isDone) { |
| 94 | + isDone = true; |
| 95 | + reject(error); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + child.stderr.on('data', (chunk) => errorMessages.push(chunk)); |
| 100 | + |
| 101 | + child.on('exit', (code) => { |
| 102 | + if (!isDone) { |
| 103 | + if (code === 0) { |
| 104 | + resolve(); |
| 105 | + } else { |
| 106 | + reject(`${errorMessages.join('')}${EOL}${command} exited with code ${code}`); |
| 107 | + } |
| 108 | + } |
| 109 | + }); |
| 110 | + }); |
| 111 | +} |
0 commit comments