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-04-06 20:38:45 +02:00
|
|
|
const camelcase = require('camelcase');
|
|
|
|
|
|
|
|
function convertKeysToCamelCase(input) {
|
|
|
|
let result = {};
|
|
|
|
Object.keys(input).map((key, _) => {
|
|
|
|
const value = input[key];
|
|
|
|
if (value !== null && value.constructor == Object) {
|
|
|
|
result[camelcase(key)] = convertKeysToCamelCase(value);
|
|
|
|
} else {
|
|
|
|
result[camelcase(key)] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
2016-03-31 23:18:08 +02:00
|
|
|
|
|
|
|
function getVersion() {
|
|
|
|
return execSync('git describe --always --dirty --long --tags').toString();
|
|
|
|
}
|
2016-03-27 20:29:47 +02:00
|
|
|
|
|
|
|
function getConfig() {
|
2016-04-06 20:38:45 +02:00
|
|
|
const yaml = require('js-yaml');
|
2016-03-27 20:29:47 +02:00
|
|
|
const merge = require('merge');
|
|
|
|
const camelcaseKeys = require('camelcase-keys');
|
|
|
|
|
2016-04-06 20:38:45 +02:00
|
|
|
function parseConfigFile(path) {
|
|
|
|
let result = yaml.load(fs.readFileSync(path, 'utf-8'));
|
|
|
|
return convertKeysToCamelCase(result);
|
2016-03-27 20:29:47 +02:00
|
|
|
}
|
|
|
|
|
2016-04-06 20:38:45 +02:00
|
|
|
let config = parseConfigFile('../config.yaml.dist');
|
2016-03-27 20:29:47 +02:00
|
|
|
|
|
|
|
try {
|
2016-04-06 20:38:45 +02:00
|
|
|
const localConfig = parseConfigFile('../config.yaml');
|
2016-03-27 20:29:47 +02:00
|
|
|
config = merge.recursive(config, localConfig);
|
|
|
|
} catch (e) {
|
|
|
|
console.warn('Local config does not exist, ignoring');
|
|
|
|
}
|
|
|
|
|
2016-04-06 20:38:45 +02:00
|
|
|
delete config.secret;
|
2016-03-27 20:29:47 +02:00
|
|
|
delete config.smtp;
|
|
|
|
delete config.database;
|
2016-03-31 23:18:08 +02:00
|
|
|
config.meta = {
|
|
|
|
version: getVersion(),
|
|
|
|
buildDate: new Date().toUTCString(),
|
|
|
|
};
|
2016-03-27 20:29:47 +02:00
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2016-03-27 21:10:41 +02:00
|
|
|
function bundleHtml(config) {
|
2016-03-27 20:29:47 +02:00
|
|
|
const minify = require('html-minifier').minify;
|
2016-04-01 18:45:25 +02:00
|
|
|
const baseHtml = fs.readFileSync('./html/index.htm', 'utf-8');
|
|
|
|
glob('./html/**/*.hbs', {}, (er, files) => {
|
2016-03-27 21:10:41 +02:00
|
|
|
let templatesHtml = '';
|
|
|
|
for (const file of files) {
|
|
|
|
templatesHtml += util.format(
|
|
|
|
'<template id=\'%s-template\'>%s</template>',
|
2016-04-01 10:24:04 +02:00
|
|
|
path.basename(file, '.hbs').replace('_', '-'),
|
2016-03-27 21:10:41 +02:00
|
|
|
fs.readFileSync(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
const finalHtml = baseHtml
|
|
|
|
.replace(/(<\/head>)/, templatesHtml + '$1')
|
|
|
|
.replace(
|
|
|
|
/(<title>)(.*)(<\/title>)/,
|
2016-04-06 20:38:45 +02:00
|
|
|
util.format('$1%s$3', config.name));
|
2016-03-27 21:10:41 +02:00
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
'./public/index.htm',
|
|
|
|
minify(
|
2016-03-30 23:41:32 +02:00
|
|
|
finalHtml, {
|
|
|
|
removeComments: true,
|
|
|
|
collapseWhitespace: true,
|
|
|
|
conservativeCollapse: true}));
|
2016-03-27 21:10:41 +02:00
|
|
|
console.info('Bundled HTML');
|
|
|
|
});
|
2016-03-27 20:29:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function bundleCss() {
|
2016-03-27 23:00:42 +02:00
|
|
|
const minify = require('csso').minify;
|
2016-04-01 18:45:25 +02:00
|
|
|
glob('./css/**/*.css', {}, (er, files) => {
|
2016-03-27 20:29:47 +02:00
|
|
|
let css = '';
|
|
|
|
for (const file of files) {
|
|
|
|
css += fs.readFileSync(file);
|
|
|
|
}
|
|
|
|
fs.writeFileSync('./public/bundle.min.css', minify(css));
|
|
|
|
console.info('Bundled CSS');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-07 18:51:29 +02:00
|
|
|
function bundleJs(config) {
|
2016-03-27 20:29:47 +02:00
|
|
|
const browserify = require('browserify');
|
|
|
|
const uglifyjs = require('uglify-js');
|
2016-04-01 18:45:25 +02:00
|
|
|
glob('./js/**/*.js', {}, function(er, files) {
|
2016-03-27 20:29:47 +02:00
|
|
|
const outputFile = fs.createWriteStream('./public/bundle.min.js');
|
|
|
|
browserify().add(files).bundle().pipe(outputFile);
|
|
|
|
outputFile.on('finish', function() {
|
2016-04-07 18:51:29 +02:00
|
|
|
if (!config.debug) {
|
|
|
|
const result = uglifyjs.minify('./public/bundle.min.js');
|
|
|
|
fs.writeFileSync('./public/bundle.min.js', result.code);
|
|
|
|
}
|
2016-03-27 20:29:47 +02:00
|
|
|
console.info('Bundled JS');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function bundleConfig(config) {
|
|
|
|
fs.writeFileSync(
|
2016-04-01 18:45:25 +02:00
|
|
|
'./js/.config.autogen.json', JSON.stringify(config));
|
2016-03-27 20:29:47 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 16:06:51 +02:00
|
|
|
function copyFile(source, target) {
|
|
|
|
fs.createReadStream(source).pipe(fs.createWriteStream(target));
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:29:47 +02:00
|
|
|
const config = getConfig();
|
|
|
|
bundleConfig(config);
|
2016-03-27 21:10:41 +02:00
|
|
|
bundleHtml(config);
|
2016-03-27 20:29:47 +02:00
|
|
|
bundleCss();
|
2016-04-07 18:51:29 +02:00
|
|
|
bundleJs(config);
|
2016-04-01 18:45:25 +02:00
|
|
|
copyFile('./img/favicon.png', './public/favicon.png');
|