@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/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 = computeNewVersion({
47
- tag,
48
- versionIncrement,
49
- isExperimental,
50
- distTags,
51
- localVersion,
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
- dryRun: {
145
- alias: 'd',
146
- describe: 'Run through the publish process without actually publishing',
147
- default: false,
148
- type: 'boolean',
149
- },
150
- skipBranchValidation: {
151
- describe: 'Bypass the branch validation check',
152
- default: false,
153
- type: 'boolean',
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
- skipVersionCheck: {
156
- describe: 'Bypass checking that the local version matches the published version',
157
- default: false,
158
- type: 'boolean',
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
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
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: (typeof VERSION_INCREMENT_OPTIONS)[number];
4
- tag: (typeof TAG_OPTIONS)[number];
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,5 +1,5 @@
1
1
  import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
- import { logger } from '../logging.js';
2
+ import { logger, uiLink } from '../logging.js';
3
3
  describe('logging', () => {
4
4
  const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
5
5
  beforeEach(() => {
@@ -61,4 +61,18 @@ describe('logging', () => {
61
61
  consoleTableSpy.mockRestore();
62
62
  });
63
63
  });
64
+ describe('uiLink', () => {
65
+ const originalIsTTY = process.stdout.isTTY;
66
+ afterEach(() => {
67
+ process.stdout.isTTY = originalIsTTY;
68
+ });
69
+ it('should return a terminal hyperlink when stdout is a TTY', () => {
70
+ process.stdout.isTTY = true;
71
+ expect(uiLink('View it', 'https://example.com')).toBe('\u001B]8;;https://example.com\u0007View it\u001B]8;;\u0007');
72
+ });
73
+ it('should include the URL as text when stdout is not a TTY', () => {
74
+ process.stdout.isTTY = false;
75
+ expect(uiLink('View it', 'https://example.com')).toBe('View it: https://example.com');
76
+ });
77
+ });
64
78
  });
@@ -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 = {
@@ -6,3 +6,4 @@ export declare const logger: {
6
6
  error(...args: unknown[]): void;
7
7
  table(...args: unknown[]): void;
8
8
  };
9
+ export declare function uiLink(text: string, url: string): string;
@@ -19,3 +19,9 @@ export const logger = {
19
19
  console.table(...args);
20
20
  },
21
21
  };
22
+ export function uiLink(text, url) {
23
+ if (!process.stdout.isTTY) {
24
+ return `${text}: ${url}`;
25
+ }
26
+ return `\u001B]8;;${url}\u0007${text}\u001B]8;;\u0007`;
27
+ }
@@ -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));