Design system.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

149 Zeilen
3.9 KiB

  1. import {
  2. cp,
  3. readFile,
  4. rm,
  5. stat,
  6. writeFile,
  7. symlink,
  8. } from 'fs/promises';
  9. import {
  10. dirname,
  11. resolve,
  12. basename,
  13. extname,
  14. join,
  15. } from 'path';
  16. import { Argv } from 'yargs';
  17. import { mkdirp } from 'mkdirp';
  18. import { PackageData } from '../utils/data';
  19. import { CommandError } from '../utils/error';
  20. import { useBasePath } from '../mixins/base-path';
  21. import { useInternalPath } from '../mixins/internal-path';
  22. export enum GenerateReturnCode {
  23. SUCCESS = 0,
  24. COULD_NOT_GENERATE_PAGES = -2,
  25. }
  26. const linkComponents = async (cwd: string) => {
  27. process.stdout.write('Linking components...\n');
  28. const projectCwd = resolve(cwd, '.amanuensis');
  29. const defaultCwd = await useInternalPath('src', 'next');
  30. const destCwd = await useInternalPath('.amanuensis', 'next', 'src');
  31. const componentsList = [
  32. 'components/Wrapper.tsx',
  33. ];
  34. try {
  35. await rm(resolve(destCwd, 'content'), { recursive: true });
  36. } catch {
  37. // noop
  38. }
  39. await Promise.all(componentsList.map(async (componentPath) => {
  40. const destPath = resolve(destCwd, componentPath);
  41. let baseCwd = projectCwd;
  42. try {
  43. await stat(resolve(baseCwd, componentPath));
  44. } catch (errRaw) {
  45. const err = errRaw as NodeJS.ErrnoException;
  46. if (err.code === 'ENOENT') {
  47. baseCwd = defaultCwd;
  48. }
  49. }
  50. await mkdirp(dirname(destPath));
  51. await cp(
  52. resolve(baseCwd, componentPath),
  53. destPath,
  54. );
  55. process.stdout.write(`Linked ${componentPath}\n`);
  56. }));
  57. const packagesPath = await useInternalPath('.amanuensis', 'packages.json');
  58. const packagesDataJson = await readFile(packagesPath, 'utf-8');
  59. const packagesData = JSON.parse(packagesDataJson) as PackageData[];
  60. try {
  61. await Promise.all(
  62. packagesData.map(async (pkg) => {
  63. const packageDir = await useBasePath(pkg.basePath);
  64. const packageLinkDir = await useInternalPath('.amanuensis', 'next', 'node_modules', pkg.name);
  65. await mkdirp(dirname(packageLinkDir));
  66. console.log(packageDir, packageLinkDir);
  67. await symlink(packageDir, packageLinkDir);
  68. await mkdirp(resolve(destCwd, 'content', pkg.basePath));
  69. await mkdirp(resolve(destCwd, 'pages', pkg.basePath));
  70. await Promise.all(
  71. pkg.markdown.map(async (m) => {
  72. const srcPath = resolve(cwd, pkg.basePath, m.filePath);
  73. const destPath = resolve(destCwd, 'content', pkg.basePath, m.name);
  74. await cp(srcPath, destPath);
  75. const pageDestPath = resolve(destCwd, 'pages', pkg.basePath, `${basename(m.name, extname(m.name))}.tsx`);
  76. await writeFile(
  77. pageDestPath,
  78. `import {NextPage} from 'next';
  79. import {Wrapper} from '@/components/Wrapper';
  80. import Content from '@/${join('content', pkg.basePath, m.name)}';
  81. const IndexPage: NextPage = () => {
  82. return (
  83. <Wrapper>
  84. <Content />
  85. </Wrapper>
  86. );
  87. };
  88. export default IndexPage;
  89. `,
  90. // todo fix problem when building with import aliases
  91. // todo find a way to build with tailwind
  92. // todo link components to next project (done)
  93. // todo merge contents of .amanuensis with next project
  94. );
  95. }),
  96. );
  97. }),
  98. );
  99. } catch (errRaw) {
  100. throw new CommandError(GenerateReturnCode.COULD_NOT_GENERATE_PAGES, 'Could not write inner page file', errRaw as Error);
  101. }
  102. try {
  103. const srcPath = resolve(cwd, 'README.md');
  104. const destPath = resolve(destCwd, 'content', 'index.md');
  105. await cp(srcPath, destPath);
  106. } catch (errRaw) {
  107. throw new CommandError(GenerateReturnCode.COULD_NOT_GENERATE_PAGES, 'Could not write index file', errRaw as Error);
  108. }
  109. process.stdout.write('done\n');
  110. };
  111. export const description = 'Generate documentation from typedoc.json' as const;
  112. export interface RefreshArgs {
  113. subcommands?: string[];
  114. }
  115. export const builder = (yargs: Argv) => yargs;
  116. const refresh = async (args: RefreshArgs) => {
  117. try {
  118. const basePath = await useBasePath();
  119. await linkComponents(basePath);
  120. } catch (errRaw) {
  121. const err = errRaw as CommandError;
  122. process.stderr.write(`${err.message}\n`);
  123. return err.exitCode;
  124. }
  125. return GenerateReturnCode.SUCCESS;
  126. };
  127. export default refresh;