From 245f959e3254ed90c7c7c25ce23dca632e529b43 Mon Sep 17 00:00:00 2001 From: Federico Grandi Date: Thu, 6 May 2021 10:28:43 +0200 Subject: [PATCH] fix: drop require --- .eslintrc.js | 1 + lib/index.js | 37 +++++++++++++++++++++---------------- src/main.ts | 6 ++++-- src/util.ts | 16 ++++++++++++++++ 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index bd00104..ce13a75 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -12,6 +12,7 @@ module.exports = { sourceType: 'module' }, plugins: ['@typescript-eslint'], + ignorePatterns: ['lib/*'], rules: { 'prettier/prettier': 'warn', 'no-cond-assign': [2, 'except-parens'], diff --git a/lib/index.js b/lib/index.js index ec76f82..4ced36a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6855,21 +6855,6 @@ module.exports = { }; -/***/ }), - -/***/ 875: -/***/ ((module) => { - -function webpackEmptyContext(req) { - var e = new Error("Cannot find module '" + req + "'"); - e.code = 'MODULE_NOT_FOUND'; - throw e; -} -webpackEmptyContext.keys = () => ([]); -webpackEmptyContext.resolve = webpackEmptyContext; -webpackEmptyContext.id = 875; -module.exports = webpackEmptyContext; - /***/ }), /***/ 3129: @@ -10845,8 +10830,12 @@ var jsYaml = { /* harmony default export */ const js_yaml = (jsYaml); +// EXTERNAL MODULE: external "fs" +var external_fs_ = __nccwpck_require__(5747); +var external_fs_default = /*#__PURE__*/__nccwpck_require__.n(external_fs_); ;// CONCATENATED MODULE: ./src/util.ts + const outputs = { committed: 'false', pushed: 'false', @@ -10881,6 +10870,21 @@ function parseBool(value) { } catch (_a) { } } +function readJSON(filePath) { + let fileContent; + try { + fileContent = external_fs_default().readFileSync(filePath, { encoding: 'utf8' }); + } + catch (_a) { + throw `Couldn't read file. File path: ${filePath}`; + } + try { + return JSON.parse(fileContent); + } + catch (_b) { + throw `Couldn't parse file to JSON. File path: ${filePath}`; + } +} function setOutput(name, value) { core.debug(`Setting output: ${name}=${value}`); outputs[name] = value; @@ -11045,7 +11049,8 @@ function checkInputs() { setInput(input, value); return getInput(input); } - const eventPath = process.env.GITHUB_EVENT_PATH, event = eventPath && __nccwpck_require__(875)(eventPath), isPR = (_a = process.env.GITHUB_EVENT_NAME) === null || _a === void 0 ? void 0 : _a.includes('pull_request'), defaultBranch = isPR + const eventPath = process.env.GITHUB_EVENT_PATH, event = eventPath && readJSON(eventPath); + const isPR = (_a = process.env.GITHUB_EVENT_NAME) === null || _a === void 0 ? void 0 : _a.includes('pull_request'), defaultBranch = isPR ? (_c = (_b = event === null || event === void 0 ? void 0 : event.pull_request) === null || _b === void 0 ? void 0 : _b.head) === null || _c === void 0 ? void 0 : _c.ref : (_d = process.env.GITHUB_REF) === null || _d === void 0 ? void 0 : _d.substring(11); // #region add, remove diff --git a/src/main.ts b/src/main.ts index 04ffde1..8242a21 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,6 +16,7 @@ import { matchGitArgs, outputs, parseBool, + readJSON, setOutput } from './util' @@ -187,8 +188,9 @@ async function checkInputs() { } const eventPath = process.env.GITHUB_EVENT_PATH, - event = eventPath && require(eventPath), - isPR = process.env.GITHUB_EVENT_NAME?.includes('pull_request'), + event = eventPath && readJSON(eventPath) + + const isPR = process.env.GITHUB_EVENT_NAME?.includes('pull_request'), defaultBranch = isPR ? (event?.pull_request?.head?.ref as string) : process.env.GITHUB_REF?.substring(11) diff --git a/src/util.ts b/src/util.ts index 040e15a..d7876c6 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,4 +1,5 @@ import * as core from '@actions/core' +import fs from 'fs' export type Input = | 'add' @@ -49,6 +50,21 @@ export function parseBool(value: any) { } catch {} } +export function readJSON(filePath: string) { + let fileContent: string + try { + fileContent = fs.readFileSync(filePath, { encoding: 'utf8' }) + } catch { + throw `Couldn't read file. File path: ${filePath}` + } + + try { + return JSON.parse(fileContent) + } catch { + throw `Couldn't parse file to JSON. File path: ${filePath}` + } +} + export function setOutput(name: Output, value: 'true' | 'false') { core.debug(`Setting output: ${name}=${value}`) outputs[name] = value