Skip to content

Commit

Permalink
feat: migrate to ESM (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed May 24, 2021
1 parent 878b9d7 commit 625c6d7
Show file tree
Hide file tree
Showing 30 changed files with 1,926 additions and 1,904 deletions.
2,523 changes: 1,259 additions & 1,264 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/__tests__/aggregateReport.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const aggregateReport = require("../aggregateReport");
import aggregateReport from "../aggregateReport.js";

test("aggregateReport()", async () => {
const audit = new Map();
Expand Down
13 changes: 10 additions & 3 deletions lib/__tests__/audit.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const audit = require("../audit");
const auditJson = require("./fixtures/audit.json");
const auditJson2 = require("./fixtures/audit-2.json");
import { readFileSync } from "fs";
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
import audit from "../audit.js";

// @ts-expect-error
const fixture = (path) => new URL(`./fixtures/${path}`, import.meta.url);

const auditJson = JSON.parse(readFileSync(fixture("audit.json"), "utf8"));
const auditJson2 = JSON.parse(readFileSync(fixture("audit-2.json"), "utf8"));

const mockExec = (/** @type {unknown} */ json) =>
jest.fn().mockImplementationOnce((_1, _2, options) => {
// @ts-expect-error
options.listeners.stdout(JSON.stringify(json));
});

Expand Down
7 changes: 5 additions & 2 deletions lib/__tests__/buildCommitBody.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const buildCommitBody = require("../buildCommitBody");
const report = require("./fixtures/report.json");
import { readFileSync } from "fs";
import buildCommitBody from "../buildCommitBody.js";

// @ts-expect-error
const report = JSON.parse(readFileSync(new URL("./fixtures/report.json", import.meta.url), "utf8"));

test("buildCommitBody()", () => {
expect(buildCommitBody(report)).toEqual(`Summary:
Expand Down
7 changes: 5 additions & 2 deletions lib/__tests__/buildPullRequestBody.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const buildPullRequestBody = require("../buildPullRequestBody");
const report = require("./fixtures/report.json");
import { readFileSync } from "fs";
import buildPullRequestBody from "../buildPullRequestBody.js";

// @ts-expect-error
const report = JSON.parse(readFileSync(new URL("./fixtures/report.json", import.meta.url), "utf8"));

test("buildPullRequestBody()", () => {
expect(buildPullRequestBody(report, "7.7.0")).toBe(
Expand Down
10 changes: 6 additions & 4 deletions lib/__tests__/listPackages.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const path = require("path");
import path from "path";
import listPackages from "../listPackages.js";

const listPackages = require("../listPackages");
// @ts-expect-error
const fixtures = new URL("./fixtures", import.meta.url).pathname;

test("listPackages()", async () => {
const packages = await listPackages({ silent: true });
Expand All @@ -12,15 +14,15 @@ test("listPackages()", async () => {
test("listPackages() with an empty package.json", async () => {
const packages = await listPackages({
silent: true,
cwd: path.join(__dirname, "fixtures", "empty_package.json"),
cwd: path.join(fixtures, "empty_package.json"),
});
expect(packages.size).toEqual(0);
});

test("listPackages() with a package.json having no version", async () => {
const packages = await listPackages({
silent: true,
cwd: path.join(__dirname, "fixtures", "noversion_package.json"),
cwd: path.join(fixtures, "noversion_package.json"),
});
expect(packages.size).toEqual(0);
});
2 changes: 1 addition & 1 deletion lib/__tests__/npmArgs.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const npmArgs = require("../npmArgs");
import npmArgs from "../npmArgs.js";

test("npmArgs() without arguments", () => {
expect(npmArgs()).toEqual(["--ignore-scripts", "--no-progress"]);
Expand Down
10 changes: 5 additions & 5 deletions lib/aggregateReport.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const capitalize = require("./utils/capitalize");
const semverToNumber = require("./utils/semverToNumber");
const packageRepoUrls = require("./packageRepoUrls");
import packageRepoUrls from "./packageRepoUrls.js";
import capitalize from "./utils/capitalize.js";
import semverToNumber from "./utils/semverToNumber.js";

/**
* @param {{ name: string, version: string }} a
Expand All @@ -24,7 +24,7 @@ const byNameAndVersion = (a, b) => {
* @param {Map<string, string>} afterPackages
* @returns {Promise<Report>}
*/
module.exports = async function aggregateReport(audit, beforePackages, afterPackages) {
export default async function aggregateReport(audit, beforePackages, afterPackages) {
/** @type {Report["added"]} */
const added = [];
afterPackages.forEach((version, name) => {
Expand Down Expand Up @@ -68,4 +68,4 @@ module.exports = async function aggregateReport(audit, beforePackages, afterPack
const packageUrls = await packageRepoUrls(allPackageNames);

return { added, removed, updated, packageCount, packageUrls };
};
}
8 changes: 4 additions & 4 deletions lib/audit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { exec } = require("@actions/exec");
const npmArgs = require("./npmArgs");
import { exec } from "@actions/exec";
import npmArgs from "./npmArgs.js";

/**
* @param {typeof exec} execFn
* @returns {Promise<AuditReport>}
*/
module.exports = async function audit(execFn = exec) {
export default async function audit(execFn = exec) {
let report = "";
await execFn("npm", npmArgs("audit", "--json"), {
listeners: {
Expand Down Expand Up @@ -39,4 +39,4 @@ module.exports = async function audit(execFn = exec) {
}

throw new Error('"vulnerabilities" is missing');
};
}
10 changes: 5 additions & 5 deletions lib/auditFix.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { warning } = require("@actions/core");
const { exec } = require("@actions/exec");
const npmArgs = require("./npmArgs");
import { warning } from "@actions/core";
import { exec } from "@actions/exec";
import npmArgs from "./npmArgs.js";

module.exports = async function auditFix() {
export default async function auditFix() {
let error = "";

const returnCode = await exec("npm", npmArgs("audit", "fix"), {
Expand All @@ -21,4 +21,4 @@ module.exports = async function auditFix() {
if (returnCode !== 0) {
warning(error);
}
};
}
4 changes: 2 additions & 2 deletions lib/buildCommitBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param {Report} report
* @returns {string}
*/
module.exports = function buildCommitBody(report) {
export default function buildCommitBody(report) {
const lines = [];

lines.push("Summary:");
Expand All @@ -23,4 +23,4 @@ module.exports = function buildCommitBody(report) {
}

return lines.map((line) => `${line}\n`).join("");
};
}
6 changes: 3 additions & 3 deletions lib/buildPullRequestBody.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { PACKAGE_NAME, PACKAGE_URL } = require("./constants");
import { PACKAGE_NAME, PACKAGE_URL } from "./constants.js";

const EMPTY = "-";

Expand All @@ -7,7 +7,7 @@ const EMPTY = "-";
* @param {string} npmVersion
* @returns {string}
*/
module.exports = function buildPullRequestBody(report, npmVersion) {
export default function buildPullRequestBody(report, npmVersion) {
/**
* @param {string} name
* @param {string} version
Expand Down Expand Up @@ -106,4 +106,4 @@ module.exports = function buildPullRequestBody(report, npmVersion) {
lines.push(`Created by [${PACKAGE_NAME}](${PACKAGE_URL})`);

return lines.join("\n").trim();
};
}
6 changes: 3 additions & 3 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports.PACKAGE_NAME = "ybiquitous/npm-audit-fix-action";
module.exports.PACKAGE_URL = "https://github.com/ybiquitous/npm-audit-fix-action";
module.exports.NPM_VERSION = "7";
export const PACKAGE_NAME = "ybiquitous/npm-audit-fix-action";
export const PACKAGE_URL = "https://github.com/ybiquitous/npm-audit-fix-action";
export const NPM_VERSION = "7";
12 changes: 6 additions & 6 deletions lib/createOrUpdatePullRequest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { info } = require("@actions/core");
const { exec } = require("@actions/exec");
const github = require("@actions/github");
const splitRepo = require("./utils/splitRepo");
import { info } from "@actions/core";
import { exec } from "@actions/exec";
import * as github from "@actions/github";
import splitRepo from "./utils/splitRepo.js";

/**
* @param {{
Expand All @@ -17,7 +17,7 @@ const splitRepo = require("./utils/splitRepo");
* labels: string[],
* }} params
*/
module.exports = async function createOrUpdatePullRequest({
export default async function createOrUpdatePullRequest({
token,
branch,
baseBranch,
Expand Down Expand Up @@ -80,4 +80,4 @@ module.exports = async function createOrUpdatePullRequest({
});
info(`The labels were added successfully: ${newLabels.data.map((l) => l.name).join(", ")}`);
}
};
}
10 changes: 5 additions & 5 deletions lib/getDefaultBranch.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const github = require("@actions/github");
const splitRepo = require("./utils/splitRepo");
import { getOctokit } from "@actions/github";
import splitRepo from "./utils/splitRepo.js";

/**
* @param {{token: string, repository: string}} params
* @returns {Promise<string>}
*/
module.exports = async function getDefaultBranch({ token, repository }) {
const octokit = github.getOctokit(token);
export default async function getDefaultBranch({ token, repository }) {
const octokit = getOctokit(token);
const res = await octokit.rest.repos.get(splitRepo(repository));
return res.data.default_branch;
};
}
29 changes: 14 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const core = require("@actions/core");
const { exec } = require("@actions/exec");

const { NPM_VERSION } = require("./constants");
const audit = require("./audit");
const auditFix = require("./auditFix");
const npmArgs = require("./npmArgs");
const updateNpm = require("./updateNpm");
const listPackages = require("./listPackages");
const aggregateReport = require("./aggregateReport");
const buildPullRequestBody = require("./buildPullRequestBody");
const buildCommitBody = require("./buildCommitBody");
const createOrUpdatePullRequest = require("./createOrUpdatePullRequest");
const getDefaultBranch = require("./getDefaultBranch");
const commaSeparatedList = require("./utils/commaSeparatedList");
import * as core from "@actions/core";
import { exec } from "@actions/exec";
import aggregateReport from "./aggregateReport.js";
import audit from "./audit.js";
import auditFix from "./auditFix.js";
import buildCommitBody from "./buildCommitBody.js";
import buildPullRequestBody from "./buildPullRequestBody.js";
import { NPM_VERSION } from "./constants.js";
import createOrUpdatePullRequest from "./createOrUpdatePullRequest.js";
import getDefaultBranch from "./getDefaultBranch.js";
import listPackages from "./listPackages.js";
import npmArgs from "./npmArgs.js";
import updateNpm from "./updateNpm.js";
import commaSeparatedList from "./utils/commaSeparatedList.js";

/**
* @returns {Promise<boolean>}
Expand Down
8 changes: 4 additions & 4 deletions lib/listPackages.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { exec } = require("@actions/exec");
const npmArgs = require("./npmArgs");
import { exec } from "@actions/exec";
import npmArgs from "./npmArgs.js";

/**
* @param {import("@actions/exec").ExecOptions?} options
* @returns {Promise<Map<string, string>>}
*/
module.exports = async function listPackages(options = {}) {
export default async function listPackages(options = {}) {
let lines = "";
let stderr = "";
const returnCode = await exec("npm", npmArgs("ls", "--parseable", "--long", "--all"), {
Expand Down Expand Up @@ -53,4 +53,4 @@ module.exports = async function listPackages(options = {}) {
packages.set(name.trim(), version.trim());
});
return packages;
};
}
4 changes: 2 additions & 2 deletions lib/npmArgs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @param {string[]} args
*/
module.exports = function npmArgs(...args) {
export default function npmArgs(...args) {
return [...args, "--ignore-scripts", "--no-progress"];
};
}
10 changes: 5 additions & 5 deletions lib/packageRepoUrls.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { info } = require("@actions/core");
const { exec } = require("@actions/exec");
const hostedGitInfo = require("hosted-git-info");
import { info } from "@actions/core";
import { exec } from "@actions/exec";
import * as hostedGitInfo from "hosted-git-info";

/**
* @type {Map<string, UrlInfo>}
Expand Down Expand Up @@ -62,7 +62,7 @@ async function fetchUrl(packageName) {
* @param {string[]} packageNames
* @returns {Promise<Record<string, UrlInfo>>}
*/
module.exports = async function packageRepoUrls(packageNames) {
export default async function packageRepoUrls(packageNames) {
const allUrls = await Promise.all(packageNames.map(fetchUrl));

/**
Expand All @@ -75,4 +75,4 @@ module.exports = async function packageRepoUrls(packageNames) {
}
}
return map;
};
}
8 changes: 4 additions & 4 deletions lib/updateNpm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { exec } = require("@actions/exec");
const npmArgs = require("./npmArgs");
import { exec } from "@actions/exec";
import npmArgs from "./npmArgs.js";

/**
* @param {string} version
*/
module.exports = async function updateNpm(version) {
export default async function updateNpm(version) {
await exec("sudo", ["npm", ...npmArgs("install", "--global", `npm@${version}`)]);

let actualVersion = "";
Expand All @@ -21,4 +21,4 @@ module.exports = async function updateNpm(version) {
await exec("sudo", ["chown", "-R", `${process.env["USER"]}:`, `${process.env["HOME"]}/.config`]);

return actualVersion.trim();
};
}
2 changes: 1 addition & 1 deletion lib/utils/__tests__/commaSeparatedList.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const commaSeparatedList = require("../commaSeparatedList");
import commaSeparatedList from "../commaSeparatedList.js";

test("commaSeparatedList()", () => {
expect(commaSeparatedList("")).toEqual([]);
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/__tests__/semverToNumber.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const semverToNumber = require("../semverToNumber");
import semverToNumber from "../semverToNumber.js";

test("semverToNumber()", () => {
expect(semverToNumber("1.2.3")).toEqual(10203);
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/capitalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* @param {string} str
* @returns {string}
*/
module.exports = function capitalize(str) {
export default function capitalize(str) {
if (typeof str === "string") {
return str.charAt(0).toUpperCase() + str.slice(1);
}
return "";
};
}
4 changes: 2 additions & 2 deletions lib/utils/commaSeparatedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* @param {string} str
* @returns {string[]}
*/
module.exports = function commaSeparatedList(str) {
export default function commaSeparatedList(str) {
return str
.split(",")
.map((s) => s.trim())
.filter(Boolean);
};
}
4 changes: 2 additions & 2 deletions lib/utils/semverToNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param {string} version
* @returns {number}
*/
module.exports = function semverToNumber(version) {
export default function semverToNumber(version) {
return version
.split(".")
.slice(0, 3)
Expand All @@ -12,4 +12,4 @@ module.exports = function semverToNumber(version) {
const added = num * 10 ** (idx * 2) || 0;
return sum + added;
}, 0);
};
}
Loading

0 comments on commit 625c6d7

Please sign in to comment.