szurubooru/client/build.js

177 lines
5 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');
}
config.canSendMails = !!config.smtp.host;
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');
const minifyOptions = {
removeComments: true,
collapseWhitespace: true,
conservativeCollapse: true,
};
glob('./html/**/*.tpl', {}, (er, files) => {
let templates = {};
2016-03-27 21:10:41 +02:00
for (const file of files) {
const name = path.basename(file, '.tpl').replace(/_/g, '-');
templates[name] = minify(
fs.readFileSync(file, 'utf-8'), minifyOptions);
2016-03-27 21:10:41 +02:00
}
const templatesHolder = util.format(
'<script type=\'text/javascript\'>' +
2016-04-14 13:42:47 +02:00
'var templates = %s;' +
'</script>',
JSON.stringify(templates));
2016-03-27 21:10:41 +02:00
const finalHtml = baseHtml
.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(
'./public/index.htm', minify(finalHtml, minifyOptions));
2016-03-27 21:10:41 +02:00
console.info('Bundled HTML');
});
}
function bundleCss() {
const minify = require('csso').minify;
2016-04-13 20:06:34 +02:00
const stylus = require('stylus');
glob('./css/**/*.styl', {}, (er, files) => {
let css = '';
for (const file of files) {
2016-04-13 20:06:34 +02:00
css += stylus.render(
fs.readFileSync(file, 'utf-8'), {filename: file});
}
fs.writeFileSync('./public/app.min.css', minify(css));
console.info('Bundled CSS');
});
}
function writeJsBundle(b, path, message, compress) {
const uglifyjs = require('uglify-js');
let outputFile = fs.createWriteStream(path);
b.bundle().pipe(outputFile);
outputFile.on('finish', function() {
if (compress) {
const result = uglifyjs.minify(path);
fs.writeFileSync(path, result.code);
}
console.info(message);
});
}
function bundleJs(config) {
const babelify = require('babelify');
const browserify = require('browserify');
const external = [
'lodash',
'superagent',
'mousetrap',
'js-cookie',
'page',
'nprogress',
'babel-polyfill',
];
glob('./js/**/*.js', {}, (er, files) => {
if (!process.argv.includes('--no-vendor-js')) {
let b = browserify();
for (let lib of external) {
b.require(lib);
}
writeJsBundle(
b, './public/vendor.min.js', 'Bundled vendor JS', true);
}
if (!process.argv.includes('--no-app-js')) {
let outputFile = fs.createWriteStream('./public/app.min.js');
let b = browserify({debug: config.debug});
if (config.transpile) {
b = b.transform(babelify);
}
writeJsBundle(
b.external(external).add(files),
'./public/app.min.js',
'Bundled app JS',
!config.debug);
}
});
}
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);
if (!process.argv.includes('--no-html')) {
bundleHtml(config);
}
if (!process.argv.includes('--no-css')) {
bundleCss();
}
if (!process.argv.includes('--no-js')) {
bundleJs(config);
}
2016-04-01 18:45:25 +02:00
copyFile('./img/favicon.png', './public/favicon.png');