From 064466e7fc6e0dbdada77a85db4866b4613f2f79 Mon Sep 17 00:00:00 2001 From: Brad Sherman Date: Tue, 15 Oct 2024 10:32:00 -0500 Subject: [PATCH] feat: Working Directory configuration option --- README.md | 1 + action.yml | 4 ++++ src/action.spec.ts | 2 ++ src/action.ts | 7 +++++++ src/main.ts | 1 + src/tasks/knip.ts | 6 ++++-- 6 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9669dcf..433cd32 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ The following inputs are supported | `annotations` | Annotate the project code with the knip results. | `false` | `true` | | `verbose` | Include annotated items in the comment report. | `false` | `false` | | `ignore_results` | Do not fail the action run if knip results are found. | `false` | `false` | +| `working_directory` | Run knip in a different directory. | `false` | `.` | ### Issues diff --git a/action.yml b/action.yml index d73f6d2..0fdcd93 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,10 @@ inputs: description: Do not fail the action run if knip results are found. default: false required: false + working_directory: + description: Directory in which to run the knip action. + default: "." + required: false runs: using: node20 main: dist/index.mjs diff --git a/src/action.spec.ts b/src/action.spec.ts index 896f4a5..6856c1d 100644 --- a/src/action.spec.ts +++ b/src/action.spec.ts @@ -32,6 +32,7 @@ describe("Action", () => { annotations: actionInputs.annotations?.default, verbose: actionInputs.verbose?.default, ignore_results: actionInputs.ignore_results?.default, + working_directory: actionInputs.working_directory?.default, }; vi.spyOn(core, "getInput").mockImplementation((input: string) => { @@ -39,6 +40,7 @@ describe("Action", () => { case "token": case "command_script_name": case "comment_id": + case "working_directory": // eslint-disable-next-line @typescript-eslint/no-unsafe-return return mockEnvConfig[input]; default: diff --git a/src/action.ts b/src/action.ts index f1f2c9e..95a8686 100644 --- a/src/action.ts +++ b/src/action.ts @@ -33,6 +33,11 @@ export interface ActionConfig { * Do not fail the action run if knip results are found. */ ignoreResults: boolean; + + /** + * Directory in which to run the knip action. + */ + workingDirectory: string; } export function getConfig(): ActionConfig { @@ -43,6 +48,7 @@ export function getConfig(): ActionConfig { annotations: core.getBooleanInput("annotations", { required: false }), verbose: core.getBooleanInput("verbose", { required: false }), ignoreResults: core.getBooleanInput("ignore_results", { required: false }), + workingDirectory: core.getInput("working_directory", { required: false }) || ".", }; } @@ -54,5 +60,6 @@ export function configToStr(cfg: ActionConfig): string { annotations: ${cfg.annotations} verbose: ${cfg.verbose} ignoreResults: ${cfg.ignoreResults} + workingDirectory: ${cfg.workingDirectory} `; } diff --git a/src/main.ts b/src/main.ts index 1ecc616..32067f6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -40,6 +40,7 @@ async function run(): Promise { config.commandScriptName, config.annotations, config.verbose, + config.workingDirectory, ); await runCommentTask( diff --git a/src/tasks/knip.ts b/src/tasks/knip.ts index 279eaf6..7fd4d46 100644 --- a/src/tasks/knip.ts +++ b/src/tasks/knip.ts @@ -8,9 +8,10 @@ import { GITHUB_COMMENT_MAX_COMMENT_LENGTH } from "../api.ts"; import { timeTask } from "./task.ts"; import type { ItemMeta } from "./types.ts"; -export async function buildRunKnipCommand(buildScriptName: string): Promise { +export async function buildRunKnipCommand(buildScriptName: string, cwd: string): Promise { const cmd = await getCliCommand(parseNr, [buildScriptName, "--reporter json"], { programmatic: true, + cwd, }); if (!cmd) { throw new Error("Unable to generate command for package manager"); @@ -548,11 +549,12 @@ export async function runKnipTasks( buildScriptName: string, annotationsEnabled: boolean, verboseEnabled: boolean, + cwd: string, ): Promise<{ sections: string[]; annotations: ItemMeta[] }> { const taskMs = Date.now(); core.info("- Running Knip tasks"); - const cmd = await timeTask("Build knip command", () => buildRunKnipCommand(buildScriptName)); + const cmd = await timeTask("Build knip command", () => buildRunKnipCommand(buildScriptName, cwd)); const output = await timeTask("Run knip", async () => getJsonFromOutput(await run(cmd))); const report = await timeTask("Parse knip report", () => Promise.resolve(parseJsonReport(output)),