-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.js
93 lines (86 loc) · 2.42 KB
/
go.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var exec = require('child_process').exec;
var fs = require("fs");
var path = require('path');
var UglifyJS = require("uglify-js");
//settings
var src="./src";
var dest = "./proc";
var fdest ="./dest";
var dirs = [
'global',
'umd',
'es6'
];
var except = [
'require','exports','module', 'export', 'default'
];
//end settings
var extract = function(x){
var startIndex = x.indexOf("///{");
if(startIndex < 0) return x;
else startIndex=startIndex+4;
var endIndex = x.lastIndexOf("///}");
if(endIndex>0) return x.slice(startIndex, endIndex);
else return x.slice(startIndex);
}
var walk = function(directoryName, ext) {
var res= [];
ext=ext || ".ts";
var files=fs.readdirSync(directoryName);
files.forEach(function(file) {
let fullPath = path.join(directoryName,file);
let f= fs.statSync(fullPath);
if (f.isDirectory()) {
res=res.concat(walk(fullPath));
} else {
if(fullPath.match(ext+"$"))
res.push(fullPath)
}
});
return res;
};
var toProcess=walk(src);
toProcess.forEach(function(file){
dirs.forEach(function(dir){
let toAdd=file.substr(0, file.length-3)+"."+dir+".add";
let outFile=path.join(dest, dir, path.basename(file));
if(fs.existsSync(toAdd)){
let input = extract(fs.readFileSync(file, 'utf8'));
fs.writeFileSync(outFile,
fs.readFileSync(toAdd, 'utf8').replace("///{}", input));
}
else{
fs.writeFileSync(outFile,
fs.readFileSync(file));
}
})
});
for(let i=0; i<dirs.length; i++)
{
let config = 'tsc -p tsconfig.'+dirs[i]+'.json';
console.log("start "+dirs[i]);
exec(config, function(error, stdout, stderr) {
console.log(stdout);
console.error(stderr);
if(dirs[i] != 'es6') {
let files = walk(path.join(fdest, dirs[i]), ".js");
files.forEach(function(file){
let baseName=file.substr(0, file.length-3);
if(baseName.match(".min$")) return;
let inMap = file+".map";
if(!fs.existsSync(inMap)) inMap=undefined;
let outFile = baseName+".min.js";
let outMap = baseName+".min.js.map";
let res=UglifyJS.minify(file, {
outSourceMap: path.basename(outMap),
outFileName: path.basename(outFile),
inSourceMap: inMap,
except:except
});
fs.writeFileSync(outFile, res.code);
fs.writeFileSync(outMap, res.map);
});
}
console.log("finished "+dirs[i]);
});
}