mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 11:03:30 +01:00
ci: fix typechecking in ci
This commit is contained in:
25
packages/docgen/src/cli.ts
Normal file
25
packages/docgen/src/cli.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env node
|
||||
import { createCommand } from 'commander';
|
||||
import { build } from './index.js';
|
||||
import packageFile from '../package.json';
|
||||
|
||||
export interface CLIOptions {
|
||||
input: string[];
|
||||
custom: string;
|
||||
root: string;
|
||||
output: string;
|
||||
typescript: boolean;
|
||||
}
|
||||
|
||||
const command = createCommand()
|
||||
.version(packageFile.version)
|
||||
.option('-i, --input <string...>', 'Source directories to parse JSDocs in')
|
||||
.option('-c, --custom <string>', 'Custom docs definition file to use')
|
||||
.option('-r, --root [string]', 'Root directory of the project', '.')
|
||||
.option('-o, --output <string>', 'Path to output file')
|
||||
.option('--typescript', '', false);
|
||||
|
||||
const program = command.parse(process.argv);
|
||||
const options = program.opts<CLIOptions>();
|
||||
|
||||
build(options);
|
||||
@@ -1,20 +1,10 @@
|
||||
#!/usr/bin/env node
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join, basename, extname, dirname, relative } from 'node:path';
|
||||
import { createCommand } from 'commander';
|
||||
import { dirname, join, extname, basename, relative } from 'node:path';
|
||||
import jsdoc2md from 'jsdoc-to-markdown';
|
||||
import { Application, DeclarationReflection, TSConfigReader } from 'typedoc';
|
||||
import { Documentation } from './documentation.js';
|
||||
import type { ChildTypes, CustomDocs, RootTypes } from './interfaces/index.js';
|
||||
import packageFile from '../package.json';
|
||||
|
||||
interface CLIOptions {
|
||||
input: string[];
|
||||
custom: string;
|
||||
root: string;
|
||||
output: string;
|
||||
typescript: boolean;
|
||||
}
|
||||
import { type DeclarationReflection, Application, TSConfigReader } from 'typedoc';
|
||||
import type { CLIOptions } from './cli';
|
||||
import { Documentation } from './documentation';
|
||||
import type { RootTypes, ChildTypes, CustomDocs } from './interfaces';
|
||||
|
||||
interface CustomFiles {
|
||||
id?: string;
|
||||
@@ -27,79 +17,70 @@ interface CustomFiles {
|
||||
}[];
|
||||
}
|
||||
|
||||
const command = createCommand()
|
||||
.version(packageFile.version)
|
||||
.option('-i, --input <string...>', 'Source directories to parse JSDocs in')
|
||||
.option('-c, --custom <string>', 'Custom docs definition file to use')
|
||||
.option('-r, --root [string]', 'Root directory of the project', '.')
|
||||
.option('-o, --output <string>', 'Path to output file')
|
||||
.option('--typescript', '', false);
|
||||
|
||||
const program = command.parse(process.argv);
|
||||
const options = program.opts<CLIOptions>();
|
||||
|
||||
let data: (RootTypes & ChildTypes)[] | DeclarationReflection[] = [];
|
||||
if (options.typescript) {
|
||||
console.log('Parsing Typescript in source files...');
|
||||
const app = new Application();
|
||||
app.options.addReader(new TSConfigReader());
|
||||
app.bootstrap({ entryPoints: options.input });
|
||||
const project = app.convert();
|
||||
if (project) {
|
||||
// @ts-expect-error
|
||||
data = app.serializer.toObject(project).children!;
|
||||
console.log(`${data.length} items parsed.`);
|
||||
}
|
||||
} else {
|
||||
console.log('Parsing JSDocs in source files...');
|
||||
data = jsdoc2md.getTemplateDataSync({ files: options.input }) as (RootTypes & ChildTypes)[];
|
||||
console.log(`${data.length} JSDoc items parsed.`);
|
||||
}
|
||||
|
||||
const custom: Record<string, CustomDocs> = {};
|
||||
if (options.custom) {
|
||||
console.log('Loading custom docs files...');
|
||||
const customDir = dirname(options.custom);
|
||||
const file = readFileSync(options.custom, 'utf-8');
|
||||
const data = JSON.parse(file) as CustomFiles[];
|
||||
|
||||
for (const category of data) {
|
||||
const categoryId = category.id ?? category.name.toLowerCase();
|
||||
const dir = join(customDir, category.path ?? categoryId);
|
||||
custom[categoryId] = {
|
||||
name: category.name || category.id!,
|
||||
files: {},
|
||||
};
|
||||
|
||||
for (const f of category.files) {
|
||||
const fileRootPath = join(dir, f.path);
|
||||
const extension = extname(f.path);
|
||||
const fileId = f.id ?? basename(f.path, extension);
|
||||
const fileData = readFileSync(fileRootPath, 'utf-8');
|
||||
custom[categoryId]!.files[fileId] = {
|
||||
name: f.name,
|
||||
type: extension.toLowerCase().replace(/^\./, ''),
|
||||
content: fileData,
|
||||
path: relative(options.root, fileRootPath).replace(/\\/g, '/'),
|
||||
};
|
||||
export function build({ input, custom: customDocs, root, output, typescript }: CLIOptions) {
|
||||
let data: (RootTypes & ChildTypes)[] | DeclarationReflection[] = [];
|
||||
if (typescript) {
|
||||
console.log('Parsing Typescript in source files...');
|
||||
const app = new Application();
|
||||
app.options.addReader(new TSConfigReader());
|
||||
app.bootstrap({ entryPoints: input });
|
||||
const project = app.convert();
|
||||
if (project) {
|
||||
// @ts-expect-error
|
||||
data = app.serializer.toObject(project).children!;
|
||||
console.log(`${data.length} items parsed.`);
|
||||
}
|
||||
} else {
|
||||
console.log('Parsing JSDocs in source files...');
|
||||
data = jsdoc2md.getTemplateDataSync({ files: input }) as (RootTypes & ChildTypes)[];
|
||||
console.log(`${data.length} JSDoc items parsed.`);
|
||||
}
|
||||
|
||||
const fileCount = Object.keys(custom)
|
||||
.map((k) => Object.keys(custom[k]!))
|
||||
.reduce((prev, c) => prev + c.length, 0);
|
||||
const categoryCount = Object.keys(custom).length;
|
||||
console.log(
|
||||
`${fileCount} custom docs file${fileCount === 1 ? '' : 's'} in ` +
|
||||
`${categoryCount} categor${categoryCount === 1 ? 'y' : 'ies'} loaded.`,
|
||||
);
|
||||
}
|
||||
const custom: Record<string, CustomDocs> = {};
|
||||
if (customDocs) {
|
||||
console.log('Loading custom docs files...');
|
||||
const customDir = dirname(customDocs);
|
||||
const file = readFileSync(customDocs, 'utf-8');
|
||||
const data = JSON.parse(file) as CustomFiles[];
|
||||
|
||||
console.log(`Serializing documentation with format version ${Documentation.FORMAT_VERSION}...`);
|
||||
const docs = new Documentation(data, options, custom);
|
||||
for (const category of data) {
|
||||
const categoryId = category.id ?? category.name.toLowerCase();
|
||||
const dir = join(customDir, category.path ?? categoryId);
|
||||
custom[categoryId] = {
|
||||
name: category.name || category.id!,
|
||||
files: {},
|
||||
};
|
||||
|
||||
if (options.output) {
|
||||
console.log(`Writing to ${options.output}...`);
|
||||
writeFileSync(options.output, JSON.stringify(docs.serialize()));
|
||||
for (const f of category.files) {
|
||||
const fileRootPath = join(dir, f.path);
|
||||
const extension = extname(f.path);
|
||||
const fileId = f.id ?? basename(f.path, extension);
|
||||
const fileData = readFileSync(fileRootPath, 'utf-8');
|
||||
custom[categoryId]!.files[fileId] = {
|
||||
name: f.name,
|
||||
type: extension.toLowerCase().replace(/^\./, ''),
|
||||
content: fileData,
|
||||
path: relative(root, fileRootPath).replace(/\\/g, '/'),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const fileCount = Object.keys(custom)
|
||||
.map((k) => Object.keys(custom[k]!))
|
||||
.reduce((prev, c) => prev + c.length, 0);
|
||||
const categoryCount = Object.keys(custom).length;
|
||||
console.log(
|
||||
`${fileCount} custom docs file${fileCount === 1 ? '' : 's'} in ` +
|
||||
`${categoryCount} categor${categoryCount === 1 ? 'y' : 'ies'} loaded.`,
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`Serializing documentation with format version ${Documentation.FORMAT_VERSION}...`);
|
||||
const docs = new Documentation(data, { input, custom: customDocs, root, output, typescript }, custom);
|
||||
|
||||
if (output) {
|
||||
console.log(`Writing to ${output}...`);
|
||||
writeFileSync(output, JSON.stringify(docs.serialize()));
|
||||
}
|
||||
console.log('Done!');
|
||||
}
|
||||
console.log('Done!');
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Type } from './index.js';
|
||||
|
||||
export interface Return {
|
||||
type: Type;
|
||||
type: Required<Type>;
|
||||
nullable?: boolean;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export interface Type {
|
||||
names?: string[];
|
||||
names?: string[] | undefined;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Type } from './index.js';
|
||||
|
||||
export interface VarType extends Type {
|
||||
type?: Required<Type>;
|
||||
description?: string;
|
||||
nullable?: boolean;
|
||||
type?: Required<Type> | undefined;
|
||||
description?: string | undefined;
|
||||
nullable?: boolean | undefined;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user