Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default async function typedoc(config: ITypeDocConfig) {
const packages = await getDocumentablePackages();
const srcPaths = packages.map(name => path.join(PACKAGES_FOLDER, name, "src"));
const project = compileProject(srcPaths);
const interfaces = project.getReflectionsByKind(
ReflectionKind.Interface
) as DeclarationReflection[];
const props = interfaces.filter(intf => /^I.*(?!Default)Props/.test(intf.name));
const classComponents = project.getReflectionsByKind(ReflectionKind.Class);
const functionalComponents = project
.getReflectionsByKind(ReflectionKind.Function)
.filter(({ name }) => /^[A-Z]/.test(name) && !/Wrapper$/.test(name));
const components = classComponents.concat(functionalComponents) as DeclarationReflection[];
console.log("Creating component documentation...");
const tempDir = path.join(process.cwd(), "docs");
await fs.ensureDir(tempDir);
await fs.emptyDir(tempDir);
await Promise.all(
components.map(component => {
// workaround for the ResizeObserrver
const name = component.name.replace(/Comp$/, "");
const defaultProps = component.getChildByName("defaultProps") as DeclarationReflection | null;
const documented: DocumentedComponent = {
public read(rootPath: string): Reflections {
const expandedFiles = this.typedocApp.expandInputFiles([rootPath]);
const projectReflection = this.typedocApp.convert(expandedFiles);
// console.log(JSON.stringify(this.typedocApp.serializer.projectToObject(projectReflection)));
const externalModules = this.externalModulesWithoutTestsAndMocks(projectReflection);
const classReflections = this.reflections(externalModules, ReflectionKind.Class);
const interfaceReflections = this.reflections(externalModules, ReflectionKind.Interface);
const enumReflections = this.reflections(externalModules, ReflectionKind.Enum);
return {
classReflections,
interfaceReflections,
enumReflections
};
}
node.kind === ReflectionKind.Enum ||
node.kind === ReflectionKind.TypeAlias
) {
processMarkdown(node);
this.constructs.push(new TSConstruct(node));
createAnchor(node);
let title = TSHelper.getNodeTitle(node);
this.sections.push({
title: title,
anchor: node.anchorId,
depth: 3,
});
// build sections for children
let children = node.children;
if (
(node.kind === ReflectionKind.Class ||
node.kind === ReflectionKind.Interface ||
node.kind === ReflectionKind.ObjectLiteral ||
node.kind === ReflectionKind.Module ||
node.kind === ReflectionKind.Enum) &&
children &&
children.length > 0
) {
children.forEach((child: Node) => {
if (
child.inheritedFrom ||
child.flags.isPrivate ||
child.flags.isProtected
) {
child.shouldDocument = false;
} else {
// This is needed in UI, good to keep the eligibility logic at one place
node.kind === ReflectionKind.Enum ||
node.kind === ReflectionKind.TypeAlias
) {
processMarkdown(node);
this.constructs.push(new TSConstruct(node));
createAnchor(node);
let title = TSHelper.getNodeTitle(node);
this.sections.push({
title: title,
anchor: node.anchorId,
depth: 3,
});
// build sections for children
let children = node.children;
if (
(node.kind === ReflectionKind.Class ||
node.kind === ReflectionKind.Interface ||
node.kind === ReflectionKind.ObjectLiteral ||
node.kind === ReflectionKind.Module ||
node.kind === ReflectionKind.Enum) &&
children &&
children.length > 0
) {
children.forEach((child: Node) => {
if (
child.inheritedFrom ||
child.flags.isPrivate ||
child.flags.isProtected
) {
child.shouldDocument = false;
} else {
// This is needed in UI, good to keep the eligibility logic at one place
'-i',
/vtils@[^/]+/g,
`vtils@${pkg.version}`,
readMeFile,
)
// 构建包
_.exec(`typedoc --ignoreCompilerErrors --excludeNotExported --excludePrivate --excludeProtected --json ${typedocDataFile} --mode file src/index.ts`)
// 创建文档
const list = (await fs.readJSON(typedocDataFile)).children as Item[]
const listByKind = groupBy(list, item => item.kind)
const briefListByKind: Record = {} as any
const readMeFlagByKind: Partial> = {
[ReflectionKind.Function]: '工具函数',
[ReflectionKind.Class]: '工具类',
[ReflectionKind.TypeAlias]: '工具类型',
}
const contentItemCountPerLineByKind: Partial> = {
[ReflectionKind.Function]: 4,
[ReflectionKind.Class]: 3,
[ReflectionKind.TypeAlias]: 9,
}
let readme = (await fs.readFile(readMeFile)).toString()
forOwn(listByKind, (list, kind) => {
switch (Number(kind)) {
case ReflectionKind.Function:
list.forEach(item => {
(briefListByKind[kind] || (briefListByKind[kind] = [])).push({
name: item.name,
brief => {
const sourceUrl = `https://github.com/fjc0k/vtils/blob/master/packages/taro/src/${brief.source.fileName}#L${brief.source.line}`
const apiUrl = (
Number(kind) === ReflectionKind.Class
? `https://fjc0k.github.io/vtils/taro/classes/${brief.name.toLowerCase()}.html`
: `https://fjc0k.github.io/vtils/taro/globals.html#${brief.name.toLowerCase()}`
)
return dedent`
#### ${brief.name}
<small>[源码](${sourceUrl}) | [API](${apiUrl}) | [回目录](#目录)</small>
${brief.body}
`
},
).join('\n\n')
brief => {
const sourceUrl = `https://github.com/fjc0k/vtils/blob/master/packages/react/src/${brief.source.fileName}#L${brief.source.line}`
const apiUrl = (
Number(kind) === ReflectionKind.Class
? `https://fjc0k.github.io/vtils/react/classes/${brief.name.toLowerCase()}.html`
: `https://fjc0k.github.io/vtils/react/globals.html#${brief.name.toLowerCase()}`
)
return dedent`
#### ${brief.name}
<small>[源码](${sourceUrl}) | [API](${apiUrl}) | [回目录](#目录)</small>
${brief.body}
`
},
).join('\n\n')
function getNavigationGroup(reflection: DeclarationReflection) {
if (reflection.kind === ReflectionKind.ExternalModule) {
return externalModulesNavigation;
}
if (reflection.kind === ReflectionKind.Module) {
return modulesNavigation;
}
if (reflection.kind === ReflectionKind.Class) {
return classesNavigation;
}
if (reflection.kind === ReflectionKind.Enum) {
return enumsNavigation;
}
if (reflection.kind === ReflectionKind.Interface) {
return interfacesNavigation;
}
return null;
}