Skip to content

Commit

Permalink
refactor: use assert instead of if statements (#150)
Browse files Browse the repository at this point in the history
* refactor: use assert instead of if statements

* chore: rebuild project
  • Loading branch information
byCedric authored Jan 15, 2022
1 parent 4648212 commit e4ef6e6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 73 deletions.
42 changes: 17 additions & 25 deletions build/preview-comment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -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;
/**
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
30 changes: 13 additions & 17 deletions build/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
9 changes: 3 additions & 6 deletions src/expo.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -72,9 +73,7 @@ export async function projectInfo(dir: string): Promise<ProjectInfo> {
* 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);
Expand All @@ -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) {
Expand Down
15 changes: 7 additions & 8 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getOctokit, context } from '@actions/github';
import { ok as assert } from 'assert';

type IssueContext = typeof context['issue'];

Expand Down Expand Up @@ -62,11 +63,8 @@ export async function createIssueComment(issue: IssueContext, comment: Comment)
* This uses the 'GITHUB_TOKEN' environment variable.
*/
export function githubApi(): ReturnType<typeof getOctokit> {
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']);
}

/**
Expand All @@ -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;
}
23 changes: 10 additions & 13 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -51,20 +52,16 @@ export async function patchWatchers(): Promise<void> {
}

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());
}
4 changes: 2 additions & 2 deletions tests/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down

0 comments on commit e4ef6e6

Please sign in to comment.