Design system.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

119 lignes
3.1 KiB

  1. import {
  2. cp, readFile, rm, stat, writeFile,
  3. } from 'fs/promises';
  4. import { dirname, resolve, basename, extname, join } from 'path';
  5. import { Argv } from 'yargs';
  6. import { mkdirp } from 'mkdirp';
  7. import { TypedocData } from '../utils/data';
  8. const linkComponents = async () => {
  9. process.stdout.write('Linking components...\n');
  10. const projectCwd = resolve(process.cwd(), '.amanuensis');
  11. const defaultCwd = resolve(__dirname, '..', '..', '..', 'src', 'next');
  12. const destCwd = resolve(__dirname, '..', '..', '..', '.amanuensis', 'next');
  13. const componentsList = [
  14. 'components/Wrapper.tsx',
  15. ];
  16. try {
  17. await rm(resolve(destCwd, 'content'), { recursive: true });
  18. } catch {
  19. // noop
  20. }
  21. await Promise.all(componentsList.map(async (componentPath) => {
  22. const destPath = resolve(destCwd, componentPath);
  23. let baseCwd = projectCwd;
  24. try {
  25. await stat(resolve(baseCwd, componentPath));
  26. } catch (errRaw) {
  27. const err = errRaw as NodeJS.ErrnoException;
  28. if (err.code === 'ENOENT') {
  29. baseCwd = defaultCwd;
  30. }
  31. }
  32. await mkdirp(dirname(destPath));
  33. await cp(
  34. resolve(baseCwd, componentPath),
  35. destPath,
  36. );
  37. process.stdout.write(`Linked ${componentPath}\n`);
  38. }));
  39. const typedocDataJsonPath = resolve(__dirname, '..', '..', '..', '.amanuensis', 'data.json');
  40. const typedocDataJson = await readFile(typedocDataJsonPath, 'utf-8');
  41. const typedocData = JSON.parse(typedocDataJson) as TypedocData;
  42. await Promise.all(
  43. typedocData.packages.map(async (pkg: any) => {
  44. await mkdirp(resolve(destCwd, 'content', pkg.basePath));
  45. await mkdirp(resolve(destCwd, 'pages', pkg.basePath));
  46. await Promise.all(
  47. pkg.markdown.map(async (m: any) => {
  48. const srcPath = resolve(process.cwd(), pkg.basePath, m.filePath);
  49. const destPath = resolve(destCwd, 'content', pkg.basePath, m.name);
  50. const pageDestPath = resolve(destCwd, 'pages', pkg.basePath, `${basename(m.name, extname(m.name))}.tsx`);
  51. await cp(srcPath, destPath);
  52. await writeFile(
  53. pageDestPath,
  54. `import {NextPage} from 'next';
  55. import {Wrapper} from '@/components/Wrapper';
  56. import Content from '@/${join('content', pkg.basePath, m.name)}';
  57. const IndexPage: NextPage = () => {
  58. return (
  59. <Wrapper>
  60. <Content />
  61. </Wrapper>
  62. );
  63. };
  64. export default IndexPage;
  65. `,
  66. // todo fix problem when building with import aliases
  67. // todo find a way to build with tailwind
  68. // todo link components to next project
  69. );
  70. }),
  71. );
  72. }),
  73. );
  74. const srcPath = resolve(process.cwd(), 'README.md');
  75. const destPath = resolve(destCwd, 'content', 'index.md');
  76. await cp(srcPath, destPath);
  77. process.stdout.write('done\n');
  78. };
  79. export const description = 'Generate documentation from typedoc.json' as const;
  80. export enum GenerateReturnCode {
  81. SUCCESS = 0,
  82. COULD_NOT_GENERATE_PAGES = -1,
  83. }
  84. export interface RefreshArgs {
  85. subcommands?: string[];
  86. }
  87. export const builder = (yargs: Argv) => yargs
  88. .option('typedocJsonPath', {
  89. type: 'string',
  90. alias: 't',
  91. });
  92. const refresh = async (args: RefreshArgs) => {
  93. try {
  94. await linkComponents();
  95. } catch {
  96. return GenerateReturnCode.COULD_NOT_GENERATE_PAGES;
  97. }
  98. return GenerateReturnCode.SUCCESS;
  99. };
  100. export default refresh;