@hubspot/npm-scripts 0.0.6 → 0.0.7-experimental.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.
Files changed (48) hide show
  1. package/README.md +72 -2
  2. package/package.json +8 -4
  3. package/src/__tests__/release.test.d.ts +1 -0
  4. package/src/__tests__/release.test.js +494 -0
  5. package/src/constants/release.d.ts +2 -1
  6. package/src/constants/release.js +7 -1
  7. package/src/constants/repo-sync.d.ts +5 -0
  8. package/src/constants/repo-sync.js +5 -0
  9. package/src/release/__tests__/steps.test.d.ts +1 -0
  10. package/src/release/__tests__/steps.test.js +382 -0
  11. package/src/release/__tests__/validate.test.d.ts +1 -0
  12. package/src/release/__tests__/validate.test.js +148 -0
  13. package/src/release/__tests__/version.test.d.ts +1 -0
  14. package/src/release/__tests__/version.test.js +104 -0
  15. package/src/release/errors.d.ts +4 -0
  16. package/src/release/errors.js +9 -0
  17. package/src/release/steps.d.ts +11 -0
  18. package/src/release/steps.js +159 -0
  19. package/src/release/types.d.ts +80 -0
  20. package/src/release/types.js +1 -0
  21. package/src/release/validate.d.ts +6 -0
  22. package/src/release/validate.js +33 -0
  23. package/src/release/version.d.ts +2 -0
  24. package/src/release/version.js +23 -0
  25. package/src/release.d.ts +1 -1
  26. package/src/release.js +112 -146
  27. package/src/repo-sync.d.ts +5 -0
  28. package/src/repo-sync.js +141 -0
  29. package/src/types.d.ts +18 -0
  30. package/src/utils/__tests__/filesystem.test.d.ts +1 -0
  31. package/src/utils/__tests__/filesystem.test.js +154 -0
  32. package/src/utils/__tests__/git.test.d.ts +1 -0
  33. package/src/utils/__tests__/git.test.js +292 -0
  34. package/src/utils/__tests__/logging.test.d.ts +1 -0
  35. package/src/utils/__tests__/logging.test.js +64 -0
  36. package/src/utils/__tests__/npm.test.d.ts +1 -0
  37. package/src/utils/__tests__/npm.test.js +246 -0
  38. package/src/utils/__tests__/process.test.d.ts +1 -0
  39. package/src/utils/__tests__/process.test.js +12 -0
  40. package/src/utils/__tests__/prompting.test.d.ts +1 -0
  41. package/src/utils/__tests__/prompting.test.js +100 -0
  42. package/src/utils/filesystem.d.ts +1 -0
  43. package/src/utils/filesystem.js +72 -0
  44. package/src/utils/git.d.ts +7 -1
  45. package/src/utils/git.js +112 -3
  46. package/src/utils/logging.js +2 -2
  47. package/src/utils/npm.d.ts +4 -2
  48. package/src/utils/npm.js +18 -19
@@ -0,0 +1,5 @@
1
+ export const DEFAULT_INTERNAL_MAIN_BRANCH = 'master';
2
+ export const DEFAULT_EXTERNAL_MAIN_BRANCH = 'main';
3
+ export const SYNC_BRANCH_PREFIX = 'hs-repo-sync';
4
+ export const TEMP_SYNC_DIR = 'hs-temp-repo-sync';
5
+ export const PUBLIC_README_PATH = 'PUBLIC_README.md';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,382 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { exec as _exec, execSync } from 'node:child_process';
3
+ import open from 'open';
4
+ import { logger } from '../../utils/logging.js';
5
+ import { EXIT_CODES } from '../../utils/process.js';
6
+ import { gitCleanup } from '../../utils/git.js';
7
+ import { publish, updateNextTag, updatePreviousTag } from '../../utils/npm.js';
8
+ import { confirm } from '../../utils/prompting.js';
9
+ import { TAG, INCREMENT } from '../../constants/release.js';
10
+ import { confirmRelease, runBuild, tryBuild, createTempBranch, bumpVersion, ensureNpmAuth, tryPublish, updateDistTagsForLatest, finalizeRelease, logReleaseSuccess, } from '../steps.js';
11
+ import { ReleaseError } from '../errors.js';
12
+ vi.mock('util', () => ({ promisify: vi.fn(fn => fn) }));
13
+ vi.mock('node:child_process', () => ({ exec: vi.fn(), execSync: vi.fn() }));
14
+ vi.mock('open', () => ({ default: vi.fn().mockResolvedValue(undefined) }));
15
+ vi.mock('../../utils/logging.js', () => ({ logger: { log: vi.fn(), error: vi.fn(), success: vi.fn() } }));
16
+ vi.mock('../../utils/process.js', () => ({ EXIT_CODES: { SUCCESS: 0, ERROR: 1 } }));
17
+ vi.mock('../../utils/git.js', () => ({ gitCleanup: vi.fn() }));
18
+ vi.mock('../../utils/npm.js', () => ({ publish: vi.fn(), updateNextTag: vi.fn(), updatePreviousTag: vi.fn() }));
19
+ vi.mock('../../utils/prompting.js', () => ({ confirm: vi.fn() }));
20
+ const mockedExec = vi.mocked(_exec);
21
+ const mockedExecSync = vi.mocked(execSync);
22
+ const mockedGitCleanup = vi.mocked(gitCleanup);
23
+ const mockedPublish = vi.mocked(publish);
24
+ const mockedUpdateNextTag = vi.mocked(updateNextTag);
25
+ const mockedUpdatePreviousTag = vi.mocked(updatePreviousTag);
26
+ const mockedConfirm = vi.mocked(confirm);
27
+ const REGISTRY = 'https://registry.npmjs.org/';
28
+ const PACKAGE_NAME = '@hubspot/test-package';
29
+ describe('release/steps', () => {
30
+ beforeEach(() => {
31
+ vi.resetAllMocks();
32
+ mockedExec.mockResolvedValue({ stdout: '', stderr: '' });
33
+ mockedGitCleanup.mockResolvedValue(undefined);
34
+ mockedPublish.mockResolvedValue(undefined);
35
+ mockedUpdateNextTag.mockResolvedValue(undefined);
36
+ mockedUpdatePreviousTag.mockResolvedValue(undefined);
37
+ mockedConfirm.mockResolvedValue(true);
38
+ });
39
+ describe('confirmRelease', () => {
40
+ it('should log \\nDRY RUN when isDryRun is true', async () => {
41
+ await confirmRelease({
42
+ isDryRun: true,
43
+ currentVersion: '1.0.0-beta.1',
44
+ newVersion: '1.0.1-beta.0',
45
+ tag: TAG.NEXT,
46
+ localVersion: '1.0.0-beta.1',
47
+ versionIncrement: INCREMENT.PATCH,
48
+ });
49
+ expect(logger.log).toHaveBeenCalledWith('\nDRY RUN');
50
+ });
51
+ it('should not log \\nDRY RUN when isDryRun is false', async () => {
52
+ await confirmRelease({
53
+ isDryRun: false,
54
+ currentVersion: '1.0.0-beta.1',
55
+ newVersion: '1.0.1-beta.0',
56
+ tag: TAG.NEXT,
57
+ localVersion: '1.0.0-beta.1',
58
+ versionIncrement: INCREMENT.PATCH,
59
+ });
60
+ expect(logger.log).not.toHaveBeenCalledWith('\nDRY RUN');
61
+ });
62
+ it('should throw ReleaseError with SUCCESS exit code when user declines', async () => {
63
+ mockedConfirm.mockResolvedValue(false);
64
+ await expect(confirmRelease({
65
+ isDryRun: false,
66
+ currentVersion: '1.0.0-beta.1',
67
+ newVersion: '1.0.1-beta.0',
68
+ tag: TAG.NEXT,
69
+ localVersion: '1.0.0-beta.1',
70
+ versionIncrement: INCREMENT.PATCH,
71
+ })).rejects.toMatchObject({ exitCode: EXIT_CODES.SUCCESS });
72
+ });
73
+ it('should only call confirm once for non-latest tags', async () => {
74
+ await confirmRelease({
75
+ isDryRun: false,
76
+ currentVersion: '1.0.0-beta.1',
77
+ newVersion: '1.0.1-beta.0',
78
+ tag: TAG.NEXT,
79
+ localVersion: '1.0.0-beta.1',
80
+ versionIncrement: INCREMENT.PATCH,
81
+ });
82
+ expect(mockedConfirm).toHaveBeenCalledTimes(1);
83
+ });
84
+ it('should only call confirm once when localVersion contains beta and tag is latest', async () => {
85
+ await confirmRelease({
86
+ isDryRun: false,
87
+ currentVersion: '1.0.0',
88
+ newVersion: '1.0.1',
89
+ tag: TAG.LATEST,
90
+ localVersion: '1.0.0-beta.1',
91
+ versionIncrement: INCREMENT.PATCH,
92
+ });
93
+ expect(mockedConfirm).toHaveBeenCalledTimes(1);
94
+ });
95
+ it('should call confirm twice when tag is latest and localVersion has no beta', async () => {
96
+ await confirmRelease({
97
+ isDryRun: false,
98
+ currentVersion: '1.0.0',
99
+ newVersion: '1.0.1',
100
+ tag: TAG.LATEST,
101
+ localVersion: '1.0.0',
102
+ versionIncrement: INCREMENT.PATCH,
103
+ });
104
+ expect(mockedConfirm).toHaveBeenCalledTimes(2);
105
+ });
106
+ it('should throw ReleaseError with SUCCESS when user declines the beta warning', async () => {
107
+ mockedConfirm.mockResolvedValueOnce(true).mockResolvedValueOnce(false);
108
+ await expect(confirmRelease({
109
+ isDryRun: false,
110
+ currentVersion: '1.0.0',
111
+ newVersion: '1.0.1',
112
+ tag: TAG.LATEST,
113
+ localVersion: '1.0.0',
114
+ versionIncrement: INCREMENT.PATCH,
115
+ })).rejects.toMatchObject({ exitCode: EXIT_CODES.SUCCESS });
116
+ });
117
+ });
118
+ describe('runBuild', () => {
119
+ it('should call the provided build function when given', async () => {
120
+ const build = vi.fn().mockResolvedValue(undefined);
121
+ await runBuild(build);
122
+ expect(build).toHaveBeenCalled();
123
+ });
124
+ it('should not run yarn build when a custom build function is provided', async () => {
125
+ const build = vi.fn().mockResolvedValue(undefined);
126
+ await runBuild(build);
127
+ expect(mockedExec).not.toHaveBeenCalledWith('yarn build');
128
+ });
129
+ it('should run yarn and yarn build when no build function is provided', async () => {
130
+ await runBuild();
131
+ expect(mockedExec).toHaveBeenCalledWith('yarn');
132
+ expect(mockedExec).toHaveBeenCalledWith('yarn build');
133
+ });
134
+ it('should propagate errors from the custom build function', async () => {
135
+ const build = vi.fn().mockRejectedValue(new Error('build failed'));
136
+ await expect(runBuild(build)).rejects.toThrow('build failed');
137
+ });
138
+ });
139
+ describe('tryBuild', () => {
140
+ const BASE_ARGS = {
141
+ newVersion: '1.0.1-beta.0',
142
+ isExperimental: false,
143
+ onTempBranch: true,
144
+ tempBranch: 'v1.0.1-beta.0-publish',
145
+ originalBranch: 'master',
146
+ };
147
+ it('should run the build successfully', async () => {
148
+ await tryBuild(BASE_ARGS);
149
+ expect(mockedExec).toHaveBeenCalledWith('yarn build');
150
+ });
151
+ it('should call gitCleanup with tempBranch and originalBranch when build fails and onTempBranch is true', async () => {
152
+ mockedExec.mockImplementation((cmd) => {
153
+ if (cmd === 'yarn build') {
154
+ return Promise.reject(new Error('build failed'));
155
+ }
156
+ return Promise.resolve({ stdout: '', stderr: '' });
157
+ });
158
+ await expect(tryBuild(BASE_ARGS)).rejects.toThrow(ReleaseError);
159
+ expect(mockedGitCleanup).toHaveBeenCalledWith('1.0.1-beta.0', true, 'v1.0.1-beta.0-publish', 'master');
160
+ });
161
+ it('should call gitCleanup without tempBranch when onTempBranch is false and build fails', async () => {
162
+ mockedExec.mockImplementation((cmd) => {
163
+ if (cmd === 'yarn build') {
164
+ return Promise.reject(new Error('build failed'));
165
+ }
166
+ return Promise.resolve({ stdout: '', stderr: '' });
167
+ });
168
+ await expect(tryBuild({ ...BASE_ARGS, onTempBranch: false })).rejects.toThrow(ReleaseError);
169
+ expect(mockedGitCleanup).toHaveBeenCalledWith('1.0.1-beta.0', true, undefined, undefined);
170
+ });
171
+ it('should throw ReleaseError with empty message when build fails', async () => {
172
+ mockedExec.mockImplementation((cmd) => {
173
+ if (cmd === 'yarn build') {
174
+ return Promise.reject(new Error('build failed'));
175
+ }
176
+ return Promise.resolve({ stdout: '', stderr: '' });
177
+ });
178
+ await expect(tryBuild(BASE_ARGS)).rejects.toMatchObject({ message: '' });
179
+ });
180
+ });
181
+ describe('createTempBranch', () => {
182
+ it('should create a branch when on the main branch and not dry run', async () => {
183
+ await createTempBranch('master', 'master', undefined, false, 'v1.0.1-beta.0-publish');
184
+ expect(mockedExec).toHaveBeenCalledWith('git checkout -b v1.0.1-beta.0-publish');
185
+ });
186
+ it('should create a branch when on the previous branch and not dry run', async () => {
187
+ await createTempBranch('previous-branch', 'master', 'previous-branch', false, 'v0.9.1-publish');
188
+ expect(mockedExec).toHaveBeenCalledWith('git checkout -b v0.9.1-publish');
189
+ });
190
+ it('should not create a branch when on a feature branch', async () => {
191
+ await createTempBranch('feature-branch', 'master', undefined, false, 'v1.0.1-beta.0-publish');
192
+ expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git checkout -b'));
193
+ });
194
+ it('should not create a branch during a dry run even on main', async () => {
195
+ await createTempBranch('master', 'master', undefined, true, 'v1.0.1-beta.0-publish');
196
+ expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git checkout -b'));
197
+ });
198
+ });
199
+ describe('bumpVersion', () => {
200
+ it('should run yarn version with --no-git-tag-version for experimental releases', async () => {
201
+ await bumpVersion('1.0.0-experimental.2', true);
202
+ expect(mockedExec).toHaveBeenCalledWith('yarn version --no-git-tag-version --new-version 1.0.0-experimental.2');
203
+ });
204
+ it('should run yarn version without --no-git-tag-version for non-experimental releases', async () => {
205
+ await bumpVersion('1.0.1-beta.0', false);
206
+ expect(mockedExec).toHaveBeenCalledWith('yarn version --new-version 1.0.1-beta.0');
207
+ });
208
+ it('should log success after bumping', async () => {
209
+ await bumpVersion('1.0.1-beta.0', false);
210
+ expect(logger.success).toHaveBeenCalled();
211
+ });
212
+ });
213
+ describe('ensureNpmAuth', () => {
214
+ it('should return without prompting when isDryRun is true', async () => {
215
+ await ensureNpmAuth(true, REGISTRY);
216
+ expect(mockedExec).not.toHaveBeenCalled();
217
+ });
218
+ it('should check whoami and return when already authenticated', async () => {
219
+ await ensureNpmAuth(false, REGISTRY);
220
+ expect(mockedExec).toHaveBeenCalledWith(`npm whoami --registry ${REGISTRY}`);
221
+ });
222
+ it('should call npm adduser when whoami fails', async () => {
223
+ mockedExec.mockRejectedValue(new Error('not logged in'));
224
+ await ensureNpmAuth(false, REGISTRY);
225
+ expect(mockedExecSync).toHaveBeenCalledWith(`npm adduser --registry ${REGISTRY}`, { stdio: 'inherit' });
226
+ });
227
+ it('should throw ReleaseError with message Failed to authenticate with npm when adduser also fails', async () => {
228
+ mockedExec.mockRejectedValue(new Error('not logged in'));
229
+ mockedExecSync.mockImplementation(() => { throw new Error('adduser failed'); });
230
+ await expect(ensureNpmAuth(false, REGISTRY)).rejects.toThrow('Failed to authenticate with npm');
231
+ });
232
+ });
233
+ describe('tryPublish', () => {
234
+ const BASE_ARGS = {
235
+ tag: TAG.NEXT,
236
+ isDryRun: false,
237
+ ignoreScripts: false,
238
+ registry: REGISTRY,
239
+ newVersion: '1.0.1-beta.0',
240
+ isExperimental: false,
241
+ onTempBranch: true,
242
+ tempBranch: 'v1.0.1-beta.0-publish',
243
+ originalBranch: 'master',
244
+ };
245
+ it('should call publish with the correct tag isDryRun ignoreScripts registry args', async () => {
246
+ await tryPublish(BASE_ARGS);
247
+ expect(mockedPublish).toHaveBeenCalledWith(TAG.NEXT, false, false, REGISTRY);
248
+ });
249
+ it('should call gitCleanup with tempBranch and originalBranch when onTempBranch is true and publish fails', async () => {
250
+ mockedPublish.mockRejectedValue(new Error('publish failed'));
251
+ await expect(tryPublish(BASE_ARGS)).rejects.toThrow(ReleaseError);
252
+ expect(mockedGitCleanup).toHaveBeenCalledWith('1.0.1-beta.0', true, 'v1.0.1-beta.0-publish', 'master');
253
+ });
254
+ it('should call gitCleanup with undefined tempBranch and originalBranch when onTempBranch is false and publish fails', async () => {
255
+ mockedPublish.mockRejectedValue(new Error('publish failed'));
256
+ await expect(tryPublish({ ...BASE_ARGS, onTempBranch: false })).rejects.toThrow(ReleaseError);
257
+ expect(mockedGitCleanup).toHaveBeenCalledWith('1.0.1-beta.0', true, undefined, undefined);
258
+ });
259
+ it('should throw ReleaseError with empty message after a publish failure', async () => {
260
+ mockedPublish.mockRejectedValue(new Error('publish failed'));
261
+ await expect(tryPublish(BASE_ARGS)).rejects.toMatchObject({ message: '' });
262
+ });
263
+ });
264
+ describe('updateDistTagsForLatest', () => {
265
+ const BASE_ARGS = {
266
+ tag: TAG.LATEST,
267
+ versionIncrement: INCREMENT.PATCH,
268
+ packageName: PACKAGE_NAME,
269
+ newVersion: '1.0.1',
270
+ currentLatestTag: '1.0.0',
271
+ isDryRun: false,
272
+ registry: REGISTRY,
273
+ tempBranch: 'v1.0.1-publish',
274
+ };
275
+ it('should do nothing when tag is not latest', async () => {
276
+ await updateDistTagsForLatest({ ...BASE_ARGS, tag: TAG.NEXT });
277
+ expect(mockedUpdateNextTag).not.toHaveBeenCalled();
278
+ });
279
+ it('should call updateNextTag for a latest release', async () => {
280
+ await updateDistTagsForLatest(BASE_ARGS);
281
+ expect(mockedUpdateNextTag).toHaveBeenCalledWith(PACKAGE_NAME, '1.0.1', false, REGISTRY);
282
+ });
283
+ it('should call updatePreviousTag only for a major latest release', async () => {
284
+ await updateDistTagsForLatest({ ...BASE_ARGS, versionIncrement: INCREMENT.MAJOR });
285
+ expect(mockedUpdatePreviousTag).toHaveBeenCalledWith(PACKAGE_NAME, '1.0.0', false, REGISTRY);
286
+ });
287
+ it('should not call updatePreviousTag for a non-major latest release', async () => {
288
+ await updateDistTagsForLatest(BASE_ARGS);
289
+ expect(mockedUpdatePreviousTag).not.toHaveBeenCalled();
290
+ });
291
+ it('should log an error and resolve when updateNextTag fails', async () => {
292
+ mockedUpdateNextTag.mockRejectedValue(new Error('dist-tag failed'));
293
+ await expect(updateDistTagsForLatest(BASE_ARGS)).resolves.toBeUndefined();
294
+ expect(logger.error).toHaveBeenCalled();
295
+ });
296
+ it('should log an error and resolve when updatePreviousTag fails', async () => {
297
+ mockedUpdatePreviousTag.mockRejectedValue(new Error('dist-tag failed'));
298
+ await expect(updateDistTagsForLatest({ ...BASE_ARGS, versionIncrement: INCREMENT.MAJOR })).resolves.toBeUndefined();
299
+ expect(logger.error).toHaveBeenCalled();
300
+ });
301
+ });
302
+ describe('finalizeRelease', () => {
303
+ const BASE_ARGS = {
304
+ isDryRun: false,
305
+ isExperimental: false,
306
+ newVersion: '1.0.1-beta.0',
307
+ localVersion: '1.0.0-beta.1',
308
+ tempBranch: 'v1.0.1-beta.0-publish',
309
+ isPrevious: false,
310
+ previousBranch: 'previous-branch',
311
+ mainBranch: 'master',
312
+ repositoryUrl: 'https://github.com/test/repo',
313
+ };
314
+ it('should call gitCleanup and throw ReleaseError with SUCCESS exit code on dry run', async () => {
315
+ await expect(finalizeRelease({ ...BASE_ARGS, isDryRun: true })).rejects.toMatchObject({ exitCode: EXIT_CODES.SUCCESS });
316
+ expect(mockedGitCleanup).toHaveBeenCalledWith('1.0.1-beta.0', true);
317
+ });
318
+ it('should not push or open a browser on dry run', async () => {
319
+ await expect(finalizeRelease({ ...BASE_ARGS, isDryRun: true })).rejects.toThrow(ReleaseError);
320
+ expect(mockedExec).not.toHaveBeenCalledWith(expect.stringContaining('git push'));
321
+ expect(vi.mocked(open)).not.toHaveBeenCalled();
322
+ });
323
+ it('should reset the package.json version and return on experimental release', async () => {
324
+ await finalizeRelease({ ...BASE_ARGS, isExperimental: true });
325
+ expect(mockedExec).toHaveBeenCalledWith(`yarn version --no-git-tag-version --new-version ${BASE_ARGS.localVersion}`);
326
+ });
327
+ it('should run git push with the correct tempBranch and newVersion on a normal release', async () => {
328
+ await finalizeRelease(BASE_ARGS);
329
+ expect(mockedExec).toHaveBeenCalledWith(`git push --atomic origin ${BASE_ARGS.tempBranch} v${BASE_ARGS.newVersion}`);
330
+ });
331
+ it('should use mainBranch in the compare URL when isPrevious is false', async () => {
332
+ await finalizeRelease(BASE_ARGS);
333
+ expect(vi.mocked(open)).toHaveBeenCalledWith(`https://github.com/test/repo/compare/master...v1.0.1-beta.0-publish`);
334
+ });
335
+ it('should use previousBranch in the compare URL when isPrevious is true', async () => {
336
+ await finalizeRelease({ ...BASE_ARGS, isPrevious: true });
337
+ expect(vi.mocked(open)).toHaveBeenCalledWith(`https://github.com/test/repo/compare/previous-branch...v1.0.1-beta.0-publish`);
338
+ });
339
+ });
340
+ describe('logReleaseSuccess', () => {
341
+ const BASE_ARGS = {
342
+ packageName: PACKAGE_NAME,
343
+ newVersion: '1.0.1-beta.0',
344
+ tag: TAG.NEXT,
345
+ repositoryUrl: 'https://github.com/test/repo',
346
+ isDryRun: false,
347
+ };
348
+ it('should log the package name and npm URL', async () => {
349
+ await logReleaseSuccess(BASE_ARGS);
350
+ expect(logger.success).toHaveBeenCalledWith(`${PACKAGE_NAME} version 1.0.1-beta.0 published successfully`);
351
+ expect(logger.log).toHaveBeenCalledWith(`https://www.npmjs.com/package/${PACKAGE_NAME}?activeTab=versions`);
352
+ });
353
+ it('should not log the GitHub release reminder for next tag', async () => {
354
+ await logReleaseSuccess(BASE_ARGS);
355
+ expect(logger.log).not.toHaveBeenCalledWith(expect.stringContaining('Remember to create a new release on Github'));
356
+ });
357
+ it('should log the GitHub release reminder for latest tag', async () => {
358
+ await logReleaseSuccess({ ...BASE_ARGS, tag: TAG.LATEST, newVersion: '1.0.1' });
359
+ expect(logger.log).toHaveBeenCalledWith(expect.stringContaining('Remember to create a new release on Github'));
360
+ });
361
+ it('should invoke postLatestRelease with newVersion and dryRun for latest tag', async () => {
362
+ const postLatestRelease = vi.fn().mockResolvedValue(undefined);
363
+ await logReleaseSuccess({
364
+ ...BASE_ARGS,
365
+ tag: TAG.LATEST,
366
+ newVersion: '1.0.1',
367
+ postLatestRelease,
368
+ });
369
+ expect(postLatestRelease).toHaveBeenCalledWith({ newVersion: '1.0.1', dryRun: false });
370
+ });
371
+ it('should not invoke postLatestRelease for previous tag even if provided', async () => {
372
+ const postLatestRelease = vi.fn().mockResolvedValue(undefined);
373
+ await logReleaseSuccess({
374
+ ...BASE_ARGS,
375
+ tag: TAG.PREVIOUS,
376
+ newVersion: '0.9.1',
377
+ postLatestRelease,
378
+ });
379
+ expect(postLatestRelease).not.toHaveBeenCalled();
380
+ });
381
+ });
382
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,148 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { validatePreviousBranchConfigured, validateBranchForTag, validateTagIncrementCombination, validateVersionSync, } from '../validate.js';
3
+ import { ReleaseError } from '../errors.js';
4
+ import { TAG, INCREMENT } from '../../constants/release.js';
5
+ const DIST_TAGS = {
6
+ latest: '1.0.0',
7
+ next: '1.0.0-beta.1',
8
+ experimental: '1.0.0-experimental.1',
9
+ previous: '0.9.0',
10
+ };
11
+ describe('release/validate', () => {
12
+ describe('validatePreviousBranchConfigured', () => {
13
+ it('should not throw when isPrevious is false', () => {
14
+ expect(() => validatePreviousBranchConfigured(false)).not.toThrow();
15
+ });
16
+ it('should not throw when isPrevious is true and previousBranch is provided', () => {
17
+ expect(() => validatePreviousBranchConfigured(true, 'previous-branch')).not.toThrow();
18
+ });
19
+ it('should throw ReleaseError with message containing previousBranch is not configured when isPrevious is true and no branch is provided', () => {
20
+ expect(() => validatePreviousBranchConfigured(true)).toThrow(ReleaseError);
21
+ expect(() => validatePreviousBranchConfigured(true)).toThrow('previousBranch is not configured');
22
+ });
23
+ });
24
+ describe('validateBranchForTag', () => {
25
+ it('should not throw for experimental release from a feature branch', () => {
26
+ expect(() => validateBranchForTag({
27
+ isExperimental: true,
28
+ isPrevious: false,
29
+ branch: 'feature-branch',
30
+ mainBranch: 'master',
31
+ previousBranch: 'previous-branch',
32
+ })).not.toThrow();
33
+ });
34
+ it('should throw for experimental release from the main branch', () => {
35
+ expect(() => validateBranchForTag({
36
+ isExperimental: true,
37
+ isPrevious: false,
38
+ branch: 'master',
39
+ mainBranch: 'master',
40
+ previousBranch: 'previous-branch',
41
+ })).toThrow(ReleaseError);
42
+ });
43
+ it('should throw for experimental release from the previous branch', () => {
44
+ expect(() => validateBranchForTag({
45
+ isExperimental: true,
46
+ isPrevious: false,
47
+ branch: 'previous-branch',
48
+ mainBranch: 'master',
49
+ previousBranch: 'previous-branch',
50
+ })).toThrow(ReleaseError);
51
+ });
52
+ it('should throw for previous release from the wrong branch', () => {
53
+ expect(() => validateBranchForTag({
54
+ isExperimental: false,
55
+ isPrevious: true,
56
+ branch: 'master',
57
+ mainBranch: 'master',
58
+ previousBranch: 'previous-branch',
59
+ })).toThrow(ReleaseError);
60
+ });
61
+ it('should not throw for previous release from the correct previous branch', () => {
62
+ expect(() => validateBranchForTag({
63
+ isExperimental: false,
64
+ isPrevious: true,
65
+ branch: 'previous-branch',
66
+ mainBranch: 'master',
67
+ previousBranch: 'previous-branch',
68
+ })).not.toThrow();
69
+ });
70
+ it('should throw for latest/next release from a non-main branch', () => {
71
+ expect(() => validateBranchForTag({
72
+ isExperimental: false,
73
+ isPrevious: false,
74
+ branch: 'feature-branch',
75
+ mainBranch: 'master',
76
+ previousBranch: 'previous-branch',
77
+ })).toThrow(ReleaseError);
78
+ });
79
+ it('should not throw for latest/next release from the main branch', () => {
80
+ expect(() => validateBranchForTag({
81
+ isExperimental: false,
82
+ isPrevious: false,
83
+ branch: 'master',
84
+ mainBranch: 'master',
85
+ previousBranch: 'previous-branch',
86
+ })).not.toThrow();
87
+ });
88
+ });
89
+ describe('validateTagIncrementCombination', () => {
90
+ it('should throw for latest tag with prerelease increment', () => {
91
+ expect(() => validateTagIncrementCombination(TAG.LATEST, INCREMENT.PRERELEASE)).toThrow(ReleaseError);
92
+ });
93
+ it('should throw for previous tag with prerelease increment', () => {
94
+ expect(() => validateTagIncrementCombination(TAG.PREVIOUS, INCREMENT.PRERELEASE)).toThrow(ReleaseError);
95
+ });
96
+ it('should not throw for next tag with prerelease increment', () => {
97
+ expect(() => validateTagIncrementCombination(TAG.NEXT, INCREMENT.PRERELEASE)).not.toThrow();
98
+ });
99
+ it('should not throw for experimental tag with prerelease increment', () => {
100
+ expect(() => validateTagIncrementCombination(TAG.EXPERIMENTAL, INCREMENT.PRERELEASE)).not.toThrow();
101
+ });
102
+ it('should not throw for latest tag with patch increment', () => {
103
+ expect(() => validateTagIncrementCombination(TAG.LATEST, INCREMENT.PATCH)).not.toThrow();
104
+ });
105
+ });
106
+ describe('validateVersionSync', () => {
107
+ it('should not throw when next tag version matches local version', () => {
108
+ expect(() => validateVersionSync({
109
+ isExperimental: false,
110
+ isPrevious: false,
111
+ localVersion: '1.0.0-beta.1',
112
+ distTags: DIST_TAGS,
113
+ })).not.toThrow();
114
+ });
115
+ it('should throw with out of sync message when next tag version does not match local version', () => {
116
+ expect(() => validateVersionSync({
117
+ isExperimental: false,
118
+ isPrevious: false,
119
+ localVersion: '1.0.0-beta.2',
120
+ distTags: DIST_TAGS,
121
+ })).toThrow('out of sync');
122
+ });
123
+ it('should not throw for experimental regardless of version mismatch', () => {
124
+ expect(() => validateVersionSync({
125
+ isExperimental: true,
126
+ isPrevious: false,
127
+ localVersion: 'anything',
128
+ distTags: DIST_TAGS,
129
+ })).not.toThrow();
130
+ });
131
+ it('should not throw when previous version matches local version', () => {
132
+ expect(() => validateVersionSync({
133
+ isExperimental: false,
134
+ isPrevious: true,
135
+ localVersion: '0.9.0',
136
+ distTags: DIST_TAGS,
137
+ })).not.toThrow();
138
+ });
139
+ it('should throw when previous version does not match local version', () => {
140
+ expect(() => validateVersionSync({
141
+ isExperimental: false,
142
+ isPrevious: true,
143
+ localVersion: '0.8.0',
144
+ distTags: DIST_TAGS,
145
+ })).toThrow('out of sync');
146
+ });
147
+ });
148
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,104 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { computeNewVersion } from '../version.js';
3
+ import { ReleaseError } from '../errors.js';
4
+ import { TAG, INCREMENT } from '../../constants/release.js';
5
+ const DIST_TAGS = {
6
+ latest: '1.0.0',
7
+ next: '1.0.0-beta.1',
8
+ experimental: '1.0.0-experimental.1',
9
+ previous: '0.9.0',
10
+ };
11
+ describe('release/version', () => {
12
+ describe('computeNewVersion', () => {
13
+ it('should return 1.0.1-beta.0 for next tag with patch increment', () => {
14
+ expect(computeNewVersion({
15
+ tag: TAG.NEXT,
16
+ versionIncrement: INCREMENT.PATCH,
17
+ isExperimental: false,
18
+ distTags: DIST_TAGS,
19
+ localVersion: '1.0.0-beta.1',
20
+ })).toBe('1.0.1-beta.0');
21
+ });
22
+ it('should return 1.1.0-beta.0 for next tag with minor increment', () => {
23
+ expect(computeNewVersion({
24
+ tag: TAG.NEXT,
25
+ versionIncrement: INCREMENT.MINOR,
26
+ isExperimental: false,
27
+ distTags: DIST_TAGS,
28
+ localVersion: '1.0.0-beta.1',
29
+ })).toBe('1.1.0-beta.0');
30
+ });
31
+ it('should return 2.0.0-beta.0 for next tag with major increment', () => {
32
+ expect(computeNewVersion({
33
+ tag: TAG.NEXT,
34
+ versionIncrement: INCREMENT.MAJOR,
35
+ isExperimental: false,
36
+ distTags: DIST_TAGS,
37
+ localVersion: '1.0.0-beta.1',
38
+ })).toBe('2.0.0-beta.0');
39
+ });
40
+ it('should return 1.0.0-beta.2 for next tag with prerelease increment', () => {
41
+ expect(computeNewVersion({
42
+ tag: TAG.NEXT,
43
+ versionIncrement: INCREMENT.PRERELEASE,
44
+ isExperimental: false,
45
+ distTags: DIST_TAGS,
46
+ localVersion: '1.0.0-beta.1',
47
+ })).toBe('1.0.0-beta.2');
48
+ });
49
+ it('should return 1.0.1 for latest tag with patch increment', () => {
50
+ expect(computeNewVersion({
51
+ tag: TAG.LATEST,
52
+ versionIncrement: INCREMENT.PATCH,
53
+ isExperimental: false,
54
+ distTags: DIST_TAGS,
55
+ localVersion: '1.0.0',
56
+ })).toBe('1.0.1');
57
+ });
58
+ it('should return 1.1.0 for latest tag with minor increment', () => {
59
+ expect(computeNewVersion({
60
+ tag: TAG.LATEST,
61
+ versionIncrement: INCREMENT.MINOR,
62
+ isExperimental: false,
63
+ distTags: DIST_TAGS,
64
+ localVersion: '1.0.0',
65
+ })).toBe('1.1.0');
66
+ });
67
+ it('should return 2.0.0 for latest tag with major increment', () => {
68
+ expect(computeNewVersion({
69
+ tag: TAG.LATEST,
70
+ versionIncrement: INCREMENT.MAJOR,
71
+ isExperimental: false,
72
+ distTags: DIST_TAGS,
73
+ localVersion: '1.0.0',
74
+ })).toBe('2.0.0');
75
+ });
76
+ it('should return 0.9.1 for previous tag with patch increment', () => {
77
+ expect(computeNewVersion({
78
+ tag: TAG.PREVIOUS,
79
+ versionIncrement: INCREMENT.PATCH,
80
+ isExperimental: false,
81
+ distTags: DIST_TAGS,
82
+ localVersion: '0.9.0',
83
+ })).toBe('0.9.1');
84
+ });
85
+ it('should return 1.0.0-experimental.2 for experimental tag with prerelease increment', () => {
86
+ expect(computeNewVersion({
87
+ tag: TAG.EXPERIMENTAL,
88
+ versionIncrement: INCREMENT.PRERELEASE,
89
+ isExperimental: true,
90
+ distTags: DIST_TAGS,
91
+ localVersion: '1.0.0-beta.1',
92
+ })).toBe('1.0.0-experimental.2');
93
+ });
94
+ it('should throw ReleaseError when semver cannot parse the version', () => {
95
+ expect(() => computeNewVersion({
96
+ tag: TAG.LATEST,
97
+ versionIncrement: INCREMENT.PATCH,
98
+ isExperimental: false,
99
+ distTags: DIST_TAGS,
100
+ localVersion: 'not-a-version',
101
+ })).toThrow(ReleaseError);
102
+ });
103
+ });
104
+ });
@@ -0,0 +1,4 @@
1
+ export declare class ReleaseError extends Error {
2
+ readonly exitCode: number;
3
+ constructor(message: string, exitCode?: number);
4
+ }
@@ -0,0 +1,9 @@
1
+ import { EXIT_CODES } from '../utils/process.js';
2
+ export class ReleaseError extends Error {
3
+ exitCode;
4
+ constructor(message, exitCode = EXIT_CODES.ERROR) {
5
+ super(message);
6
+ this.name = 'ReleaseError';
7
+ this.exitCode = exitCode;
8
+ }
9
+ }