This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
270 lines (224 loc) · 7 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict';
const fs = require('fs'),
path = require('path'),
hogan = require('hogan.js'),
sass = require('node-sass'),
Uglify = require('uglify-js'),
colors = require('colors'),
rimraf = require('rimraf'),
ts = require('typescript');
const config = {
templateDir: 'public/templates/',
};
const javascript = [
'node_modules/hogan.js/lib/template.js',
'node_modules/es6-promise/dist/es6-promise.auto.js',
'dist/templates.js',
'dist/main.js'
];
config.scripts = javascript.map(x => ({script: path.basename(x)}));
if(fs.existsSync('config.json')) {
Object.assign(config, JSON.parse(fs.readFileSync('config.json')));
}
let watching = false;
for(const arg of process.argv.slice(2)) {
switch(arg) {
case "--development":
case "--dev":
case "-d":
config.development = true;
break;
case "--release":
case "-r":
config.development = false;
break;
case "--watch":
case "-w":
watching = true;
break;
default:
console.error(`Unknown option ${arg}`);
process.exit(1);
}
}
const updateConfig = () => {
const date = new Date();
const dateObj = config.date = {};
for(const field of Object.getOwnPropertyNames(Date.prototype)) {
if(field.startsWith("get")) {
dateObj[field.charAt(3).toLowerCase() + field.substr(4)] = Date.prototype[field].call(date);
}
}
if(config.development) {
config.version = 'dev' + date.getTime();
} else {
config.version = JSON.parse(fs.readFileSync('package.json')).version;
}
console.log('Building Howl.CI: ' + config.version + " at " + date);
}
updateConfig();
const step = function(name, func, run) {
if(arguments.length < 3 || run) {
console.log(colors.green('Starting ' + name));
const start = Date.now();
func();
const end = Date.now();
console.log(colors.cyan(' ' + name + ' took ' + (end - start) + " milliseconds"));
} else {
console.log(colors.yellow('Skipping ' + name));
}
return () => step.apply(null, arguments);
}
step('Ensure exists', () => {
if(!fs.existsSync('dist')) fs.mkdirSync('dist');
if(!fs.existsSync('temp')) fs.mkdirSync('temp');
});
step('Clean', () => {
rimraf.sync('dist/**/*');
});
step('Copy font', () => {
const contents = fs.readFileSync("public/termFont.png");
fs.writeFileSync("dist/termFont.png", contents);
});
const listDir = root => {
const results = [];
const queue = [''];
while(queue.length > 0) {
const dir = queue.pop();
const wholeDir = path.join(root, dir);
for(const file of fs.readdirSync(wholeDir)) {
const wholeFile = path.join(wholeDir, file);
const relFile = path.join(dir, file);
if(fs.statSync(wholeFile).isDirectory()) {
queue.push(relFile);
} else {
results.push(relFile);
}
}
}
return results;
}
const templatesLayout = step('Generate layout.html', () => {
const layout = hogan.compile(fs.readFileSync(config.templateDir + 'layout.html', 'utf8'));
const sidebar = hogan.compile(fs.readFileSync(config.templateDir + 'partial/sidebar.html', 'utf8'));
const root = path.join(config.templateDir, 'static');
const templates = listDir(root)
.filter(x => x.endsWith(".html"));
for(const template of templates) {
let dir = 'dist/' + path.dirname(template);
while(!fs.existsSync(dir)) {
fs.mkdirSync(dir);
dir = path.dirname(dir);
}
let base = "";
let levels = template.split(path.sep).length - 1;
if(levels == 0) base = "./";
for(let i = 0; i < levels; i++) base += "../";
fs.writeFileSync('dist/' + template, layout.render(Object.assign({
javascript: template == 'index.html',
base: base,
}, config), {
['content']: hogan.compile(fs.readFileSync(path.join(root, template), 'utf8')),
['partial/sidebar']: sidebar
}));
}
});
const templatesMake = step('Generate templates', () => {
const templates = listDir(config.templateDir)
.filter(x => x != 'layout.html' && !x.startsWith("static") && x.endsWith(".html"))
.map(file => {
const contents = fs.readFileSync(config.templateDir + file, 'utf8');
const template = hogan.compile(contents, { asString: true});
const name = file.replace(/\..*$/, '').replace('\\', '/');
return 'templates["' + name + '"] = new Hogan.Template(' + template + ');';
});
const header = 'var HowlCI; (function (HowlCI) { var templates = HowlCI.templates = {};\n';
const footer = '\n})(HowlCI || (HowlCI = {}));';
fs.writeFileSync('dist/templates.js', header + templates.join('\n') + footer);
});
const jsCopy = step('Copy JS', () => {
for(const file of javascript) {
if(file.startsWith('dist/')) continue;
const contents = fs.readFileSync(file, 'utf8');
fs.writeFileSync('dist/' + path.basename(file), contents);
}
}, config.development);
const tsBuild = step('Compile TS', () => {
const config = JSON.parse(fs.readFileSync('tsconfig.json', 'utf8'));
const configP = ts.parseJsonConfigFileContent(config, ts.sys, path.resolve("."), null, path.resolve("tsconfig.json"));
// TODO: Incremental compilation?: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API
const program = ts.createProgram(configP.fileNames, configP.options);
const emitResult = program.emit();
const diagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
for(const diagnostic of diagnostics) {
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
if(diagnostic.start && diagnostic.file) {
const pos = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
console.log(colors.red(`${diagnostic.file.fileName} (${pos.line + 1},${pos.character + 1}): `) + message);
} else {
console.log(colors.red(message));
}
}
});
const jsMinify = step('Minify JS', () => {
var result = Uglify.minify(javascript, {
mangle: { toplevel: true },
compress: false,
output: { comments: "some" },
});
fs.writeFileSync('dist/main.min.js', result.code);
for(const file of javascript) {
if(file.startsWith('dist/')) fs.unlinkSync(file);
}
}, !config.development);
const sassCombine = step('Compile Sass', () => {
const contents = sass.renderSync({
file: 'public/css/main.scss',
outputStyle: 'expanded',
});
fs.writeFileSync('dist/main.css', contents.css);
}, config.development);
const sassMinify = step('Minify Sass', () => {
const contents = sass.renderSync({
file: 'public/css/main.scss',
outputStyle: 'compressed',
});
fs.writeFileSync('dist/main.min.css', contents.css);
}, !config.development);
const watcher = (file, callback) => {
let triggered = false;
const defer = () => {
try {
updateConfig();
templatesLayout();
callback();
} catch(e) {
console.log(colors.red(e));
}
triggered = false;
};
fs.watch(file, { recursive: true, persistent: true }, () => {
if(triggered) return;
triggered = true;
setTimeout(defer, 100);
});
};
if(watching) {
watcher("public/css", () => {
sassCombine();
sassMinify();
});
watcher("public/js", () => {
tsBuild();
jsMinify();
});
watcher("tsconfig.json", () => {
tsBuild();
jsMinify();
});
watcher("public/templates", () => {
templatesMake();
jsMinify();
});
}
console.log(colors.green("Build completed"));