Skip to content

Commit

Permalink
chore: code cleanup (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
npalm authored Mar 7, 2021
1 parent 4f869cb commit c72eba5
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 324 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
"eslint-plugin-github": "^4.1.1",
"eslint-plugin-jest": "^24.1.3",
"jest": "^26.6.3",
"jest-circus": "^26.6.3",
"mocha": "^8.3.1",
"nyc": "^15.1.0",
"prettier": "2.2.1",
"semantic-release": "^17.3.9",
Expand Down
127 changes: 72 additions & 55 deletions src/action-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ interface ActionMarkdown {
runs: string;
}

interface ActionYml {
name: string;
description: string;
inputs: ActionInputsOutputs;
outputs: ActionInputsOutputs;
runs: RunType;
}

interface RunType {
using: string;
main: string;
}

interface DefaultOptions {
tocLevel: number;
actionFile: string;
Expand All @@ -33,16 +46,30 @@ export const defaultOptions: DefaultOptions = {
readmeFile: "README.md",
lineBreaks: "LF",
};

type ActionInputsOutputs = Record<string, ActionInput | ActionOutput>;

interface ActionInput {
required?: boolean;
description: string;
default?: string;
}

function createMdTable(options: DefaultOptions, data: string[][]): string {
interface ActionOutput {
description: string;
}

function createMdTable(
data: ActionInputsOutputs,
options: DefaultOptions,
type: "input" | "output"
): string {
const tableData = getInputOutput(data, type);
const tableArray = tableData.headers.concat(tableData.rows);

let result = "";

for (const line of data) {
for (const line of tableArray) {
result = `${result}|`;
for (const c of line) {
result = `${result} ${c} |`;
Expand Down Expand Up @@ -80,6 +107,26 @@ export async function generateActionMarkdownDocs(
return `${docs.description + docs.inputs + docs.outputs + docs.runs}`;
}

function generateActionDocs(options: DefaultOptions): ActionMarkdown {
const yml = load(readFileSync(options.actionFile, "utf-8"), {
json: true,
}) as ActionYml;

const inputMdTable = createMdTable(yml.inputs, options, "input");
const outputMdTable = createMdTable(yml.outputs, options, "output");

return {
description: createMarkdownSection(options, yml.description, "Description"),
inputs: createMarkdownSection(options, inputMdTable, "Inputs"),
outputs: createMarkdownSection(options, outputMdTable, "Outputs"),
runs: createMarkdownSection(
options,
`This action is an \`${yml.runs.using}\` action.`,
"Runs"
),
};
}

async function updateReadme(
options: DefaultOptions,
text: string,
Expand Down Expand Up @@ -114,64 +161,34 @@ function createMarkdownSection(
: "";
}

function generateActionDocs(options: DefaultOptions): ActionMarkdown {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const yml: any = load(readFileSync(options.actionFile, "utf-8"), {
json: true,
});

const inputData = getInputOutput(yml.inputs, "input");
const inputMdTable = createMdTable(
options,
inputData.headers.concat(inputData.rows)
);

const outputData = getInputOutput(yml.outputs, "output");
const outputMdTable = createMdTable(
options,
outputData.headers.concat(outputData.rows)
);

return {
description: createMarkdownSection(options, yml.description, "Description"),
inputs: createMarkdownSection(options, inputMdTable, "Inputs"),
outputs: createMarkdownSection(options, outputMdTable, "Outputs"),
runs: createMarkdownSection(
options,
`This action is an \`${yml.runs.using}\` action.`,
"Runs"
),
};
}

function getInputOutput(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inputs: any,
data: ActionInputsOutputs,
type: "input" | "output"
): { headers: string[][]; rows: string[][] } {
const headers: string[][] = [];
const rows: string[][] = [];
if (inputs !== undefined) {
headers[0] =
type === "input"
? ["parameter", "description", "required", "default"]
: ["parameter", "description"];
headers[1] = Array(headers[0].length).fill("-");

//let i = 0;
for (let i = 0; i < Object.keys(inputs).length; i++) {
const key = Object.keys(inputs)[i];
const input = inputs[key] as ActionInput;
rows[i] = [];
rows[i].push(key);
rows[i].push(input.description);

if (type === "input") {
rows[i].push(
input.required ? `\`${String(input.required)}\`` : "`false`"
);
rows[i].push(input.default ? input.default : "");
}
if (data === undefined) {
return { headers, rows };
}

headers[0] =
type === "input"
? ["parameter", "description", "required", "default"]
: ["parameter", "description"];
headers[1] = Array(headers[0].length).fill("-");

for (let i = 0; i < Object.keys(data).length; i++) {
const key = Object.keys(data)[i];
const value = data[key] as ActionInput;
rows[i] = [];
rows[i].push(key);
rows[i].push(value.description);

if (type === "input") {
rows[i].push(
value.required ? `\`${String(value.required)}\`` : "`false`"
);
rows[i].push(value.default ? value.default : "");
}
}
return { headers, rows };
Expand Down
Loading

0 comments on commit c72eba5

Please sign in to comment.