|
| 1 | +const { exec } = require('child_process') |
| 2 | + |
| 3 | +function isValidSemanticVersion(version) { |
| 4 | + const semVerPattern = |
| 5 | + /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$/ |
| 6 | + return semVerPattern.test(version) |
| 7 | +} |
| 8 | + |
| 9 | +function executeGitCommand(command) { |
| 10 | + return new Promise((resolve, reject) => { |
| 11 | + const child = exec(command, (error, stdout, stderr) => { |
| 12 | + if (error) { |
| 13 | + console.error(`Error executing command: ${command}\n${stderr}`) |
| 14 | + reject(error) |
| 15 | + } else { |
| 16 | + console.log(`Success executing command: ${command}\n${stdout}`) |
| 17 | + resolve(stdout) |
| 18 | + } |
| 19 | + }) |
| 20 | + |
| 21 | + setTimeout(() => { |
| 22 | + child.kill() |
| 23 | + reject(new Error(`Command timed out: ${command}`)) |
| 24 | + }, 60000) |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +async function safeExecuteGitCommand(command) { |
| 29 | + try { |
| 30 | + await executeGitCommand(command) |
| 31 | + } catch (error) { |
| 32 | + console.error('An error occurred:', error) |
| 33 | + process.exit(1) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +async function main() { |
| 38 | + const versionId = process.argv[2] |
| 39 | + |
| 40 | + if (!versionId) { |
| 41 | + console.error('Please provide a version ID as an argument.') |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + if (!isValidSemanticVersion(versionId)) { |
| 46 | + console.error( |
| 47 | + 'The provided version ID does not follow semantic versioning. Please provide a valid version ID.', |
| 48 | + ) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + await safeExecuteGitCommand('git checkout unstable') |
| 53 | + await safeExecuteGitCommand('git pull') |
| 54 | + |
| 55 | + await safeExecuteGitCommand(`npm version ${versionId} --no-git-tag-version`) |
| 56 | + await safeExecuteGitCommand('git add ../../package.json') |
| 57 | + await safeExecuteGitCommand( |
| 58 | + `git commit --allow-empty -m "siren version updated: ${versionId}" --no-verify`, |
| 59 | + ) |
| 60 | + await safeExecuteGitCommand('git push') |
| 61 | + |
| 62 | + await safeExecuteGitCommand('git checkout stable') |
| 63 | + await safeExecuteGitCommand('git merge --ff-only unstable') |
| 64 | + await safeExecuteGitCommand('git push origin stable') |
| 65 | + |
| 66 | + await safeExecuteGitCommand(`git tag v${versionId}`) |
| 67 | + await safeExecuteGitCommand(`git push origin v${versionId}`) |
| 68 | + |
| 69 | + console.log(`Successfully tagged and initiated release ${versionId}`) |
| 70 | + process.exit(1) |
| 71 | +} |
| 72 | + |
| 73 | +main() |
0 commit comments