diff options
| author | rr- | 2016-03-27 20:29:47 +0200 |
|---|---|---|
| committer | rr- | 2016-03-27 23:05:10 +0200 |
| commit | 1846a1fa2ca9e4917dbf8a32bf234556e6d5fa98 (patch) | |
| tree | ed7cb125c7ad3734e48e03d4daff8d7d676c34b6 /scripts | |
| parent | 797ace982f7ae2952654d078e61bbe0c9e7bb9a3 (diff) | |
front/build: replace npm scripts with 1 script
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/build-frontend.js | 83 | ||||
| -rwxr-xr-x | scripts/host-waitress | 30 |
2 files changed, 113 insertions, 0 deletions
diff --git a/scripts/build-frontend.js b/scripts/build-frontend.js new file mode 100644 index 0000000..3da863c --- /dev/null +++ b/scripts/build-frontend.js @@ -0,0 +1,83 @@ +'use strict'; + +const glob = require('glob'); +const fs = require('fs'); + +function getConfig() { + const ini = require('ini'); + const merge = require('merge'); + const camelcaseKeys = require('camelcase-keys'); + + function parseIniFile(path) { + let result = ini.parse(fs.readFileSync(path, 'utf-8') + .replace(/#.+$/gm, '') + .replace(/\s+$/gm, '')); + Object.keys(result).map((key, _) => { + result[key] = camelcaseKeys(result[key]); + }); + return result; + } + + let config = parseIniFile('./config.ini.dist'); + + try { + const localConfig = parseIniFile('./config.ini'); + config = merge.recursive(config, localConfig); + } catch (e) { + console.warn('Local config does not exist, ignoring'); + } + + delete config.basic.secret; + delete config.smtp; + delete config.database; + config.service.userRanks = config.service.userRanks.split(/,\s*/); + config.service.tagCategories = config.service.tagCategories.split(/,\s*/); + + return config; +} + +function bundleHtml() { + const minify = require('html-minifier').minify; + const html = fs.readFileSync('./static/html/index.htm', 'utf-8'); + fs.writeFileSync( + './public/index.htm', + minify(html, {removeComments: true, collapseWhitespace: true})); + console.info('Bundled HTML'); +} + +function bundleCss() { + const minify = require('cssmin'); + glob('static/**/*.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() { + const browserify = require('browserify'); + const uglifyjs = require('uglify-js'); + glob('./static/js/**/*.js', {}, function(er, files) { + const outputFile = fs.createWriteStream('./public/bundle.min.js'); + browserify().add(files).bundle().pipe(outputFile); + outputFile.on('finish', function() { + 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( + './static/js/.config.autogen.json', JSON.stringify(config)); +} + +const config = getConfig(); +bundleConfig(config); +bundleHtml(); +bundleCss(); +bundleJs(); diff --git a/scripts/host-waitress b/scripts/host-waitress new file mode 100755 index 0000000..8b369ff --- /dev/null +++ b/scripts/host-waitress @@ -0,0 +1,30 @@ +#!/bin/python3 + +''' +Script facade for direct execution with waitress WSGI server. +Note that szurubooru can be also run using ``python -m szurubooru``, when in +the repository's root directory. +''' + +import argparse +import os.path +import sys +import waitress + +sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.pardir)) +from szurubooru.app import create_app + +def main(): + parser = argparse.ArgumentParser('Starts szurubooru using waitress.') + parser.add_argument( + '-p', '--port', + type=int, help='port to listen on', default=6666) + parser.add_argument( + '--host', help='IP to listen on', default='0.0.0.0') + args = parser.parse_args() + + app = create_app() + waitress.serve(app, host=args.host, port=args.port) + +if __name__ == '__main__': + main() |