Skip to content

Commit 0f8bf0b

Browse files
committed
ci(release): Improve handling release channel and enable providing it as an option
1 parent 9c2264c commit 0f8bf0b

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

tools/scripts/release/cli.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import { parseCSV } from 'nx/src/command-line/yargs-utils/shared-options';
33
import chalk from 'chalk';
44
import { printHeader } from '../utils/output';
55
import { execaSync } from 'execa';
6+
import { ReleaseChannel, releaseChannelPreid } from './release.consts';
7+
import { ReleasePreidValue } from './release.types';
68

79
export function parseReleaseCliOptions() {
810
return yargs
911
.version(false) // don't use the default meaning of version in yargs
10-
.option('version', {
11-
description: 'Explicit version specifier to use, if overriding conventional commits',
12+
.option('channel', {
13+
alias: 'c',
14+
description: 'Explicit channel specifier to use when not deploying based on branch',
1215
type: 'string',
16+
choices: Object.values(ReleaseChannel),
1317
})
1418
.option('dryRun', {
1519
alias: 'd',
@@ -40,18 +44,42 @@ export function parseReleaseCliOptions() {
4044
.parseAsync();
4145
}
4246

43-
export function getOptionsBasedOnBranch(): { isPrerelease: boolean; preid?: string; tag: string } {
44-
// Get current branch
45-
const { stdout: branch } = execaSync('git', ['branch', '--show-current']);
47+
export function parseReleaseOptions({ channel }: { channel?: ReleaseChannel }): {
48+
isPrerelease: boolean;
49+
preid?: string;
50+
tag: ReleaseChannel;
51+
} {
52+
let isPrerelease: boolean;
53+
let preid: ReleasePreidValue;
54+
let tag: ReleaseChannel;
4655

47-
// Determine options based on branch
48-
const isPrerelease = branch !== 'main';
49-
const preid: string | undefined = isPrerelease ? 'preview' : undefined;
50-
const tag: string = isPrerelease ? 'preview' : 'latest';
56+
let selectedChannel: ReleaseChannel;
5157

52-
// Output which branch is used
53-
console.log(printHeader('branch', 'blueBright'), `Detecting current git branch\n`);
54-
console.log(`${chalk.blueBright(branch)} 🔀 Using "${chalk.yellow(preid ?? '[empty]')}" preid\n`);
58+
console.log(printHeader('channel', 'blueBright'), `Detecting release channel...\n`);
59+
60+
if (!channel) {
61+
// Auto determine based on branch
62+
const { stdout: branch } = execaSync('git', ['branch', '--show-current']);
63+
console.log(`🚫 No channel specified, using current git branch ${chalk.blueBright(branch)}`);
64+
selectedChannel = branch === 'main' ? ReleaseChannel.Latest : ReleaseChannel.Preview;
65+
} else {
66+
selectedChannel = channel;
67+
}
68+
69+
console.log(`🔀 ${chalk.yellow(selectedChannel)} channel selected for release\n`);
70+
71+
switch (selectedChannel) {
72+
case ReleaseChannel.Latest:
73+
isPrerelease = false;
74+
break;
75+
case ReleaseChannel.Preview:
76+
default:
77+
isPrerelease = true;
78+
break;
79+
}
80+
81+
preid = releaseChannelPreid[selectedChannel];
82+
tag = selectedChannel;
5583

5684
return {
5785
isPrerelease,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ReleasePreidValue } from './release.types';
2+
3+
export enum ReleaseChannel {
4+
Preview = 'preview',
5+
Latest = 'latest',
6+
}
7+
8+
export const releaseChannelPreid = {
9+
[ReleaseChannel.Preview]: 'preview',
10+
[ReleaseChannel.Latest]: undefined,
11+
} satisfies Record<ReleaseChannel, ReleasePreidValue>;

tools/scripts/release/release.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import chalk from 'chalk';
44
import { createProjectGraphAsync } from 'nx/src/project-graph/project-graph';
55
import { printHeader } from '../utils/output';
66
import { syncPackageJson } from './publish';
7-
import { getOptionsBasedOnBranch, parseReleaseCliOptions } from './cli';
7+
import { parseReleaseOptions, parseReleaseCliOptions } from './cli';
88
import { handlePrereleaseVersioning, handleRegularReleaseVersioning } from './version';
99
import { handlePrereleaseChangelog, handleRegularReleaseChangelog } from './changelog';
1010

1111
(async () => {
1212
const options = await parseReleaseCliOptions();
1313
const graph = await createProjectGraphAsync({ exitOnError: true });
14-
const { tag, preid, isPrerelease } = getOptionsBasedOnBranch();
14+
const { tag, preid, isPrerelease } = parseReleaseOptions(options);
1515

1616
const releaseEnabled = process.env.RELEASE_ENABLED === '1' || process.env.RELEASE_ENABLED === 'true';
1717

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type ReleasePreidValue = string | undefined;

0 commit comments

Comments
 (0)