chore(routes) es2015-ify

This commit is contained in:
coderaiser 2016-12-20 10:22:28 +02:00
parent 807813ecdb
commit 3e1db58144

View file

@ -1,56 +1,53 @@
'use strict';
var DIR = __dirname + '/../../',
DIR_TMPL = DIR + 'tmpl/',
DIR_HTML = DIR + 'html/',
DIR_COMMON = DIR + 'common/',
DIR_JSON = DIR + 'json/',
DIR_SERVER = __dirname + '/',
DIR_FS = DIR_TMPL + 'fs/',
fs = require('fs'),
flop = require('flop'),
ponse = require('ponse'),
files = require('files-io'),
rendy = require('rendy'),
exec = require('execon'),
minify = require('minify'),
format = require('format-io'),
squad = require('squad'),
apart = require('apart'),
config = require(DIR_SERVER + 'config'),
root = require(DIR_SERVER + 'root'),
prefixer = require(DIR_SERVER + 'prefixer'),
prefix = squad(prefixer, apart(config, 'prefix')),
CloudFunc = require(DIR_COMMON + 'cloudfunc'),
PATH_INDEX = DIR_HTML + 'index.html',
TMPL_PATH = [
'file',
'panel',
'path',
'pathLink',
'link'
],
Template = {},
FS = CloudFunc.FS,
CSS_URL = require(DIR_JSON + 'css.json')
.map(function(name) {
return 'css/' + name + '.css';
}).join(':');
const DIR = __dirname + '/../../';
const DIR_TMPL = DIR + 'tmpl/';
const DIR_HTML = DIR + 'html/';
const DIR_COMMON = DIR + 'common/';
const DIR_JSON = DIR + 'json/';
const DIR_SERVER = __dirname + '/';
const DIR_FS = DIR_TMPL + 'fs/';
module.exports = function(req, res, next) {
const fs = require('fs');
const flop = require('flop');
const ponse = require('ponse');
const files = require('files-io');
const rendy = require('rendy');
const exec = require('execon');
const minify = require('minify');
const format = require('format-io');
const squad = require('squad');
const apart = require('apart');
const config = require(DIR_SERVER + 'config');
const root = require(DIR_SERVER + 'root');
const prefixer = require(DIR_SERVER + 'prefixer');
const prefix = squad(prefixer, apart(config, 'prefix'));
const CloudFunc = require(DIR_COMMON + 'cloudfunc');
const PATH_INDEX = DIR_HTML + 'index.html';
const TMPL_PATH = [
'file',
'panel',
'path',
'pathLink',
'link'
];
const Template = {};
const FS = CloudFunc.FS;
const CSS_URL = require(DIR_JSON + 'css.json')
.map((name) => {
return 'css/' + name + '.css'
}).join(':');
module.exports = (req, res, next) => {
check(req, res, next);
readFiles(function() {
readFiles(() => {
route(req, res, next);
});
};
@ -59,40 +56,42 @@ module.exports = function(req, res, next) {
* additional processing of index file
*/
function indexProcessing(options) {
var from, to,
left = '',
right = '',
keysPanel = '<div id="js-keyspanel" class="{{ className }}',
isOnePanel = config('onePanelMode'),
noConfig = !config('configDialog'),
noConsole = !config('console'),
data = options.data,
panel = options.panel;
let from;
let to;
let left = '';
let right = '';
const keysPanel = '<div id="js-keyspanel" class="{{ className }}';
const isOnePanel = config('onePanelMode');
const noConfig = !config('configDialog');
const noConsole = !config('console');
const panel = options.panel;
let data = options.data;
if (!config('showKeysPanel')) {
from = rendy(keysPanel, {
from = rendy(keysPanel, {
className: 'keyspanel'
});
to = rendy(keysPanel, {
to = rendy(keysPanel, {
className: 'keyspanel hidden'
});
data = data.replace(from, to);
data = data.replace(from, to);
}
if (isOnePanel)
data = data
.replace('icon-move', 'icon-move none')
.replace('icon-copy', 'icon-copy none');
if (noConfig)
data = data
.replace('icon-config', 'icon-config none');
if (noConsole)
data = data
.replace('icon-console', 'icon-console none');
if (noConfig)
data = data
.replace('icon-config', 'icon-config none');
if (noConsole)
data = data
.replace('icon-console', 'icon-console none');
left = rendy(Template.panel, {
side : 'left',
@ -118,54 +117,48 @@ function indexProcessing(options) {
}
function readFiles(callback) {
var filesList,
paths = {},
lengthTmpl = Object.keys(Template).length,
lenthPath = TMPL_PATH.length,
isRead = lengthTmpl === lenthPath;
const paths = {};
const lengthTmpl = Object.keys(Template).length;
const lenthPath = TMPL_PATH.length;
const isRead = lengthTmpl === lenthPath;
if (typeof callback !== 'function')
throw Error('callback should be function!');
throw Error('callback should be a function!');
if (isRead) {
callback();
} else {
filesList = TMPL_PATH.map(function(name) {
var path = DIR_FS + name + '.hbs';
if (isRead)
return callback();
const filesList = TMPL_PATH.map((name) => {
const path = DIR_FS + name + '.hbs';
paths[path] = name;
return path;
});
files.read(filesList, 'utf8', (error, files) => {
if (error)
throw error;
paths[path] = name;
Object.keys(files).forEach((path) => {
const name = paths[path];
return path;
Template[name] = files[path];
});
files.read(filesList, 'utf8', function(error, files) {
if (error)
throw error;
else
Object.keys(files).forEach(function(path) {
var name = paths[path];
Template[name] = files[path];
});
callback();
});
}
callback();
});
}
/**
* routing of server queries
*/
function route(request, response, callback) {
var name, p, isAuth, isFS, fullPath;
let name = ponse.getPathName(request);
name = ponse.getPathName(request);
isAuth = RegExp('^(/auth|/auth/github)$').test(name);
isFS = RegExp('^/$|^' + FS).test(name);
p = {
const isAuth = RegExp('^(/auth|/auth/github)$').test(name);
const isFS = RegExp('^/$|^' + FS).test(name);
const p = {
request : request,
response : response,
gzip : true,
@ -173,66 +166,71 @@ function route(request, response, callback) {
};
if (!isAuth && !isFS)
callback();
else if (isAuth) {
return callback();
if (isAuth) {
p.name = DIR_HTML + name + '.html';
ponse.sendFile(p);
} else if (isFS) {
name = name.replace(CloudFunc.FS, '') || '/';
fullPath = root(name);
flop.read(fullPath, function(error, dir) {
if (dir)
dir.path = format.addSlashToEnd(name);
if (!error)
buildIndex(dir, function(error, data) {
p.name = PATH_INDEX;
if (error)
ponse.sendError(error, p);
else
ponse.send(data, p);
});
else if (error.code !== 'ENOTDIR')
ponse.sendError(error, p);
else
fs.realpath(fullPath, function(error, pathReal) {
if (!error)
p.name = pathReal;
else
p.name = name;
p.gzip = false;
ponse.sendFile(p);
});
});
return;
}
if (!isFS)
return;
name = name.replace(CloudFunc.FS, '') || '/';
const fullPath = root(name);
flop.read(fullPath, (error, dir) => {
if (dir)
dir.path = format.addSlashToEnd(name);
if (!error)
return buildIndex(dir, (error, data) => {
p.name = PATH_INDEX;
if (error)
return ponse.sendError(error, p);
ponse.send(data, p);
});
if (error.code !== 'ENOTDIR')
return ponse.sendError(error, p);
fs.realpath(fullPath, (error, pathReal) => {
if (!error)
p.name = pathReal;
else
p.name = name;
p.gzip = false;
ponse.sendFile(p);
});
});
}
function buildIndex(json, callback) {
var isMinify = config('minify');
const isMinify = config('minify');
exec.if(!isMinify, function(error, name) {
fs.readFile(name || PATH_INDEX, 'utf8', function(error, template) {
var panel, data;
if (!error) {
panel = CloudFunc.buildFromJSON({
data : json,
prefix : prefix(),
template : Template
}),
exec.if(!isMinify, (error, name) => {
fs.readFile(name || PATH_INDEX, 'utf8', (error, template) => {
if (error)
return;
data = indexProcessing({
panel : panel,
data : template
});
}
const panel = CloudFunc.buildFromJSON({
data : json,
prefix : prefix(),
template : Template
});
const data = indexProcessing({
panel : panel,
data : template
});
callback(error, data);
});
}, function(callback) {
}, (callback) => {
minify(PATH_INDEX, 'name', callback);
});
}
@ -247,3 +245,4 @@ function check(req, res, next) {
if (typeof next !== 'function')
throw Error('next should be function!');
}