Skip to content

feat: infer Knip Addons from disk #2086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions src/blocks/blockKnip.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { testBlock } from "bingo-stratum-testers";
import { describe, expect, test, vi } from "vitest";
import { testBlock, testIntake } from "bingo-stratum-testers";
import { describe, expect, it, test, vi } from "vitest";

import { blockKnip } from "./blockKnip.js";
import { optionsBase } from "./options.fakes.js";
Expand Down Expand Up @@ -206,4 +206,36 @@ describe("blockKnip", () => {
}
`);
});

describe("intake", () => {
it("returns undefined when knip.json does not exist", () => {
const actual = testIntake(blockKnip, {
files: {},
});

expect(actual).toBeUndefined();
});

it("returns undefined when knip.json does not contain ignoreDependencies", () => {
const actual = testIntake(blockKnip, {
files: {
"knip.json": [JSON.stringify({ other: true })],
},
});

expect(actual).toBeUndefined();
});

it("returns ignoreDependencies when knip.json contains ignoreDependencies", () => {
const ignoreDependencies = ["a", "b", "c"];

const actual = testIntake(blockKnip, {
files: {
"knip.json": [JSON.stringify({ ignoreDependencies })],
},
});

expect(actual).toEqual({ ignoreDependencies });
});
});
});
17 changes: 16 additions & 1 deletion src/blocks/blockKnip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,28 @@ import { blockGitHubActionsCI } from "./blockGitHubActionsCI.js";
import { blockPackageJson } from "./blockPackageJson.js";
import { blockRemoveFiles } from "./blockRemoveFiles.js";
import { blockRemoveWorkflows } from "./blockRemoveWorkflows.js";
import { intakeFileAsJson } from "./intake/intakeFileAsJson.js";

const zIgnoreDependencies = z.array(z.string());

export const blockKnip = base.createBlock({
about: {
name: "Knip",
},
addons: {
ignoreDependencies: z.array(z.string()).optional(),
ignoreDependencies: zIgnoreDependencies.optional(),
},
intake({ files }) {
const knipJson = intakeFileAsJson(files, ["knip.json"]);
if (!knipJson?.ignoreDependencies) {
return undefined;
}

return {
ignoreDependencies: zIgnoreDependencies.safeParse(
knipJson.ignoreDependencies,
).data,
};
},
produce({ addons }) {
const { ignoreDependencies } = addons;
Expand Down
35 changes: 35 additions & 0 deletions src/blocks/intake/intakeFileAsJson.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";

import { intakeFileAsJson } from "./intakeFileAsJson.js";

describe(intakeFileAsJson, () => {
it("returns undefined when the file does not exist", () => {
const actual = intakeFileAsJson({}, []);

expect(actual).toBeUndefined();
});

it("returns undefined when the file does not have valid JSON", () => {
const actual = intakeFileAsJson(
{
"file.json": ["{"],
},
["file.json"],
);

expect(actual).toBeUndefined();
});

it("returns loaded file contents when the file has valid JSON", () => {
const value = { key: "value" };

const actual = intakeFileAsJson(
{
"file.json": [JSON.stringify(value)],
},
["file.json"],
);

expect(actual).toEqual(value);
});
});
14 changes: 14 additions & 0 deletions src/blocks/intake/intakeFileAsJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IntakeDirectory } from "bingo-fs";
import JSON5 from "json5";

import { intakeFile } from "./intakeFile.js";

export function intakeFileAsJson(files: IntakeDirectory, filePath: string[]) {
const file = intakeFile(files, filePath);

try {
return file && JSON5.parse<Record<string, unknown> | undefined>(file[0]);
} catch {
return undefined;
}
}