|
- import { stat, writeFile } from 'fs/promises';
- import { resolve } from 'path';
- import { Argv } from 'yargs';
- import { Stats } from 'fs';
- import { getPackages } from '../utils/data';
- import { useBasePath } from '../mixins/base-path';
- import { CommandError } from '../utils/error';
- import { useInternalPath } from '../mixins/internal-path';
-
- export enum GenerateReturnCode {
- SUCCESS = 0,
- NO_TYPEDOC_JSON = -3,
- COULD_NOT_GENERATE_TYPEDOC_DATA = -4,
- COULD_NOT_GENERATE_PACKAGE_DATA = -5,
- }
-
- 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');
- throw new CommandError(GenerateReturnCode.NO_TYPEDOC_JSON, 'Could not find typedoc.json');
- }
- process.stdout.write('maybe?\n');
- throw new CommandError(GenerateReturnCode.NO_TYPEDOC_JSON, 'Could not ensure typedoc.json', err);
- }
-
- if (statResult.isDirectory()) {
- process.stdout.write('no\n');
- throw new CommandError(GenerateReturnCode.NO_TYPEDOC_JSON, 'typedoc.json is a directory');
- }
- process.stdout.write('yes\n');
- };
-
- const generateTypedocData = async (basePath: string, internalPath: string) => {
- process.stdout.write('Generating typedoc data...\n');
- const outPath = resolve(internalPath, '.amanuensis', 'types.json');
- const typedocBinPath = resolve(internalPath, 'node_modules', '.bin', 'typedoc');
- const { execa } = await import('execa');
-
- const result = await execa(typedocBinPath, ['--json', outPath, '--pretty', 'false'], {
- stdout: 'inherit',
- stderr: 'inherit',
- cwd: basePath,
- });
-
- if (result.exitCode !== 0) {
- process.stdout.write('failed\n');
- throw new CommandError(GenerateReturnCode.COULD_NOT_GENERATE_TYPEDOC_DATA, 'Could not generate typedoc data');
- }
-
- process.stdout.write('done\n');
- };
-
- const generatePackageData = async (
- basePath: string,
- internalPath: string,
- ) => {
- process.stdout.write('Grouping typedoc data...\n');
- const configPath = resolve(basePath, 'amanuensis.config.json');
- const typedocDataJsonPath = resolve(internalPath, '.amanuensis', 'packages.json');
-
- const packages = await getPackages(configPath, basePath);
-
- try {
- await writeFile(typedocDataJsonPath, JSON.stringify(packages));
- process.stdout.write(`File written to ${typedocDataJsonPath}\n`);
- } catch (errRaw) {
- const err = errRaw as NodeJS.ErrnoException;
- process.stderr.write(`Could not write to ${typedocDataJsonPath}: ${err.message}\n`);
- throw new CommandError(GenerateReturnCode.COULD_NOT_GENERATE_PACKAGE_DATA, 'Could not generate package data', err);
- }
- };
-
- export const description = 'Analyze project for fetching documentation data' as const;
-
- export interface AnalyzeArgs {
- typedocJsonPath?: string;
- subcommands?: string[];
- }
-
- export const builder = (yargs: Argv) => yargs
- .option('typedocJsonPath', {
- type: 'string',
- alias: 't',
- });
-
- const analyze = async (args: AnalyzeArgs) => {
- try {
- const basePath = await useBasePath();
- process.stdout.write(`Using base path: ${basePath}\n`);
-
- const typedocJsonPath = args.typedocJsonPath ?? resolve(basePath, 'typedoc.json');
- await ensureTypedocJson(typedocJsonPath);
-
- const internalPath = await useInternalPath();
- await generateTypedocData(basePath, internalPath);
- await generatePackageData(basePath, internalPath);
- } catch (errRaw) {
- const err = errRaw as CommandError;
- process.stderr.write(`${err.message}\n`);
- return err.exitCode;
- }
-
- return GenerateReturnCode.SUCCESS;
- };
-
- export default analyze;
|