-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
221 lines (199 loc) · 6.23 KB
/
build.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var files = [
'init.js',
'css/index.js',
'css/*.js',
'methods/*.js',
'effects/*.js',
'interface/magicNumbers.js',
'interface/proxyObject.js',
'interface/elementWrapper.js',
'interface/queryWrapper.js',
'interface/mainInterface.js',
'index.js',
];
function Glob(files) {
var fs = require('fs')
function regexIndexOf(string, regex, startpos) {
var indexOf = string.substring(startpos || 0).search(regex);
return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
}
function regexLastIndexOf(string, regex, startpos) {
regex = (regex.global) ? regex : new RegExp(regex.source, "g" + (regex.ignoreCase ? "i" : "") + (regex.multiLine ? "m" : ""));
if (typeof (startpos) == "undefined") {
startpos = string.length;
} else if (startpos < 0) {
startpos = 0;
}
var stringToWorkWith = string.substring(0, startpos + 1);
var lastIndexOf = -1;
var nextStop = 0;
while ((result = regex.exec(stringToWorkWith)) != null) {
lastIndexOf = result.index;
regex.lastIndex = ++nextStop;
}
return lastIndexOf;
}
var out = [];
function get(file) {
if (/[*?\[\]]/.test(file)) {
var ind = regexIndexOf(file, /[*?\[\]]/),
ind2 = file.lastIndexOf('/', ind);
var before = file.substring(0, ind2),
after = file.substring(ind2 + 1)
try {
fs.statSync(before);
after = after.split('/');
function walk(path, i) {
try {
var current = fs.readdirSync(path);
var rule = after[i];
if (/[*?\[\]]/.test(rule)) {
var regex = new RegExp(rule.replace(/[*?]/g, function (a) {
return '.' + (a === '*' ? '*' : '')
}));
current.forEach((p) => {
if (regex.test(p)) {
if (i === after.length - 1) {
out.push(path + '/' + p);
} else {
walk(path + '/' + p, i + 1)
}
}
});
} else {
if (current.indexOf(rule) !== -1) {
if (i === after.length - 1) {
out.push(path + '/' + rule);
} else {
walk(path + '/' + rule, i + 1)
}
}
}
} catch (e) {
}
}
walk(before, 0)
} catch (e) {
}
} else {
if (fs.existsSync(file)) {
out.push(file);
}
}
}
if (typeof files === 'object') {
files.forEach((file) => {
get(file)
})
} else {
get(files)
}
return out;
}
var http = require("http"),
https = require("https"),
querystring = require("querystring");
var request = function (f, b, c, d) {
var e = !1,
h = !1;
if (c) {
e = querystring.stringify(b);
var g = c;
h = d
} else g = b, h = c;
d = !1;
b = "/";
var m = "";
c = "";
var a = f.split("://");
d = "https" == a[0] ? !0 : !1;
a = a[1] ? a.slice(1).join("://") : a[0];
a = a.split("/");
var k = a[0].split(":");
f = k[0];
k[1] && (c = parseInt(k[1]));
a[1] && (b += a.slice(1).join("/"));
d = d ? https : http;
h && (b += "?" + Date.now());
try {
var n = e ? {
host: f,
path: b,
port: c,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": Buffer.byteLength(e)
}
} : {
host: f,
path: b,
port: c
},
l = d.request(n, function (a) {
a.setEncoding("utf8");
a.on("data", function (a) {
m += a
});
a.on("end", function () {
g(!1, a, m)
})
});
l.on("error", function (a) {
g(a, null, null)
});
e && l.write(e);
l.end()
} catch (p) {
g(p, null, null)
}
};
function compile(code, callback) {
request('https://closure-compiler.appspot.com/compile', {
"js_code": code,
"compilation_level": "SIMPLE_OPTIMIZATIONS",
"output_format": "text",
"output_info": "compiled_code"
}, function (e, r, b) {
callback(b)
})
}
var fs = require('fs');
files = Glob(files.map((f) => {
return __dirname + '/source/' + f;
})).filter((v, i, a) => a.indexOf(v) === i).map((file) => {
return '// ' + file.replace(__dirname + '/source/', '') + '\n' + fs.readFileSync(file, 'utf8');
});
var out = '(function (window) {\n' + files.join('') + '})(window)';
console.log("Compiling...")
out = require("@babel/core").transform(out, {
"presets": ["@babel/preset-env"]
}).code;
var version = "0.0.1";
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
var date = dd + '/' + mm + '/' + yyyy;
var top = `\
/*\n\
Aquery: The world's best DOM wrapper\n\
\n\
Author: Andrews54757\n\
License: MIT (https://github.com/ThreeLetters/AQuery/blob/master/LICENSE)\n\
Source: https://github.com/ThreeLetters/AQuery\n\
Build: v${version}\n\
Built on: ${date}\n\
*/\n\n`;
fs.writeFileSync(__dirname + '/dist/AQuery.js', top + out)
console.log("Minifying...")
compile(out, function (compiled) {
fs.writeFileSync(__dirname + '/dist/AQuery.min.js', top + compiled)
console.log("Compiled " + files.length + " files")
})