From 2e24d2b205ffeea29a33c4b295dd19efc43da53c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Tue, 1 Aug 2023 10:26:08 +0100 Subject: [PATCH] feat: improve table --- .../markdown-table-workflow/index.js | 145 ++++++++++++------ README.md | 2 +- 2 files changed, 96 insertions(+), 51 deletions(-) diff --git a/.github/workflows/markdown-table-workflow/index.js b/.github/workflows/markdown-table-workflow/index.js index de15a53c..ad52b76d 100644 --- a/.github/workflows/markdown-table-workflow/index.js +++ b/.github/workflows/markdown-table-workflow/index.js @@ -1,71 +1,116 @@ -import * as fs from "fs"; -import * as path from "path"; -import {markdownTable} from 'markdown-table'; +import fs from "node:fs"; +import path from "node:path"; +import { markdownTable } from "markdown-table"; const verboseRuntimes = { - cpp: "C++", - dart: "Dart", - deno: "Deno", - dotnet: ".NET", - java: "Java", - kotlin: "Kotlin", - node: "Node.js", - php: "PHP", - python: "Python", - ruby: "Ruby", - swift: "Swift" + cpp: "C++", + dart: "Dart", + deno: "Deno", + dotnet: ".NET", + java: "Java", + kotlin: "Kotlin", + node: "Node.js", + php: "PHP", + python: "Python", + ruby: "Ruby", + swift: "Swift", }; -const folderDenylist = [ '.github', '.git' ]; +const folderDenylist = [".github", ".git"]; -const runtimes = fs.readdirSync(path.join('.', '../../../'), { withFileTypes: true }) - .filter(dirent => dirent.isDirectory()) - .map(dirent => dirent.name) - .filter((folder) => !folderDenylist.includes(folder)) - .sort(); +const generateUniqueTemplates = (runtimes) => { + let templates = []; -const templates = []; + for (const runtime of runtimes) { + const folders = fs + .readdirSync(path.join(".", `../../../${runtime}`), { + withFileTypes: true, + }) + .filter((dirent) => dirent.isDirectory()) + .map((dirent) => dirent.name); -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); -} + templates.push(...folders); + } -const uniqueTemplates = [...new Set(templates)]; + return [...new Set(templates)]; +}; -const rows = uniqueTemplates.map((template) => { +const generateTableRows = (templates, runtimes) => { + return templates.map((template) => { const languagesSupport = runtimes.map((runtime) => { - return fs.existsSync(path.join('.', `../../../${runtime}/${template}`)) ? `[✅](/${runtime}/${template})` : '❌'; - }) + return fs.existsSync(path.join(".", `../../../${runtime}/${template}`)) + ? `[✅](/${runtime}/${template})` + : "🏗️"; + }); return [template, ...languagesSupport]; -}); + }); +}; -const table = markdownTable([ - ['Template', ...runtimes.map((r) => verboseRuntimes[r] ? verboseRuntimes[r] : r)], - ...rows.sort((a, b) => { - const aCount = a.filter((column) => column !== ''); - const bCount = b.filter((column) => column !== ''); +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}`)) + ); + + return bTemplates.length - aTemplates.length; + }); +}; - return aCount > bCount ? -1 : 1; - }) - ]); +const updateReadmeFile = (readmePath, table) => { + const readme = fs.readFileSync(readmePath).toString(); + + if ( + readme.includes("") && + readme.includes("") + ) { + const newReadme = `${ + readme.split("")[0] + }\n${table}\n${ + readme.split("")[1] + }`; + + 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 readmePath = path.join('.', "../../../README.md"); -const readme = fs.readFileSync(readmePath).toString(); -let newReadme = ''; +const uniqueTemplates = generateUniqueTemplates(runtimes); +runtimes = sortRuntimesBySupport(runtimes, uniqueTemplates); +const tableRows = generateTableRows(uniqueTemplates, runtimes); -if(readme.includes('') && readme.includes('')) { - newReadme += readme.split('')[0]; - newReadme += '\n'; +const sortedTableRows = tableRows.sort((a, b) => { + const aCount = a.filter((column) => column !== "").length; + const bCount = b.filter((column) => column !== "").length; - newReadme += table; + return aCount > bCount ? -1 : 1; +}); - newReadme += '\n'; - newReadme += readme.split('')[1]; +const styles = ` + +`; -fs.writeFileSync(readmePath, newReadme); \ No newline at end of file +const table = markdownTable([ + [ + "Template", + ...runtimes.map((r) => (verboseRuntimes[r] ? verboseRuntimes[r] : r)), + ], + ...sortedTableRows, +]); + +const tableWithStyles = `${styles}\n${table}`; +const readmePath = path.join(".", "../../../README.md"); +updateReadmeFile(readmePath, tableWithStyles); diff --git a/README.md b/README.md index 51aca26a..6c33a3da 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Templates for [Appwrite](https://appwrite.io/) Functions. These templates can be ✅ = Done - Function is implemented in this runtime. -❌ = Missing - Function isn't implemented in this runtime yet. Contributions are welcomed. +🏗️ = Missing - Function isn't implemented in this runtime yet. Contributions are welcomed. ## Contributing