From 659bea3b74c109f37fc28231bb672d8148ae3018 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Sat, 15 Jan 2022 16:41:08 +0100 Subject: [PATCH 1/2] refactor: use assert instead of if statements --- src/expo.ts | 9 +++------ src/github.ts | 15 +++++++-------- src/worker.ts | 23 ++++++++++------------- tests/github.test.ts | 4 ++-- tests/worker.test.ts | 4 ++-- 5 files changed, 24 insertions(+), 31 deletions(-) diff --git a/src/expo.ts b/src/expo.ts index dd9b197b..450d766b 100644 --- a/src/expo.ts +++ b/src/expo.ts @@ -1,6 +1,7 @@ import { info, exportVariable } from '@actions/core'; import { exec, getExecOutput } from '@actions/exec'; import { which } from '@actions/io'; +import { ok as assert } from 'assert'; import { URL } from 'url'; export type CliName = 'expo' | 'eas'; @@ -72,9 +73,7 @@ export async function projectInfo(dir: string): Promise { * Create a QR code for an update on project, with an optional release channel. */ export function projectQR(project: ProjectInfo, channel?: string): string { - if (!project.owner) { - throw new Error('Could not create a QR code for project without owner'); - } + assert(project.owner, 'Could not create a QR code for project without owner'); const url = new URL('https://qr.expo.dev/expo-go'); url.searchParams.append('owner', project.owner); @@ -90,9 +89,7 @@ export function projectQR(project: ProjectInfo, channel?: string): string { * Create a link for the project in Expo. */ export function projectLink(project: ProjectInfo, channel?: string): string { - if (!project.owner) { - throw new Error('Could not create a project link without owner'); - } + assert(project.owner, 'Could not create a QR code for project without owner'); const url = new URL(`https://expo.dev/@${project.owner}/${project.slug}`); if (channel) { diff --git a/src/github.ts b/src/github.ts index a4d42729..d8ae006f 100644 --- a/src/github.ts +++ b/src/github.ts @@ -1,4 +1,5 @@ import { getOctokit, context } from '@actions/github'; +import { ok as assert } from 'assert'; type IssueContext = typeof context['issue']; @@ -62,11 +63,8 @@ export async function createIssueComment(issue: IssueContext, comment: Comment) * This uses the 'GITHUB_TOKEN' environment variable. */ export function githubApi(): ReturnType { - const githubToken = process.env['GITHUB_TOKEN']; - if (!githubToken) { - throw new Error(`This step requires a 'GITHUB_TOKEN' environment variable to create comments.`); - } - return getOctokit(githubToken); + assert(process.env['GITHUB_TOKEN'], 'This step requires a GITHUB_TOKEN environment variable to create comments'); + return getOctokit(process.env['GITHUB_TOKEN']); } /** @@ -80,8 +78,9 @@ export function pullContext(): IssueContext { return { ...context.repo, number: Number(process.env['EXPO_TEST_GITHUB_PULL']) }; } - if (context.eventName !== 'pull_request') { - throw new Error(`Could not find the pull context, make sure to run this from a pull request.`); - } + assert( + context.eventName === 'pull_request', + 'Could not find the pull request context, make sure to run this from a pull_request triggered workflow' + ); return context.issue; } diff --git a/src/worker.ts b/src/worker.ts index 86adf962..8367a1c4 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -1,5 +1,6 @@ import { addPath, info, setFailed, warning } from '@actions/core'; import { exec } from '@actions/exec'; +import { ok as assert } from 'assert'; import os from 'os'; import path from 'path'; @@ -51,20 +52,16 @@ export async function patchWatchers(): Promise { } export function tempPath(name: string, version: string): string { - const temp = process.env['RUNNER_TEMP'] || ''; - if (!temp) { - throw new Error(`Could not resolve temporary path, 'RUNNER_TEMP' not defined.`); - } - - return path.join(temp, name, version, os.arch()); + assert(process.env['RUNNER_TEMP'], 'Could not resolve temporary path, RUNNER_TEMP not defined'); + return path.join(process.env['RUNNER_TEMP'], name, version, os.arch()); } +/** + * Get the package path to the tool cache. + * + * @see https://github.com/actions/toolkit/blob/daf8bb00606d37ee2431d9b1596b88513dcf9c59/packages/tool-cache/src/tool-cache.ts#L747-L749 + */ export function toolPath(name: string, version: string): string { - const toolCache = process.env['RUNNER_TOOL_CACHE'] || ''; - if (!toolCache) { - throw new Error(`Could not resolve the local tool cache, 'RUNNER_TOOL_CACHE' not defined.`); - } - - // https://github.com/actions/toolkit/blob/daf8bb00606d37ee2431d9b1596b88513dcf9c59/packages/tool-cache/src/tool-cache.ts#L747-L749 - return path.join(toolCache, name, version, os.arch()); + assert(process.env['RUNNER_TOOL_CACHE'], 'Could not resolve the local tool cache, RUNNER_TOOL_CACHE not defined'); + return path.join(process.env['RUNNER_TOOL_CACHE'], name, version, os.arch()); } diff --git a/tests/github.test.ts b/tests/github.test.ts index 7f49a0c1..843ea3f6 100644 --- a/tests/github.test.ts +++ b/tests/github.test.ts @@ -10,7 +10,7 @@ describe(githubApi, () => { it('throws when GITHUB_TOKEN is undefined', () => { setEnv('GITHUB_TOKEN', ''); - expect(() => githubApi()).toThrow(`requires a 'GITHUB_TOKEN'`); + expect(() => githubApi()).toThrow(`requires a GITHUB_TOKEN`); }); it('returns an octokit instance', () => { @@ -25,7 +25,7 @@ describe(githubApi, () => { describe(pullContext, () => { it('throws when github context event is not a pull request', () => { jest.mocked(github.context).eventName = 'push'; - expect(() => pullContext()).toThrow('Could not find the pull context'); + expect(() => pullContext()).toThrow('Could not find the pull request context'); }); it('returns pull request context', () => { diff --git a/tests/worker.test.ts b/tests/worker.test.ts index e6374e25..676e3932 100644 --- a/tests/worker.test.ts +++ b/tests/worker.test.ts @@ -14,7 +14,7 @@ describe(tempPath, () => { it('throws when RUNNER_TEMP is undefined', () => { setEnv('RUNNER_TEMP', ''); - expect(tempPath).toThrow(`'RUNNER_TEMP' not defined`); + expect(tempPath).toThrow(`RUNNER_TEMP not defined`); }); it('returns path with name, version, and arch type', () => { @@ -28,7 +28,7 @@ describe(toolPath, () => { it('throws when RUNNER_TOOL_CACHE is undefined', () => { setEnv('RUNNER_TOOL_CACHE', ''); - expect(toolPath).toThrow(`'RUNNER_TOOL_CACHE' not defined`); + expect(toolPath).toThrow(`RUNNER_TOOL_CACHE not defined`); }); it('returns path with name, version, and arch type', () => { From cc9c7293134d09fc219136a3c18c01e27b9ed7f1 Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Sat, 15 Jan 2022 16:42:32 +0100 Subject: [PATCH 2/2] chore: rebuild project --- build/preview-comment/index.js | 42 ++++++++++++++-------------------- build/setup/index.js | 30 +++++++++++------------- 2 files changed, 30 insertions(+), 42 deletions(-) diff --git a/build/preview-comment/index.js b/build/preview-comment/index.js index b0dcd877..d732ef4e 100644 --- a/build/preview-comment/index.js +++ b/build/preview-comment/index.js @@ -13619,6 +13619,7 @@ exports.projectLink = exports.projectQR = exports.projectInfo = exports.projectO const core_1 = __nccwpck_require__(2186); const exec_1 = __nccwpck_require__(1514); const io_1 = __nccwpck_require__(7436); +const assert_1 = __nccwpck_require__(9491); const url_1 = __nccwpck_require__(7310); /** * Try to authenticate the user using either Expo or EAS CLI. @@ -13679,9 +13680,7 @@ exports.projectInfo = projectInfo; * Create a QR code for an update on project, with an optional release channel. */ function projectQR(project, channel) { - if (!project.owner) { - throw new Error('Could not create a QR code for project without owner'); - } + (0, assert_1.ok)(project.owner, 'Could not create a QR code for project without owner'); const url = new url_1.URL('https://qr.expo.dev/expo-go'); url.searchParams.append('owner', project.owner); url.searchParams.append('slug', project.slug); @@ -13695,9 +13694,7 @@ exports.projectQR = projectQR; * Create a link for the project in Expo. */ function projectLink(project, channel) { - if (!project.owner) { - throw new Error('Could not create a project link without owner'); - } + (0, assert_1.ok)(project.owner, 'Could not create a QR code for project without owner'); const url = new url_1.URL(`https://expo.dev/@${project.owner}/${project.slug}`); if (channel) { url.searchParams.append('release-channel', channel); @@ -13717,6 +13714,7 @@ exports.projectLink = projectLink; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.pullContext = exports.githubApi = exports.createIssueComment = exports.fetchIssueComment = void 0; const github_1 = __nccwpck_require__(5438); +const assert_1 = __nccwpck_require__(9491); /** * Determine if a comment exists on an issue or pull with the provided identifier. * This will iterate all comments received from GitHub, and try to exit early if it exists. @@ -13767,11 +13765,8 @@ exports.createIssueComment = createIssueComment; * This uses the 'GITHUB_TOKEN' environment variable. */ function githubApi() { - const githubToken = process.env['GITHUB_TOKEN']; - if (!githubToken) { - throw new Error(`This step requires a 'GITHUB_TOKEN' environment variable to create comments.`); - } - return (0, github_1.getOctokit)(githubToken); + (0, assert_1.ok)(process.env['GITHUB_TOKEN'], 'This step requires a GITHUB_TOKEN environment variable to create comments'); + return (0, github_1.getOctokit)(process.env['GITHUB_TOKEN']); } exports.githubApi = githubApi; /** @@ -13784,9 +13779,7 @@ function pullContext() { if (process.env['EXPO_TEST_GITHUB_PULL']) { return { ...github_1.context.repo, number: Number(process.env['EXPO_TEST_GITHUB_PULL']) }; } - if (github_1.context.eventName !== 'pull_request') { - throw new Error(`Could not find the pull context, make sure to run this from a pull request.`); - } + (0, assert_1.ok)(github_1.context.eventName === 'pull_request', 'Could not find the pull request context, make sure to run this from a pull_request triggered workflow'); return github_1.context.issue; } exports.pullContext = pullContext; @@ -13806,6 +13799,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.toolPath = exports.tempPath = exports.patchWatchers = exports.installToolFromPackage = exports.executeAction = exports.cacheTool = exports.findTool = void 0; const core_1 = __nccwpck_require__(2186); const exec_1 = __nccwpck_require__(1514); +const assert_1 = __nccwpck_require__(9491); const os_1 = __importDefault(__nccwpck_require__(2037)); const path_1 = __importDefault(__nccwpck_require__(1017)); var tool_cache_1 = __nccwpck_require__(7784); @@ -13857,20 +13851,18 @@ async function patchWatchers() { } exports.patchWatchers = patchWatchers; function tempPath(name, version) { - const temp = process.env['RUNNER_TEMP'] || ''; - if (!temp) { - throw new Error(`Could not resolve temporary path, 'RUNNER_TEMP' not defined.`); - } - return path_1.default.join(temp, name, version, os_1.default.arch()); + (0, assert_1.ok)(process.env['RUNNER_TEMP'], 'Could not resolve temporary path, RUNNER_TEMP not defined'); + return path_1.default.join(process.env['RUNNER_TEMP'], name, version, os_1.default.arch()); } exports.tempPath = tempPath; +/** + * Get the package path to the tool cache. + * + * @see https://github.com/actions/toolkit/blob/daf8bb00606d37ee2431d9b1596b88513dcf9c59/packages/tool-cache/src/tool-cache.ts#L747-L749 + */ function toolPath(name, version) { - const toolCache = process.env['RUNNER_TOOL_CACHE'] || ''; - if (!toolCache) { - throw new Error(`Could not resolve the local tool cache, 'RUNNER_TOOL_CACHE' not defined.`); - } - // https://github.com/actions/toolkit/blob/daf8bb00606d37ee2431d9b1596b88513dcf9c59/packages/tool-cache/src/tool-cache.ts#L747-L749 - return path_1.default.join(toolCache, name, version, os_1.default.arch()); + (0, assert_1.ok)(process.env['RUNNER_TOOL_CACHE'], 'Could not resolve the local tool cache, RUNNER_TOOL_CACHE not defined'); + return path_1.default.join(process.env['RUNNER_TOOL_CACHE'], name, version, os_1.default.arch()); } exports.toolPath = toolPath; diff --git a/build/setup/index.js b/build/setup/index.js index d2431125..d0a6d2a3 100644 --- a/build/setup/index.js +++ b/build/setup/index.js @@ -64699,6 +64699,7 @@ exports.projectLink = exports.projectQR = exports.projectInfo = exports.projectO const core_1 = __nccwpck_require__(2186); const exec_1 = __nccwpck_require__(1514); const io_1 = __nccwpck_require__(7436); +const assert_1 = __nccwpck_require__(9491); const url_1 = __nccwpck_require__(7310); /** * Try to authenticate the user using either Expo or EAS CLI. @@ -64759,9 +64760,7 @@ exports.projectInfo = projectInfo; * Create a QR code for an update on project, with an optional release channel. */ function projectQR(project, channel) { - if (!project.owner) { - throw new Error('Could not create a QR code for project without owner'); - } + (0, assert_1.ok)(project.owner, 'Could not create a QR code for project without owner'); const url = new url_1.URL('https://qr.expo.dev/expo-go'); url.searchParams.append('owner', project.owner); url.searchParams.append('slug', project.slug); @@ -64775,9 +64774,7 @@ exports.projectQR = projectQR; * Create a link for the project in Expo. */ function projectLink(project, channel) { - if (!project.owner) { - throw new Error('Could not create a project link without owner'); - } + (0, assert_1.ok)(project.owner, 'Could not create a QR code for project without owner'); const url = new url_1.URL(`https://expo.dev/@${project.owner}/${project.slug}`); if (channel) { url.searchParams.append('release-channel', channel); @@ -64848,6 +64845,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.toolPath = exports.tempPath = exports.patchWatchers = exports.installToolFromPackage = exports.executeAction = exports.cacheTool = exports.findTool = void 0; const core_1 = __nccwpck_require__(2186); const exec_1 = __nccwpck_require__(1514); +const assert_1 = __nccwpck_require__(9491); const os_1 = __importDefault(__nccwpck_require__(2037)); const path_1 = __importDefault(__nccwpck_require__(1017)); var tool_cache_1 = __nccwpck_require__(7784); @@ -64899,20 +64897,18 @@ async function patchWatchers() { } exports.patchWatchers = patchWatchers; function tempPath(name, version) { - const temp = process.env['RUNNER_TEMP'] || ''; - if (!temp) { - throw new Error(`Could not resolve temporary path, 'RUNNER_TEMP' not defined.`); - } - return path_1.default.join(temp, name, version, os_1.default.arch()); + (0, assert_1.ok)(process.env['RUNNER_TEMP'], 'Could not resolve temporary path, RUNNER_TEMP not defined'); + return path_1.default.join(process.env['RUNNER_TEMP'], name, version, os_1.default.arch()); } exports.tempPath = tempPath; +/** + * Get the package path to the tool cache. + * + * @see https://github.com/actions/toolkit/blob/daf8bb00606d37ee2431d9b1596b88513dcf9c59/packages/tool-cache/src/tool-cache.ts#L747-L749 + */ function toolPath(name, version) { - const toolCache = process.env['RUNNER_TOOL_CACHE'] || ''; - if (!toolCache) { - throw new Error(`Could not resolve the local tool cache, 'RUNNER_TOOL_CACHE' not defined.`); - } - // https://github.com/actions/toolkit/blob/daf8bb00606d37ee2431d9b1596b88513dcf9c59/packages/tool-cache/src/tool-cache.ts#L747-L749 - return path_1.default.join(toolCache, name, version, os_1.default.arch()); + (0, assert_1.ok)(process.env['RUNNER_TOOL_CACHE'], 'Could not resolve the local tool cache, RUNNER_TOOL_CACHE not defined'); + return path_1.default.join(process.env['RUNNER_TOOL_CACHE'], name, version, os_1.default.arch()); } exports.toolPath = toolPath;