@hubspot/npm-scripts 0.3.4 → 0.4.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/README.md +88 -11
- package/package.json +4 -2
- package/src/__tests__/release.test.js +490 -10
- package/src/constants/release.d.ts +3 -0
- package/src/constants/release.js +7 -0
- package/src/release.js +124 -39
- package/src/types.d.ts +5 -2
- package/src/utils/__tests__/logging.test.js +15 -1
- package/src/utils/__tests__/npm.test.js +18 -1
- package/src/utils/logging.d.ts +1 -0
- package/src/utils/logging.js +6 -0
- package/src/utils/npm.d.ts +1 -0
- package/src/utils/npm.js +5 -0
- package/src/utils/release/__tests__/steps.test.js +589 -6
- package/src/utils/release/__tests__/validate.test.js +9 -1
- package/src/utils/release/__tests__/version.test.js +40 -34
- package/src/utils/release/steps.d.ts +29 -4
- package/src/utils/release/steps.js +350 -16
- package/src/utils/release/types.d.ts +22 -2
- package/src/utils/release/validate.d.ts +3 -0
- package/src/utils/release/validate.js +6 -0
- package/src/utils/release/version.d.ts +2 -1
- package/src/utils/release/version.js +22 -6
|
@@ -1,21 +1,13 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { computeNewVersion } from '../version.js';
|
|
2
|
+
import { computeExperimentalVersion, computeNewVersion } from '../version.js';
|
|
3
3
|
import { ReleaseError } from '../errors.js';
|
|
4
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
5
|
describe('release/version', () => {
|
|
12
6
|
describe('computeNewVersion', () => {
|
|
13
7
|
it('should return 1.0.1-beta.0 for next tag with patch increment', () => {
|
|
14
8
|
expect(computeNewVersion({
|
|
15
9
|
tag: TAG.NEXT,
|
|
16
10
|
versionIncrement: INCREMENT.PATCH,
|
|
17
|
-
isExperimental: false,
|
|
18
|
-
distTags: DIST_TAGS,
|
|
19
11
|
localVersion: '1.0.0-beta.1',
|
|
20
12
|
})).toBe('1.0.1-beta.0');
|
|
21
13
|
});
|
|
@@ -23,8 +15,6 @@ describe('release/version', () => {
|
|
|
23
15
|
expect(computeNewVersion({
|
|
24
16
|
tag: TAG.NEXT,
|
|
25
17
|
versionIncrement: INCREMENT.MINOR,
|
|
26
|
-
isExperimental: false,
|
|
27
|
-
distTags: DIST_TAGS,
|
|
28
18
|
localVersion: '1.0.0-beta.1',
|
|
29
19
|
})).toBe('1.1.0-beta.0');
|
|
30
20
|
});
|
|
@@ -32,8 +22,6 @@ describe('release/version', () => {
|
|
|
32
22
|
expect(computeNewVersion({
|
|
33
23
|
tag: TAG.NEXT,
|
|
34
24
|
versionIncrement: INCREMENT.MAJOR,
|
|
35
|
-
isExperimental: false,
|
|
36
|
-
distTags: DIST_TAGS,
|
|
37
25
|
localVersion: '1.0.0-beta.1',
|
|
38
26
|
})).toBe('2.0.0-beta.0');
|
|
39
27
|
});
|
|
@@ -41,8 +29,6 @@ describe('release/version', () => {
|
|
|
41
29
|
expect(computeNewVersion({
|
|
42
30
|
tag: TAG.NEXT,
|
|
43
31
|
versionIncrement: INCREMENT.PRERELEASE,
|
|
44
|
-
isExperimental: false,
|
|
45
|
-
distTags: DIST_TAGS,
|
|
46
32
|
localVersion: '1.0.0-beta.1',
|
|
47
33
|
})).toBe('1.0.0-beta.2');
|
|
48
34
|
});
|
|
@@ -50,8 +36,6 @@ describe('release/version', () => {
|
|
|
50
36
|
expect(computeNewVersion({
|
|
51
37
|
tag: TAG.LATEST,
|
|
52
38
|
versionIncrement: INCREMENT.PATCH,
|
|
53
|
-
isExperimental: false,
|
|
54
|
-
distTags: DIST_TAGS,
|
|
55
39
|
localVersion: '1.0.0',
|
|
56
40
|
})).toBe('1.0.1');
|
|
57
41
|
});
|
|
@@ -59,8 +43,6 @@ describe('release/version', () => {
|
|
|
59
43
|
expect(computeNewVersion({
|
|
60
44
|
tag: TAG.LATEST,
|
|
61
45
|
versionIncrement: INCREMENT.MINOR,
|
|
62
|
-
isExperimental: false,
|
|
63
|
-
distTags: DIST_TAGS,
|
|
64
46
|
localVersion: '1.0.0',
|
|
65
47
|
})).toBe('1.1.0');
|
|
66
48
|
});
|
|
@@ -68,8 +50,6 @@ describe('release/version', () => {
|
|
|
68
50
|
expect(computeNewVersion({
|
|
69
51
|
tag: TAG.LATEST,
|
|
70
52
|
versionIncrement: INCREMENT.MAJOR,
|
|
71
|
-
isExperimental: false,
|
|
72
|
-
distTags: DIST_TAGS,
|
|
73
53
|
localVersion: '1.0.0',
|
|
74
54
|
})).toBe('2.0.0');
|
|
75
55
|
});
|
|
@@ -77,28 +57,54 @@ describe('release/version', () => {
|
|
|
77
57
|
expect(computeNewVersion({
|
|
78
58
|
tag: TAG.PREVIOUS,
|
|
79
59
|
versionIncrement: INCREMENT.PATCH,
|
|
80
|
-
isExperimental: false,
|
|
81
|
-
distTags: DIST_TAGS,
|
|
82
60
|
localVersion: '0.9.0',
|
|
83
61
|
})).toBe('0.9.1');
|
|
84
62
|
});
|
|
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
63
|
it('should throw ReleaseError when semver cannot parse the version', () => {
|
|
95
64
|
expect(() => computeNewVersion({
|
|
96
65
|
tag: TAG.LATEST,
|
|
97
66
|
versionIncrement: INCREMENT.PATCH,
|
|
98
|
-
isExperimental: false,
|
|
99
|
-
distTags: DIST_TAGS,
|
|
100
67
|
localVersion: 'not-a-version',
|
|
101
68
|
})).toThrow(ReleaseError);
|
|
102
69
|
});
|
|
103
70
|
});
|
|
71
|
+
describe('computeExperimentalVersion', () => {
|
|
72
|
+
it('should increment the highest experimental number on the latest base', () => {
|
|
73
|
+
expect(computeExperimentalVersion('8.1.0', [
|
|
74
|
+
'8.0.0',
|
|
75
|
+
'8.1.0-experimental.0',
|
|
76
|
+
'8.1.0-experimental.2',
|
|
77
|
+
'8.1.0-experimental.1',
|
|
78
|
+
'8.1.0',
|
|
79
|
+
])).toBe('8.1.0-experimental.3');
|
|
80
|
+
});
|
|
81
|
+
it('should start at 0 when only other bases have experimental versions', () => {
|
|
82
|
+
expect(computeExperimentalVersion('8.1.0', [
|
|
83
|
+
'8.0.1-experimental.1',
|
|
84
|
+
'8.1.0',
|
|
85
|
+
'8.2.0-experimental.4',
|
|
86
|
+
])).toBe('8.1.0-experimental.0');
|
|
87
|
+
});
|
|
88
|
+
it('should start at 0 when there are no published versions', () => {
|
|
89
|
+
expect(computeExperimentalVersion('1.0.0', [])).toBe('1.0.0-experimental.0');
|
|
90
|
+
});
|
|
91
|
+
it('should compare experimental numbers numerically', () => {
|
|
92
|
+
expect(computeExperimentalVersion('1.0.0', [
|
|
93
|
+
'1.0.0-experimental.9',
|
|
94
|
+
'1.0.0-experimental.10',
|
|
95
|
+
])).toBe('1.0.0-experimental.11');
|
|
96
|
+
});
|
|
97
|
+
it('should ignore beta and malformed experimental versions on the same base', () => {
|
|
98
|
+
expect(computeExperimentalVersion('1.0.0', [
|
|
99
|
+
'1.0.0-beta.7',
|
|
100
|
+
'1.0.0-experimental',
|
|
101
|
+
'1.0.0-experimental.foo',
|
|
102
|
+
'1.0.0-experimental.1.2',
|
|
103
|
+
'not-a-version',
|
|
104
|
+
])).toBe('1.0.0-experimental.0');
|
|
105
|
+
});
|
|
106
|
+
it('should throw ReleaseError when latest is not a valid version', () => {
|
|
107
|
+
expect(() => computeExperimentalVersion('nope', [])).toThrow(ReleaseError);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
104
110
|
});
|
|
@@ -1,11 +1,36 @@
|
|
|
1
|
-
import type { ConfirmReleaseOptions, TryBuildOptions, TryPublishOptions, UpdateDistTagsOptions, FinalizeReleaseOptions, LogReleaseSuccessOptions } from './types.js';
|
|
2
|
-
|
|
1
|
+
import type { ConfirmReleaseOptions, TryBuildOptions, TryPublishOptions, UpdateDistTagsOptions, FinalizeReleaseOptions, LogReleaseSuccessOptions, OpenReleasePrOptions, RunExperimentalWorkflowOptions } from './types.js';
|
|
2
|
+
import type { Tag } from '../../types.js';
|
|
3
|
+
export declare function resolveVersionIncrement(tag: Tag, versionIncrement: string | undefined, skipPrompts: boolean): Promise<string>;
|
|
4
|
+
export declare function confirmRelease({ isDryRun, currentVersion, currentVersionLabel, lastExperimentalVersion, newVersion, tag, localVersion, versionIncrement, skipPrompts, }: ConfirmReleaseOptions): Promise<void>;
|
|
3
5
|
export declare function runBuild(build?: () => Promise<void> | void): Promise<void>;
|
|
4
6
|
export declare function createTempBranch(branch: string, mainBranch: string, previousBranch: string | undefined, isDryRun: boolean, tempBranch: string): Promise<void>;
|
|
5
|
-
export declare function bumpVersion(newVersion: string,
|
|
7
|
+
export declare function bumpVersion(newVersion: string, skipGitTag: boolean): Promise<void>;
|
|
6
8
|
export declare function tryBuild({ build, newVersion, isExperimental, onTempBranch, tempBranch, originalBranch, }: TryBuildOptions): Promise<void>;
|
|
7
|
-
export declare function ensureNpmAuth(isDryRun: boolean, registry: string): Promise<void>;
|
|
9
|
+
export declare function ensureNpmAuth(isDryRun: boolean, registry: string, skipPrompts?: boolean): Promise<void>;
|
|
10
|
+
export declare function ensureCleanWorkingTree(): Promise<void>;
|
|
11
|
+
export declare function ensureGhCli(repositoryUrl: string): Promise<void>;
|
|
12
|
+
export declare function ensureBranchPushed(branch: string): Promise<string>;
|
|
13
|
+
type WorkflowRun = {
|
|
14
|
+
databaseId: number;
|
|
15
|
+
displayTitle: string;
|
|
16
|
+
url: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function findWorkflowRun(workflow: string, sha: string, existingRunIds: Set<number>, attempts?: number, intervalMs?: number): Promise<WorkflowRun | undefined>;
|
|
19
|
+
type WorkflowJob = {
|
|
20
|
+
name: string;
|
|
21
|
+
status: string;
|
|
22
|
+
conclusion: string;
|
|
23
|
+
};
|
|
24
|
+
type WorkflowRunStatus = {
|
|
25
|
+
status: string;
|
|
26
|
+
conclusion: string;
|
|
27
|
+
jobs: WorkflowJob[];
|
|
28
|
+
};
|
|
29
|
+
export declare function watchWorkflowRun(runId: number, intervalMs?: number, maxStatusErrors?: number): Promise<WorkflowRunStatus>;
|
|
30
|
+
export declare function runExperimentalWorkflow({ workflow, mainBranch, branch, sha, packageName, isDryRun, }: RunExperimentalWorkflowOptions): Promise<void>;
|
|
31
|
+
export declare function openReleasePr({ tag, newVersion, isDryRun, baseBranch, repositoryUrl, headBranch, onTempBranch, tempBranch, originalBranch, }: OpenReleasePrOptions): Promise<void>;
|
|
8
32
|
export declare function tryPublish({ tag, isDryRun, ignoreScripts, registry, newVersion, isExperimental, onTempBranch, tempBranch, originalBranch, }: TryPublishOptions): Promise<void>;
|
|
9
33
|
export declare function updateDistTagsForLatest({ tag, versionIncrement, packageName, newVersion, currentLatestTag, isDryRun, registry, tempBranch, }: UpdateDistTagsOptions): Promise<void>;
|
|
10
34
|
export declare function finalizeRelease({ isDryRun, isExperimental, newVersion, localVersion, tempBranch, isPrevious, previousBranch, mainBranch, repositoryUrl, }: FinalizeReleaseOptions): Promise<void>;
|
|
11
35
|
export declare function logReleaseSuccess({ packageName, newVersion, tag, repositoryUrl, isDryRun, postLatestRelease, }: LogReleaseSuccessOptions): Promise<void>;
|
|
36
|
+
export {};
|
|
@@ -1,31 +1,65 @@
|
|
|
1
|
-
import { exec as _exec, execSync } from 'node:child_process';
|
|
1
|
+
import { exec as _exec, execFile as _execFile, execSync, } from 'node:child_process';
|
|
2
2
|
import { promisify } from 'util';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import os from 'os';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import fs from 'fs-extra';
|
|
3
7
|
import open from 'open';
|
|
4
|
-
import { logger } from '../../utils/logging.js';
|
|
8
|
+
import { logger, uiLink } from '../../utils/logging.js';
|
|
5
9
|
import { EXIT_CODES } from '../../utils/process.js';
|
|
6
10
|
import { gitCleanup } from '../../utils/git.js';
|
|
7
11
|
import { publish, updateNextTag, updatePreviousTag } from '../../utils/npm.js';
|
|
8
|
-
import { confirm } from '../../utils/prompting.js';
|
|
9
|
-
import { TAG, INCREMENT, PRERELEASE_IDENTIFIER, } from '../../constants/release.js';
|
|
12
|
+
import { confirm, select } from '../../utils/prompting.js';
|
|
13
|
+
import { TAG, INCREMENT, PRERELEASE_IDENTIFIER, EXPERIMENTAL_VERSION_ARTIFACT, LATEST_INCREMENT_OPTIONS, VERSION_INCREMENT_OPTIONS, } from '../../constants/release.js';
|
|
10
14
|
import { ReleaseError } from './errors.js';
|
|
11
15
|
const exec = promisify(_exec);
|
|
12
|
-
|
|
16
|
+
const execFile = promisify(_execFile);
|
|
17
|
+
const WORKFLOW_RUN_POLL_ATTEMPTS = 10;
|
|
18
|
+
const WORKFLOW_RUN_POLL_INTERVAL_MS = 3000;
|
|
19
|
+
const WORKFLOW_RUN_MAX_STATUS_ERRORS = 5;
|
|
20
|
+
export async function resolveVersionIncrement(tag, versionIncrement, skipPrompts) {
|
|
21
|
+
if (versionIncrement) {
|
|
22
|
+
return versionIncrement;
|
|
23
|
+
}
|
|
24
|
+
const choices = tag === TAG.NEXT ? VERSION_INCREMENT_OPTIONS : LATEST_INCREMENT_OPTIONS;
|
|
25
|
+
if (skipPrompts) {
|
|
26
|
+
throw new ReleaseError(`No version increment given. Pass -v <${choices.join('|')}> when using --yes.`);
|
|
27
|
+
}
|
|
28
|
+
return select({
|
|
29
|
+
message: `Select the version increment for the ${tag} release:`,
|
|
30
|
+
choices: choices.map(choice => ({ name: choice, value: choice })),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export async function confirmRelease({ isDryRun, currentVersion, currentVersionLabel = 'Current version', lastExperimentalVersion, newVersion, tag, localVersion, versionIncrement, skipPrompts, }) {
|
|
13
34
|
if (isDryRun) {
|
|
14
35
|
logger.log('\nDRY RUN');
|
|
15
36
|
}
|
|
16
|
-
logger.log(`\
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
if (
|
|
22
|
-
|
|
37
|
+
logger.log(`\n${currentVersionLabel}: ${currentVersion}`);
|
|
38
|
+
if (lastExperimentalVersion) {
|
|
39
|
+
logger.log(`Last experimental release: ${lastExperimentalVersion}`);
|
|
40
|
+
}
|
|
41
|
+
logger.log(`New version to release: ${newVersion}\n`);
|
|
42
|
+
if (skipPrompts) {
|
|
43
|
+
logger.log(`Releasing version ${newVersion} on tag ${tag} (--yes)`);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const shouldRelease = await confirm({
|
|
47
|
+
message: `Release version ${newVersion} on tag ${tag}?`,
|
|
48
|
+
});
|
|
49
|
+
if (!shouldRelease) {
|
|
50
|
+
throw new ReleaseError('', EXIT_CODES.SUCCESS);
|
|
51
|
+
}
|
|
23
52
|
}
|
|
24
53
|
if (tag !== TAG.LATEST || localVersion.includes(PRERELEASE_IDENTIFIER.NEXT)) {
|
|
25
54
|
return;
|
|
26
55
|
}
|
|
56
|
+
const betaWarning = `The current changes have not yet been released in beta. It's recommended to release and test all changes on the ${TAG.NEXT} tag before releasing them to ${TAG.LATEST}.`;
|
|
57
|
+
if (skipPrompts) {
|
|
58
|
+
logger.warn(`${betaWarning} Continuing because --yes was passed.`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
27
61
|
const proceed = await confirm({
|
|
28
|
-
message:
|
|
62
|
+
message: `${betaWarning} Are you sure you want to proceed?`,
|
|
29
63
|
default: false,
|
|
30
64
|
});
|
|
31
65
|
if (!proceed) {
|
|
@@ -50,9 +84,9 @@ export async function createTempBranch(branch, mainBranch, previousBranch, isDry
|
|
|
50
84
|
logger.log(`Creating a temporary branch '${tempBranch}' for the release`);
|
|
51
85
|
await exec(`git checkout -b ${tempBranch}`);
|
|
52
86
|
}
|
|
53
|
-
export async function bumpVersion(newVersion,
|
|
87
|
+
export async function bumpVersion(newVersion, skipGitTag) {
|
|
54
88
|
logger.log(`\nUpdating version to ${newVersion}...`);
|
|
55
|
-
if (
|
|
89
|
+
if (skipGitTag) {
|
|
56
90
|
await exec(`yarn version --no-git-tag-version --new-version ${newVersion}`);
|
|
57
91
|
}
|
|
58
92
|
else {
|
|
@@ -71,7 +105,7 @@ export async function tryBuild({ build, newVersion, isExperimental, onTempBranch
|
|
|
71
105
|
throw new ReleaseError('');
|
|
72
106
|
}
|
|
73
107
|
}
|
|
74
|
-
export async function ensureNpmAuth(isDryRun, registry) {
|
|
108
|
+
export async function ensureNpmAuth(isDryRun, registry, skipPrompts) {
|
|
75
109
|
if (isDryRun) {
|
|
76
110
|
return;
|
|
77
111
|
}
|
|
@@ -79,6 +113,9 @@ export async function ensureNpmAuth(isDryRun, registry) {
|
|
|
79
113
|
await exec(`npm whoami --registry ${registry}`);
|
|
80
114
|
}
|
|
81
115
|
catch (_err) {
|
|
116
|
+
if (skipPrompts) {
|
|
117
|
+
throw new ReleaseError(`Not logged in to npm on ${registry}. Run \`npm login --registry ${registry}\` and re-run the release, or re-run without --yes to log in interactively.`);
|
|
118
|
+
}
|
|
82
119
|
try {
|
|
83
120
|
execSync(`npm adduser --registry ${registry}`, { stdio: 'inherit' });
|
|
84
121
|
}
|
|
@@ -87,6 +124,303 @@ export async function ensureNpmAuth(isDryRun, registry) {
|
|
|
87
124
|
}
|
|
88
125
|
}
|
|
89
126
|
}
|
|
127
|
+
export async function ensureCleanWorkingTree() {
|
|
128
|
+
const { stdout } = await exec('git status --porcelain');
|
|
129
|
+
if (stdout.trim()) {
|
|
130
|
+
throw new ReleaseError(`Working tree has uncommitted changes. Commit or stash them before releasing, because the release rolls back with \`git checkout .\` and would discard them:\n${stdout.trimEnd()}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function getGitHost(repositoryUrl) {
|
|
134
|
+
try {
|
|
135
|
+
return new URL(repositoryUrl).hostname || undefined;
|
|
136
|
+
}
|
|
137
|
+
catch (_e) {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
export async function ensureGhCli(repositoryUrl) {
|
|
142
|
+
try {
|
|
143
|
+
await exec('gh --version');
|
|
144
|
+
}
|
|
145
|
+
catch (_e) {
|
|
146
|
+
throw new ReleaseError('The GitHub CLI (gh) is required to open the release PR. Install it from https://cli.github.com, run `gh auth login`, and re-run the release.');
|
|
147
|
+
}
|
|
148
|
+
const host = getGitHost(repositoryUrl);
|
|
149
|
+
const hostArg = host ? ` --hostname ${host}` : '';
|
|
150
|
+
try {
|
|
151
|
+
await exec(`gh auth status${hostArg}`);
|
|
152
|
+
}
|
|
153
|
+
catch (_e) {
|
|
154
|
+
throw new ReleaseError(`The GitHub CLI (gh) is not logged in${host ? ` to ${host}` : ''}. Run \`gh auth login${hostArg}\` and re-run the release.`);
|
|
155
|
+
}
|
|
156
|
+
if (!host) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
await execFile('gh', ['repo', 'view', repositoryUrl, '--json', 'name']);
|
|
161
|
+
}
|
|
162
|
+
catch (_e) {
|
|
163
|
+
const account = await getGhAccount(host);
|
|
164
|
+
throw new ReleaseError(`Your active gh account${account ? ` (${account})` : ''} can't access ${repositoryUrl}. Switch to an account that can with \`gh auth switch --hostname ${host}\`, or run \`gh auth login --hostname ${host}\` to add one, then re-run the release.`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async function getGhAccount(host) {
|
|
168
|
+
try {
|
|
169
|
+
const { stdout } = await execFile('gh', [
|
|
170
|
+
'api',
|
|
171
|
+
'user',
|
|
172
|
+
'--hostname',
|
|
173
|
+
host,
|
|
174
|
+
'--jq',
|
|
175
|
+
'.login',
|
|
176
|
+
]);
|
|
177
|
+
return stdout.trim() || undefined;
|
|
178
|
+
}
|
|
179
|
+
catch (_e) {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
export async function ensureBranchPushed(branch) {
|
|
184
|
+
const pushHint = `Push your branch first: git push -u origin ${branch}`;
|
|
185
|
+
try {
|
|
186
|
+
await exec(`git fetch origin ${branch}`);
|
|
187
|
+
}
|
|
188
|
+
catch (_e) {
|
|
189
|
+
throw new ReleaseError(`Could not fetch origin/${branch}. ${pushHint}`);
|
|
190
|
+
}
|
|
191
|
+
const { stdout: localSha } = await exec('git rev-parse HEAD');
|
|
192
|
+
const { stdout: remoteSha } = await exec(`git rev-parse origin/${branch}`);
|
|
193
|
+
if (localSha.trim() !== remoteSha.trim()) {
|
|
194
|
+
throw new ReleaseError(`Local HEAD (${localSha.trim()}) does not match origin/${branch} (${remoteSha.trim()}). ${pushHint}`);
|
|
195
|
+
}
|
|
196
|
+
return localSha.trim();
|
|
197
|
+
}
|
|
198
|
+
async function listWorkflowRuns(workflow) {
|
|
199
|
+
const { stdout } = await execFile('gh', [
|
|
200
|
+
'run',
|
|
201
|
+
'list',
|
|
202
|
+
'--workflow',
|
|
203
|
+
workflow,
|
|
204
|
+
'--event',
|
|
205
|
+
'workflow_dispatch',
|
|
206
|
+
'--json',
|
|
207
|
+
'databaseId,displayTitle,url',
|
|
208
|
+
'-L',
|
|
209
|
+
'20',
|
|
210
|
+
]);
|
|
211
|
+
return JSON.parse(stdout);
|
|
212
|
+
}
|
|
213
|
+
export async function findWorkflowRun(workflow, sha, existingRunIds, attempts = WORKFLOW_RUN_POLL_ATTEMPTS, intervalMs = WORKFLOW_RUN_POLL_INTERVAL_MS) {
|
|
214
|
+
for (let attempt = 0; attempt < attempts; attempt++) {
|
|
215
|
+
if (attempt > 0) {
|
|
216
|
+
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
|
217
|
+
}
|
|
218
|
+
const runs = await listWorkflowRuns(workflow);
|
|
219
|
+
const run = runs.find(({ databaseId, displayTitle }) => !existingRunIds.has(databaseId) && displayTitle.includes(sha));
|
|
220
|
+
if (run) {
|
|
221
|
+
return run;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return undefined;
|
|
225
|
+
}
|
|
226
|
+
async function getWorkflowRunStatus(runId) {
|
|
227
|
+
const { stdout } = await execFile('gh', [
|
|
228
|
+
'run',
|
|
229
|
+
'view',
|
|
230
|
+
String(runId),
|
|
231
|
+
'--json',
|
|
232
|
+
'status,conclusion,jobs',
|
|
233
|
+
]);
|
|
234
|
+
return JSON.parse(stdout);
|
|
235
|
+
}
|
|
236
|
+
function formatJob({ name, conclusion }) {
|
|
237
|
+
if (conclusion === 'success') {
|
|
238
|
+
return `${chalk.green('✓')} ${name}`;
|
|
239
|
+
}
|
|
240
|
+
if (conclusion === 'skipped') {
|
|
241
|
+
return chalk.dim(`- ${name} (skipped)`);
|
|
242
|
+
}
|
|
243
|
+
return `${chalk.red('✗')} ${name} (${conclusion})`;
|
|
244
|
+
}
|
|
245
|
+
export async function watchWorkflowRun(runId, intervalMs = WORKFLOW_RUN_POLL_INTERVAL_MS, maxStatusErrors = WORKFLOW_RUN_MAX_STATUS_ERRORS) {
|
|
246
|
+
const reportedJobs = new Set();
|
|
247
|
+
let statusErrors = 0;
|
|
248
|
+
for (let attempt = 0;; attempt++) {
|
|
249
|
+
if (attempt > 0) {
|
|
250
|
+
await new Promise(resolve => setTimeout(resolve, intervalMs));
|
|
251
|
+
}
|
|
252
|
+
let run;
|
|
253
|
+
try {
|
|
254
|
+
run = await getWorkflowRunStatus(runId);
|
|
255
|
+
statusErrors = 0;
|
|
256
|
+
}
|
|
257
|
+
catch (e) {
|
|
258
|
+
statusErrors++;
|
|
259
|
+
if (statusErrors >= maxStatusErrors) {
|
|
260
|
+
throw e;
|
|
261
|
+
}
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
for (const job of run.jobs) {
|
|
265
|
+
if (job.status === 'completed' && !reportedJobs.has(job.name)) {
|
|
266
|
+
reportedJobs.add(job.name);
|
|
267
|
+
logger.log(formatJob(job));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (run.status === 'completed') {
|
|
271
|
+
return run;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
async function readExperimentalVersion(runId) {
|
|
276
|
+
const downloadDir = await fs.mkdtemp(path.join(os.tmpdir(), 'experimental-version-'));
|
|
277
|
+
try {
|
|
278
|
+
await execFile('gh', [
|
|
279
|
+
'run',
|
|
280
|
+
'download',
|
|
281
|
+
String(runId),
|
|
282
|
+
'-n',
|
|
283
|
+
EXPERIMENTAL_VERSION_ARTIFACT,
|
|
284
|
+
'-D',
|
|
285
|
+
downloadDir,
|
|
286
|
+
]);
|
|
287
|
+
const version = await fs.readFile(path.join(downloadDir, 'version.txt'), 'utf8');
|
|
288
|
+
return version.trim();
|
|
289
|
+
}
|
|
290
|
+
finally {
|
|
291
|
+
await fs.remove(downloadDir);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
export async function runExperimentalWorkflow({ workflow, mainBranch, branch, sha, packageName, isDryRun, }) {
|
|
295
|
+
const dispatchArgs = [
|
|
296
|
+
'workflow',
|
|
297
|
+
'run',
|
|
298
|
+
workflow,
|
|
299
|
+
'--ref',
|
|
300
|
+
mainBranch,
|
|
301
|
+
'-f',
|
|
302
|
+
`branch=${branch}`,
|
|
303
|
+
'-f',
|
|
304
|
+
`sha=${sha}`,
|
|
305
|
+
];
|
|
306
|
+
if (isDryRun) {
|
|
307
|
+
logger.log('\nDry run: skipping workflow dispatch. Would run:');
|
|
308
|
+
logger.log(` gh ${dispatchArgs.join(' ')}`);
|
|
309
|
+
logger.log('Dry run release finished successfully');
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
let existingRunIds;
|
|
313
|
+
try {
|
|
314
|
+
existingRunIds = new Set((await listWorkflowRuns(workflow)).map(({ databaseId }) => databaseId));
|
|
315
|
+
}
|
|
316
|
+
catch (e) {
|
|
317
|
+
logger.error(e);
|
|
318
|
+
throw new ReleaseError(`Failed to list runs of ${workflow}. Check that the workflow exists on ${mainBranch} and that your gh account can read Actions runs in this repository.`);
|
|
319
|
+
}
|
|
320
|
+
logger.log('\nInitiating release via GitHub Actions...');
|
|
321
|
+
try {
|
|
322
|
+
await execFile('gh', dispatchArgs);
|
|
323
|
+
}
|
|
324
|
+
catch (e) {
|
|
325
|
+
logger.error(e);
|
|
326
|
+
throw new ReleaseError(`Failed to dispatch ${workflow}. Check that the workflow exists on ${mainBranch} and that your gh account can run workflows in this repository.`);
|
|
327
|
+
}
|
|
328
|
+
const run = await findWorkflowRun(workflow, sha, existingRunIds);
|
|
329
|
+
if (!run) {
|
|
330
|
+
throw new ReleaseError(`Dispatched ${workflow}, but couldn't find its run. Look for "Experimental release ${branch}@${sha}" with: gh run list --workflow ${workflow}`);
|
|
331
|
+
}
|
|
332
|
+
logger.log(uiLink('View workflow in GitHub', run.url));
|
|
333
|
+
let result;
|
|
334
|
+
try {
|
|
335
|
+
result = await watchWorkflowRun(run.databaseId);
|
|
336
|
+
}
|
|
337
|
+
catch (e) {
|
|
338
|
+
logger.error(e);
|
|
339
|
+
throw new ReleaseError(`Couldn't read the status of the experimental release workflow. Check it at ${run.url}`);
|
|
340
|
+
}
|
|
341
|
+
if (result.conclusion !== 'success') {
|
|
342
|
+
const failedJob = result.jobs.find(({ conclusion }) => conclusion && conclusion !== 'success' && conclusion !== 'skipped');
|
|
343
|
+
throw new ReleaseError(`The experimental release workflow ${failedJob ? `failed in the ${failedJob.name} job` : `finished with '${result.conclusion}'`}. See ${run.url}`);
|
|
344
|
+
}
|
|
345
|
+
logger.log('Workflow completed successfully');
|
|
346
|
+
let version;
|
|
347
|
+
try {
|
|
348
|
+
version = await readExperimentalVersion(run.databaseId);
|
|
349
|
+
}
|
|
350
|
+
catch (e) {
|
|
351
|
+
logger.error(e);
|
|
352
|
+
logger.warn(`The workflow succeeded, but the ${EXPERIMENTAL_VERSION_ARTIFACT} artifact could not be read. Check the published version at ${run.url}`);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
logger.success(`Published ${packageName}@${version} on the experimental tag`);
|
|
356
|
+
logger.log(`Install it with: yarn add ${packageName}@${version}`);
|
|
357
|
+
}
|
|
358
|
+
export async function openReleasePr({ tag, newVersion, isDryRun, baseBranch, repositoryUrl, headBranch, onTempBranch, tempBranch, originalBranch, }) {
|
|
359
|
+
const title = `chore: release v${newVersion}`;
|
|
360
|
+
const body = `Merging this PR publishes v${newVersion} to npm on the ${tag} tag. The publish runs in CI after the merge.`;
|
|
361
|
+
const prArgs = [
|
|
362
|
+
'pr',
|
|
363
|
+
'create',
|
|
364
|
+
'--base',
|
|
365
|
+
baseBranch,
|
|
366
|
+
'--head',
|
|
367
|
+
headBranch,
|
|
368
|
+
'--title',
|
|
369
|
+
title,
|
|
370
|
+
'--body',
|
|
371
|
+
body,
|
|
372
|
+
];
|
|
373
|
+
let committed = false;
|
|
374
|
+
let pushed = false;
|
|
375
|
+
try {
|
|
376
|
+
await bumpVersion(newVersion, true);
|
|
377
|
+
if (isDryRun) {
|
|
378
|
+
logger.log('\nDry run: skipping commit, push, and PR creation. Would run:');
|
|
379
|
+
logger.log(` git commit -m "${title}"`);
|
|
380
|
+
logger.log(` git push -u origin ${headBranch}`);
|
|
381
|
+
logger.log(` gh pr create --base ${baseBranch} --head ${headBranch} --title "${title}" --body "${body}"`);
|
|
382
|
+
await gitCleanup(newVersion, false);
|
|
383
|
+
logger.log('Dry run release finished successfully');
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
await exec('git add package.json');
|
|
387
|
+
await exec(`git commit -m "${title}"`);
|
|
388
|
+
committed = true;
|
|
389
|
+
logger.log(`\nPushing ${headBranch} to Github...`);
|
|
390
|
+
await exec(`git push -u origin ${headBranch}`);
|
|
391
|
+
pushed = true;
|
|
392
|
+
logger.log('Opening release PR...');
|
|
393
|
+
const { stdout } = await execFile('gh', prArgs);
|
|
394
|
+
logger.success(`Release PR opened: ${stdout.trim()}`);
|
|
395
|
+
logger.log(`Merging it publishes v${newVersion} to npm on the ${tag} tag from CI.`);
|
|
396
|
+
}
|
|
397
|
+
catch (e) {
|
|
398
|
+
logger.error(e);
|
|
399
|
+
if (pushed) {
|
|
400
|
+
logger.error(`Branch '${headBranch}' was pushed but the PR could not be created. Open it manually at ${repositoryUrl}/compare/${baseBranch}...${headBranch}, or delete the remote branch with \`git push origin --delete ${headBranch}\` before re-running the release.`);
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
logger.error('Failed to open the release PR. Cleaning up...');
|
|
404
|
+
}
|
|
405
|
+
try {
|
|
406
|
+
if (committed) {
|
|
407
|
+
await exec('git reset HEAD~');
|
|
408
|
+
}
|
|
409
|
+
await gitCleanup(newVersion, false, onTempBranch ? tempBranch : undefined, onTempBranch ? originalBranch : undefined);
|
|
410
|
+
}
|
|
411
|
+
catch (cleanupError) {
|
|
412
|
+
logger.error(cleanupError);
|
|
413
|
+
const recoveryCommands = [
|
|
414
|
+
committed ? 'git reset HEAD~' : undefined,
|
|
415
|
+
'git checkout .',
|
|
416
|
+
onTempBranch ? `git checkout ${originalBranch}` : undefined,
|
|
417
|
+
onTempBranch ? `git branch -D ${tempBranch}` : undefined,
|
|
418
|
+
].filter(Boolean);
|
|
419
|
+
logger.error(`Automatic cleanup failed. To restore your repository, run: ${recoveryCommands.join(' && ')}`);
|
|
420
|
+
}
|
|
421
|
+
throw new ReleaseError('');
|
|
422
|
+
}
|
|
423
|
+
}
|
|
90
424
|
export async function tryPublish({ tag, isDryRun, ignoreScripts, registry, newVersion, isExperimental, onTempBranch, tempBranch, originalBranch, }) {
|
|
91
425
|
try {
|
|
92
426
|
await publish(tag, isDryRun, ignoreScripts, registry);
|
|
@@ -21,17 +21,18 @@ export type VersionSyncOptions = {
|
|
|
21
21
|
export type ComputeNewVersionOptions = {
|
|
22
22
|
tag: Tag;
|
|
23
23
|
versionIncrement: string;
|
|
24
|
-
isExperimental: boolean;
|
|
25
|
-
distTags: DistTags;
|
|
26
24
|
localVersion: string;
|
|
27
25
|
};
|
|
28
26
|
export type ConfirmReleaseOptions = {
|
|
29
27
|
isDryRun: boolean;
|
|
30
28
|
currentVersion: string;
|
|
29
|
+
currentVersionLabel?: string;
|
|
30
|
+
lastExperimentalVersion?: string;
|
|
31
31
|
newVersion: string;
|
|
32
32
|
tag: Tag;
|
|
33
33
|
localVersion: string;
|
|
34
34
|
versionIncrement: string;
|
|
35
|
+
skipPrompts?: boolean;
|
|
35
36
|
};
|
|
36
37
|
export type TryBuildOptions = {
|
|
37
38
|
build?: () => Promise<void> | void;
|
|
@@ -84,3 +85,22 @@ export type LogReleaseSuccessOptions = {
|
|
|
84
85
|
dryRun: boolean;
|
|
85
86
|
}) => Promise<void> | void;
|
|
86
87
|
};
|
|
88
|
+
export type RunExperimentalWorkflowOptions = {
|
|
89
|
+
workflow: string;
|
|
90
|
+
mainBranch: string;
|
|
91
|
+
branch: string;
|
|
92
|
+
sha: string;
|
|
93
|
+
packageName: string;
|
|
94
|
+
isDryRun: boolean;
|
|
95
|
+
};
|
|
96
|
+
export type OpenReleasePrOptions = {
|
|
97
|
+
tag: Tag;
|
|
98
|
+
newVersion: string;
|
|
99
|
+
isDryRun: boolean;
|
|
100
|
+
baseBranch: string;
|
|
101
|
+
repositoryUrl: string;
|
|
102
|
+
headBranch: string;
|
|
103
|
+
onTempBranch: boolean;
|
|
104
|
+
tempBranch: string;
|
|
105
|
+
originalBranch: string;
|
|
106
|
+
};
|
|
@@ -5,3 +5,6 @@ export declare function validateBranchForTag({ isExperimental, isPrevious, branc
|
|
|
5
5
|
export declare function validateTagIncrementCombination(tag: Tag, versionIncrement: string): void;
|
|
6
6
|
export declare function validateVersionSync({ isExperimental, isPrevious, localVersion, distTags, }: VersionSyncOptions): void;
|
|
7
7
|
export declare function validateNextReleaseIsNotOverIncrement({ tag, versionIncrement, localVersion, distTags, }: NextReleaseIncrementOptions): void;
|
|
8
|
+
export declare function rejectVersionIncrement(argv: {
|
|
9
|
+
versionIncrement?: unknown;
|
|
10
|
+
}): true;
|
|
@@ -80,3 +80,9 @@ export function validateNextReleaseIsNotOverIncrement({ tag, versionIncrement, l
|
|
|
80
80
|
`Using "${versionIncrement}" would skip ${localVersion.split('-')[0]} on the latest tag. ` +
|
|
81
81
|
`Use "prerelease" to continue iterating on this beta, or use a higher-level increment (e.g. "major" over a minor beta).`);
|
|
82
82
|
}
|
|
83
|
+
export function rejectVersionIncrement(argv) {
|
|
84
|
+
if (argv.versionIncrement !== undefined) {
|
|
85
|
+
throw new Error(`Experimental releases don't take -v: the version comes from the current ${TAG.LATEST} dist-tag.`);
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import type { ComputeNewVersionOptions } from './types.js';
|
|
2
|
-
export declare function computeNewVersion({ tag, versionIncrement,
|
|
2
|
+
export declare function computeNewVersion({ tag, versionIncrement, localVersion, }: ComputeNewVersionOptions): string;
|
|
3
|
+
export declare function computeExperimentalVersion(latestVersion: string, publishedVersions: string[]): string;
|