fix(cli): prevent crash if GITHUB_REPOSITORY is not specified

This commit is contained in:
LongYinan 2022-04-01 13:34:42 +08:00
parent bd08787270
commit ac8406c842
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7

View file

@ -77,7 +77,7 @@ export class PrePublishCommand extends Command {
cwd: pkgDir, cwd: pkgDir,
env: process.env, env: process.env,
}) })
if (!this.skipGHRelease) { if (!this.skipGHRelease && repo && owner) {
debug( debug(
`Start upload [${chalk.greenBright( `Start upload [${chalk.greenBright(
dstPath, dstPath,
@ -85,14 +85,14 @@ export class PrePublishCommand extends Command {
) )
try { try {
const releaseInfo = await octokit!.repos.getReleaseByTag({ const releaseInfo = await octokit!.repos.getReleaseByTag({
repo: repo!, repo: repo,
owner: owner!, owner: owner,
tag: pkgInfo.tag!, tag: pkgInfo.tag,
}) })
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: releaseInfo.data.id,
mediaType: { format: 'raw' }, mediaType: { format: 'raw' },
@ -135,9 +135,16 @@ export class PrePublishCommand extends Command {
const headCommit = (await spawn('git log -1 --pretty=%B')) const headCommit = (await spawn('git log -1 --pretty=%B'))
.toString('utf8') .toString('utf8')
.trim() .trim()
const { GITHUB_REPOSITORY } = process.env
debug(`Github repository: ${process.env.GITHUB_REPOSITORY}`) if (!GITHUB_REPOSITORY) {
const [owner, repo] = process.env.GITHUB_REPOSITORY!.split('/') return {
owner: null,
repo: null,
pkgInfo: { name: null, version: null, tag: null },
}
}
debug(`Github repository: ${GITHUB_REPOSITORY}`)
const [owner, repo] = GITHUB_REPOSITORY.split('/')
const octokit = new Octokit({ const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN, auth: process.env.GITHUB_TOKEN,
}) })