-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathparser.ts
More file actions
40 lines (36 loc) · 986 Bytes
/
parser.ts
File metadata and controls
40 lines (36 loc) · 986 Bytes
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
import * as fs from "fs";
import * as path from "path";
import { createSourceFile, ScriptTarget, SyntaxKind } from "typescript";
export function getAST(sdkpkgName) {
return new Promise(async (resolve, reject) => {
try {
const file = path.join(
__dirname,
`../../../node_modules/oci-${sdkpkgName.toLowerCase()}/lib/client.d.ts`
);
const ast = createSourceFile(
file,
fs.readFileSync(file).toString(),
ScriptTarget.Latest,
true
);
let cloned = null;
await ast.forEachChild(child => {
if (SyntaxKind[child.kind] === "ClassDeclaration") {
cloned = Object.assign({}, child);
}
});
if (!cloned) {
reject(new Error("CLASS NOT FOUND"));
} else {
resolve(cloned);
}
} catch (error) {
if (error.code === "ENOENT") {
reject(new Error("PACKAGE NOT FOUND"));
} else {
reject(error);
}
}
});
}