release-with-ease 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/bin/release-with-ease.js +326 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Happo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# release-with-ease
|
|
2
|
+
|
|
3
|
+
A script that helps you bump the version of an npm library and update release
|
|
4
|
+
notes. Uses OpenAI to analyze commits.
|
|
5
|
+
|
|
6
|
+
# Usage
|
|
7
|
+
|
|
8
|
+
Run the script provided with the library:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npx release-with-ease
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
If you just want to preview the changes that would be made, use the `--dry-run` flag:
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npx release-with-ease --dry-run
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
# Prerequisites
|
|
21
|
+
|
|
22
|
+
The script requires these environment variables to be set:
|
|
23
|
+
|
|
24
|
+
- `OPEN_AI_API_KEY`
|
|
25
|
+
|
|
26
|
+
You can get a key from https://platform.openai.com/api-keys.
|
|
27
|
+
|
|
28
|
+
The script also assumes that your README.md file has a `# Changelog` section.
|
|
29
|
+
|
|
30
|
+
# Changelog
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/*
|
|
3
|
+
Release helper script
|
|
4
|
+
|
|
5
|
+
- Analyzes git history and suggests semver bump (major/minor/patch) using OpenAI
|
|
6
|
+
- Prompts for confirmation or choice override
|
|
7
|
+
- Generates concise release notes using OpenAI
|
|
8
|
+
- Inserts a new entry at the top of the Changelog in README.md
|
|
9
|
+
- Commits changelog update, bumps version via npm, and pushes with tags
|
|
10
|
+
|
|
11
|
+
Requirements:
|
|
12
|
+
- OPENAI_API_KEY environment variable must be set
|
|
13
|
+
|
|
14
|
+
Usage:
|
|
15
|
+
npx release-with-ease # Normal release
|
|
16
|
+
npx release-with-ease --dry-run # Preview what would be done
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const { execSync } = require('child_process');
|
|
20
|
+
const fs = require('fs');
|
|
21
|
+
const path = require('path');
|
|
22
|
+
const readline = require('readline');
|
|
23
|
+
const os = require('os');
|
|
24
|
+
const crypto = require('crypto');
|
|
25
|
+
|
|
26
|
+
const repoRoot = path.resolve(__dirname, '..');
|
|
27
|
+
const readmePath = path.join(repoRoot, 'README.md');
|
|
28
|
+
const packageJsonPath = path.join(repoRoot, 'package.json');
|
|
29
|
+
|
|
30
|
+
function run(cmd, opts = {}) {
|
|
31
|
+
return execSync(cmd, { stdio: 'pipe', encoding: 'utf8', ...opts });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function safeRun(cmd, opts = {}) {
|
|
35
|
+
try {
|
|
36
|
+
return { ok: true, out: run(cmd, opts) };
|
|
37
|
+
} catch (err) {
|
|
38
|
+
return { ok: false, err };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function fetchOriginTags() {
|
|
43
|
+
const res = safeRun('git fetch origin --tags');
|
|
44
|
+
if (res.ok) return res.out.trim();
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getLastVersionTag() {
|
|
49
|
+
const res = safeRun(
|
|
50
|
+
'git describe --tags --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=0',
|
|
51
|
+
);
|
|
52
|
+
if (res.ok) return res.out.trim();
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function getCommitRange(lastTag) {
|
|
57
|
+
if (lastTag) {
|
|
58
|
+
return safeRun(
|
|
59
|
+
`git log ${lastTag}..HEAD --pretty=format:%H%x1f%s%x1f%b%x1e`,
|
|
60
|
+
).out;
|
|
61
|
+
}
|
|
62
|
+
// No tag yet; use last 100 commits
|
|
63
|
+
return safeRun('git log -n 100 --pretty=format:%H%x1f%s%x1f%b%x1e').out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function parseCommits(raw) {
|
|
67
|
+
if (!raw) return [];
|
|
68
|
+
return raw
|
|
69
|
+
.split('\x1e')
|
|
70
|
+
.map(chunk => chunk.trim())
|
|
71
|
+
.filter(Boolean)
|
|
72
|
+
.map(chunk => {
|
|
73
|
+
const [hash, subject, body] = chunk.split('\x1f');
|
|
74
|
+
return { hash, subject: subject || '', body: body || '' };
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function askOpenAIForRelease(commits) {
|
|
79
|
+
const apiKey = process.env.OPENAI_API_KEY;
|
|
80
|
+
if (!apiKey) return null;
|
|
81
|
+
const messages = [
|
|
82
|
+
{
|
|
83
|
+
role: 'system',
|
|
84
|
+
content:
|
|
85
|
+
'You are a release assistant. Given recent git commits, decide one of: major, minor, or patch following semver. Consider conventional commits, breaking changes, and scope. Also generate concise release notes for a public changelog. Respond with JSON containing "bump" (major/minor/patch), "reasoning" (brief explanation for version bump), and "notes" (array of 3-8 short bullet points of the most important user-facing changes). Use present tense for release notes (e.g. "Add script" not "Added script" or "Adds script"). Do not wrap the JSON in ```json or anything else.',
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
role: 'user',
|
|
89
|
+
content: commits
|
|
90
|
+
.map(c => `- ${c.subject}\n${c.body ? c.body.trim() : ''}`)
|
|
91
|
+
.join('\n'),
|
|
92
|
+
},
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
const res = await fetch('https://api.openai.com/v1/chat/completions', {
|
|
96
|
+
method: 'POST',
|
|
97
|
+
headers: {
|
|
98
|
+
Authorization: `Bearer ${apiKey}`,
|
|
99
|
+
'Content-Type': 'application/json',
|
|
100
|
+
},
|
|
101
|
+
body: JSON.stringify({
|
|
102
|
+
model: 'gpt-4o-mini',
|
|
103
|
+
messages,
|
|
104
|
+
temperature: 0.2,
|
|
105
|
+
max_tokens: 500,
|
|
106
|
+
}),
|
|
107
|
+
});
|
|
108
|
+
if (!res.ok) {
|
|
109
|
+
// throw an actionable error here
|
|
110
|
+
console.error(await res.text());
|
|
111
|
+
throw new Error(
|
|
112
|
+
`Failed to determine version bump: ${res.statusText} ${res.status}`,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
const data = await res.json();
|
|
116
|
+
const content = (data.choices?.[0]?.message?.content || '').trim();
|
|
117
|
+
|
|
118
|
+
const parsed = JSON.parse(content);
|
|
119
|
+
if (!parsed.bump || !['major', 'minor', 'patch'].includes(parsed.bump)) {
|
|
120
|
+
throw new Error('Invalid bump value in OpenAI response');
|
|
121
|
+
}
|
|
122
|
+
if (!parsed.reasoning) {
|
|
123
|
+
throw new Error('Missing reasoning in OpenAI response');
|
|
124
|
+
}
|
|
125
|
+
if (!parsed.notes || !Array.isArray(parsed.notes)) {
|
|
126
|
+
throw new Error('Missing or invalid notes array in OpenAI response');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
bump: parsed.bump,
|
|
131
|
+
reasoning: parsed.reasoning,
|
|
132
|
+
notes: parsed.notes.map(note => `- ${note}`),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function bumpVersionString(cur, bump) {
|
|
137
|
+
const [maj, min, pat] = cur.split('.').map(n => parseInt(n, 10));
|
|
138
|
+
if (Number.isNaN(maj) || Number.isNaN(min) || Number.isNaN(pat)) {
|
|
139
|
+
throw new Error(`Invalid version in package.json: ${cur}`);
|
|
140
|
+
}
|
|
141
|
+
if (bump === 'major') return `${maj + 1}.0.0`;
|
|
142
|
+
if (bump === 'minor') return `${maj}.${min + 1}.0`;
|
|
143
|
+
return `${maj}.${min}.${pat + 1}`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function insertChangelogEntry(readmeContent, newReadmeLines) {
|
|
147
|
+
const lines = readmeContent.split('\n');
|
|
148
|
+
const changelogIdx = lines.findIndex(l =>
|
|
149
|
+
/^#\s*Changelog\s*$/i.test(l.trim()),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
if (changelogIdx === -1) {
|
|
153
|
+
throw new Error('Could not find "# Changelog" section in README.md');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Find insertion point: after the Changelog heading and any blank line
|
|
157
|
+
let insertAt = changelogIdx + 1;
|
|
158
|
+
while (insertAt < lines.length && lines[insertAt].trim() === '') {
|
|
159
|
+
insertAt += 1;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Insert new entry before the current first version subsection
|
|
163
|
+
lines.splice(insertAt, 0, ...newReadmeLines, '');
|
|
164
|
+
|
|
165
|
+
return lines.join('\n');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function prompt(question) {
|
|
169
|
+
const rl = readline.createInterface({
|
|
170
|
+
input: process.stdin,
|
|
171
|
+
output: process.stdout,
|
|
172
|
+
});
|
|
173
|
+
return new Promise(resolve => {
|
|
174
|
+
rl.question(question, ans => {
|
|
175
|
+
rl.close();
|
|
176
|
+
resolve(ans);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function parseArgs() {
|
|
182
|
+
const args = process.argv.slice(2);
|
|
183
|
+
const dryRun = args.includes('--dry-run');
|
|
184
|
+
return { dryRun };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
(async function main() {
|
|
188
|
+
try {
|
|
189
|
+
const { dryRun } = parseArgs();
|
|
190
|
+
|
|
191
|
+
// Check for required environment variable early
|
|
192
|
+
if (!process.env.OPENAI_API_KEY) {
|
|
193
|
+
console.error('ā OPENAI_API_KEY environment variable is required.');
|
|
194
|
+
console.error(
|
|
195
|
+
' You can get one from https://platform.openai.com/api-keys',
|
|
196
|
+
);
|
|
197
|
+
console.error(
|
|
198
|
+
' Please add it to your .env file: OPENAI_API_KEY=your_key_here',
|
|
199
|
+
);
|
|
200
|
+
process.exit(1);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (dryRun) {
|
|
204
|
+
console.log('š DRY RUN MODE - No changes will be made\n');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
fetchOriginTags();
|
|
208
|
+
const lastVersionTag = getLastVersionTag();
|
|
209
|
+
const raw = getCommitRange(lastVersionTag);
|
|
210
|
+
const commits = parseCommits(raw);
|
|
211
|
+
if (!commits.length) {
|
|
212
|
+
console.log('No commits found since last tag. Aborting.');
|
|
213
|
+
process.exit(1);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
console.log(
|
|
217
|
+
`\nš Analyzing ${commits.length} commits since ${
|
|
218
|
+
lastVersionTag || 'beginning'
|
|
219
|
+
}:`,
|
|
220
|
+
);
|
|
221
|
+
commits.forEach(commit => {
|
|
222
|
+
const shortSha = commit.hash.substring(0, 7);
|
|
223
|
+
console.log(` ${shortSha} ${commit.subject}`);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
console.log('\nWaiting for OpenAI to analyze commits...');
|
|
227
|
+
|
|
228
|
+
const result = await askOpenAIForRelease(commits);
|
|
229
|
+
|
|
230
|
+
const { bump, reasoning, notes } = result;
|
|
231
|
+
|
|
232
|
+
console.log(`\nSuggested version bump: ${bump}\n`);
|
|
233
|
+
console.log(`Reasoning:\n\n${reasoning}\n`);
|
|
234
|
+
const confirm = (
|
|
235
|
+
await prompt('Proceed with this bump? [Y/n/major/minor/patch] ')
|
|
236
|
+
)
|
|
237
|
+
.trim()
|
|
238
|
+
.toLowerCase();
|
|
239
|
+
let finalBump = bump;
|
|
240
|
+
if (['major', 'minor', 'patch'].includes(confirm)) finalBump = confirm;
|
|
241
|
+
else if (confirm === 'n' || confirm === 'no') {
|
|
242
|
+
console.log('Aborted by user.');
|
|
243
|
+
process.exit(1);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
|
|
247
|
+
const curVersion = pkg.version;
|
|
248
|
+
const newVersion = bumpVersionString(curVersion, finalBump);
|
|
249
|
+
|
|
250
|
+
console.log(`\nš Release notes for ${newVersion}:`);
|
|
251
|
+
notes.forEach(note => console.log(` ${note}`));
|
|
252
|
+
|
|
253
|
+
// Create a temporary file with just the changelog entry
|
|
254
|
+
const randomName = `changelog-entry-${crypto
|
|
255
|
+
.randomBytes(8)
|
|
256
|
+
.toString('hex')}.tmp`;
|
|
257
|
+
const tempEntryPath = path.join(os.tmpdir(), randomName);
|
|
258
|
+
const entryContent = [`## ${newVersion}`, '', ...notes, ''].join('\n');
|
|
259
|
+
fs.writeFileSync(tempEntryPath, entryContent);
|
|
260
|
+
|
|
261
|
+
console.log(
|
|
262
|
+
`\nš Opening editor to review changelog entry for ${newVersion}...`,
|
|
263
|
+
);
|
|
264
|
+
console.log(
|
|
265
|
+
' Edit the changelog entry as needed, then save and close the editor.',
|
|
266
|
+
);
|
|
267
|
+
|
|
268
|
+
// Open editor with the temporary entry file
|
|
269
|
+
const editor = process.env.EDITOR || process.env.VISUAL || 'nano';
|
|
270
|
+
const editorCmd = `${editor} "${tempEntryPath}"`;
|
|
271
|
+
|
|
272
|
+
try {
|
|
273
|
+
run(editorCmd, { stdio: 'inherit' });
|
|
274
|
+
} catch (err) {
|
|
275
|
+
console.error(
|
|
276
|
+
'ā Failed to open editor. Please set EDITOR or VISUAL environment variable.',
|
|
277
|
+
);
|
|
278
|
+
fs.unlinkSync(tempEntryPath);
|
|
279
|
+
process.exit(1);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Check if user saved the file (file should still exist)
|
|
283
|
+
if (!fs.existsSync(tempEntryPath)) {
|
|
284
|
+
console.log('ā Editor was closed without saving. Aborting release.');
|
|
285
|
+
process.exit(1);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (dryRun) {
|
|
289
|
+
console.log(`\nš DRY RUN - Would have done the following:`);
|
|
290
|
+
console.log(
|
|
291
|
+
` 1. Insert changelog entry for ${newVersion} into README.md`,
|
|
292
|
+
);
|
|
293
|
+
console.log(` 2. git add README.md`);
|
|
294
|
+
console.log(` 3. git commit -m "Update changelog for ${newVersion}"`);
|
|
295
|
+
console.log(` 4. npm version ${finalBump} -m "%s"`);
|
|
296
|
+
console.log(` 5. git push origin main --tags`);
|
|
297
|
+
console.log(`\nā
Dry run complete. Use without --dry-run to execute.`);
|
|
298
|
+
fs.unlinkSync(tempEntryPath);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Read the edited entry and insert it into README
|
|
303
|
+
const editedEntry = fs.readFileSync(tempEntryPath, 'utf8');
|
|
304
|
+
const readme = fs.readFileSync(readmePath, 'utf8');
|
|
305
|
+
const updatedReadme = insertChangelogEntry(
|
|
306
|
+
readme,
|
|
307
|
+
editedEntry.trim().split('\n'),
|
|
308
|
+
);
|
|
309
|
+
fs.writeFileSync(readmePath, updatedReadme);
|
|
310
|
+
fs.unlinkSync(tempEntryPath);
|
|
311
|
+
|
|
312
|
+
run('git add README.md');
|
|
313
|
+
run(`git commit -m "Update changelog for ${newVersion}"`);
|
|
314
|
+
|
|
315
|
+
// Use npm version keyword per user preference
|
|
316
|
+
run(`npm version ${finalBump} -m "%s"`);
|
|
317
|
+
|
|
318
|
+
// Push commit and tags explicitly
|
|
319
|
+
run('git push origin main --tags');
|
|
320
|
+
|
|
321
|
+
console.log(`\nRelease ${newVersion} created and pushed with tags.`);
|
|
322
|
+
} catch (err) {
|
|
323
|
+
console.error(err?.stderr?.toString?.() || err?.message || err);
|
|
324
|
+
process.exit(1);
|
|
325
|
+
}
|
|
326
|
+
})();
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "release-with-ease",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A script to bump the version of an npm library and update release notes. Uses OpenAI to analyze commits.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"release-with-ease": "bin/release-with-ease.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"release": "node --env-file-if-exists=.env bin/release-with-ease.js"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/happo/release-with-ease.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"release",
|
|
18
|
+
"openai",
|
|
19
|
+
"semver",
|
|
20
|
+
"bump",
|
|
21
|
+
"ai"
|
|
22
|
+
],
|
|
23
|
+
"author": "henric.persson@happo.io,joe.lencioni@happo.io",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/happo/release-with-ease/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/happo/release-with-ease#readme"
|
|
29
|
+
}
|