-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathgenerateDTS.ts
More file actions
67 lines (61 loc) · 2.12 KB
/
Copy pathgenerateDTS.ts
File metadata and controls
67 lines (61 loc) · 2.12 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
import * as path from 'path';
import { writeFileSync, pathExistsSync } from 'fs-extra';
import { loadFile } from '../../utils';
import { debug } from '../../core';
const log = debug.extend('parse:ts:generate_dts');
function getTypeDir(workDir: string, dsl: string) {
const typePkgName = `@types/${dsl}`;
let typeDir = path.join(workDir, 'node_modules', typePkgName);
/** 适配 workspace 的情况,如果当前目录没有对应文件,从 workspace 依赖中寻找 */
if (!pathExistsSync(typeDir)) {
typeDir = path.join(workDir, '../../node_modules', typePkgName);
}
return typeDir;
}
/**
* Generate alias dts file by removing some needless interfaces.
* Replace original file at present, which will cause type pollution, looking for better solution
* @param {string} workDir - the dir containing the module to be parsed
* @returns {string} - the path of generated xxx.d.ts
*/
export default function generateDTS({
workDir,
dslType = 'react',
}: {
workDir: string;
dslType?: string;
}): {
originalTypePath: string;
newTypePath: string;
} {
const typeDir = getTypeDir(workDir, dslType);
const typePath = path.join(typeDir, 'index.d.ts');
const fileContent = loadFile(typePath);
// const materialParserTypeDir = path.join(workDir, `node_modules/material-parser-types/${type}`);
// ensureDirSync(materialParserTypeDir);
const materialParserTypeDir = typeDir;
const newTypePath = path.join(materialParserTypeDir, 'index.d.ts');
// if (!pathExistsSync(newTypePath)) {
// copySync(
// path.join(typeDir, 'global.d.ts'),
// path.join(materialParserTypeDir, 'global.d.ts'),
// );
let newContent = fileContent.replace(
/(?<=interface HTMLAttributes[^e]+)(extends[^}]+)/,
`{
style?: CSSProperties;
className?: string;
`,
);
newContent = newContent.replace(/(?<=interface IntrinsicElements {)([^}]+)/, '');
newContent = newContent.replace(/type LibraryManagedAttributes[^;]+;/, '');
writeFileSync(newTypePath, newContent);
log('generate dts', newTypePath);
// } else {
// log('found dts', newTypePath);
// }
return {
originalTypePath: typePath,
newTypePath,
};
}