From e2f3744c80f18abd9124a1c10b55b4d45adf50e4 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Aug 2023 11:35:59 +0100 Subject: [PATCH 1/4] feat: Table Generator Support Cases --- .../markdown-table-workflow/index.js | 137 ++++++++++-------- 1 file changed, 80 insertions(+), 57 deletions(-) diff --git a/.github/workflows/markdown-table-workflow/index.js b/.github/workflows/markdown-table-workflow/index.js index ad934d82..d0d1492a 100644 --- a/.github/workflows/markdown-table-workflow/index.js +++ b/.github/workflows/markdown-table-workflow/index.js @@ -16,49 +16,93 @@ const verboseRuntimes = { swift: "Swift", }; -const folderDenylist = [".github", ".git"]; +const verboseTemplates = { + "Analyze With Perspectiveapi": "Analyze With PerspectiveAPI", + "Generate Pdf": "Generate PDF", + "Prompt Chatgpt": "Prompt ChatGPT", + "Push Notifications With Fcm": "Push Notifications With FCM", + "Url Shortener": "URL Shortener", + "Whatsapp With Vonage": "WhatsApp With Vonage", +}; -const generateUniqueTemplates = (runtimes) => { - let templates = []; +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; +} - for (const runtime of runtimes) { - const folders = fs - .readdirSync(path.join(".", `../../../${runtime}`), { - withFileTypes: true, - }) - .filter((dirent) => dirent.isDirectory()) - .map((dirent) => dirent.name); +const folderDenylist = [".github", ".git"]; - templates.push(...folders); +function getDirectories(dirPath) { + return fs + .readdirSync(dirPath, { withFileTypes: true }) + .filter((dirent) => dirent.isDirectory()) + .map((dirent) => dirent.name) + .filter((name) => !folderDenylist.includes(name)); +} + +const runtimeDirs = getDirectories(path.join(".", "../../../")); +console.table(runtimeDirs); + +const runtimeToTemplate = {}; +for (const runtimeDir of runtimeDirs) { + const runtime = verboseRuntimes[runtimeDir] || runtimeDir; + + const templateDirs = getDirectories(path.join(".", "../../../", runtimeDir)); + runtimeToTemplate[runtime] = []; + console.log(runtime, templateDirs); + + for (const templateDir of templateDirs) { + const template = normalizeTemplate(templateDir); + runtimeToTemplate[runtime].push({ + name: template, + dir: path.join(".", runtimeDir, templateDir), + }); } - - return [...new Set(templates)]; -}; - -const generateTableRows = (templates, runtimes) => { - return templates.map((template) => { - const languagesSupport = runtimes.map((runtime) => { - return fs.existsSync(path.join(".", `../../../${runtime}/${template}`)) - ? `[✅](/${runtime}/${template})` - : "🏗️"; +} + +const templateToRuntimes = {}; +for (const runtime of Object.keys(runtimeToTemplate)) { + for (const template of runtimeToTemplate[runtime]) { + if (!(template.name in templateToRuntimes)) { + templateToRuntimes[template.name] = []; + } + + templateToRuntimes[template.name].push({ + name: runtime, + dir: template.dir, }); + } +} - return [template, ...languagesSupport]; - }); -}; - -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}`)) - ); +const sortedRuntimes = Object.keys(runtimeToTemplate).sort((a, b) => { + return runtimeToTemplate[b].length - runtimeToTemplate[a].length; +}); - return bTemplates.length - aTemplates.length; +const sortedTableRows = Object.keys(templateToRuntimes) + .sort((a, b) => { + return templateToRuntimes[b].length - templateToRuntimes[a].length; + }) + .map((template) => { + return [ + template, + ...sortedRuntimes.map((runtime) => { + const matchingRuntime = templateToRuntimes[template].find( + (r) => r.name === runtime + ); + return matchingRuntime ? `[✅](${matchingRuntime.dir})` : "🏗️"; + }), + ]; }); -}; const updateReadmeFile = (readmePath, table) => { const readme = fs.readFileSync(readmePath).toString(); @@ -77,29 +121,8 @@ const updateReadmeFile = (readmePath, table) => { } }; -let runtimes = fs - .readdirSync(path.join(".", "../../../"), { withFileTypes: true }) - .filter((dirent) => dirent.isDirectory()) - .map((dirent) => dirent.name) - .filter((folder) => !folderDenylist.includes(folder)) - .sort(); - -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; - - return aCount > bCount ? -1 : 1; -}); - const table = markdownTable([ - [ - "Template", - ...runtimes.map((r) => (verboseRuntimes[r] ? verboseRuntimes[r] : r)), - ], + ["Template", ...sortedRuntimes], ...sortedTableRows, ]); From 6a87010c1ecd715562d05931c8680fad9ca74efb Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Aug 2023 12:27:24 +0100 Subject: [PATCH 2/4] chore: cleanup --- .../markdown-table-workflow/index.js | 106 ++++++++++-------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/.github/workflows/markdown-table-workflow/index.js b/.github/workflows/markdown-table-workflow/index.js index d0d1492a..8b7b19bf 100644 --- a/.github/workflows/markdown-table-workflow/index.js +++ b/.github/workflows/markdown-table-workflow/index.js @@ -25,6 +25,16 @@ const verboseTemplates = { "Whatsapp With Vonage": "WhatsApp With Vonage", }; +const folderDenylist = [".github", ".git"]; + +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, " ") @@ -40,59 +50,47 @@ function normalizeTemplate(template) { : titleCase; } -const folderDenylist = [".github", ".git"]; - -function getDirectories(dirPath) { - return fs - .readdirSync(dirPath, { withFileTypes: true }) - .filter((dirent) => dirent.isDirectory()) - .map((dirent) => dirent.name) - .filter((name) => !folderDenylist.includes(name)); -} - -const runtimeDirs = getDirectories(path.join(".", "../../../")); -console.table(runtimeDirs); - -const runtimeToTemplate = {}; -for (const runtimeDir of runtimeDirs) { - const runtime = verboseRuntimes[runtimeDir] || runtimeDir; - - const templateDirs = getDirectories(path.join(".", "../../../", runtimeDir)); - runtimeToTemplate[runtime] = []; - console.log(runtime, templateDirs); - - for (const templateDir of templateDirs) { - const template = normalizeTemplate(templateDir); - runtimeToTemplate[runtime].push({ - name: template, - dir: path.join(".", runtimeDir, templateDir), +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) + ); + + runtimeToTemplates[runtime] = templateDirs.map((templateDir) => { + const template = normalizeTemplate(templateDir); + return { + name: template, + dir: path.join(".", runtimeDir, templateDir), + }; }); } + + return runtimeToTemplates; } -const templateToRuntimes = {}; -for (const runtime of Object.keys(runtimeToTemplate)) { - for (const template of runtimeToTemplate[runtime]) { - if (!(template.name in templateToRuntimes)) { - templateToRuntimes[template.name] = []; +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, + }); } - - templateToRuntimes[template.name].push({ - name: runtime, - dir: template.dir, - }); } + return templateToRuntimes; } -const sortedRuntimes = Object.keys(runtimeToTemplate).sort((a, b) => { - return runtimeToTemplate[b].length - runtimeToTemplate[a].length; -}); - -const sortedTableRows = Object.keys(templateToRuntimes) - .sort((a, b) => { - return templateToRuntimes[b].length - templateToRuntimes[a].length; - }) - .map((template) => { +function generateTableRows(sortedTemplates) { + return sortedTemplates.map((template) => { return [ template, ...sortedRuntimes.map((runtime) => { @@ -103,8 +101,9 @@ const sortedTableRows = Object.keys(templateToRuntimes) }), ]; }); +} -const updateReadmeFile = (readmePath, table) => { +function updateReadmeFile(readmePath, table) { const readme = fs.readFileSync(readmePath).toString(); if ( @@ -119,11 +118,22 @@ const updateReadmeFile = (readmePath, table) => { fs.writeFileSync(readmePath, newReadme); } -}; +} + +const runtimeToTemplate = getRuntimeToTemplates(); +const templateToRuntimes = getTemplateToRuntimes(runtimeToTemplate); + +const sortedRuntimes = Object.keys(runtimeToTemplate).sort((a, b) => { + return runtimeToTemplate[b].length - runtimeToTemplate[a].length; +}); + +const sortedTemplates = Object.keys(templateToRuntimes).sort((a, b) => { + return templateToRuntimes[b].length - templateToRuntimes[a].length; +}); const table = markdownTable([ ["Template", ...sortedRuntimes], - ...sortedTableRows, + ...generateTableRows(sortedTemplates), ]); const readmePath = path.join(".", "../../../README.md"); From 68124c34857bae7f1d528ecc72654b4d0c02b5c0 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Aug 2023 12:36:24 +0100 Subject: [PATCH 3/4] fix: typo --- .github/workflows/markdown-table-workflow/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/markdown-table-workflow/index.js b/.github/workflows/markdown-table-workflow/index.js index 8b7b19bf..8fa28499 100644 --- a/.github/workflows/markdown-table-workflow/index.js +++ b/.github/workflows/markdown-table-workflow/index.js @@ -20,7 +20,7 @@ const verboseTemplates = { "Analyze With Perspectiveapi": "Analyze With PerspectiveAPI", "Generate Pdf": "Generate PDF", "Prompt Chatgpt": "Prompt ChatGPT", - "Push Notifications With Fcm": "Push Notifications With FCM", + "Push Notification With Fcm": "Push Notifications With FCM", "Url Shortener": "URL Shortener", "Whatsapp With Vonage": "WhatsApp With Vonage", }; From ae9f4a556ecd2893d0f7a5c95efeca5395948758 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 3 Aug 2023 10:14:22 +0100 Subject: [PATCH 4/4] feat: override words --- .../markdown-table-workflow/index.js | 74 +++++++++++++------ 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/.github/workflows/markdown-table-workflow/index.js b/.github/workflows/markdown-table-workflow/index.js index 8fa28499..965e4b51 100644 --- a/.github/workflows/markdown-table-workflow/index.js +++ b/.github/workflows/markdown-table-workflow/index.js @@ -16,13 +16,41 @@ const verboseRuntimes = { swift: "Swift", }; -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", +const overrideWords = { + a: "a", + an: "an", + and: "and", + as: "as", + at: "at", + but: "but", + by: "by", + for: "for", + if: "if", + in: "in", + nor: "nor", + of: "of", + on: "on", + or: "or", + so: "so", + the: "the", + to: "to", + up: "up", + with: "with", + yet: "yet", + is: "is", + are: "are", + was: "was", + were: "were", + has: "has", + have: "have", + been: "been", + am: "am", + perspectiveapi: "PerspectiveAPI", + pdf: "PDF", + chatgpt: "ChatGPT", + fcm: "FCM", + url: "URL", + whatsapp: "WhatsApp", }; const folderDenylist = [".github", ".git"]; @@ -35,19 +63,21 @@ function getDirectories(dirPath) { .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 normalizeTemplateName(template) { + return template + .replace(/[_-]|([a-z])([A-Z])/g, (_, p1, p2) => (p1 ? `${p1} ${p2}` : " ")) + .toLowerCase() + .split(" ") + .map((word, i, words) => { + const overrideWord = overrideWords[word]; + if (!overrideWord) { + return word.charAt(0).toUpperCase() + word.slice(1); + } + return i === 0 || i === words.length - 1 + ? overrideWord.charAt(0).toUpperCase() + overrideWord.slice(1) + : overrideWord; + }) + .join(" "); } function getRuntimeToTemplates() { @@ -61,9 +91,9 @@ function getRuntimeToTemplates() { ); runtimeToTemplates[runtime] = templateDirs.map((templateDir) => { - const template = normalizeTemplate(templateDir); + const name = normalizeTemplateName(templateDir); return { - name: template, + name: name, dir: path.join(".", runtimeDir, templateDir), }; });