refactor: async/await install/web.js

v1.18.x
Barış Soner Uşaklı 4 years ago
parent 1fd2eba6f2
commit 3881ac309f

@ -1,21 +1,26 @@
'use strict'; 'use strict';
var winston = require('winston'); const winston = require('winston');
var express = require('express'); const express = require('express');
var bodyParser = require('body-parser'); const bodyParser = require('body-parser');
var fs = require('fs'); const fs = require('fs');
var path = require('path'); const path = require('path');
var childProcess = require('child_process'); const childProcess = require('child_process');
var less = require('less'); const less = require('less');
var async = require('async'); const util = require('util');
var uglify = require('uglify-es'); const lessRenderAsync = util.promisify(
var nconf = require('nconf'); (style, opts, cb) => less.render(String(style), opts, cb)
var Benchpress = require('benchpressjs'); );
const uglify = require('uglify-es');
var app = express(); const nconf = require('nconf');
var server;
const Benchpress = require('benchpressjs');
var formats = [ const { paths } = require('../src/constants');
const app = express();
let server;
const formats = [
winston.format.colorize(), winston.format.colorize(),
]; ];
@ -42,9 +47,9 @@ winston.configure({
], ],
}); });
var web = module.exports; const web = module.exports;
var scripts = [ const scripts = [
'node_modules/jquery/dist/jquery.js', 'node_modules/jquery/dist/jquery.js',
'node_modules/xregexp/xregexp-all.js', 'node_modules/xregexp/xregexp-all.js',
'public/src/modules/slugify.js', 'public/src/modules/slugify.js',
@ -53,39 +58,39 @@ var scripts = [
'node_modules/zxcvbn/dist/zxcvbn.js', 'node_modules/zxcvbn/dist/zxcvbn.js',
]; ];
var installing = false; let installing = false;
var success = false; let success = false;
var error = false; let error = false;
var launchUrl; let launchUrl;
web.install = function (port) { web.install = async function (port) {
port = port || 4567; port = port || 4567;
winston.info('Launching web installer on port ' + port); winston.info('Launching web installer on port ' + port);
app.use(express.static('public', {})); app.use(express.static('public', {}));
app.engine('tpl', function (filepath, options, callback) { app.engine('tpl', function (filepath, options, callback) {
async.waterfall([ filepath = filepath.replace(/\.tpl$/, '.js');
function (next) {
fs.readFile(filepath, 'utf-8', next); Benchpress.__express(filepath, options, callback);
},
function (buffer, next) {
Benchpress.compileParse(buffer.toString(), options, next);
},
], callback);
}); });
app.set('view engine', 'tpl'); app.set('view engine', 'tpl');
app.set('views', path.join(__dirname, '../src/views')); const viewsDir = path.join(paths.baseDir, 'build/public/templates');
app.set('views', viewsDir);
app.use(bodyParser.urlencoded({ app.use(bodyParser.urlencoded({
extended: true, extended: true,
})); }));
try {
async.parallel([compileLess, compileJS, copyCSS, loadDefaults], function (err) { await Promise.all([
if (err) { compileLess(),
winston.error(err.stack); compileJS(),
} copyCSS(),
loadDefaults(),
]);
setupRoutes(); setupRoutes();
launchExpress(port); launchExpress(port);
}); } catch (err) {
winston.error(err.stack);
}
}; };
@ -180,7 +185,8 @@ function install(req, res) {
}); });
} }
function launch(req, res) { async function launch(req, res) {
try {
res.json({}); res.json({});
server.close(); server.close();
req.setTimeout(0); req.setTimeout(0);
@ -204,96 +210,72 @@ function launch(req, res) {
}); });
} }
var filesToDelete = [ const filesToDelete = [
'installer.css', 'installer.css',
'installer.min.js', 'installer.min.js',
'bootstrap.min.css', 'bootstrap.min.css',
]; ];
await Promise.all(
async.each(filesToDelete, function (filename, next) { filesToDelete.map(
fs.unlink(path.join(__dirname, '../public', filename), next); filename => fs.promises.unlink(path.join(__dirname, '../public', filename))
}, function (err) { )
if (err) { );
winston.warn('Unable to remove installer files');
}
child.unref(); child.unref();
process.exit(0); process.exit(0);
}); } catch (err) {
winston.error(err.stack);
throw err;
} }
function compileLess(callback) {
var installSrc = path.join(__dirname, '../public/less/install.less');
fs.readFile(installSrc, function (err, style) {
if (err) {
return winston.error('Unable to read LESS install file: ', err.stack);
} }
less.render(style.toString(), { async function compileLess() {
filename: path.resolve(installSrc), try {
}, function (err, css) { const installSrc = path.join(__dirname, '../public/less/install.less');
if (err) { const style = await fs.promises.readFile(installSrc);
return winston.error('Unable to compile LESS: ', err.stack); const css = await lessRenderAsync(style, { filename: path.resolve(installSrc) });
await fs.promises.writeFile(path.join(__dirname, '../public/installer.css'), css.css);
} catch (err) {
winston.error('Unable to compile LESS: \n' + err.stack);
throw err;
} }
fs.writeFile(path.join(__dirname, '../public/installer.css'), css.css, callback);
});
});
} }
function compileJS(callback) { async function compileJS() {
var code = ''; let code = '';
async.eachSeries(scripts, function (srcPath, next) {
fs.readFile(path.join(__dirname, '..', srcPath), function (err, buffer) {
if (err) {
return next(err);
}
for (const srcPath of scripts) {
// eslint-disable-next-line no-await-in-loop
const buffer = await fs.promises.readFile(path.join(__dirname, '..', srcPath));
code += buffer.toString(); code += buffer.toString();
next();
});
}, function (err) {
if (err) {
return callback(err);
} }
try { const minified = uglify.minify(code, {
var minified = uglify.minify(code, {
compress: false, compress: false,
}); });
if (!minified.code) { if (!minified.code) {
return callback(new Error('[[error:failed-to-minify]]')); throw new Error('[[error:failed-to-minify]]');
} }
fs.writeFile(path.join(__dirname, '../public/installer.min.js'), minified.code, callback); await fs.promises.writeFile(path.join(__dirname, '../public/installer.min.js'), minified.code);
} catch (e) {
callback(e);
}
});
} }
function copyCSS(next) { async function copyCSS() {
async.waterfall([ const src = await fs.promises.readFile(
function (next) { path.join(__dirname, '../node_modules/bootstrap/dist/css/bootstrap.min.css'), 'utf8'
fs.readFile(path.join(__dirname, '../node_modules/bootstrap/dist/css/bootstrap.min.css'), 'utf8', next); );
}, await fs.promises.writeFile(path.join(__dirname, '../public/bootstrap.min.css'), src);
function (src, next) {
fs.writeFile(path.join(__dirname, '../public/bootstrap.min.css'), src, next);
},
], next);
} }
function loadDefaults(next) { async function loadDefaults() {
var setupDefaultsPath = path.join(__dirname, '../setup.json'); const setupDefaultsPath = path.join(__dirname, '../setup.json');
fs.access(setupDefaultsPath, fs.constants.F_OK | fs.constants.R_OK, function (err) { try {
if (err) { await fs.promises.access(setupDefaultsPath, fs.constants.F_OK | fs.constants.R_OK);
} catch (err) {
// setup.json not found or inaccessible, proceed with no defaults // setup.json not found or inaccessible, proceed with no defaults
return setImmediate(next); if (err.code !== 'ENOENT') {
throw err;
}
} }
winston.info('[installer] Found setup.json, populating default values'); winston.info('[installer] Found setup.json, populating default values');
nconf.file({ nconf.file({
file: setupDefaultsPath, file: setupDefaultsPath,
}); });
next();
});
} }

Loading…
Cancel
Save