Skip to content
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

chore: code cleanup #25

Merged
merged 4 commits into from
Mar 7, 2021
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
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