From 99d3ad0a6473e7e7906681627a450150a772ee1d Mon Sep 17 00:00:00 2001 From: Danny McCormick Date: Fri, 30 Aug 2019 13:02:45 -0400 Subject: [PATCH] Use readFileSync instead of require (#101) * Use readFileSync instead of require * error handling --- packages/github/src/context.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/github/src/context.ts b/packages/github/src/context.ts index b4230426..a06a8e0a 100644 --- a/packages/github/src/context.ts +++ b/packages/github/src/context.ts @@ -1,7 +1,7 @@ // Originally pulled from https://github.com/JasonEtco/actions-toolkit/blob/master/src/context.ts import {WebhookPayload} from './interfaces' - -/* eslint-disable @typescript-eslint/no-require-imports */ +import {readFileSync, existsSync} from 'fs' +import {EOL} from 'os' export class Context { /** @@ -20,9 +20,20 @@ export class Context { * Hydrate the context from the environment */ constructor() { - this.payload = process.env.GITHUB_EVENT_PATH - ? require(process.env.GITHUB_EVENT_PATH) - : {} + this.payload = {} + if (process.env.GITHUB_EVENT_PATH) { + if (existsSync(process.env.GITHUB_EVENT_PATH)) { + this.payload = JSON.parse( + readFileSync(process.env.GITHUB_EVENT_PATH, {encoding: 'utf8'}) + ) + } else { + process.stdout.write( + `GITHUB_EVENT_PATH ${ + process.env.GITHUB_EVENT_PATH + } does not exist${EOL}` + ) + } + } this.eventName = process.env.GITHUB_EVENT_NAME as string this.sha = process.env.GITHUB_SHA as string this.ref = process.env.GITHUB_REF as string