|
- import path from 'path';
- import fs from 'fs/promises';
-
- export const PLATFORMS = {
- react: {
- label: 'React',
- href: '/web/react',
- },
- };
-
- export const getMainReadmeFileContents = async (p: string) => {
- const readmePath = path.resolve('../..', p);
- try {
- const contents = await fs.readFile(readmePath);
- return contents.toString('utf-8');
- } catch (errRaw) {
- const err = errRaw as NodeJS.ErrnoException;
-
- if (err.code === 'ENOENT') {
- return undefined;
- }
- throw err;
- }
- };
-
- export interface GetDocsOptions {
- deriveLabelFromFilename?: (filename: string) => string;
- deriveHrefFromFilename?: (filename: string) => string;
- }
-
- export const getDocs = async (options = {} as GetDocsOptions) => {
- const docsPath = path.resolve('../../docs');
- const docs = await fs.readdir(docsPath);
- const {
- deriveLabelFromFilename = (filename) => filename,
- deriveHrefFromFilename = (filename) => `/docs/${filename}`,
- } = options;
- return docs.map((d) => ({
- id: d,
- href: deriveHrefFromFilename(d),
- label: deriveLabelFromFilename(d),
- }));
- };
-
- export const getPlatforms = async () => {
- const categoriesPath = path.resolve('../../categories');
- const categories = await fs.readdir(categoriesPath);
- const platformsCheck = await Promise.all(categories.map(async (category) => {
- const categoryDir = path.resolve(categoriesPath, category);
- const availableCategoryPlatforms = await fs.readdir(categoryDir);
- const platformsWithReadmeCheck = await Promise.all(availableCategoryPlatforms.map(async (platform) => {
- const platformDir = path.resolve(categoryDir, platform);
- try {
- const platformReadmePath = path.resolve(platformDir, 'README.md');
- await fs.stat(platformReadmePath);
- return [platform, true];
- } catch (errorRaw) {
- const error = errorRaw as NodeJS.ErrnoException;
- if (error.code !== 'ENOENT') {
- throw error;
- }
- }
- return [platform, false];
- }));
- return platformsWithReadmeCheck
- .filter(([, hasReadme]) => hasReadme)
- .map(([platform]) => platform)
- }));
- return Array.from(new Set(platformsCheck.flat()));
- };
|