From ac8406c8428227a1ee45c2b4606cc09eae6f44c5 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 1 Apr 2022 13:34:42 +0800 Subject: [PATCH] fix(cli): prevent crash if GITHUB_REPOSITORY is not specified --- cli/src/pre-publish.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cli/src/pre-publish.ts b/cli/src/pre-publish.ts index bc4bb633..6d8d9045 100644 --- a/cli/src/pre-publish.ts +++ b/cli/src/pre-publish.ts @@ -77,7 +77,7 @@ export class PrePublishCommand extends Command { cwd: pkgDir, env: process.env, }) - if (!this.skipGHRelease) { + if (!this.skipGHRelease && repo && owner) { debug( `Start upload [${chalk.greenBright( dstPath, @@ -85,14 +85,14 @@ export class PrePublishCommand extends Command { ) try { const releaseInfo = await octokit!.repos.getReleaseByTag({ - repo: repo!, - owner: owner!, - tag: pkgInfo.tag!, + repo: repo, + owner: owner, + tag: pkgInfo.tag, }) const dstFileStats = statSync(dstPath) const assetInfo = await octokit!.repos.uploadReleaseAsset({ - owner: owner!, - repo: repo!, + owner: owner, + repo: repo, name: filename, release_id: releaseInfo.data.id, mediaType: { format: 'raw' }, @@ -135,9 +135,16 @@ export class PrePublishCommand extends Command { const headCommit = (await spawn('git log -1 --pretty=%B')) .toString('utf8') .trim() - - debug(`Github repository: ${process.env.GITHUB_REPOSITORY}`) - const [owner, repo] = process.env.GITHUB_REPOSITORY!.split('/') + const { GITHUB_REPOSITORY } = process.env + if (!GITHUB_REPOSITORY) { + 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({ auth: process.env.GITHUB_TOKEN, })