-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.ts
58 lines (47 loc) · 1.49 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as fs from 'fs-extra';
import * as path from 'path';
import { DocsParser } from './DocsParser';
type ParseOptions = {
baseDirectory: string;
useReadme: boolean;
moduleVersion: string;
packageMode?: 'single' | 'multi';
};
export async function parseDocs(options: ParseOptions) {
const packageMode = options.packageMode || 'single';
const apiDocsPath = options.baseDirectory || path.resolve('./', 'docs', 'api');
const structuresPath = path.resolve(apiDocsPath, 'structures');
let structures: string[] = [];
let apis: string[] = [];
if (options.useReadme) {
const readmePath = path.resolve(options.baseDirectory, 'README.md');
if (!fs.existsSync(readmePath)) {
throw new Error('README.md file not found');
}
apis = [readmePath];
} else {
structures = await getAllMarkdownFiles(structuresPath);
apis = await getAllMarkdownFiles(apiDocsPath);
}
const parser = new DocsParser(
options.baseDirectory,
options.moduleVersion,
apis,
structures,
packageMode,
);
return await parser.parse();
}
async function getAllMarkdownFiles(inDir: string) {
const allMarkdownFiles: string[] = [];
const children = await fs.readdir(inDir);
for (const child of children) {
const childPath = path.resolve(inDir, child);
const stats = await fs.stat(childPath);
if (path.extname(childPath) === '.md' && stats.isFile()) {
allMarkdownFiles.push(childPath);
}
}
return allMarkdownFiles;
}
export * from './ParsedDocumentation';