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-08 20:09:32 +02:00
|
|
|
config.canSendMails = !!config.smtp.host;
|
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');
|
2016-04-13 18:58:34 +02:00
|
|
|
const minifyOptions = {
|
|
|
|
removeComments: true,
|
|
|
|
collapseWhitespace: true,
|
|
|
|
conservativeCollapse: true,
|
|
|
|
};
|
2016-04-01 18:45:25 +02:00
|
|
|
glob('./html/**/*.hbs', {}, (er, files) => {
|
2016-04-13 18:58:34 +02:00
|
|
|
let templates = {};
|
2016-03-27 21:10:41 +02:00
|
|
|
for (const file of files) {
|
2016-04-13 18:58:34 +02:00
|
|
|
const name = path.basename(file, '.hbs').replace(/_/g, '-');
|
|
|
|
templates[name] = minify(
|
|
|
|
fs.readFileSync(file, 'utf-8'), minifyOptions);
|
2016-03-27 21:10:41 +02:00
|
|
|
}
|
|
|
|
|
2016-04-13 18:58:34 +02:00
|
|
|
const templatesHolder = util.format(
|
|
|
|
'<script type=\'text/javascript\'>' +
|
|
|
|
'const templates = %s;' +
|
|
|
|
'</script>',
|
|
|
|
JSON.stringify(templates));
|
|
|
|
|
2016-03-27 21:10:41 +02:00
|
|
|
const finalHtml = baseHtml
|
2016-04-13 18:58:34 +02:00
|
|
|
.replace(/(<\/head>)/, templatesHolder + '$1')
|
2016-03-27 21:10:41 +02:00
|
|
|
.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(
|
2016-04-13 18:58:34 +02:00
|
|
|
'./public/index.htm', minify(finalHtml, minifyOptions));
|
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');
|
2016-04-07 22:27:40 +02:00
|
|
|
browserify({debug: config.debug}).add(files).bundle().pipe(outputFile);
|
2016-03-27 20:29:47 +02:00
|
|
|
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');
|