|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const { spawnSync } = require('child_process'); |
| 6 | + |
| 7 | +const rootDir = path.resolve(__dirname, '..'); |
| 8 | +const packageJson = require(path.join(rootDir, 'package.json')); |
| 9 | + |
| 10 | +function parseArgs(argv) { |
| 11 | + const args = {}; |
| 12 | + for (let index = 0; index < argv.length; index += 1) { |
| 13 | + const current = argv[index]; |
| 14 | + if (!current.startsWith('--')) { |
| 15 | + continue; |
| 16 | + } |
| 17 | + |
| 18 | + const key = current.slice(2); |
| 19 | + const next = argv[index + 1]; |
| 20 | + if (!next || next.startsWith('--')) { |
| 21 | + args[key] = true; |
| 22 | + continue; |
| 23 | + } |
| 24 | + |
| 25 | + args[key] = next; |
| 26 | + index += 1; |
| 27 | + } |
| 28 | + |
| 29 | + return args; |
| 30 | +} |
| 31 | + |
| 32 | +function run(command, args, options = {}) { |
| 33 | + const result = spawnSync(command, args, { |
| 34 | + cwd: options.cwd || rootDir, |
| 35 | + encoding: 'utf8', |
| 36 | + shell: false, |
| 37 | + }); |
| 38 | + |
| 39 | + if (result.status !== 0) { |
| 40 | + const output = `${result.stdout || ''}${result.stderr || ''}`.trim(); |
| 41 | + throw new Error(`Command failed: ${command} ${args.join(' ')}\n${output}`); |
| 42 | + } |
| 43 | + |
| 44 | + return (result.stdout || '').trim(); |
| 45 | +} |
| 46 | + |
| 47 | +function tryRun(command, args, options = {}) { |
| 48 | + const result = spawnSync(command, args, { |
| 49 | + cwd: options.cwd || rootDir, |
| 50 | + encoding: 'utf8', |
| 51 | + shell: false, |
| 52 | + }); |
| 53 | + |
| 54 | + if (result.status !== 0) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + return (result.stdout || '').trim(); |
| 59 | +} |
| 60 | + |
| 61 | +function git(args) { |
| 62 | + return run('git', args); |
| 63 | +} |
| 64 | + |
| 65 | +function tryGit(args) { |
| 66 | + return tryRun('git', args); |
| 67 | +} |
| 68 | + |
| 69 | +function resolveTag(args) { |
| 70 | + return args.tag || process.env.RELEASE_TAG || packageJson.version; |
| 71 | +} |
| 72 | + |
| 73 | +function resolvePreviousTag(tag, explicitPreviousTag) { |
| 74 | + if (explicitPreviousTag) { |
| 75 | + return explicitPreviousTag; |
| 76 | + } |
| 77 | + |
| 78 | + const tagRef = tryGit(['rev-parse', '--verify', `refs/tags/${tag}`]); |
| 79 | + if (tagRef) { |
| 80 | + return tryGit(['describe', '--tags', '--abbrev=0', `${tag}^`]); |
| 81 | + } |
| 82 | + |
| 83 | + return tryGit(['describe', '--tags', '--abbrev=0', 'HEAD']); |
| 84 | +} |
| 85 | + |
| 86 | +function getCommitLines(previousTag) { |
| 87 | + const range = previousTag ? `${previousTag}..HEAD` : 'HEAD'; |
| 88 | + const raw = git(['log', '--no-merges', '--pretty=format:%h%x09%s', range]); |
| 89 | + |
| 90 | + if (!raw) { |
| 91 | + return ['- No non-merge commits found in this release range.']; |
| 92 | + } |
| 93 | + |
| 94 | + return raw |
| 95 | + .split('\n') |
| 96 | + .map(line => line.trim()) |
| 97 | + .filter(Boolean) |
| 98 | + .map(line => { |
| 99 | + const [hash, ...subjectParts] = line.split('\t'); |
| 100 | + const subject = subjectParts.join('\t').trim(); |
| 101 | + return `- \`${hash}\` ${subject}`; |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +function buildReleaseNotes(tag, previousTag, commitLines) { |
| 106 | + const body = [ |
| 107 | + `OSpec CLI release \`${tag}\` is now available on npm as \`${packageJson.name}\`.`, |
| 108 | + '', |
| 109 | + '## Upgrade', |
| 110 | + '', |
| 111 | + '```bash', |
| 112 | + `npm install -g ${packageJson.name}@${tag}`, |
| 113 | + 'ospec update', |
| 114 | + '```', |
| 115 | + '', |
| 116 | + '## Notes', |
| 117 | + '', |
| 118 | + '- Existing OSpec projects should run `ospec update` after upgrading the CLI.', |
| 119 | + '- Release tags use bare semantic versions such as `0.3.7`, without a `v` prefix.', |
| 120 | + '', |
| 121 | + '## Git History', |
| 122 | + '' |
| 123 | + ]; |
| 124 | + |
| 125 | + if (previousTag) { |
| 126 | + body.push(`Range: \`${previousTag}..${tag}\``); |
| 127 | + body.push(''); |
| 128 | + } |
| 129 | + |
| 130 | + body.push(...commitLines); |
| 131 | + return `${body.join('\n')}\n`; |
| 132 | +} |
| 133 | + |
| 134 | +function writeOutput(outputPath, content) { |
| 135 | + fs.writeFileSync(path.resolve(rootDir, outputPath), content, 'utf8'); |
| 136 | +} |
| 137 | + |
| 138 | +function main() { |
| 139 | + const args = parseArgs(process.argv.slice(2)); |
| 140 | + const tag = resolveTag(args); |
| 141 | + const previousTag = resolvePreviousTag(tag, args['previous-tag']); |
| 142 | + const commitLines = getCommitLines(previousTag); |
| 143 | + const body = buildReleaseNotes(tag, previousTag, commitLines); |
| 144 | + |
| 145 | + if (args.output) { |
| 146 | + writeOutput(args.output, body); |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + process.stdout.write(body); |
| 151 | +} |
| 152 | + |
| 153 | +main(); |
0 commit comments