|
- import {
- cp, readFile, rm, stat, writeFile,
- } from 'fs/promises';
- import { dirname, resolve, basename, extname, join } from 'path';
- import { Argv } from 'yargs';
- import { mkdirp } from 'mkdirp';
- import { TypedocData } from '../utils/data';
-
- const linkComponents = async () => {
- process.stdout.write('Linking components...\n');
-
- const projectCwd = resolve(process.cwd(), '.amanuensis');
- const defaultCwd = resolve(__dirname, '..', '..', '..', 'src', 'next');
- const destCwd = resolve(__dirname, '..', '..', '..', '.amanuensis', 'next');
- const componentsList = [
- 'components/Wrapper.tsx',
- ];
-
- try {
- await rm(resolve(destCwd, 'content'), { recursive: true });
- } catch {
- // noop
- }
- await Promise.all(componentsList.map(async (componentPath) => {
- const destPath = resolve(destCwd, componentPath);
- let baseCwd = projectCwd;
-
- try {
- await stat(resolve(baseCwd, componentPath));
- } catch (errRaw) {
- const err = errRaw as NodeJS.ErrnoException;
- if (err.code === 'ENOENT') {
- baseCwd = defaultCwd;
- }
- }
-
- await mkdirp(dirname(destPath));
- await cp(
- resolve(baseCwd, componentPath),
- destPath,
- );
- process.stdout.write(`Linked ${componentPath}\n`);
- }));
-
- const typedocDataJsonPath = resolve(__dirname, '..', '..', '..', '.amanuensis', 'data.json');
- const typedocDataJson = await readFile(typedocDataJsonPath, 'utf-8');
- const typedocData = JSON.parse(typedocDataJson) as TypedocData;
-
- await Promise.all(
- typedocData.packages.map(async (pkg: any) => {
- await mkdirp(resolve(destCwd, 'content', pkg.basePath));
- await mkdirp(resolve(destCwd, 'pages', pkg.basePath));
- await Promise.all(
- pkg.markdown.map(async (m: any) => {
- const srcPath = resolve(process.cwd(), pkg.basePath, m.filePath);
- const destPath = resolve(destCwd, 'content', pkg.basePath, m.name);
- const pageDestPath = resolve(destCwd, 'pages', pkg.basePath, `${basename(m.name, extname(m.name))}.tsx`);
- await cp(srcPath, destPath);
- await writeFile(
- pageDestPath,
- `import {NextPage} from 'next';
- import {Wrapper} from '@/components/Wrapper';
- import Content from '@/${join('content', pkg.basePath, m.name)}';
-
- const IndexPage: NextPage = () => {
- return (
- <Wrapper>
- <Content />
- </Wrapper>
- );
- };
-
- export default IndexPage;
- `,
- // todo fix problem when building with import aliases
- // todo find a way to build with tailwind
- // todo link components to next project
- );
- }),
- );
- }),
- );
-
- const srcPath = resolve(process.cwd(), 'README.md');
- const destPath = resolve(destCwd, 'content', 'index.md');
- await cp(srcPath, destPath);
-
- process.stdout.write('done\n');
- };
-
- export const description = 'Generate documentation from typedoc.json' as const;
-
- export enum GenerateReturnCode {
- SUCCESS = 0,
- COULD_NOT_GENERATE_PAGES = -1,
- }
-
- export interface RefreshArgs {
- subcommands?: string[];
- }
-
- export const builder = (yargs: Argv) => yargs
- .option('typedocJsonPath', {
- type: 'string',
- alias: 't',
- });
-
- const refresh = async (args: RefreshArgs) => {
- try {
- await linkComponents();
- } catch {
- return GenerateReturnCode.COULD_NOT_GENERATE_PAGES;
- }
-
- return GenerateReturnCode.SUCCESS;
- };
-
- export default refresh;
|