@hubspot/npm-scripts 0.3.3 → 0.3.4-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.
- package/README.md +84 -11
- package/package.json +6 -3
- package/src/__tests__/release.test.js +483 -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__/git.test.js +57 -12
- package/src/utils/__tests__/npm.test.js +18 -1
- package/src/utils/__tests__/prompting.test.js +32 -1
- package/src/utils/git.js +32 -4
- package/src/utils/npm.d.ts +1 -0
- package/src/utils/npm.js +5 -0
- package/src/utils/prompting.d.ts +2 -0
- package/src/utils/prompting.js +10 -0
- package/src/utils/release/__tests__/steps.test.js +499 -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 +18 -4
- package/src/utils/release/steps.js +292 -14
- 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
package/src/release.js
CHANGED
|
@@ -2,20 +2,24 @@ import yargs from 'yargs';
|
|
|
2
2
|
import fs from 'fs-extra';
|
|
3
3
|
import { logger } from './utils/logging.js';
|
|
4
4
|
import { EXIT_CODES } from './utils/process.js';
|
|
5
|
-
import { DEFAULT_MAIN_BRANCH, TAG, TAG_OPTIONS, VERSION_INCREMENT_OPTIONS, } from './constants/release.js';
|
|
5
|
+
import { DEFAULT_EXPERIMENTAL_WORKFLOW, DEFAULT_MAIN_BRANCH, INCREMENT, LATEST_INCREMENT_OPTIONS, TAG, TAG_OPTIONS, VERSION_INCREMENT_OPTIONS, } from './constants/release.js';
|
|
6
6
|
import { getCurrentGitBranch } from './utils/git.js';
|
|
7
|
-
import { getDistTags } from './utils/npm.js';
|
|
7
|
+
import { getDistTags, getPublishedVersions } from './utils/npm.js';
|
|
8
8
|
import { ReleaseError } from './utils/release/errors.js';
|
|
9
|
-
import { validatePreviousBranchConfigured, validateBranchForTag, validateTagIncrementCombination, validateVersionSync, validateNextReleaseIsNotOverIncrement, } from './utils/release/validate.js';
|
|
10
|
-
import { computeNewVersion } from './utils/release/version.js';
|
|
11
|
-
import { confirmRelease, createTempBranch, bumpVersion, tryBuild, ensureNpmAuth, tryPublish, updateDistTagsForLatest, finalizeRelease, logReleaseSuccess, } from './utils/release/steps.js';
|
|
12
|
-
function buildHandler({ build, postLatestRelease, packageName, localVersion, registry, repositoryUrl, mainBranch, previousBranch, ignoreScripts, }) {
|
|
13
|
-
return async function handler({ versionIncrement, tag, dryRun, skipBranchValidation, skipVersionCheck, }) {
|
|
9
|
+
import { validatePreviousBranchConfigured, validateBranchForTag, validateTagIncrementCombination, validateVersionSync, validateNextReleaseIsNotOverIncrement, rejectVersionIncrement, } from './utils/release/validate.js';
|
|
10
|
+
import { computeExperimentalVersion, computeNewVersion, } from './utils/release/version.js';
|
|
11
|
+
import { confirmRelease, createTempBranch, bumpVersion, tryBuild, ensureNpmAuth, tryPublish, updateDistTagsForLatest, finalizeRelease, logReleaseSuccess, ensureCleanWorkingTree, ensureGhCli, ensureBranchPushed, openReleasePr, resolveVersionIncrement, runExperimentalWorkflow, } from './utils/release/steps.js';
|
|
12
|
+
function buildHandler({ build, postLatestRelease, packageName, localVersion, registry, repositoryUrl, mainBranch, previousBranch, ignoreScripts, publishFromCI, experimentalWorkflow, }, fixedTag) {
|
|
13
|
+
return async function handler({ versionIncrement: versionIncrementArg, tag: tagArg, dryRun, skipBranchValidation, skipVersionCheck, yes, }) {
|
|
14
14
|
try {
|
|
15
|
+
const tag = (fixedTag ?? tagArg);
|
|
15
16
|
const branch = await getCurrentGitBranch();
|
|
16
17
|
const isExperimental = tag === TAG.EXPERIMENTAL;
|
|
17
18
|
const isPrevious = tag === TAG.PREVIOUS;
|
|
18
19
|
const isDryRun = Boolean(dryRun);
|
|
20
|
+
const skipPrompts = Boolean(yes);
|
|
21
|
+
const releaseViaPr = Boolean(publishFromCI) && !isExperimental;
|
|
22
|
+
const releaseViaWorkflow = Boolean(publishFromCI) && isExperimental;
|
|
19
23
|
const resolvedMainBranch = mainBranch ?? DEFAULT_MAIN_BRANCH;
|
|
20
24
|
validatePreviousBranchConfigured(isPrevious, previousBranch);
|
|
21
25
|
if (!skipBranchValidation) {
|
|
@@ -27,6 +31,9 @@ function buildHandler({ build, postLatestRelease, packageName, localVersion, reg
|
|
|
27
31
|
previousBranch,
|
|
28
32
|
});
|
|
29
33
|
}
|
|
34
|
+
const versionIncrement = isExperimental
|
|
35
|
+
? INCREMENT.PRERELEASE
|
|
36
|
+
: await resolveVersionIncrement(tag, versionIncrementArg, skipPrompts);
|
|
30
37
|
validateTagIncrementCombination(tag, versionIncrement);
|
|
31
38
|
const distTags = await getDistTags(packageName);
|
|
32
39
|
if (!skipVersionCheck) {
|
|
@@ -43,27 +50,67 @@ function buildHandler({ build, postLatestRelease, packageName, localVersion, reg
|
|
|
43
50
|
localVersion,
|
|
44
51
|
distTags,
|
|
45
52
|
});
|
|
46
|
-
const newVersion =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
const newVersion = isExperimental
|
|
54
|
+
? computeExperimentalVersion(distTags.latest, await getPublishedVersions(packageName))
|
|
55
|
+
: computeNewVersion({
|
|
56
|
+
tag,
|
|
57
|
+
versionIncrement,
|
|
58
|
+
localVersion,
|
|
59
|
+
});
|
|
53
60
|
const tempBranch = `v${newVersion}-publish`;
|
|
54
|
-
const currentVersion = isExperimental
|
|
55
|
-
? distTags.experimental
|
|
56
|
-
: localVersion;
|
|
61
|
+
const currentVersion = isExperimental ? distTags.latest : localVersion;
|
|
57
62
|
const onTempBranch = (branch === resolvedMainBranch || branch === previousBranch) &&
|
|
58
63
|
!isDryRun;
|
|
64
|
+
await ensureCleanWorkingTree();
|
|
65
|
+
if (releaseViaPr || releaseViaWorkflow) {
|
|
66
|
+
await ensureGhCli(repositoryUrl);
|
|
67
|
+
}
|
|
68
|
+
const headSha = releaseViaWorkflow
|
|
69
|
+
? await ensureBranchPushed(branch)
|
|
70
|
+
: undefined;
|
|
59
71
|
await confirmRelease({
|
|
60
72
|
isDryRun,
|
|
61
73
|
currentVersion,
|
|
74
|
+
currentVersionLabel: isExperimental
|
|
75
|
+
? `Current ${TAG.LATEST} version`
|
|
76
|
+
: undefined,
|
|
77
|
+
lastExperimentalVersion: isExperimental
|
|
78
|
+
? (distTags.experimental ?? 'none')
|
|
79
|
+
: undefined,
|
|
62
80
|
newVersion,
|
|
63
81
|
tag,
|
|
64
82
|
localVersion,
|
|
65
83
|
versionIncrement,
|
|
84
|
+
skipPrompts,
|
|
66
85
|
});
|
|
86
|
+
if (releaseViaWorkflow && headSha) {
|
|
87
|
+
await runExperimentalWorkflow({
|
|
88
|
+
workflow: experimentalWorkflow ?? DEFAULT_EXPERIMENTAL_WORKFLOW,
|
|
89
|
+
mainBranch: resolvedMainBranch,
|
|
90
|
+
branch,
|
|
91
|
+
sha: headSha,
|
|
92
|
+
packageName,
|
|
93
|
+
isDryRun,
|
|
94
|
+
});
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (releaseViaPr) {
|
|
98
|
+
await createTempBranch(branch, resolvedMainBranch, previousBranch, isDryRun, tempBranch);
|
|
99
|
+
await openReleasePr({
|
|
100
|
+
tag,
|
|
101
|
+
newVersion,
|
|
102
|
+
isDryRun,
|
|
103
|
+
baseBranch: isPrevious && previousBranch ? previousBranch : resolvedMainBranch,
|
|
104
|
+
repositoryUrl,
|
|
105
|
+
headBranch: branch === resolvedMainBranch || branch === previousBranch
|
|
106
|
+
? tempBranch
|
|
107
|
+
: branch,
|
|
108
|
+
onTempBranch,
|
|
109
|
+
tempBranch,
|
|
110
|
+
originalBranch: branch,
|
|
111
|
+
});
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
67
114
|
await createTempBranch(branch, resolvedMainBranch, previousBranch, isDryRun, tempBranch);
|
|
68
115
|
await bumpVersion(newVersion, isExperimental);
|
|
69
116
|
await tryBuild({
|
|
@@ -74,7 +121,7 @@ function buildHandler({ build, postLatestRelease, packageName, localVersion, reg
|
|
|
74
121
|
tempBranch,
|
|
75
122
|
originalBranch: branch,
|
|
76
123
|
});
|
|
77
|
-
await ensureNpmAuth(isDryRun, registry);
|
|
124
|
+
await ensureNpmAuth(isDryRun, registry, skipPrompts);
|
|
78
125
|
await tryPublish({
|
|
79
126
|
tag,
|
|
80
127
|
isDryRun,
|
|
@@ -127,6 +174,30 @@ function buildHandler({ build, postLatestRelease, packageName, localVersion, reg
|
|
|
127
174
|
}
|
|
128
175
|
};
|
|
129
176
|
}
|
|
177
|
+
const SHARED_OPTIONS = {
|
|
178
|
+
dryRun: {
|
|
179
|
+
alias: 'd',
|
|
180
|
+
describe: 'Run through the publish process without actually publishing',
|
|
181
|
+
default: false,
|
|
182
|
+
type: 'boolean',
|
|
183
|
+
},
|
|
184
|
+
skipBranchValidation: {
|
|
185
|
+
describe: 'Bypass the branch validation check',
|
|
186
|
+
default: false,
|
|
187
|
+
type: 'boolean',
|
|
188
|
+
},
|
|
189
|
+
skipVersionCheck: {
|
|
190
|
+
describe: 'Bypass checking that the local version matches the published version',
|
|
191
|
+
default: false,
|
|
192
|
+
type: 'boolean',
|
|
193
|
+
},
|
|
194
|
+
yes: {
|
|
195
|
+
alias: 'y',
|
|
196
|
+
describe: 'Skip all prompts, failing with instructions when input or npm login is needed',
|
|
197
|
+
default: false,
|
|
198
|
+
type: 'boolean',
|
|
199
|
+
},
|
|
200
|
+
};
|
|
130
201
|
async function builder(yargs) {
|
|
131
202
|
return yargs.options({
|
|
132
203
|
versionIncrement: {
|
|
@@ -141,23 +212,32 @@ async function builder(yargs) {
|
|
|
141
212
|
describe: 'Tag for the next release',
|
|
142
213
|
choices: TAG_OPTIONS,
|
|
143
214
|
},
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
215
|
+
...SHARED_OPTIONS,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
function buildIncrementBuilder(choices) {
|
|
219
|
+
return async (yargs) => yargs
|
|
220
|
+
.options({
|
|
221
|
+
versionIncrement: {
|
|
222
|
+
alias: 'v',
|
|
223
|
+
describe: 'SemVer increment type for the next release. Prompts when omitted.',
|
|
224
|
+
choices,
|
|
154
225
|
},
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
226
|
+
...SHARED_OPTIONS,
|
|
227
|
+
})
|
|
228
|
+
.strict();
|
|
229
|
+
}
|
|
230
|
+
async function experimentalBuilder(yargs) {
|
|
231
|
+
return yargs
|
|
232
|
+
.options({
|
|
233
|
+
versionIncrement: {
|
|
234
|
+
alias: 'v',
|
|
235
|
+
hidden: true,
|
|
159
236
|
},
|
|
160
|
-
|
|
237
|
+
...SHARED_OPTIONS,
|
|
238
|
+
})
|
|
239
|
+
.check(rejectVersionIncrement)
|
|
240
|
+
.strict();
|
|
161
241
|
}
|
|
162
242
|
export function buildReleaseScript({ packageJsonLocation, buildHandlerOptions, }) {
|
|
163
243
|
try {
|
|
@@ -165,17 +245,22 @@ export function buildReleaseScript({ packageJsonLocation, buildHandlerOptions, }
|
|
|
165
245
|
if (!packageJsonContents.publishConfig?.registry) {
|
|
166
246
|
logger.error(`publishConfig.registry must be defined in ${packageJsonLocation}`);
|
|
167
247
|
}
|
|
168
|
-
|
|
169
|
-
yargs(process.argv.slice(2))
|
|
170
|
-
.scriptName('yarn')
|
|
171
|
-
.usage('Hubspot npm release script')
|
|
172
|
-
.command('release', `Create a new npm release of the ${packageJsonContents.name} with the specified version and tag`, builder, buildHandler({
|
|
248
|
+
const handlerOptions = {
|
|
173
249
|
...buildHandlerOptions,
|
|
174
250
|
packageName: packageJsonContents.name,
|
|
175
251
|
localVersion: packageJsonContents.version,
|
|
176
252
|
registry: packageJsonContents.publishConfig?.registry,
|
|
177
253
|
mainBranch: buildHandlerOptions.mainBranch || DEFAULT_MAIN_BRANCH,
|
|
178
|
-
}
|
|
254
|
+
};
|
|
255
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
256
|
+
yargs(process.argv.slice(2))
|
|
257
|
+
.scriptName('yarn')
|
|
258
|
+
.usage('Hubspot npm release script')
|
|
259
|
+
.command('release', `Create a new npm release of the ${packageJsonContents.name} with the specified version and tag`, builder, buildHandler(handlerOptions))
|
|
260
|
+
.command(TAG.LATEST, `Release a new version of ${packageJsonContents.name} on the ${TAG.LATEST} tag`, buildIncrementBuilder(LATEST_INCREMENT_OPTIONS), buildHandler(handlerOptions, TAG.LATEST))
|
|
261
|
+
.command(TAG.NEXT, `Release a new beta version of ${packageJsonContents.name} on the ${TAG.NEXT} tag`, buildIncrementBuilder(VERSION_INCREMENT_OPTIONS), buildHandler(handlerOptions, TAG.NEXT))
|
|
262
|
+
.command(TAG.PREVIOUS, `Release a new version of ${packageJsonContents.name} on the ${TAG.PREVIOUS} tag from the previous branch`, buildIncrementBuilder(LATEST_INCREMENT_OPTIONS), buildHandler(handlerOptions, TAG.PREVIOUS))
|
|
263
|
+
.command(TAG.EXPERIMENTAL, `Release an experimental version of ${packageJsonContents.name} from the current branch`, experimentalBuilder, buildHandler(handlerOptions, TAG.EXPERIMENTAL))
|
|
179
264
|
.version(false)
|
|
180
265
|
.help().argv;
|
|
181
266
|
}
|
package/src/types.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { TAG, TAG_OPTIONS, VERSION_INCREMENT_OPTIONS } from './constants/release.js';
|
|
2
2
|
export type ReleaseArguments = {
|
|
3
|
-
versionIncrement
|
|
4
|
-
tag
|
|
3
|
+
versionIncrement?: (typeof VERSION_INCREMENT_OPTIONS)[number];
|
|
4
|
+
tag?: (typeof TAG_OPTIONS)[number];
|
|
5
5
|
dryRun: boolean;
|
|
6
6
|
skipBranchValidation: boolean;
|
|
7
7
|
skipVersionCheck: boolean;
|
|
8
|
+
yes: boolean;
|
|
8
9
|
};
|
|
9
10
|
export type RepoSyncArguments = {
|
|
10
11
|
version: string;
|
|
@@ -28,6 +29,8 @@ export interface BuildReleaseScriptBase {
|
|
|
28
29
|
ignoreScripts?: boolean;
|
|
29
30
|
mainBranch?: string;
|
|
30
31
|
previousBranch?: string;
|
|
32
|
+
publishFromCI?: boolean;
|
|
33
|
+
experimentalWorkflow?: string;
|
|
31
34
|
}
|
|
32
35
|
export interface BuildReleaseScriptOptions extends BuildReleaseScriptBase {
|
|
33
36
|
packageName: string;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
2
|
import { getCurrentGitBranch, gitCleanup, getGitEnvWithSshKey, cloneRepo, checkoutBranch, createBranch, commitChanges, pushBranch, } from '../git.js';
|
|
3
3
|
import { exec as _exec } from 'node:child_process';
|
|
4
|
+
import os from 'os';
|
|
5
|
+
import path from 'path';
|
|
4
6
|
import fs from 'fs-extra';
|
|
5
7
|
import { logger } from '../logging.js';
|
|
6
|
-
import {
|
|
8
|
+
import { select } from '../prompting.js';
|
|
7
9
|
vi.mock('util', () => ({
|
|
8
10
|
promisify: vi.fn(fn => fn),
|
|
9
11
|
}));
|
|
@@ -13,12 +15,15 @@ vi.mock('node:child_process', () => ({
|
|
|
13
15
|
vi.mock('fs-extra', () => ({
|
|
14
16
|
default: {
|
|
15
17
|
remove: vi.fn(),
|
|
18
|
+
readdir: vi.fn(),
|
|
16
19
|
},
|
|
17
20
|
remove: vi.fn(),
|
|
21
|
+
readdir: vi.fn(),
|
|
18
22
|
}));
|
|
19
23
|
vi.mock('../logging.js', () => ({
|
|
20
24
|
logger: {
|
|
21
25
|
log: vi.fn(),
|
|
26
|
+
warn: vi.fn(),
|
|
22
27
|
error: vi.fn(),
|
|
23
28
|
},
|
|
24
29
|
}));
|
|
@@ -29,7 +34,7 @@ vi.mock('../process.js', () => ({
|
|
|
29
34
|
},
|
|
30
35
|
}));
|
|
31
36
|
vi.mock('../prompting.js', () => ({
|
|
32
|
-
|
|
37
|
+
select: vi.fn(),
|
|
33
38
|
}));
|
|
34
39
|
const mockedExec = vi.mocked(_exec);
|
|
35
40
|
describe('git', () => {
|
|
@@ -97,11 +102,16 @@ describe('git', () => {
|
|
|
97
102
|
});
|
|
98
103
|
});
|
|
99
104
|
describe('getGitEnvWithSshKey', () => {
|
|
100
|
-
|
|
101
|
-
|
|
105
|
+
const sshDir = path.join(os.homedir(), '.ssh');
|
|
106
|
+
it('should return process.env without prompting when sshKeyPath is not provided and no .pub files exist', async () => {
|
|
107
|
+
vi.mocked(fs.readdir).mockResolvedValue([
|
|
108
|
+
'config',
|
|
109
|
+
'known_hosts',
|
|
110
|
+
]);
|
|
102
111
|
const result = await getGitEnvWithSshKey();
|
|
103
112
|
expect(result).toBe(process.env);
|
|
104
|
-
expect(
|
|
113
|
+
expect(select).not.toHaveBeenCalled();
|
|
114
|
+
expect(vi.mocked(logger.warn)).toHaveBeenCalledWith(expect.stringContaining('No SSH public keys'));
|
|
105
115
|
});
|
|
106
116
|
it('should return process.env with GIT_SSH_COMMAND when sshKeyPath is provided', async () => {
|
|
107
117
|
const result = await getGitEnvWithSshKey('/path/to/key');
|
|
@@ -109,17 +119,52 @@ describe('git', () => {
|
|
|
109
119
|
...process.env,
|
|
110
120
|
GIT_SSH_COMMAND: 'ssh -i /path/to/key -o IdentitiesOnly=yes',
|
|
111
121
|
});
|
|
112
|
-
expect(
|
|
122
|
+
expect(select).not.toHaveBeenCalled();
|
|
123
|
+
expect(fs.readdir).not.toHaveBeenCalled();
|
|
113
124
|
});
|
|
114
|
-
it('should prompt
|
|
115
|
-
vi.mocked(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
125
|
+
it('should prompt with the default option and the sorted .pub files in ~/.ssh', async () => {
|
|
126
|
+
vi.mocked(fs.readdir).mockResolvedValue([
|
|
127
|
+
'id_rsa',
|
|
128
|
+
'id_rsa.pub',
|
|
129
|
+
'known_hosts',
|
|
130
|
+
'config',
|
|
131
|
+
'id_ed25519.pub',
|
|
132
|
+
]);
|
|
133
|
+
vi.mocked(select).mockResolvedValue(path.join(sshDir, 'id_rsa'));
|
|
134
|
+
await getGitEnvWithSshKey();
|
|
135
|
+
expect(fs.readdir).toHaveBeenCalledWith(sshDir);
|
|
136
|
+
expect(select).toHaveBeenCalledWith({
|
|
137
|
+
message: 'Select the SSH key to use:',
|
|
138
|
+
choices: [
|
|
139
|
+
{ name: 'Default SSH configuration', value: undefined },
|
|
140
|
+
{
|
|
141
|
+
name: 'id_ed25519.pub',
|
|
142
|
+
value: path.join(sshDir, 'id_ed25519'),
|
|
143
|
+
},
|
|
144
|
+
{ name: 'id_rsa.pub', value: path.join(sshDir, 'id_rsa') },
|
|
145
|
+
],
|
|
119
146
|
});
|
|
147
|
+
});
|
|
148
|
+
it('should return process.env without prompting when ~/.ssh cannot be read', async () => {
|
|
149
|
+
vi.mocked(fs.readdir).mockRejectedValue(new Error('ENOENT'));
|
|
150
|
+
const result = await getGitEnvWithSshKey();
|
|
151
|
+
expect(result).toBe(process.env);
|
|
152
|
+
expect(select).not.toHaveBeenCalled();
|
|
153
|
+
expect(vi.mocked(logger.warn)).toHaveBeenCalledWith(expect.stringContaining('No SSH public keys'));
|
|
154
|
+
});
|
|
155
|
+
it('should return process.env when the user selects the default SSH configuration', async () => {
|
|
156
|
+
vi.mocked(fs.readdir).mockResolvedValue(['id_rsa.pub']);
|
|
157
|
+
vi.mocked(select).mockResolvedValue(undefined);
|
|
158
|
+
const result = await getGitEnvWithSshKey();
|
|
159
|
+
expect(result).toBe(process.env);
|
|
160
|
+
});
|
|
161
|
+
it('should set GIT_SSH_COMMAND with the private key path of the selected key', async () => {
|
|
162
|
+
vi.mocked(fs.readdir).mockResolvedValue(['id_rsa.pub']);
|
|
163
|
+
vi.mocked(select).mockResolvedValue(path.join(sshDir, 'id_rsa'));
|
|
164
|
+
const result = await getGitEnvWithSshKey();
|
|
120
165
|
expect(result).toEqual({
|
|
121
166
|
...process.env,
|
|
122
|
-
GIT_SSH_COMMAND:
|
|
167
|
+
GIT_SSH_COMMAND: `ssh -i ${path.join(sshDir, 'id_rsa')} -o IdentitiesOnly=yes`,
|
|
123
168
|
});
|
|
124
169
|
});
|
|
125
170
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { getDistTags, publish, updateDistTag, updateNextTag, updatePreviousTag, } from '../npm.js';
|
|
2
|
+
import { getDistTags, getPublishedVersions, publish, updateDistTag, updateNextTag, updatePreviousTag, } from '../npm.js';
|
|
3
3
|
import { exec as _exec, spawn } from 'node:child_process';
|
|
4
4
|
import { logger } from '../logging.js';
|
|
5
5
|
import { EXIT_CODES } from '../process.js';
|
|
@@ -29,6 +29,23 @@ describe('npm', () => {
|
|
|
29
29
|
beforeEach(() => {
|
|
30
30
|
vi.resetAllMocks();
|
|
31
31
|
});
|
|
32
|
+
describe('getPublishedVersions', () => {
|
|
33
|
+
it('should return the parsed versions array from npm view', async () => {
|
|
34
|
+
mockedExec.mockResolvedValue({
|
|
35
|
+
stdout: '["1.0.0","1.0.1-experimental.0"]\n',
|
|
36
|
+
stderr: '',
|
|
37
|
+
});
|
|
38
|
+
const result = await getPublishedVersions('@hubspot/test-package');
|
|
39
|
+
expect(mockedExec).toHaveBeenCalledWith('npm view @hubspot/test-package versions --json');
|
|
40
|
+
expect(result).toEqual(['1.0.0', '1.0.1-experimental.0']);
|
|
41
|
+
});
|
|
42
|
+
it('should wrap a single published version in an array', async () => {
|
|
43
|
+
mockedExec.mockResolvedValue({ stdout: '"1.0.0"', stderr: '' });
|
|
44
|
+
expect(await getPublishedVersions('@hubspot/test-package')).toEqual([
|
|
45
|
+
'1.0.0',
|
|
46
|
+
]);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
32
49
|
describe('getDistTags', () => {
|
|
33
50
|
it('should return parsed dist tags from npm view', async () => {
|
|
34
51
|
const mockDistTags = {
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { confirm, input, checkbox } from '../prompting.js';
|
|
2
|
+
import { confirm, input, checkbox, select } from '../prompting.js';
|
|
3
3
|
import { logger } from '../logging.js';
|
|
4
4
|
import _confirm from '@inquirer/confirm';
|
|
5
5
|
import _input from '@inquirer/input';
|
|
6
6
|
import _checkbox from '@inquirer/checkbox';
|
|
7
|
+
import _select from '@inquirer/select';
|
|
7
8
|
vi.mock('@inquirer/confirm', () => ({
|
|
8
9
|
default: vi.fn(),
|
|
9
10
|
}));
|
|
@@ -13,6 +14,9 @@ vi.mock('@inquirer/input', () => ({
|
|
|
13
14
|
vi.mock('@inquirer/checkbox', () => ({
|
|
14
15
|
default: vi.fn(),
|
|
15
16
|
}));
|
|
17
|
+
vi.mock('@inquirer/select', () => ({
|
|
18
|
+
default: vi.fn(),
|
|
19
|
+
}));
|
|
16
20
|
vi.mock('../logging.js', () => ({
|
|
17
21
|
logger: {
|
|
18
22
|
log: vi.fn(),
|
|
@@ -97,4 +101,31 @@ describe('prompting', () => {
|
|
|
97
101
|
exitSpy.mockRestore();
|
|
98
102
|
});
|
|
99
103
|
});
|
|
104
|
+
describe('select', () => {
|
|
105
|
+
it('should return the result from inquirer select', async () => {
|
|
106
|
+
const mockSelectFn = vi.mocked(_select);
|
|
107
|
+
mockSelectFn.mockResolvedValue('option1');
|
|
108
|
+
const result = await select({
|
|
109
|
+
message: 'Select an option:',
|
|
110
|
+
choices: [{ value: 'option1' }, { value: 'option2' }],
|
|
111
|
+
});
|
|
112
|
+
expect(result).toBe('option1');
|
|
113
|
+
expect(mockSelectFn).toHaveBeenCalledWith({
|
|
114
|
+
message: 'Select an option:',
|
|
115
|
+
choices: [{ value: 'option1' }, { value: 'option2' }],
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
it('should exit on ExitPromptError', async () => {
|
|
119
|
+
const mockSelectFn = vi.mocked(_select);
|
|
120
|
+
const exitError = new Error('User exited');
|
|
121
|
+
exitError.name = 'ExitPromptError';
|
|
122
|
+
mockSelectFn.mockRejectedValue(exitError);
|
|
123
|
+
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => {
|
|
124
|
+
throw new Error('process.exit called');
|
|
125
|
+
});
|
|
126
|
+
await expect(select({ message: 'Test', choices: [{ value: 'option1' }] })).rejects.toThrow('process.exit called');
|
|
127
|
+
expect(vi.mocked(logger.log)).toHaveBeenCalledWith('Exiting...');
|
|
128
|
+
exitSpy.mockRestore();
|
|
129
|
+
});
|
|
130
|
+
});
|
|
100
131
|
});
|
package/src/utils/git.js
CHANGED
|
@@ -1,11 +1,41 @@
|
|
|
1
|
+
import os from 'os';
|
|
1
2
|
import { promisify } from 'util';
|
|
2
3
|
import path from 'path';
|
|
3
4
|
import fs from 'fs-extra';
|
|
4
5
|
import { exec as _exec } from 'node:child_process';
|
|
5
6
|
import { logger } from './logging.js';
|
|
6
7
|
import { EXIT_CODES } from './process.js';
|
|
7
|
-
import {
|
|
8
|
+
import { select } from './prompting.js';
|
|
8
9
|
const exec = promisify(_exec);
|
|
10
|
+
const SSH_DIR = path.join(os.homedir(), '.ssh');
|
|
11
|
+
const PUBLIC_KEY_EXTENSION = '.pub';
|
|
12
|
+
const DEFAULT_SSH_CONFIG_LABEL = 'Default SSH configuration';
|
|
13
|
+
async function getSshPublicKeyFiles() {
|
|
14
|
+
try {
|
|
15
|
+
const files = await fs.readdir(SSH_DIR);
|
|
16
|
+
return files.filter(file => file.endsWith(PUBLIC_KEY_EXTENSION)).sort();
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async function promptForSshKeyPath() {
|
|
23
|
+
const publicKeyFiles = await getSshPublicKeyFiles();
|
|
24
|
+
if (publicKeyFiles.length === 0) {
|
|
25
|
+
logger.warn(`No SSH public keys (*${PUBLIC_KEY_EXTENSION}) found in ${SSH_DIR}. Using the default SSH configuration.`);
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
return select({
|
|
29
|
+
message: 'Select the SSH key to use:',
|
|
30
|
+
choices: [
|
|
31
|
+
{ name: DEFAULT_SSH_CONFIG_LABEL, value: undefined },
|
|
32
|
+
...publicKeyFiles.map(file => ({
|
|
33
|
+
name: file,
|
|
34
|
+
value: path.join(SSH_DIR, file.slice(0, -PUBLIC_KEY_EXTENSION.length)),
|
|
35
|
+
})),
|
|
36
|
+
],
|
|
37
|
+
});
|
|
38
|
+
}
|
|
9
39
|
function getRepoPartsFromUrl(repoUrl) {
|
|
10
40
|
// Strip protocol if present, e.g. "https://git.mycorp.com/Org/repo-name" -> "git.mycorp.com/Org/repo-name"
|
|
11
41
|
const withoutProtocol = repoUrl.trim().replace(/^[a-z]+:\/\//i, '');
|
|
@@ -34,9 +64,7 @@ export async function gitCleanup(newVersion, deleteTag, tempBranch, originalBran
|
|
|
34
64
|
}
|
|
35
65
|
export async function getGitEnvWithSshKey(sshKeyPath) {
|
|
36
66
|
if (!sshKeyPath) {
|
|
37
|
-
sshKeyPath = await
|
|
38
|
-
message: 'Enter the SSH key path to use (leave blank to use the default):',
|
|
39
|
-
});
|
|
67
|
+
sshKeyPath = await promptForSshKeyPath();
|
|
40
68
|
if (!sshKeyPath) {
|
|
41
69
|
return process.env;
|
|
42
70
|
}
|
package/src/utils/npm.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { DistTags, Tag } from '../types.js';
|
|
2
2
|
export declare function getDistTags(packageName: string): Promise<DistTags>;
|
|
3
|
+
export declare function getPublishedVersions(packageName: string): Promise<string[]>;
|
|
3
4
|
export declare function publish(tag: Tag, isDryRun: boolean, ignoreScripts: boolean, registry: string): Promise<void>;
|
|
4
5
|
export declare function updateDistTag(packageName: string, version: string, tag: string, isDryRun: boolean, registry: string): Promise<void>;
|
|
5
6
|
export declare function updateNextTag(packageName: string, newVersion: string, isDryRun: boolean, registry: string): Promise<void>;
|
package/src/utils/npm.js
CHANGED
|
@@ -9,6 +9,11 @@ export async function getDistTags(packageName) {
|
|
|
9
9
|
const distTags = stdout.trim();
|
|
10
10
|
return JSON.parse(distTags);
|
|
11
11
|
}
|
|
12
|
+
export async function getPublishedVersions(packageName) {
|
|
13
|
+
const { stdout } = await exec(`npm view ${packageName} versions --json`);
|
|
14
|
+
const versions = JSON.parse(stdout.trim());
|
|
15
|
+
return Array.isArray(versions) ? versions : [versions];
|
|
16
|
+
}
|
|
12
17
|
export async function publish(tag, isDryRun, ignoreScripts, registry) {
|
|
13
18
|
logger.log(`\nPublishing to ${tag}...`);
|
|
14
19
|
logger.log('-'.repeat(50));
|
package/src/utils/prompting.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import _confirm from '@inquirer/confirm';
|
|
2
2
|
import _input from '@inquirer/input';
|
|
3
3
|
import _checkbox from '@inquirer/checkbox';
|
|
4
|
+
import _select from '@inquirer/select';
|
|
4
5
|
export declare function confirm(...args: Parameters<typeof _confirm>): Promise<ReturnType<typeof _confirm>>;
|
|
5
6
|
export declare function input(...args: Parameters<typeof _input>): Promise<ReturnType<typeof _input>>;
|
|
6
7
|
export declare function checkbox(...args: Parameters<typeof _checkbox>): Promise<ReturnType<typeof _checkbox>>;
|
|
8
|
+
export declare function select<Value>(...args: Parameters<typeof _select<Value>>): Promise<ReturnType<typeof _select<Value>>>;
|
package/src/utils/prompting.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import _confirm from '@inquirer/confirm';
|
|
2
2
|
import _input from '@inquirer/input';
|
|
3
3
|
import _checkbox from '@inquirer/checkbox';
|
|
4
|
+
import _select from '@inquirer/select';
|
|
4
5
|
import { EXIT_CODES } from './process.js';
|
|
5
6
|
import { logger } from './logging.js';
|
|
6
7
|
function exitIfPromptError(e) {
|
|
@@ -36,3 +37,12 @@ export async function checkbox(...args) {
|
|
|
36
37
|
throw e;
|
|
37
38
|
}
|
|
38
39
|
}
|
|
40
|
+
export async function select(...args) {
|
|
41
|
+
try {
|
|
42
|
+
return await _select(...args);
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
exitIfPromptError(e);
|
|
46
|
+
throw e;
|
|
47
|
+
}
|
|
48
|
+
}
|