-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.es6
119 lines (91 loc) · 2.84 KB
/
build.es6
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var jossy = require('jossy');
var escaper = require('escaper');
var spawn = require('child_process').spawn;
var path = require('path');
var fs = require('fs');
var V = require('./lib/core')['VERSION'].join('.');
var program = require('commander');
program
.option('-d, --dev')
.parse(process.argv);
function build(file, flags) {
jossy.compile(path.join(__dirname, 'lib/core.js'), null, flags, (err, res) => {
if (err) {
throw err;
}
res = res
.replace(/\bBoolean\((.*?)\)/gm, '!!($1)')
.replace(/\bString\((.*?)\)/gm, "(($1) + '')")
.replace(/\bNumber\((.*?)\)/gm, '+($1)')
// Всякие хаки, чтобы GCC не ругался
.replace(/(@param {.*?[^=]}) \[(\w+)=.*?[^\]]]/gm, '$1 $2')
.replace(/{\.\.\.\(?([^}]+)\)?}/gm, '{...($1|Array)}')
.replace(/\/\*, (\w+) \*\//gm, ', $1')
.replace(/\/\*= (\w+) \*\//gm, '$1')
.replace(/\/\/= (.*)/gm, '$1')
.replace(/^\\n/gm, '');
var desc = file !== 'snakeskin' ? ` (${file.replace(/snakeskin\./, '')})` : '';
res =
`/*!
* Snakeskin v${V}${desc}
* https://github.com/kobezzza/Snakeskin
*
* Released under the MIT license
* https://github.com/kobezzza/Snakeskin/blob/master/LICENSE
*
* Date: ${new Date().toUTCString()}
*/
` + res;
fs.writeFileSync(path.join(__dirname, `build/${file}.js`), res);
if (program['dev']) {
return;
}
var gcc = spawn('java', [
'-jar',
'compiler.jar',
'--js',
`build/${file}.js`,
'--compilation_level',
'ADVANCED_OPTIMIZATIONS',
'--use_types_for_optimization',
'--externs',
'predefs.js',
'--language_in',
'ECMASCRIPT5',
'--jscomp_warning',
'invalidCasts',
'--js_output_file',
`build/${file}.min.js`
]);
gcc.stderr.on('data', (data) => {
fs.writeFileSync(path.join(__dirname, 'build/log.txt'), data);
console.log(String(data));
});
gcc.on('close', (code) => {
var min = path.join(__dirname, `build/${file}.min.js`);
var content = escaper.replace(fs.readFileSync(min).toString(), {
'\'': true,
'/': true
});
// Хакерски вырезаем лишнее :)
var res = escaper.paste(content.replace(/\\t/gm, ''));
res = `/*! Snakeskin v${V}${desc} | https://github.com/kobezzza/Snakeskin/blob/master/LICENSE */` + res;
fs.writeFileSync(min, res);
console.log(`${file} compiled, code ${code}`);
function updateManifest(url) {
fs.writeFileSync(url, fs.readFileSync(url).toString().replace(/"version": ".*?"/gm, `"version": "${V}"`));
}
updateManifest(path.join(__dirname, 'package.json'));
updateManifest(path.join(__dirname, 'bower.json'));
var index = path.join(__dirname, 'index.js');
fs.writeFileSync(index, fs.readFileSync(index).toString().replace(/^\/\//, ''));
});
});
}
var builds = Object(require('./builds'));
for (let key in builds) {
if (!builds.hasOwnProperty(key)) {
continue;
}
build(key, builds[key]);
}