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: improve docs #35

Merged
merged 1 commit into from
Aug 1, 2023
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
145 changes: 95 additions & 50 deletions .github/workflows/markdown-table-workflow/index.js
Original file line number Diff line number Diff line change
@@ -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("<!-- TABLE:START -->") &&
readme.includes("<!-- TABLE:END -->")
) {
const newReadme = `${
readme.split("<!-- TABLE:START -->")[0]
}<!-- TABLE:START -->\n${table}\n<!-- TABLE:END -->${
readme.split("<!-- TABLE:END -->")[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('<!-- TABLE:START -->') && readme.includes('<!-- TABLE:END -->')) {
newReadme += readme.split('<!-- TABLE:START -->')[0];
newReadme += '<!-- TABLE:START -->\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<!-- TABLE:END -->';
newReadme += readme.split('<!-- TABLE:END -->')[1];
const styles = `
<style>
table th:first-of-type {
width: 200px;
}
</style>
`;

fs.writeFileSync(readmePath, newReadme);
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);
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down