-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathcombineDefinition.js
60 lines (47 loc) · 1.52 KB
/
combineDefinition.js
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
/**
功能:合并TypeScript类型定义文件(.d.ts)。
*/
/**
* 入口文件
*/
var entry = './ThirdParty/three.js/src/Three.d.ts';
/**
* 输出合并结果文件
*/
var outFilename = './Source/THREE.d.ts';
var fs = require('fs');
var path = require('path');
var regexp = /[import|export]+\s+({[\/\sa-zA-Z_0-9,]+})*.*\s*from\s*['|"].*['|"];/g;
//标记已经导入过的文件
var fileExists = {};
//递归处理d.ts文件
function parseeFile(filename) {
var currDir = path.dirname(filename);
var fileCont = fs.readFileSync(filename, { encoding: 'utf-8' })
var matches = fileCont.match(regexp);
if (!matches || !matches.length) {
return fileCont;
}
var importFileConts = [];
matches.forEach((item) => {
if (/^\/\//.test(item.trimLeft())) return
var m = item.match(/['|"](?<filename>.*)['|"]/)
if (m.groups && m.groups.filename) {
var importFilename = m.groups.filename + '.d.ts'
importFilename = path.join(currDir, importFilename);
var absFname = path.resolve(currDir, importFilename);
if (fileExists[absFname]) {
return;
}
fileExists[absFname] = true;
var importFileCont = parseeFile(importFilename);
importFileConts.push(importFileCont);
}
});
importFileConts.push(fileCont.replace(regexp, '').trimLeft());
return importFileConts.join('\r\n');
}
var result = parseeFile(entry);
fs.writeFileSync(outFilename, result, {
encoding: 'utf-8'
})