-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
176 lines (147 loc) · 4.92 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
var files = [
"src/TreeBucket.js",
"src/HashGrid.js",
"src/HashBounds.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;
}
var out = [];
function get(file) {
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) {
//
}
}
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("/");
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;
}
function compile(code, callback) {
const ClosureCompiler = require("google-closure-compiler").compiler;
const closureCompiler = new ClosureCompiler({
js: code,
compilation_level: "SIMPLE",
rewrite_polyfills: false
});
closureCompiler.run((exitCode, stdOut, stdErr) => {
console.log(stdErr);
callback(stdOut);
//compilation complete
});
}
var fs = require("fs");
files = Glob(files.map((f) => {
return __dirname + "/" + f;
})).filter((v, i, a) => a.indexOf(v) === i).map((file) => {
var dt = fs.readFileSync(file, "utf8");
var ind = dt.indexOf("class ");
var ind2 = dt.lastIndexOf('/**', ind);
var cutoff = Math.max(ind2 != -1 ? ind2 : ind, 0);
// "// " + file.replace(__dirname + "/src/", "")
return "\n" + dt.substring(cutoff).split("module.exports")[0];
});
var out = files.join("");
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 dt = JSON.parse(fs.readFileSync("package.json", "utf8"));
var top = `\
/*\n\
${dt.name.charAt(0).toUpperCase() + dt.name.slice(1)}: ${dt.description}\n\
\n\
Author: ${dt.author}\n\
License: ${dt.license} (https://github.com/ThreeLetters/HashBounds/blob/master/LICENSE)\n\
Source: https://github.com/ThreeLetters/HashBounds\n\
Build: v${dt.version}\n\
Built on: ${date}\n\
*/\n\n`;
fs.writeFileSync(__dirname + "/dist/HashBounds.js", top + out);
console.log("Built HashBounds.js. Minifying...");
const exec = require('child_process').exec;
compile(__dirname + "/dist/HashBounds.js", function (compiled) {
fs.writeFileSync(__dirname + "/dist/HashBounds.min.js", top + compiled);
console.log("Compiled " + files.length + " files");
exec('eslint .',
(error, stdout, stderr) => {
console.log(stderr);
console.log(stdout);
if (error !== null) {
console.log(`error: ${error}`);
}
exec('jest --coverage',
(error, stdout, stderr) => {
console.log(stderr);
console.log(stdout);
if (error !== null) {
console.log(`error: ${error}`);
}
exec('documentation readme index.js --section=API --github',
(error, stdout, stderr) => {
console.log(stderr);
if (error !== null) {
console.log(`error: ${error}`);
}
});
});
});
});