|
- import {
- cp, readFile, rm, stat, writeFile,
- } from 'fs/promises';
- import { dirname, resolve } from 'path';
- import { Argv } from 'yargs';
- import { Stats } from 'fs';
- import { mkdirp } from 'mkdirp';
- import { getPackages, TypedocData } from '../utils/data';
-
- const ensureTypedocJson = async (typedocPath: string) => {
- const trueTypedocPath = resolve(typedocPath);
- process.stdout.write(`Using typedoc.json path: ${trueTypedocPath}\n`);
- process.stdout.write('Does the file exist? ');
- let statResult: Stats;
- try {
- statResult = await stat(trueTypedocPath);
- } catch (errRaw) {
- const err = errRaw as NodeJS.ErrnoException;
- if (err.code === 'ENOENT') {
- process.stdout.write('no\n');
- process.stderr.write('Could not find typedoc.json\n');
- throw new Error('Could not find typedoc.json');
- }
- process.stdout.write('maybe?\n');
- process.stderr.write('Could not ensure typedoc.json\n');
- throw err;
- }
- if (statResult.isDirectory()) {
- process.stdout.write('no\n');
- process.stderr.write('typedoc.json is a directory\n');
- throw new Error('typedoc.json is a directory');
- }
- process.stdout.write('yes\n');
- };
-
- const generateTypedocData = async () => {
- process.stdout.write('Generating typedoc data...\n');
-
- const outPath = resolve(__dirname, '..', '..', '..', '.amanuensis', 'data.json');
- const typedocBinPath = resolve(__dirname, '..', '..', '..', 'node_modules', '.bin', 'typedoc');
- const { execa } = await import('execa');
-
- await execa(typedocBinPath, ['--json', outPath], {
- stdout: 'inherit',
- stderr: 'inherit',
- });
-
- process.stdout.write('done\n');
- };
-
- const produceGroupings = async () => {
- process.stdout.write('Grouping typedoc data...\n');
-
- const typedocDataJsonPath = resolve(__dirname, '..', '..', '..', '.amanuensis', 'data.json');
- const typedocDataJson = await readFile(typedocDataJsonPath, 'utf-8');
- const typedocData = JSON.parse(typedocDataJson) as TypedocData;
-
- const packages = await getPackages(process.cwd());
- const groupings = {
- packages,
- typedocData,
- };
-
- await writeFile(typedocDataJsonPath, JSON.stringify(groupings, null, 2));
-
- process.stdout.write(`File written to ${typedocDataJsonPath}\n`);
- };
-
- const linkComponents = async () => {
- process.stdout.write('Linking components...\n');
-
- const projectCwd = resolve(process.cwd(), '.amanuensis');
- const defaultCwd = resolve(__dirname, '..', '..', '..', 'default');
- const destCwd = resolve(__dirname, '..', '..', '..');
- const componentsList = [
- 'components/Wrapper.tsx',
- ];
-
- try {
- await rm(resolve(destCwd, 'components'), { 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, '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, 'pages', pkg.basePath, m.name);
- console.log(srcPath);
- await cp(srcPath, destPath);
- }),
- );
- }),
- );
-
- // try {
- // await rm(resolve(destCwd, 'pages'), { recursive: true });
- // } catch {
- // // noop
- // }
- //
- // await cp(
- // resolve(defaultCwd, 'pages'),
- // resolve(destCwd, 'pages'),
- // { recursive: true },
- // );
-
- process.stdout.write('done\n');
- };
-
- export const description = 'Generate documentation from typedoc.json' as const;
-
- export enum GenerateReturnCode {
- SUCCESS = 0,
- NO_TYPEDOC_JSON = -1,
- COULD_NOT_GENERATE_TYPEDOC_DATA = -2,
- COULD_NOT_PRODUCE_GROUPINGS = -3,
- COULD_NOT_GENERATE_PAGES = -4,
- }
-
- export interface GenerateArgs {
- typedocJsonPath?: string;
- subcommands?: string[];
- }
-
- export const builder = (yargs: Argv) => yargs
- .option('typedocJsonPath', {
- type: 'string',
- alias: 't',
- });
-
- const generate = async (args: GenerateArgs) => {
- const {
- typedocJsonPath = resolve(process.cwd(), 'typedoc.json'),
- } = args;
-
- try {
- await ensureTypedocJson(typedocJsonPath);
- } catch {
- return GenerateReturnCode.NO_TYPEDOC_JSON;
- }
-
- try {
- await generateTypedocData();
- } catch {
- return GenerateReturnCode.COULD_NOT_GENERATE_TYPEDOC_DATA;
- }
-
- try {
- await produceGroupings();
- } catch {
- return GenerateReturnCode.COULD_NOT_PRODUCE_GROUPINGS;
- }
-
- try {
- await linkComponents();
- } catch {
- return GenerateReturnCode.COULD_NOT_GENERATE_PAGES;
- }
-
- return GenerateReturnCode.SUCCESS;
- };
-
- export default generate;
|