1
0
Fork 0

Use readFileSync instead of require (#101)

* Use readFileSync instead of require

* error handling
pull/110/head
Danny McCormick 2019-08-30 13:02:45 -04:00 committed by GitHub
parent ac36ca4405
commit 99d3ad0a64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 5 deletions

View File

@ -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