Skip to content

Commit

Permalink
Avoid "input: Options" mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed May 7, 2024
1 parent 5eb2c29 commit 02e458f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
4 changes: 2 additions & 2 deletions __tests__/no-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { execSync } from "node:child_process";
import path from "node:path";
import { compare } from "../src/compare";
import { report } from "../src/report";
import type { Options } from "../src/types";
import type { Input } from "../src/types";
import {
createDummyBaseAnalysis,
getExampleDirectories,
Expand All @@ -14,7 +14,7 @@ describe("examples w/o base analysis", () => {
const fixturesPath = path.join(__dirname, "__fixtures__", "examples");
for (const example of getExampleDirectories(fixturesPath)) {
describe(`example ${example.name}`, () => {
const input: Options = {
const input: Input = {
analyzerDirectory: ".analyzer",
percentExtraAttention: 20,
includeExtensions: [".js", ".mjs", ".cjs"],
Expand Down
4 changes: 2 additions & 2 deletions __tests__/with-base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { execSync } from "node:child_process";
import path from "node:path";

import { run } from "../src/index";
import type { Options } from "../src/types";
import type { Input } from "../src/types";
import {
getExampleDirectories,
readAnalysisComment,
Expand All @@ -23,7 +23,7 @@ describe("examples w/ base analysis", () => {
? ["out/meta1.json", "out/meta2.json"]
: ["out/meta.json"];

const input: Options = {
const input: Input = {
analyzerDirectory: ".analyzer",
percentExtraAttention: 20,
includeExtensions: [".js", ".mjs", ".cjs"],
Expand Down
26 changes: 13 additions & 13 deletions dist/index.mjs

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions src/compare.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from "node:fs";
import path from "node:path";
import type { CompareResult, Options, Report, TreeMapNode } from "./types";
import type { CompareResult, Input, Report, TreeMapNode } from "./types";
import { loadAnalysisJson, loadMetaFile } from "./utils";

export function compare(input: Options): void {
export function compare(input: Input): void {
let hasAnyChange = false;
let output = `## 📦 esbuild Bundle Analysis for ${input.name}
Expand Down Expand Up @@ -71,7 +71,7 @@ function treeKey(metafile: string, outfile: string): string {

// Write the output to a file which is later read in
// as comment contents by the actions workflow.
function writeComment(input: Options, output: string): void {
function writeComment(input: Input, output: string): void {
fs.mkdirSync(path.join(process.cwd(), input.analyzerDirectory), {
recursive: true,
});
Expand All @@ -85,7 +85,7 @@ function writeComment(input: Options, output: string): void {
);
}

function detail(input: Options): string {
function detail(input: Input): string {
if (!input.showDetails) {
return "";
}
Expand All @@ -102,7 +102,7 @@ function detail(input: Options): string {
</details>\n`;
}

function loadBaseAnalysisJson(input: Options): Report {
function loadBaseAnalysisJson(input: Input): Report {
try {
return loadAnalysisJson(
path.join(
Expand All @@ -118,7 +118,7 @@ function loadBaseAnalysisJson(input: Options): Report {
}
}

function buildFileTree(input: Options) {
function buildFileTree(input: Input) {
function buildRoot(
input: Record<string, { bytesInOutput: number }>,
): TreeMapNode {
Expand Down
26 changes: 13 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
import { pathToFileURL } from "node:url";
import { compare } from "./compare";
import { report } from "./report";
import type { Options } from "./types";
import { getInput } from "./utils";
import type { Input } from "./types";
import { getSingleInput } from "./utils";

function getOptions(): Options {
const rawMetafiles = getInput("metafiles");
function getInput(): Input {
const rawMetafiles = getSingleInput("metafiles");
if (!rawMetafiles) {
throw new Error("metafiles is not specified");
}
const name = getInput("name");
const name = getSingleInput("name");
if (!name) {
throw new Error("name is not specified");
}
return {
percentExtraAttention: Number.parseInt(
getInput("percent_extra_attention") || "20",
getSingleInput("percent_extra_attention") || "20",
10,
),
showDetails: ["true", "True", "TRUE"].includes(
getInput("show_details") || "true",
getSingleInput("show_details") || "true",
),
topNLargestPaths: Number.parseInt(
getInput("top_n_largest_paths") || "20",
getSingleInput("top_n_largest_paths") || "20",
10,
),
includeExtensions: (
getInput("include_extensions") || ".js,.mjs,.cjs"
getSingleInput("include_extensions") || ".js,.mjs,.cjs"
).split(","),
name,
analyzerDirectory: getInput("analyze_directory") || ".analyzer",
analyzerDirectory: getSingleInput("analyze_directory") || ".analyzer",
metafiles: rawMetafiles.split(","),
};
}

export function run(options: Options = getOptions()): void {
report(options);
compare(options);
export function run(input: Input = getInput()): void {
report(input);
compare(input);
}

if (import.meta.url === pathToFileURL(process.argv[1]).href) {
Expand Down
6 changes: 3 additions & 3 deletions src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import fs from "node:fs";
import path from "node:path";
import process from "node:process";

import type { Options, Report } from "./types";
import type { Input, Report } from "./types";
import { loadMetaFile } from "./utils";

export function report(input: Options): void {
export function report(input: Input): void {
const allPageSizes = getAllPageSizes(input);
fs.mkdirSync(path.join(process.cwd(), input.analyzerDirectory), {
recursive: true,
Expand All @@ -19,7 +19,7 @@ export function report(input: Options): void {
console.log(`Wrote ${resultJsonPath}`);
}

function getAllPageSizes(input: Options): Report {
function getAllPageSizes(input: Input): Report {
const acc: Report = {};
return input.metafiles.reduce((acc, metafile) => {
const metaFilePath = path.join(process.cwd(), metafile);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface CompareResult {
tree: TreeMapNode | undefined;
}

export interface Options {
export interface Input {
name: string;
metafiles: Array<string>;
includeExtensions: Array<string>;
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function loadAnalysisJson(path: string): Report {
}

// https://github.com/actions/toolkit/blob/81a73aba8bedd532f6eddcc41ed3a0fad8b1cfeb/packages/core/src/core.ts#L126
export function getInput(name: string): string {
export function getSingleInput(name: string): string {
const val = process.env[`INPUT_${name.toUpperCase()}`] || "";
return val.trim();
}

0 comments on commit 02e458f

Please sign in to comment.