szurubooru/client/build.js

129 lines
3.7 KiB
JavaScript
Raw Normal View History

'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();
}
function getConfig() {
2016-04-06 20:38:45 +02:00
const yaml = require('js-yaml');
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-04-06 20:38:45 +02:00
let config = parseConfigFile('../config.yaml.dist');
try {
2016-04-06 20:38:45 +02:00
const localConfig = parseConfigFile('../config.yaml');
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;
delete config.smtp;
delete config.database;
2016-03-31 23:18:08 +02:00
config.meta = {
version: getVersion(),
buildDate: new Date().toUTCString(),
};
return config;
}
2016-03-27 21:10:41 +02:00
function bundleHtml(config) {
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(
finalHtml, {
removeComments: true,
collapseWhitespace: true,
conservativeCollapse: true}));
2016-03-27 21:10:41 +02:00
console.info('Bundled HTML');
});
}
function bundleCss() {
const minify = require('csso').minify;
2016-04-01 18:45:25 +02:00
glob('./css/**/*.css', {}, (er, files) => {
let css = '';
for (const file of files) {
css += fs.readFileSync(file);
}
fs.writeFileSync('./public/bundle.min.css', minify(css));
console.info('Bundled CSS');
});
}
function bundleJs(config) {
const browserify = require('browserify');
const uglifyjs = require('uglify-js');
2016-04-01 18:45:25 +02:00
glob('./js/**/*.js', {}, function(er, files) {
const outputFile = fs.createWriteStream('./public/bundle.min.js');
browserify().add(files).bundle().pipe(outputFile);
outputFile.on('finish', function() {
if (!config.debug) {
const result = uglifyjs.minify('./public/bundle.min.js');
fs.writeFileSync('./public/bundle.min.js', result.code);
}
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-30 16:06:51 +02:00
function copyFile(source, target) {
fs.createReadStream(source).pipe(fs.createWriteStream(target));
}
const config = getConfig();
bundleConfig(config);
2016-03-27 21:10:41 +02:00
bundleHtml(config);
bundleCss();
bundleJs(config);
2016-04-01 18:45:25 +02:00
copyFile('./img/favicon.png', './public/favicon.png');