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

137 行
3.6 KiB

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