|
- import {
- cp,
- readFile,
- rm,
- stat,
- writeFile,
- symlink,
- } from 'fs/promises';
- import {
- dirname,
- resolve,
- basename,
- extname,
- join,
- } from 'path';
- import { Argv } from 'yargs';
- import { mkdirp } from 'mkdirp';
- import { PackageData } from '../utils/data';
- import { CommandError } from '../utils/error';
- import { useBasePath } from '../mixins/base-path';
- import { useInternalPath } from '../mixins/internal-path';
-
- export enum GenerateReturnCode {
- SUCCESS = 0,
- COULD_NOT_GENERATE_PAGES = -2,
- }
-
- const linkComponents = async (cwd: string) => {
- process.stdout.write('Linking components...\n');
-
- const projectCwd = resolve(cwd, '.amanuensis');
- const defaultCwd = await useInternalPath('src', 'next');
- const destCwd = await useInternalPath('.amanuensis', 'next', 'src');
- 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 packagesPath = await useInternalPath('.amanuensis', 'packages.json');
- const packagesDataJson = await readFile(packagesPath, 'utf-8');
- const packagesData = JSON.parse(packagesDataJson) as PackageData[];
-
- try {
- await Promise.all(
- packagesData.map(async (pkg) => {
- const packageDir = await useBasePath(pkg.basePath);
- const packageLinkDir = await useInternalPath('.amanuensis', 'next', 'node_modules', pkg.name);
- await mkdirp(dirname(packageLinkDir));
- console.log(packageDir, packageLinkDir);
- await symlink(packageDir, packageLinkDir);
-
- await mkdirp(resolve(destCwd, 'content', pkg.basePath));
- await mkdirp(resolve(destCwd, 'pages', pkg.basePath));
- await Promise.all(
- pkg.markdown.map(async (m) => {
- const srcPath = resolve(cwd, pkg.basePath, m.filePath);
- const destPath = resolve(destCwd, 'content', pkg.basePath, m.name);
- await cp(srcPath, destPath);
-
- const pageDestPath = resolve(destCwd, 'pages', pkg.basePath, `${basename(m.name, extname(m.name))}.tsx`);
- 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 (done)
- // todo merge contents of .amanuensis with next project
- );
- }),
- );
- }),
- );
- } catch (errRaw) {
- throw new CommandError(GenerateReturnCode.COULD_NOT_GENERATE_PAGES, 'Could not write inner page file', errRaw as Error);
- }
-
- try {
- const srcPath = resolve(cwd, 'README.md');
- const destPath = resolve(destCwd, 'content', 'index.md');
- await cp(srcPath, destPath);
- } catch (errRaw) {
- throw new CommandError(GenerateReturnCode.COULD_NOT_GENERATE_PAGES, 'Could not write index file', errRaw as Error);
- }
-
- process.stdout.write('done\n');
- };
-
- export const description = 'Generate documentation from typedoc.json' as const;
-
- export interface RefreshArgs {
- subcommands?: string[];
- }
-
- export const builder = (yargs: Argv) => yargs;
-
- const refresh = async (args: RefreshArgs) => {
- try {
- const basePath = await useBasePath();
- await linkComponents(basePath);
- } catch (errRaw) {
- const err = errRaw as CommandError;
- process.stderr.write(`${err.message}\n`);
- return err.exitCode;
- }
-
- return GenerateReturnCode.SUCCESS;
- };
-
- export default refresh;
|