Design system.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

168 linhas
4.5 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. try {
  40. await rm(resolve(destCwd, 'pages'), { recursive: true });
  41. await cp(resolve(defaultCwd, 'pages'), resolve(destCwd, 'pages'), { recursive: true });
  42. } catch {
  43. // noop
  44. }
  45. await Promise.all(componentsList.map(async (componentPath) => {
  46. const destPath = resolve(destCwd, componentPath);
  47. let baseCwd = projectCwd;
  48. try {
  49. await stat(resolve(baseCwd, componentPath));
  50. } catch (errRaw) {
  51. const err = errRaw as NodeJS.ErrnoException;
  52. if (err.code === 'ENOENT') {
  53. baseCwd = defaultCwd;
  54. }
  55. }
  56. await mkdirp(dirname(destPath));
  57. await cp(
  58. resolve(baseCwd, componentPath),
  59. destPath,
  60. );
  61. process.stdout.write(`Linked ${componentPath}\n`);
  62. }));
  63. const packagesPath = await useInternalPath('.amanuensis', 'packages.json');
  64. const packagesDataJson = await readFile(packagesPath, 'utf-8');
  65. const packagesData = JSON.parse(packagesDataJson) as PackageData[];
  66. try {
  67. await Promise.all(
  68. packagesData.map(async (pkg) => {
  69. const packageDir = await useBasePath(pkg.basePath);
  70. const packageLinkDir = await useInternalPath('.amanuensis', 'next', 'node_modules', pkg.name);
  71. await mkdirp(dirname(packageLinkDir));
  72. try {
  73. await symlink(packageDir, packageLinkDir);
  74. } catch {
  75. // noop
  76. }
  77. await mkdirp(resolve(destCwd, 'content', pkg.basePath));
  78. await mkdirp(resolve(destCwd, 'pages', pkg.basePath));
  79. await Promise.all(
  80. pkg.markdown.map(async (m) => {
  81. const srcPath = resolve(cwd, pkg.basePath, m.filePath);
  82. const destPath = resolve(destCwd, 'content', pkg.basePath, m.name);
  83. await cp(srcPath, destPath);
  84. const pageDestPath = resolve(destCwd, 'pages', pkg.basePath, `${basename(m.name, extname(m.name))}.tsx`);
  85. const preambleImport = `@/${join('content', pkg.basePath, m.name)}`;
  86. await writeFile(
  87. pageDestPath,
  88. `import {NextPage} from 'next';
  89. import {Wrapper} from '@/components/Wrapper';
  90. import {ComponentContext} from '@/contexts/Component';
  91. import {PreambleContext} from '@/contexts/Preamble';
  92. import Preamble from '${preambleImport}';
  93. import {PageLayout} from '@/components/PageLayout';
  94. const IndexPage: NextPage = () => {
  95. return (
  96. <PreambleContext.Provider value={Preamble}>
  97. <Wrapper>
  98. <PageLayout />
  99. </Wrapper>
  100. </PreambleContext.Provider>
  101. );
  102. };
  103. export default IndexPage;
  104. `,
  105. // todo fetch components for display to props and preamble
  106. // todo fix problem when building with import aliases
  107. // todo find a way to build with tailwind
  108. // todo link components to next project (done)
  109. // todo merge contents of .amanuensis with next project
  110. );
  111. }),
  112. );
  113. }),
  114. );
  115. } catch (errRaw) {
  116. throw new CommandError(GenerateReturnCode.COULD_NOT_GENERATE_PAGES, 'Could not write inner page file', errRaw as Error);
  117. }
  118. try {
  119. const srcPath = resolve(cwd, 'README.md');
  120. const destPath = resolve(destCwd, 'content', 'index.md');
  121. await cp(srcPath, destPath);
  122. } catch (errRaw) {
  123. throw new CommandError(GenerateReturnCode.COULD_NOT_GENERATE_PAGES, 'Could not write index file', errRaw as Error);
  124. }
  125. process.stdout.write('done\n');
  126. };
  127. export const description = 'Generate documentation from typedoc.json' as const;
  128. export interface RefreshArgs {
  129. subcommands?: string[];
  130. }
  131. export const builder = (yargs: Argv) => yargs;
  132. const refresh = async (args: RefreshArgs) => {
  133. try {
  134. const basePath = await useBasePath();
  135. await linkComponents(basePath);
  136. } catch (errRaw) {
  137. const err = errRaw as CommandError;
  138. process.stderr.write(`${err.message}\n`);
  139. return err.exitCode;
  140. }
  141. return GenerateReturnCode.SUCCESS;
  142. };
  143. export default refresh;