2016-03-27 20:29:47 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
2016-03-27 21:10:41 +02:00
|
|
|
const glob = require('glob');
|
|
|
|
const path = require('path');
|
|
|
|
const util = require('util');
|
2016-03-31 23:18:08 +02:00
|
|
|
const execSync = require('child_process').execSync;
|
|
|
|
|
2016-05-21 12:33:02 +02:00
|
|
|
function readTextFile(path) {
|
|
|
|
return fs.readFileSync(path, 'utf-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeFile(path, content) {
|
|
|
|
return fs.writeFileSync(path, content);
|
|
|
|
}
|
|
|
|
|
2016-03-31 23:18:08 +02:00
|
|
|
function getVersion() {
|
2018-07-06 01:25:08 +02:00
|
|
|
let build_info = process.env.BUILD_INFO;
|
|
|
|
if (build_info) {
|
|
|
|
return build_info.trim();
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
build_info = execSync('git describe --always --dirty --long --tags')
|
|
|
|
.toString();
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Cannot find build version');
|
|
|
|
return 'unknown';
|
|
|
|
}
|
|
|
|
return build_info.trim();
|
|
|
|
}
|
2016-03-31 23:18:08 +02:00
|
|
|
}
|
2016-03-27 20:29:47 +02:00
|
|
|
|
|
|
|
function getConfig() {
|
2018-07-06 01:25:08 +02:00
|
|
|
let config = {
|
|
|
|
meta: {
|
|
|
|
version: getVersion(),
|
|
|
|
buildDate: new Date().toUTCString()
|
|
|
|
}
|
2016-03-31 23:18:08 +02:00
|
|
|
};
|
2016-03-27 20:29:47 +02:00
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2016-05-21 12:33:02 +02:00
|
|
|
function copyFile(source, target) {
|
|
|
|
fs.createReadStream(source).pipe(fs.createWriteStream(target));
|
|
|
|
}
|
|
|
|
|
2016-05-21 09:46:41 +02:00
|
|
|
function minifyJs(path) {
|
2018-07-06 22:02:48 +02:00
|
|
|
return require('terser').minify(fs.readFileSync(path, 'utf-8'), {compress: {unused: false}}).code;
|
2016-05-21 09:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function minifyCss(css) {
|
2018-07-06 22:02:48 +02:00
|
|
|
return require('csso').minify(css).css;
|
2016-05-21 09:46:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function minifyHtml(html) {
|
|
|
|
return require('html-minifier').minify(html, {
|
2016-04-13 18:58:34 +02:00
|
|
|
removeComments: true,
|
|
|
|
collapseWhitespace: true,
|
|
|
|
conservativeCollapse: true,
|
2016-05-21 09:46:41 +02:00
|
|
|
}).trim();
|
|
|
|
}
|
|
|
|
|
2018-07-06 01:25:08 +02:00
|
|
|
function bundleHtml() {
|
2016-05-21 10:02:06 +02:00
|
|
|
const underscore = require('underscore');
|
2016-05-21 09:46:41 +02:00
|
|
|
const babelify = require('babelify');
|
2016-05-21 12:33:02 +02:00
|
|
|
const baseHtml = readTextFile('./html/index.htm', 'utf-8');
|
2018-07-25 01:53:29 +02:00
|
|
|
const baseUrl = process.env.BASE_URL ? process.env.BASE_URL : '/';
|
|
|
|
const finalHtml = baseHtml.replace(
|
|
|
|
'<!-- Base HTML Placeholder -->', `<base href="${baseUrl}"/>`);
|
|
|
|
writeFile('./public/index.htm', minifyHtml(finalHtml));
|
2016-05-21 09:46:41 +02:00
|
|
|
|
2016-05-18 00:15:28 +02:00
|
|
|
glob('./html/**/*.tpl', {}, (er, files) => {
|
2016-05-21 09:46:41 +02:00
|
|
|
let compiledTemplateJs = '\'use strict\'\n';
|
2016-05-21 10:02:06 +02:00
|
|
|
compiledTemplateJs += 'let _ = require(\'underscore\');';
|
2016-05-21 09:46:41 +02:00
|
|
|
compiledTemplateJs += 'let templates = {};';
|
2016-03-27 21:10:41 +02:00
|
|
|
for (const file of files) {
|
2016-05-18 00:15:28 +02:00
|
|
|
const name = path.basename(file, '.tpl').replace(/_/g, '-');
|
2016-07-30 23:05:46 +02:00
|
|
|
const placeholders = [];
|
|
|
|
let templateText = readTextFile(file, 'utf-8');
|
|
|
|
templateText = templateText.replace(
|
|
|
|
/<%.*?%>/ig,
|
|
|
|
(match) => {
|
|
|
|
const ret = '%%%TEMPLATE' + placeholders.length;
|
|
|
|
placeholders.push(match);
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
templateText = minifyHtml(templateText);
|
|
|
|
templateText = templateText.replace(
|
|
|
|
/%%%TEMPLATE(\d+)/g,
|
|
|
|
(match, number) => { return placeholders[number]; });
|
|
|
|
|
2016-05-21 10:02:06 +02:00
|
|
|
const functionText = underscore.template(
|
2016-05-21 09:46:41 +02:00
|
|
|
templateText, {variable: 'ctx'}).source;
|
|
|
|
compiledTemplateJs += `templates['${name}'] = ${functionText};`;
|
2016-03-27 21:10:41 +02:00
|
|
|
}
|
2016-05-21 09:46:41 +02:00
|
|
|
compiledTemplateJs += 'module.exports = templates;';
|
2016-05-21 12:33:02 +02:00
|
|
|
writeFile('./js/.templates.autogen.js', compiledTemplateJs);
|
2016-03-27 21:10:41 +02:00
|
|
|
console.info('Bundled HTML');
|
|
|
|
});
|
2016-03-27 20:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function bundleCss() {
|
2016-04-13 20:06:34 +02:00
|
|
|
const stylus = require('stylus');
|
|
|
|
glob('./css/**/*.styl', {}, (er, files) => {
|
2016-03-27 20:29:47 +02:00
|
|
|
let css = '';
|
|
|
|
for (const file of files) {
|
2016-04-13 20:06:34 +02:00
|
|
|
css += stylus.render(
|
2016-05-21 12:33:02 +02:00
|
|
|
readTextFile(file), {filename: file});
|
2016-03-27 20:29:47 +02:00
|
|
|
}
|
2016-05-21 12:33:02 +02:00
|
|
|
writeFile('./public/css/app.min.css', minifyCss(css));
|
|
|
|
|
2016-05-21 12:35:41 +02:00
|
|
|
copyFile(
|
|
|
|
'./node_modules/font-awesome/css/font-awesome.min.css',
|
|
|
|
'./public/css/vendor.min.css');
|
|
|
|
|
2016-03-27 20:29:47 +02:00
|
|
|
console.info('Bundled CSS');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-06 01:25:08 +02:00
|
|
|
function bundleJs() {
|
2016-03-27 20:29:47 +02:00
|
|
|
const browserify = require('browserify');
|
2016-05-14 13:50:39 +02:00
|
|
|
const external = [
|
2016-05-21 10:02:06 +02:00
|
|
|
'underscore',
|
2016-05-14 13:50:39 +02:00
|
|
|
'superagent',
|
|
|
|
'mousetrap',
|
|
|
|
'js-cookie',
|
|
|
|
'nprogress',
|
|
|
|
];
|
2016-05-21 09:46:41 +02:00
|
|
|
|
|
|
|
function writeJsBundle(b, path, message, compress) {
|
|
|
|
let outputFile = fs.createWriteStream(path);
|
|
|
|
b.bundle().pipe(outputFile);
|
|
|
|
outputFile.on('finish', function() {
|
|
|
|
if (compress) {
|
2016-05-21 12:33:02 +02:00
|
|
|
writeFile(path, minifyJs(path));
|
2016-05-21 09:46:41 +02:00
|
|
|
}
|
|
|
|
console.info(message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-14 13:50:39 +02:00
|
|
|
glob('./js/**/*.js', {}, (er, files) => {
|
2016-05-14 14:47:48 +02:00
|
|
|
if (!process.argv.includes('--no-vendor-js')) {
|
2016-05-14 13:50:39 +02:00
|
|
|
let b = browserify();
|
|
|
|
for (let lib of external) {
|
|
|
|
b.require(lib);
|
|
|
|
}
|
2018-07-06 01:25:08 +02:00
|
|
|
if (!process.argv.includes('--no-transpile')) {
|
2016-05-20 23:10:45 +02:00
|
|
|
b.add(require.resolve('babel-polyfill'));
|
|
|
|
}
|
2016-05-14 14:47:36 +02:00
|
|
|
writeJsBundle(
|
2016-05-21 12:33:02 +02:00
|
|
|
b, './public/js/vendor.min.js', 'Bundled vendor JS', true);
|
2016-04-13 19:02:21 +02:00
|
|
|
}
|
2016-05-14 13:50:39 +02:00
|
|
|
|
2016-05-14 14:47:48 +02:00
|
|
|
if (!process.argv.includes('--no-app-js')) {
|
2016-05-21 12:33:02 +02:00
|
|
|
let outputFile = fs.createWriteStream('./public/js/app.min.js');
|
2018-07-06 01:25:08 +02:00
|
|
|
let b = browserify({debug: process.argv.includes('--debug')});
|
|
|
|
if (!process.argv.includes('--no-transpile')) {
|
2016-05-20 23:10:45 +02:00
|
|
|
b = b.transform('babelify');
|
2016-04-07 18:51:29 +02:00
|
|
|
}
|
2016-05-14 14:47:36 +02:00
|
|
|
writeJsBundle(
|
|
|
|
b.external(external).add(files),
|
2016-05-21 12:33:02 +02:00
|
|
|
'./public/js/app.min.js',
|
2016-05-14 14:47:36 +02:00
|
|
|
'Bundled app JS',
|
2018-07-06 01:25:08 +02:00
|
|
|
!process.argv.includes('--debug'));
|
2016-05-14 13:50:39 +02:00
|
|
|
}
|
2016-03-27 20:29:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function bundleConfig(config) {
|
2016-05-21 12:33:02 +02:00
|
|
|
writeFile(
|
2016-04-01 18:45:25 +02:00
|
|
|
'./js/.config.autogen.json', JSON.stringify(config));
|
2016-05-21 12:35:41 +02:00
|
|
|
glob('./node_modules/font-awesome/fonts/*.*', {}, (er, files) => {
|
|
|
|
for (let file of files) {
|
|
|
|
if (fs.lstatSync(file).isDirectory()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
copyFile(file, path.join('./public/fonts/', path.basename(file)));
|
|
|
|
}
|
|
|
|
});
|
2016-03-27 20:29:47 +02:00
|
|
|
}
|
|
|
|
|
2016-05-21 12:33:02 +02:00
|
|
|
function bundleBinaryAssets() {
|
2018-08-23 19:10:51 +02:00
|
|
|
copyFile('./img/favicon.png', './public/img/favicon.png');
|
|
|
|
copyFile('./img/transparency_grid.png', './public/img/transparency_grid.png');
|
|
|
|
|
|
|
|
const Jimp = require('jimp');
|
|
|
|
|
|
|
|
for (let icon of [
|
|
|
|
{name: 'android-chrome-192x192.png', size: 192},
|
|
|
|
{name: 'android-chrome-512x512.png', size: 512},
|
|
|
|
{name: 'apple-touch-icon.png', size: 180},
|
|
|
|
{name: 'mstile-150x150.png', size: 150}
|
|
|
|
]) {
|
|
|
|
Jimp.read('./img/app.png', (err, infile) => {
|
|
|
|
infile
|
|
|
|
.resize(icon.size, Jimp.AUTO, Jimp.RESIZE_BEZIER)
|
|
|
|
.write(path.join('./public/img/', icon.name));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
console.info('Generated webapp icons');
|
|
|
|
|
|
|
|
for (let dim of [
|
|
|
|
{w: 640, h: 1136, center: 320},
|
|
|
|
{w: 750, h: 1294, center: 375},
|
|
|
|
{w: 1125, h: 2436, center: 565},
|
|
|
|
{w: 1242, h: 2148, center: 625},
|
|
|
|
{w: 1536, h: 2048, center: 770},
|
|
|
|
{w: 1668, h: 2224, center: 820},
|
|
|
|
{w: 2048, h: 2732, center: 1024}
|
|
|
|
]) {
|
|
|
|
Jimp.read('./img/splash.png', (err, infile) => {
|
|
|
|
infile
|
|
|
|
.resize(dim.center, Jimp.AUTO, Jimp.RESIZE_BEZIER)
|
|
|
|
.background(0xFFFFFFFF)
|
|
|
|
.contain(dim.w, dim.center,
|
|
|
|
Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE)
|
|
|
|
.contain(dim.w, dim.h,
|
|
|
|
Jimp.HORIZONTAL_ALIGN_CENTER | Jimp.VERTICAL_ALIGN_MIDDLE)
|
|
|
|
.write(path.join('./public/img/',
|
|
|
|
'apple-touch-startup-image-'
|
|
|
|
+ dim.w + '-' + dim.h + '.png'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
console.info('Generated splash screens');
|
2016-03-30 16:06:51 +02:00
|
|
|
}
|
|
|
|
|
2018-08-05 02:58:59 +02:00
|
|
|
function bundleWebAppFiles() {
|
|
|
|
copyFile('./app/manifest.json', './public/manifest.json');
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:29:47 +02:00
|
|
|
const config = getConfig();
|
|
|
|
bundleConfig(config);
|
2016-05-21 12:33:02 +02:00
|
|
|
bundleBinaryAssets();
|
2018-08-05 02:58:59 +02:00
|
|
|
bundleWebAppFiles();
|
2016-05-14 14:47:48 +02:00
|
|
|
if (!process.argv.includes('--no-html')) {
|
2018-07-06 01:25:08 +02:00
|
|
|
bundleHtml();
|
2016-05-14 14:47:48 +02:00
|
|
|
}
|
|
|
|
if (!process.argv.includes('--no-css')) {
|
|
|
|
bundleCss();
|
|
|
|
}
|
|
|
|
if (!process.argv.includes('--no-js')) {
|
2018-07-06 01:25:08 +02:00
|
|
|
bundleJs();
|
2016-05-14 14:47:48 +02:00
|
|
|
}
|