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

feat: Table Generator Support Cases #37

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Changes from 3 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
149 changes: 91 additions & 58 deletions .github/workflows/markdown-table-workflow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,94 @@ const verboseRuntimes = {
swift: "Swift",
};

const folderDenylist = [".github", ".git"];

const generateUniqueTemplates = (runtimes) => {
let templates = [];

for (const runtime of runtimes) {
const folders = fs
.readdirSync(path.join(".", `../../../${runtime}`), {
withFileTypes: true,
})
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);

templates.push(...folders);
}

return [...new Set(templates)];
const verboseTemplates = {
"Analyze With Perspectiveapi": "Analyze With PerspectiveAPI",
"Generate Pdf": "Generate PDF",
"Prompt Chatgpt": "Prompt ChatGPT",
"Push Notification With Fcm": "Push Notifications With FCM",
"Url Shortener": "URL Shortener",
"Whatsapp With Vonage": "WhatsApp With Vonage",
Meldiron marked this conversation as resolved.
Show resolved Hide resolved
};

const generateTableRows = (templates, runtimes) => {
return templates.map((template) => {
const languagesSupport = runtimes.map((runtime) => {
return fs.existsSync(path.join(".", `../../../${runtime}/${template}`))
? `[✅](/${runtime}/${template})`
: "🏗️";
});

return [template, ...languagesSupport];
});
};
const folderDenylist = [".github", ".git"];

const sortRuntimesBySupport = (runtimes, uniqueTemplates) => {
return runtimes.sort((a, b) => {
const aTemplates = uniqueTemplates.filter((template) =>
fs.existsSync(path.join(".", `../../../${a}/${template}`))
);
const bTemplates = uniqueTemplates.filter((template) =>
fs.existsSync(path.join(".", `../../../${b}/${template}`))
function getDirectories(dirPath) {
return fs
.readdirSync(dirPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.filter((name) => !folderDenylist.includes(name));
}

function toTitleCase(text) {
return text
.replace(/_/g, " ")
.replace(/-/g, " ")
.replace(/([a-z])([A-Z])/g, "$1 $2")
.replace(/\w\S*/g, (w) => w.replace(/^\w/, (c) => c.toUpperCase()));
}

function normalizeTemplate(template) {
const titleCase = toTitleCase(template);
return titleCase in verboseTemplates
? verboseTemplates[titleCase]
: titleCase;
}

function getRuntimeToTemplates() {
const runtimeDirs = getDirectories(path.join(".", "../../../"));
const runtimeToTemplates = {};

for (const runtimeDir of runtimeDirs) {
const runtime = verboseRuntimes[runtimeDir] || runtimeDir;
const templateDirs = getDirectories(
path.join(".", "../../../", runtimeDir)
);

return bTemplates.length - aTemplates.length;
runtimeToTemplates[runtime] = templateDirs.map((templateDir) => {
const template = normalizeTemplate(templateDir);
return {
name: template,
dir: path.join(".", runtimeDir, templateDir),
};
});
}

return runtimeToTemplates;
}

function getTemplateToRuntimes(runtimeToTemplates) {
const templateToRuntimes = {};
for (const runtime of Object.keys(runtimeToTemplates)) {
for (const template of runtimeToTemplates[runtime]) {
if (!(template.name in templateToRuntimes)) {
templateToRuntimes[template.name] = [];
}

templateToRuntimes[template.name].push({
name: runtime,
dir: template.dir,
});
}
}
return templateToRuntimes;
}

function generateTableRows(sortedTemplates) {
return sortedTemplates.map((template) => {
return [
template,
...sortedRuntimes.map((runtime) => {
const matchingRuntime = templateToRuntimes[template].find(
(r) => r.name === runtime
);
return matchingRuntime ? `[✅](${matchingRuntime.dir})` : "🏗️";
}),
];
});
};
}

const updateReadmeFile = (readmePath, table) => {
function updateReadmeFile(readmePath, table) {
const readme = fs.readFileSync(readmePath).toString();

if (
Expand All @@ -75,32 +118,22 @@ const updateReadmeFile = (readmePath, table) => {

fs.writeFileSync(readmePath, newReadme);
}
};
}

let runtimes = fs
.readdirSync(path.join(".", "../../../"), { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name)
.filter((folder) => !folderDenylist.includes(folder))
.sort();
const runtimeToTemplate = getRuntimeToTemplates();
const templateToRuntimes = getTemplateToRuntimes(runtimeToTemplate);

const uniqueTemplates = generateUniqueTemplates(runtimes);
runtimes = sortRuntimesBySupport(runtimes, uniqueTemplates);
const tableRows = generateTableRows(uniqueTemplates, runtimes);

const sortedTableRows = tableRows.sort((a, b) => {
const aCount = a.filter((column) => column !== "").length;
const bCount = b.filter((column) => column !== "").length;
const sortedRuntimes = Object.keys(runtimeToTemplate).sort((a, b) => {
return runtimeToTemplate[b].length - runtimeToTemplate[a].length;
});

return aCount > bCount ? -1 : 1;
const sortedTemplates = Object.keys(templateToRuntimes).sort((a, b) => {
return templateToRuntimes[b].length - templateToRuntimes[a].length;
});

const table = markdownTable([
[
"Template",
...runtimes.map((r) => (verboseRuntimes[r] ? verboseRuntimes[r] : r)),
],
...sortedTableRows,
["Template", ...sortedRuntimes],
...generateTableRows(sortedTemplates),
]);

const readmePath = path.join(".", "../../../README.md");
Expand Down