Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 23 additions & 47 deletions scripts/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import prompts from 'prompts';
import semver from 'semver';
import colors from 'picocolors';
import {
args,
getLatestTag,
getPackageInfo,
getTagSha,
getVersionChoices,
isDryRun,
logRecentCommits,
Expand All @@ -18,48 +18,39 @@ import {
} from './releaseUtils.js';

(async () => {
let targetVersion: string | undefined;

const { currentVersion, pkg, pkgPath, pkgDir } = await getPackageInfo();
const { currentVersion, pkgPath, pkgDir } = await getPackageInfo();

const packageName = pkg.name;
const latestTag = await getLatestTag();
if (latestTag) {
const sha = await getTagSha(latestTag);
await logRecentCommits(latestTag, sha);
}

await logRecentCommits(packageName);
const { release }: { release: string } = await prompts({
type: 'select',
name: 'release',
message: 'Select release type',
choices: getVersionChoices(currentVersion),
});

if (!targetVersion) {
const { release }: { release: string } = await prompts({
type: 'select',
name: 'release',
message: 'Select release type',
choices: getVersionChoices(currentVersion),
let targetVersion = release;
if (release === 'custom') {
const { version }: { version: string } = await prompts({
type: 'text',
name: 'version',
message: 'Input custom version',
initial: currentVersion,
});

if (release === 'custom') {
const res: { version: string } = await prompts({
type: 'text',
name: 'version',
message: 'Input custom version',
initial: currentVersion,
});
targetVersion = res.version;
} else {
targetVersion = release;
}
targetVersion = version;
}

if (!semver.valid(targetVersion)) {
console.error(`invalid target version: ${targetVersion}`);
process.exit(1);
}

const tag = `${packageName}@${targetVersion}`;

if (targetVersion.includes('beta') && !args.tag) {
args.tag = 'beta';
}
if (targetVersion.includes('alpha') && !args.tag) {
args.tag = 'alpha';
}
const tag = `v${targetVersion}`;

const { yes }: { yes: boolean } = await prompts({
type: 'confirm',
Expand All @@ -74,21 +65,6 @@ import {
step('\nUpdating package version...');
updateVersion(pkgDir, pkgPath, targetVersion);

step('\nGenerating changelog...');
const latestTag = await getLatestTag(packageName);
if (!latestTag) {
step('\nNo previous tag, skipping changelog generation.');
} else {
const sha = (
await run('git', ['rev-list', '-n', '1', latestTag], {
stdio: 'pipe',
})
).stdout.trim();

const changelogArgs = ['generate-changelog', `${sha}..HEAD`];
await run('npx', changelogArgs, { cwd: pkgDir });
}

const { stdout } = await run('git', ['diff'], { stdio: 'pipe' });
if (stdout) {
step('\nCommitting changes...');
Expand All @@ -101,8 +77,8 @@ import {
}

step('\nPushing to GitHub...');
await runIfNotDry('git', ['push', 'origin', `refs/tags/${tag}`]);
await runIfNotDry('git', ['push']);
await runIfNotDry('git', ['push', 'origin', `refs/tags/${tag}`]);

if (isDryRun) {
console.log('\nDry run finished - run git diff to see package changes.');
Expand Down
24 changes: 11 additions & 13 deletions scripts/releaseUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,28 +163,26 @@ export function updateVersion(pkgDir: string, pkgPath: string, version: string):
execSync('npm install', { cwd: pkgDir, stdio: 'inherit' });
}

export async function getLatestTag(pkgName: string): Promise<string> {
const tags = (await run('git', ['tag'], { stdio: 'pipe' })).stdout
export async function getLatestTag(): Promise<string> {
return (await run('git', ['tag'], { stdio: 'pipe' })).stdout
.split(/\n/)
.filter(Boolean);
const prefix = `${pkgName}@`;
return tags
.filter((tag) => tag.startsWith(prefix))
.filter(Boolean)
.sort()
.reverse()[0];
}

export async function logRecentCommits(pkgName: string): Promise<void> {
const tag = await getLatestTag(pkgName);
if (!tag) return;
const sha = await run('git', ['rev-list', '-n', '1', tag], {
export async function getTagSha(tag: string): Promise<string> {
return await run('git', ['rev-list', '-n', '1', tag], {
stdio: 'pipe',
}).then((res) => res.stdout.trim());
}

export async function logRecentCommits(tag: string, sha: string): Promise<void> {
console.log(
colors.bold(
`\n${colors.blue('i')} Commits of ${colors.green(
pkgName
)} since ${colors.green(tag)} ${colors.gray(`(${sha.slice(0, 5)})`)}`
`\n${colors.blue('i')} Commits since ${colors.green(
tag,
)} ${colors.gray(`(${sha.slice(0, 5)})`)}`
)
);
await run(
Expand Down
13 changes: 2 additions & 11 deletions scripts/verifyPublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@ import { args, getPackageInfo } from './releaseUtils.js';
process.exit(1);
}

const versionIndex = tag.lastIndexOf('@');
const pkgName = tag.slice(0, versionIndex);
const version = tag.slice(versionIndex + 1);
const [, version] = tag.split('v');

const { pkg, currentVersion } = await getPackageInfo();
const packageName = pkg.name;
if (pkgName !== packageName) {
console.error(
`Package name from tag '${pkgName}' mismatches with package '${packageName}'`
);
process.exit(1);
}
const { currentVersion } = await getPackageInfo();
if (currentVersion !== version) {
console.error(
`Package version from tag '${version}' mismatches with current version '${currentVersion}'`
Expand Down
Loading