-
Notifications
You must be signed in to change notification settings - Fork 941
/
Copy pathparser.ts
30 lines (27 loc) · 1.12 KB
/
parser.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
import fs from 'fs-extra';
import { FsCache } from '@teambit/workspace.modules.fs-cache';
import { SourceFile } from '@teambit/component.sources';
import { PathOsBased } from '@teambit/toolbox.path.path';
import jsDocParse from './jsdoc';
import reactParse from './react';
import { Doclet } from './types';
export default async function parse(file: SourceFile, componentFsCache: FsCache): Promise<Doclet[]> {
const docsFromCache = await componentFsCache.getDocsFromCache(file.path);
if (docsFromCache && docsFromCache.timestamp) {
const stat = await fs.stat(file.path);
const wasFileChanged = stat.mtimeMs > docsFromCache.timestamp;
if (!wasFileChanged) {
return JSON.parse(docsFromCache.data);
}
}
const results = await parseFile(file.contents.toString(), file.relative);
await componentFsCache.saveDocsInCache(file.path, results);
return results;
}
async function parseFile(data: string, filePath: PathOsBased): Promise<Doclet[]> {
const reactDocs = await reactParse(data, filePath);
if (reactDocs && Object.keys(reactDocs).length > 0) {
return reactDocs;
}
return jsDocParse(data, filePath);
}