Skip to content

Commit

Permalink
feat: Working Directory configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
bradsherman committed Oct 15, 2024
1 parent e540c36 commit 064466e
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ 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) => {
switch (input) {
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:
Expand Down
7 changes: 7 additions & 0 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 }) || ".",
};
}

Expand All @@ -54,5 +60,6 @@ export function configToStr(cfg: ActionConfig): string {
annotations: ${cfg.annotations}
verbose: ${cfg.verbose}
ignoreResults: ${cfg.ignoreResults}
workingDirectory: ${cfg.workingDirectory}
`;
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function run(): Promise<void> {
config.commandScriptName,
config.annotations,
config.verbose,
config.workingDirectory,
);

await runCommentTask(
Expand Down
6 changes: 4 additions & 2 deletions src/tasks/knip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
export async function buildRunKnipCommand(buildScriptName: string, cwd: string): Promise<string> {
const cmd = await getCliCommand(parseNr, [buildScriptName, "--reporter json"], {
programmatic: true,
cwd,
});
if (!cmd) {
throw new Error("Unable to generate command for package manager");
Expand Down Expand Up @@ -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)),
Expand Down

0 comments on commit 064466e

Please sign in to comment.