Design system.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

71 行
2.1 KiB

  1. import path from 'path';
  2. import fs from 'fs/promises';
  3. export const PLATFORMS = {
  4. react: {
  5. label: 'React',
  6. href: '/web/react',
  7. },
  8. };
  9. export const getMainReadmeFileContents = async (p: string) => {
  10. const readmePath = path.resolve('../..', p);
  11. try {
  12. const contents = await fs.readFile(readmePath);
  13. return contents.toString('utf-8');
  14. } catch (errRaw) {
  15. const err = errRaw as NodeJS.ErrnoException;
  16. if (err.code === 'ENOENT') {
  17. return undefined;
  18. }
  19. throw err;
  20. }
  21. };
  22. export interface GetDocsOptions {
  23. deriveLabelFromFilename?: (filename: string) => string;
  24. deriveHrefFromFilename?: (filename: string) => string;
  25. }
  26. export const getDocs = async (options = {} as GetDocsOptions) => {
  27. const docsPath = path.resolve('../../docs');
  28. const docs = await fs.readdir(docsPath);
  29. const {
  30. deriveLabelFromFilename = (filename) => filename,
  31. deriveHrefFromFilename = (filename) => `/docs/${filename}`,
  32. } = options;
  33. return docs.map((d) => ({
  34. id: d,
  35. href: deriveHrefFromFilename(d),
  36. label: deriveLabelFromFilename(d),
  37. }));
  38. };
  39. export const getPlatforms = async () => {
  40. const categoriesPath = path.resolve('../../categories');
  41. const categories = await fs.readdir(categoriesPath);
  42. const platformsCheck = await Promise.all(categories.map(async (category) => {
  43. const categoryDir = path.resolve(categoriesPath, category);
  44. const availableCategoryPlatforms = await fs.readdir(categoryDir);
  45. const platformsWithReadmeCheck = await Promise.all(availableCategoryPlatforms.map(async (platform) => {
  46. const platformDir = path.resolve(categoryDir, platform);
  47. try {
  48. const platformReadmePath = path.resolve(platformDir, 'README.md');
  49. await fs.stat(platformReadmePath);
  50. return [platform, true];
  51. } catch (errorRaw) {
  52. const error = errorRaw as NodeJS.ErrnoException;
  53. if (error.code !== 'ENOENT') {
  54. throw error;
  55. }
  56. }
  57. return [platform, false];
  58. }));
  59. return platformsWithReadmeCheck
  60. .filter(([, hasReadme]) => hasReadme)
  61. .map(([platform]) => platform)
  62. }));
  63. return Array.from(new Set(platformsCheck.flat()));
  64. };