Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
added generation of command documentation (#15)
Browse files Browse the repository at this point in the history
Generates in the GitHub summary a list of all the available commands
with some information on their usage and (soon to be added) parameters.

Resolves #12
  • Loading branch information
Bullrich committed Apr 19, 2024
1 parent a10e253 commit e8ab935
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
38 changes: 38 additions & 0 deletions src/commander.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { summary } from "@actions/core";
import { readFile } from "fs/promises";
import { dirname } from "path";
import { parse } from "yaml";

import { ActionLogger } from "./github/types";
Expand All @@ -24,6 +26,7 @@ export class Commander {
for (const file of files) {
const content = await readFile(file, "utf-8");
const command = parse(content) as Command;
command.location = dirname(file);
this.logger.info(`Parsing ${file}`);
validateConfig(command);
commands.push(command);
Expand All @@ -32,4 +35,39 @@ export class Commander {
this.commands = commands;
return commands;
}

async documentCommands(): Promise<typeof summary> {
this.logger.info("Generating documentation");
const commands = await this.getCommands();
let text = summary
.addHeading("Commands")
.addRaw(`There are ${commands.length} available commands`)
.addEOL();
for (const command of commands) {
text = text.addHeading(command.name, 2);
if (command.description) {
text = text.addRaw(command.description).addEOL();
}
text = text
.addEOL()
.addDetails("File location", `<code>${command.location}</code>`)
.addEOL();
if (command.machine) {
text = text.addHeading("Runs on").addList(command.machine);
}
if (command.timeout) {
text = text
.addHeading("Timeout", 4)
.addRaw(`${command.timeout} seconds`)
.addEOL();
}

text = text
.addHeading("Command that runs", 4)
.addCodeBlock(command.commandStart, "shell")
.addEOL();
}

return text;
}
}
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ const commander = new Commander(scripts, logger);
if (context.eventName.includes("pull_request")) {
commander
.getCommands()
.then((commands) =>
.then(async (commands) => {
logger.info(
`Found ${commands.length} valid commands: ${commands
.map(({ name }) => name)
.join(", ")}`,
),
)
);
await (await commander.documentCommands()).write();
})
.catch(setFailed);
}

Expand Down
1 change: 1 addition & 0 deletions src/schema/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Command {
name: string;
location: string;
description?: string;
machine?: string[];
timeout?: number;
Expand Down
1 change: 1 addition & 0 deletions src/test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ test("test good command", () => {
const goodCommand: Command = {
name: "Hi",
timeout: 10,
location: "",
commandStart: "./bin",
};
validateConfig(goodCommand);
Expand Down

0 comments on commit e8ab935

Please sign in to comment.