Skip to content

Commit

Permalink
Refactor cli.ts a bit -- Split up run() (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane authored Dec 23, 2024
1 parent 5e20516 commit 23ae2e4
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions sphinx_js/js/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
TypeDocReader,
PackageJsonReader,
TSConfigReader,
ProjectReflection,
} from "typedoc";
import { Converter } from "./convertTopLevel.ts";
import { SphinxJsConfig } from "./sphinxJsConfig.ts";
Expand All @@ -21,6 +22,14 @@ const ExitCodes = {
Watching: 7,
};

export class ExitError extends Error {
code: number;
constructor(code: number) {
super();
this.code = code;
}
}

async function bootstrapAppTypedoc0_25(args: string[]): Promise<Application> {
return await Application.bootstrapWithPlugins(
{
Expand All @@ -36,12 +45,17 @@ async function bootstrapAppTypedoc0_25(args: string[]): Promise<Application> {
);
}

export class ExitError extends Error {
code: number;
constructor(code: number) {
super();
this.code = code;
async function makeApp(args: string[]): Promise<Application> {
// Most of this stuff is copied from typedoc/src/lib/cli.ts
let app = await bootstrapAppTypedoc0_25(args);
if (app.options.getValue("version")) {
console.log(app.toString());
throw new ExitError(ExitCodes.Ok);
}
app.extraData = {};
app.options.getValue("modifierTags").push("@hidetype");
app.options.getValue("blockTags").push("@destructure");
return app;
}

async function loadConfig(
Expand All @@ -54,24 +68,8 @@ async function loadConfig(
return configModule.config;
}

export async function run(
args: string[],
): Promise<[Application, TopLevelIR[]]> {
async function typedocConvert(app: Application): Promise<ProjectReflection> {
// Most of this stuff is copied from typedoc/src/lib/cli.ts
let app = await bootstrapAppTypedoc0_25(args);
if (app.options.getValue("version")) {
console.log(app.toString());
throw new ExitError(ExitCodes.Ok);
}
app.extraData = {};
app.options.getValue("modifierTags").push("@hidetype");
app.options.getValue("blockTags").push("@destructure");
const userConfigPath = app.options.getValue("sphinxJsConfig");
const config = await loadConfig(userConfigPath);
app.logger.info(`Loaded user config from ${userConfigPath}`);
const symbolToType = redirectPrivateTypes(app);
await config.preConvert?.(app);

const project = await app.convert();
if (!project) {
throw new ExitError(ExitCodes.CompileError);
Expand All @@ -90,7 +88,19 @@ export async function run(
) {
throw new ExitError(ExitCodes.ValidationError);
}
return project;
}

export async function run(
args: string[],
): Promise<[Application, TopLevelIR[]]> {
let app = await makeApp(args);
const userConfigPath = app.options.getValue("sphinxJsConfig");
const config = await loadConfig(userConfigPath);
app.logger.info(`Loaded user config from ${userConfigPath}`);
const symbolToType = redirectPrivateTypes(app);
await config.preConvert?.(app);
const project = await typedocConvert(app);
const basePath = app.options.getValue("basePath");
const converter = new Converter(project, basePath, config, symbolToType);
converter.computePaths();
Expand Down

0 comments on commit 23ae2e4

Please sign in to comment.