Skip to content

Commit f770dd3

Browse files
author
linyuan.yang
committed
release
1 parent ee4ab32 commit f770dd3

1 file changed

Lines changed: 61 additions & 6 deletions

File tree

‎scripts/release.js‎

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ const TARGETS = {
99
tauriConf: 'packages/app/src-tauri/tauri.conf.json',
1010
pkgJson: 'packages/app/package.json',
1111
companionPkgJsons: ['packages/vscode-extension/package.json'],
12+
overwriteExistingRelease: true,
1213
},
1314
sbot: {
1415
tagPrefix: 'sbot-v',
1516
pkgJson: 'packages/sbot/package.json',
17+
overwriteExistingRelease: true,
1618
releaseNotes: {
1719
en: 'packages/sbot/ReleaseNote.md',
1820
zh: 'packages/sbot/ReleaseNote.zh.md',
@@ -21,6 +23,7 @@ const TARGETS = {
2123
cli: {
2224
tagPrefix: 'cli-v',
2325
pkgJson: 'packages/cli/package.json',
26+
overwriteExistingRelease: true,
2427
},
2528
};
2629

@@ -41,6 +44,15 @@ function run(cmd, opts = {}) {
4144
return execSync(cmd, { stdio: 'inherit', ...opts });
4245
}
4346

47+
function commandSucceeds(cmd) {
48+
try {
49+
execSync(cmd, { stdio: 'ignore' });
50+
return true;
51+
} catch {
52+
return false;
53+
}
54+
}
55+
4456
function readJson(file) {
4557
return JSON.parse(fs.readFileSync(file, 'utf8'));
4658
}
@@ -62,11 +74,50 @@ function isWorkingTreeClean() {
6274
}
6375

6476
function tagExists(tag) {
65-
try {
66-
execSync(`git rev-parse -q --verify "refs/tags/${tag}"`, { stdio: 'ignore' });
67-
return true;
68-
} catch {
69-
return false;
77+
return commandSucceeds(`git rev-parse -q --verify "refs/tags/${tag}"`);
78+
}
79+
80+
function remoteTagExists(tag) {
81+
return commandSucceeds(`git ls-remote --exit-code origin "refs/tags/${tag}"`);
82+
}
83+
84+
function githubReleaseExists(tag) {
85+
return commandSucceeds(`gh release view "${tag}"`);
86+
}
87+
88+
function assertGithubCliReady() {
89+
if (!commandSucceeds('gh --version')) {
90+
console.error('error: GitHub CLI (gh) is required to delete an existing GitHub release');
91+
process.exit(1);
92+
}
93+
if (!commandSucceeds('gh auth status')) {
94+
console.error('error: GitHub CLI is not authenticated; run "gh auth login" before overwriting a release');
95+
process.exit(1);
96+
}
97+
}
98+
99+
function deleteExistingRelease(tag) {
100+
const hasLocalTag = tagExists(tag);
101+
const hasRemoteTag = remoteTagExists(tag);
102+
103+
if (hasLocalTag || hasRemoteTag) {
104+
assertGithubCliReady();
105+
if (githubReleaseExists(tag)) {
106+
run(`gh release delete "${tag}" --yes --cleanup-tag`);
107+
} else {
108+
console.log(`GitHub release "${tag}" not found`);
109+
}
110+
} else if (commandSucceeds('gh --version') && commandSucceeds('gh auth status') && githubReleaseExists(tag)) {
111+
run(`gh release delete "${tag}" --yes --cleanup-tag`);
112+
} else {
113+
return;
114+
}
115+
116+
if (remoteTagExists(tag)) {
117+
run(`git push origin --delete "${tag}"`);
118+
}
119+
if (tagExists(tag)) {
120+
run(`git tag -d "${tag}"`);
70121
}
71122
}
72123

@@ -145,7 +196,7 @@ function main() {
145196
if (notesChanged) console.log('notes : updated from ReleaseNote files');
146197
console.log('');
147198

148-
if (tagExists(tag)) {
199+
if (tagExists(tag) && !cfg.overwriteExistingRelease) {
149200
console.error(`error: tag "${tag}" already exists`);
150201
process.exit(1);
151202
}
@@ -192,6 +243,10 @@ function main() {
192243
run(`git commit -m "${commitMsg}"`);
193244
}
194245

246+
if (cfg.overwriteExistingRelease) {
247+
deleteExistingRelease(tag);
248+
}
249+
195250
run(`git tag -a "${tag}" -m "${target} v${nextVersion}"`);
196251
run('git push');
197252
run(`git push origin "${tag}"`);

0 commit comments

Comments
 (0)