Repository: mheap/pin-github-action
Pull request #174 (2025-01-05 21:45:12)

Convert project to ESM


Patch:
diff --git a/.eslintrc b/.eslintrc
index 12a6cec..be002d9 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -1,7 +1,8 @@
 {
     "parserOptions": {
-        "ecmaVersion": 2018,
-        "impliedStrict": true
+        "ecmaVersion": 2023,
+        "impliedStrict": true,
+        "sourceType": "module"
     },
     "plugins": [
         "prettier"
diff --git a/bin.js b/bin.js
index 290620f..9ce5124 100755
--- a/bin.js
+++ b/bin.js
@@ -1,13 +1,16 @@
 #!/usr/bin/env node
 
-const fs = require("fs");
-const path = require("path");
-const program = require("commander");
-const debug = require("debug")("pin-github-action");
+import fs from "fs";
+import { program } from "commander";
+import debugLib from "debug";
+const debug = debugLib("pin-github-action");
 
-const run = require(".");
+import run from "./index.js";
+
+const packageDetails = JSON.parse(
+  fs.readFileSync(new URL("./package.json", import.meta.url))
+);
 
-const packageDetails = require(path.join(__dirname, "package.json"));
 (async () => {
   try {
     // Allow for command line arguments
diff --git a/checkAllowedRepos.js b/checkAllowedRepos.js
index 3d387c1..daf8384 100644
--- a/checkAllowedRepos.js
+++ b/checkAllowedRepos.js
@@ -1,7 +1,7 @@
-const matcher = require("matcher");
+import matcher from "matcher";
 
 let debug = () => {};
-module.exports = function (input, ignored, log) {
+export default function (input, ignored, log) {
   debug = log.extend("check-allowed-repos");
   // Nothing ignored, so it can't match
   if (ignored.length === 0) {
@@ -21,4 +21,4 @@ module.exports = function (input, ignored, log) {
     debug(`Skipping ${input} due to pattern match in ${ignored}`);
   }
   return isMatch;
-};
+}
diff --git a/extractActions.js b/extractActions.js
index 556cd2d..b9c6de5 100644
--- a/extractActions.js
+++ b/extractActions.js
@@ -1,5 +1,5 @@
 let debug = () => {};
-module.exports = function (input, allowEmpty, comment, log) {
+export default function (input, allowEmpty, comment, log) {
   debug = log.extend("extract-actions");
   // Check if it's a composite action
   let runs = input.contents.items.filter((n) => n.key == "runs");
@@ -10,7 +10,7 @@ module.exports = function (input, allowEmpty, comment, log) {
 
   debug("Processing workflow");
   return extractFromWorkflow(input, allowEmpty, comment);
-};
+}
 
 function extractFromComposite(input, allowEmpty, comment) {
   let runs = input.contents.items.filter((n) => n.key == "runs");
@@ -103,10 +103,9 @@ function handleStep(actions, items, comment) {
 
 function parseAction(str) {
   const [name, version] = str.split("@");
-  let path = "";
 
   // The action could be in a subdirectory
-  [owner, repo, ...path] = name.split("/");
+  let [owner, repo, ...path] = name.split("/");
   path = path.join("/");
   return { owner, repo, currentVersion: version, path };
 }
diff --git a/findRefOnGithub.js b/findRefOnGithub.js
index 6e9ea9a..7524bb6 100644
--- a/findRefOnGithub.js
+++ b/findRefOnGithub.js
@@ -1,10 +1,10 @@
-const { Octokit } = require("@octokit/rest");
+import { Octokit } from "@octokit/rest";
 const github = new Octokit({
   auth: process.env.GH_ADMIN_TOKEN,
 });
 
 let debug = () => {};
-module.exports = function (action, log) {
+export default function (action, log) {
   debug = log.extend("find-ref-on-github");
   return new Promise(async function (resolve, reject) {
     const owner = action.owner;
@@ -73,7 +73,7 @@ module.exports = function (action, log) {
       `Unable to find SHA for ${owner}/${repo}@${pinned}\n${error}`
     );
   });
-};
+}
 
 function handleCommonErrors(e, name) {
   if (e.status == 404) {
diff --git a/index.js b/index.js
index 4e24481..d29657b 100644
--- a/index.js
+++ b/index.js
@@ -1,12 +1,11 @@
-const YAML = require("yaml");
-
-const extractActions = require("./extractActions");
-const replaceActions = require("./replaceActions");
-const findRefOnGithub = require("./findRefOnGithub");
-const checkAllowedRepos = require("./checkAllowedRepos");
-const isSha = require("./isSha");
-
-module.exports = async function (
+import YAML from "yaml";
+import extractActions from "./extractActions.js";
+import replaceActions from "./replaceActions.js";
+import findRefOnGithub from "./findRefOnGithub.js";
+import checkAllowedRepos from "./checkAllowedRepos.js";
+import isSha from "./isSha.js";
+
+export default async function (
   input,
   allowed,
   ignoreShas,
@@ -46,4 +45,4 @@ module.exports = async function (
     workflow,
     actions,
   };
-};
+}
diff --git a/isSha.js b/isSha.js
index 467c613..68ff87e 100644
--- a/isSha.js
+++ b/isSha.js
@@ -1,3 +1,3 @@
-module.exports = function (action) {
+export default function (action) {
   return /\b([a-f0-9]{40})\b/.test(action.currentVersion);
-};
+}
diff --git a/package-lock.json b/package-lock.json
index 5e6c102..db1bdd2 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -20,6 +20,7 @@
         "pin-github-action": "bin.js"
       },
       "devDependencies": {
+        "cross-env": "^7.0.3",
         "eslint": "^8",
         "eslint-plugin-prettier": "^4",
         "jest": "^29",
@@ -2014,6 +2015,25 @@
       "integrity": "sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==",
       "dev": true
     },
+    "node_modules/cross-env": {
+      "version": "7.0.3",
+      "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz",
+      "integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==",
+      "dev": true,
+      "license": "MIT",
+      "dependencies": {
+        "cross-spawn": "^7.0.1"
+      },
+      "bin": {
+        "cross-env": "src/bin/cross-env.js",
+        "cross-env-shell": "src/bin/cross-env-shell.js"
+      },
+      "engines": {
+        "node": ">=10.14",
+        "npm": ">=6",
+        "yarn": ">=1"
+      }
+    },
     "node_modules/cross-spawn": {
       "version": "7.0.3",
       "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
@@ -6371,6 +6391,15 @@
       "integrity": "sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==",
       "dev": true
     },
+    "cross-env": {
+      "version": "7.0.3",
+      "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.3.tgz",
+      "integrity": "sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==",
+      "dev": true,
+      "requires": {
+        "cross-spawn": "^7.0.1"
+      }
+    },
     "cross-spawn": {
       "version": "7.0.3",
       "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
diff --git a/package.json b/package.json
index a37df09..1b1edf1 100644
--- a/package.json
+++ b/package.json
@@ -2,9 +2,10 @@
   "name": "pin-github-action",
   "version": "2.0.1",
   "description": "Pin your GitHub Actions to specific versions automatically!",
-  "main": "index.js",
+  "exports": "./index.js",
+  "type": "module",
   "scripts": {
-    "test": "jest",
+    "test": "cross-env NODE_OPTIONS=--experimental-vm-modules NODE_NO_WARNINGS=1 jest",
     "lint": "eslint *.js",
     "lint-fix": "eslint --fix *.js"
   },
@@ -16,6 +17,7 @@
   "author": "Michael Heap <m@michaelheap.com>",
   "license": "MIT",
   "devDependencies": {
+    "cross-env": "^7.0.3",
     "eslint": "^8",
     "eslint-plugin-prettier": "^4",
     "jest": "^29",
@@ -33,4 +35,4 @@
   "bin": {
     "pin-github-action": "bin.js"
   }
-}
+}
\ No newline at end of file
diff --git a/replaceActions.js b/replaceActions.js
index dce5d62..d9838ff 100644
--- a/replaceActions.js
+++ b/replaceActions.js
@@ -1,6 +1,6 @@
-const escapeStringRegexp = require("escape-string-regexp");
+import escapeStringRegexp from "escape-string-regexp";
 
-module.exports = function (input, action, comment) {
+export default function (input, action, comment) {
   const { currentVersion, owner, newVersion, path, repo } = action;
 
   const actionId = `${owner}/${repo}${path ? `/${path}` : ""}`;
@@ -31,7 +31,7 @@ module.exports = function (input, action, comment) {
     regexpPin,
     `uses$1:$2${actionId}@${newVersion} #${newComment}`
   );
-};
+}
 
 function generateComment(action, comment) {
   if (!comment) {


Test patch:
diff --git a/checkIgnoredRepos.test.js b/checkIgnoredRepos.test.js
index a19372c..832aedc 100644
--- a/checkIgnoredRepos.test.js
+++ b/checkIgnoredRepos.test.js
@@ -1,5 +1,6 @@
-const debug = require("debug")("pin-github-action-test");
-const run = require("./checkAllowedRepos");
+import debugLib from "debug";
+const debug = debugLib("pin-github-action-test");
+import run from "./checkAllowedRepos";
 const checkAllowedRepos = (input, ignore) => {
   return run.apply(null, [input, ignore, debug]);
 };
diff --git a/extractActions.test.js b/extractActions.test.js
index 7c1f10f..00bf2da 100644
--- a/extractActions.test.js
+++ b/extractActions.test.js
@@ -1,7 +1,8 @@
-const YAML = require("yaml");
-const debug = require("debug")("pin-github-action-test");
+import YAML from "yaml";
+import debugLib from "debug";
+const debug = debugLib("pin-github-action-test");
 
-const run = require("./extractActions");
+import run from "./extractActions";
 const extractActions = (input, allowEmpty, comment) => {
   if (!comment) {
     comment = " pin@{ref}";
diff --git a/findRefOnGithub.test.js b/findRefOnGithub.test.js
index c41586d..7b59335 100644
--- a/findRefOnGithub.test.js
+++ b/findRefOnGithub.test.js
@@ -1,8 +1,9 @@
-const nock = require("nock");
+import nock from "nock";
 nock.disableNetConnect();
 
-const debug = require("debug")("pin-github-action-test");
-const run = require("./findRefOnGithub");
+import debugLib from "debug";
+const debug = debugLib("pin-github-action-test");
+import run from "./findRefOnGithub";
 const findRef = (action) => {
   return run.apply(null, [action, debug]);
 };
diff --git a/isSha.test.js b/isSha.test.js
index 2a0491c..a7aa7a3 100644
--- a/isSha.test.js
+++ b/isSha.test.js
@@ -1,4 +1,4 @@
-const isSha = require("./isSha");
+import isSha from "./isSha";
 
 test("returns false if a non-sha is provided", () => {
   const actual = isSha({
diff --git a/replaceActions.test.js b/replaceActions.test.js
index 3149434..d062f8d 100644
--- a/replaceActions.test.js
+++ b/replaceActions.test.js
@@ -1,5 +1,5 @@
-const replaceActions = require("./replaceActions");
-const YAML = require("yaml");
+import replaceActions from "./replaceActions";
+import YAML from "yaml";
 
 const action = {
   owner: "mheap",