forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
80 lines (76 loc) · 2.34 KB
/
index.ts
File metadata and controls
80 lines (76 loc) · 2.34 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import parseDynamic from './dynamic';
import parseJS from './js';
import parseTS from './ts';
import { install, installPeerAndDevDeps, syncTypeModules, installTypeDTS } from '../utils';
import { IMaterialScanModel, DSLType } from '../types';
import { debug } from '../core';
const log = debug.extend('parse');
export interface IParseArgs extends IMaterialScanModel {
accesser?: 'online' | 'local';
dslType?: DSLType;
npmClient?: string;
workDir: string;
moduleDir: string;
typingsFileAbsolutePath?: string;
mainFileAbsolutePath: string;
moduleFileAbsolutePath?: string;
}
export function isTSLike(str) {
return str.endsWith('ts') || str.endsWith('tsx');
}
export default async (args: IParseArgs) => {
const {
typingsFileAbsolutePath,
mainFileAbsolutePath,
moduleFileAbsolutePath = mainFileAbsolutePath,
useEntry = false,
} = args;
if (args.accesser === 'local') {
if (isTSLike(mainFileAbsolutePath)) {
await install(args);
// in case the developer forgets to install types
await installTypeDTS(args);
return parseTS(mainFileAbsolutePath, args);
} else if (typingsFileAbsolutePath) {
await installTypeDTS(args);
return parseTS(typingsFileAbsolutePath, args);
} else {
try {
return parseJS(moduleFileAbsolutePath || mainFileAbsolutePath);
} catch (e) {
log(e);
await install(args);
const info = parseDynamic(mainFileAbsolutePath);
if (!info || !info.length) {
throw Error();
}
return info;
}
}
} else if (args.accesser === 'online') {
// ts
const entryPath = useEntry ? mainFileAbsolutePath : typingsFileAbsolutePath;
if (entryPath && isTSLike(entryPath)) {
await syncTypeModules(args);
await install(args);
await installTypeDTS(args);
await installPeerAndDevDeps(args);
return parseTS(entryPath, args);
}
// js
try {
// try dynamic parsing first
await installPeerAndDevDeps(args);
const info = parseDynamic(mainFileAbsolutePath);
if (!info || !info.length) {
throw Error();
}
return info;
} catch (e) {
log(e);
// if error, use static js parsing instead
return parseJS(moduleFileAbsolutePath || mainFileAbsolutePath);
}
}
return parseJS(moduleFileAbsolutePath || mainFileAbsolutePath);
};