feat(cli): allow specifying an existing release (#1256)

This commit is contained in:
Amr Bashir 2022-08-04 06:12:18 +02:00 committed by GitHub
parent 94e8e54b38
commit b54e698237
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -39,6 +39,8 @@ export class PrePublishCommand extends Command {
ghReleaseName?: string = Option.String('--gh-release-name') ghReleaseName?: string = Option.String('--gh-release-name')
existingReleaseId?: string = Option.String('--gh-release-id')
async execute() { async execute() {
const { const {
packageJsonPath, packageJsonPath,
@ -62,10 +64,9 @@ export class PrePublishCommand extends Command {
}) })
} }
const { owner, repo, pkgInfo, octokit } = await this.createGhRelease( const { owner, repo, pkgInfo, octokit } = this.existingReleaseId
packageName, ? await this.getRepoInfo(packageName, version)
version, : await this.createGhRelease(packageName, version)
)
for (const platformDetail of platforms) { for (const platformDetail of platforms) {
const pkgDir = join( const pkgDir = join(
@ -92,17 +93,21 @@ export class PrePublishCommand extends Command {
)}] to Github release, [${chalk.greenBright(pkgInfo.tag)}]`, )}] to Github release, [${chalk.greenBright(pkgInfo.tag)}]`,
) )
try { try {
const releaseInfo = await octokit!.repos.getReleaseByTag({ const releaseId = this.existingReleaseId
? Number(this.existingReleaseId)
: (
await octokit!.repos.getReleaseByTag({
repo: repo, repo: repo,
owner: owner, owner: owner,
tag: pkgInfo.tag, tag: pkgInfo.tag,
}) })
).data.id
const dstFileStats = statSync(dstPath) const dstFileStats = statSync(dstPath)
const assetInfo = await octokit!.repos.uploadReleaseAsset({ const assetInfo = await octokit!.repos.uploadReleaseAsset({
owner: owner, owner: owner,
repo: repo, repo: repo,
name: filename, name: filename,
release_id: releaseInfo.data.id, release_id: releaseId,
mediaType: { format: 'raw' }, mediaType: { format: 'raw' },
headers: { headers: {
'content-length': dstFileStats.size, 'content-length': dstFileStats.size,
@ -140,6 +145,46 @@ export class PrePublishCommand extends Command {
pkgInfo: { name: null, version: null, tag: null }, pkgInfo: { name: null, version: null, tag: null },
} }
} }
const { repo, owner, pkgInfo, octokit } = await this.getRepoInfo(
packageName,
version,
)
if (!repo || !owner) {
return {
owner: null,
repo: null,
pkgInfo: { name: null, version: null, tag: null },
}
}
if (!this.isDryRun) {
try {
await octokit.repos.createRelease({
owner,
repo,
tag_name: pkgInfo.tag,
name: this.ghReleaseName,
prerelease:
version.includes('alpha') ||
version.includes('beta') ||
version.includes('rc'),
})
} catch (e) {
debug(
`Params: ${JSON.stringify(
{ owner, repo, tag_name: pkgInfo.tag },
null,
2,
)}`,
)
console.error(e)
}
}
return { owner, repo, pkgInfo, octokit }
}
private async getRepoInfo(packageName: string, version: string) {
const headCommit = (await spawn('git log -1 --pretty=%B')) const headCommit = (await spawn('git log -1 --pretty=%B'))
.toString('utf8') .toString('utf8')
.trim() .trim()
@ -179,29 +224,6 @@ export class PrePublishCommand extends Command {
name: packageName, name: packageName,
} }
} }
if (!this.isDryRun) {
try {
await octokit.repos.createRelease({
owner,
repo,
tag_name: pkgInfo.tag,
name: this.ghReleaseName,
prerelease:
version.includes('alpha') ||
version.includes('beta') ||
version.includes('rc'),
})
} catch (e) {
debug(
`Params: ${JSON.stringify(
{ owner, repo, tag_name: pkgInfo.tag },
null,
2,
)}`,
)
console.error(e)
}
}
return { owner, repo, pkgInfo, octokit } return { owner, repo, pkgInfo, octokit }
} }