feature(cloudcmd) move out dist from route

This commit is contained in:
coderaiser 2018-05-11 13:03:58 +03:00
parent bb44a14780
commit 4e99372e6f
6 changed files with 94 additions and 163 deletions

View file

@ -172,7 +172,7 @@ function isNoCurrent(panel) {
const namePanel = panel.getAttribute('data-name');
const nameInfoPanel = infoPanel.getAttribute('data-name');
return namePanel !== nameInfoPanel
return namePanel !== nameInfoPanel;
}
function decodePath(path){

View file

@ -30,7 +30,7 @@
<button id=f9 class="cmd-button reduce-text icon-menu" title="Menu" >F9</button>
<button id=f10 class="cmd-button reduce-text icon-config" title="Config" >F10</button>
<button id=~ class="cmd-button reduce-text icon-console" title="Console" >~</button>
<button id=shift~ class="cmd-button reduce-text icon-terminal" title="Terminal" >⇧ ~</button>
<button id=shift~ class="cmd-button reduce-text icon-terminal" title="Terminal" >⇧ ~</button>
<button id=contact class="cmd-button reduce-text icon-contact" title="Contact" ></button>
</div>
<script src="{{ prefix }}/dist/cloudcmd.common.js"></script>

View file

@ -5,6 +5,8 @@ const DIR_ROOT = DIR + '../';
const DIR_COMMON = DIR + '../common/';
const util = require('util');
const path = require('path');
const fs = require('fs');
const cloudfunc = require(DIR_COMMON + 'cloudfunc');
const authentication = require(DIR + 'auth');
@ -29,6 +31,12 @@ const deepword = require('deepword');
const nomine = require('nomine');
const fileop = require('@cloudcmd/fileop');
const isDev = process.env.NODE_ENV === 'development';
const getDist = (isDev) => isDev ? 'dist-dev' : 'dist';
const getIndexPath = (isDev) => path.join(DIR, '..', `${getDist(isDev)}/index.html`);
const defaultHtml = fs.readFileSync(getIndexPath(isDev), 'utf8');
const auth = currify(_auth);
const setUrl = currify(_setUrl);
@ -37,8 +45,6 @@ const root = () => config('root');
const notEmpty = (a) => a;
const clean = (a) => a.filter(notEmpty);
const isDev = process.env.NODE_ENV === 'development';
const deprecateOnePanelMode = (value) => {
const noop = () => {};
@ -81,6 +87,8 @@ module.exports = (params) => {
return cloudcmd(prefix, plugins, modules);
};
module.exports._getIndexPath = getIndexPath;
function defaultValue(name, options) {
const value = options[name];
const previous = config(name);
@ -214,7 +222,9 @@ function cloudcmd(prefix, plugins, modules) {
}),
rest,
route,
route({
html: defaultHtml
}),
join({
dir,

View file

@ -1,11 +1,9 @@
'use strict';
const DIR = __dirname + '/../';
const DIR_SERVER = './';
const DIR_COMMON = '../common/';
const fs = require('fs');
const path = require('path');
const flop = require('flop');
const ponse = require('ponse');
@ -13,6 +11,7 @@ const rendy = require('rendy');
const format = require('format-io');
const squad = require('squad/legacy');
const apart = require('apart');
const currify = require('currify/legacy');
const config = require(DIR_SERVER + 'config');
const root = require(DIR_SERVER + 'root');
@ -20,22 +19,64 @@ const prefixer = require(DIR_SERVER + 'prefixer');
const CloudFunc = require(DIR_COMMON + 'cloudfunc');
const prefix = squad(prefixer, apart(config, 'prefix'));
const isDev = process.env.NODE_ENV === 'development';
const getDist = (isDev) => isDev ? 'dist-dev' : 'dist';
module.exports._getIndexPath = getIndexPath;
function getIndexPath(isDev) {
const dist = getDist(isDev);
return path.join(DIR, `${dist}/index.html`);
}
const sendIndex = (params, data) => {
const ponseParams = Object.assign({}, params, {
name: 'index.html'
});
ponse.send(data, ponseParams);
};
const FS = CloudFunc.FS;
const Columns = require('./columns');
const Template = require('./template');
module.exports = route;
module.exports._getIndexPath = getIndexPath;
/**
* routing of server queries
*/
module.exports = currify((options, request, response, callback) => {
const html = options.html;
let name = ponse.getPathName(request);
const isFS = RegExp('^/$|^' + FS).test(name);
const gzip = true;
const p = {
request,
response,
gzip,
name,
};
if (!isFS)
return callback();
name = name.replace(CloudFunc.FS, '') || '/';
const fullPath = root(name);
flop.read(fullPath, (error, dir) => {
if (dir)
dir.path = format.addSlashToEnd(name);
if (!error)
return sendIndex(p, buildIndex(html, dir));
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);
});
});
});
/**
* additional processing of index file
@ -103,79 +144,16 @@ function indexProcessing(options) {
return data;
}
const sendIndex = (params) => (error, data) => {
const ponseParams = Object.assign({}, params, {
name: getIndexPath(isDev)
function buildIndex(html, json) {
const panel = CloudFunc.buildFromJSON({
data: json,
prefix: prefix(),
template: Template,
});
if (error)
return ponse.sendError(error, ponseParams);
ponse.send(data, ponseParams);
};
/**
* routing of server queries
*/
function route(request, response, callback) {
check(request, response, callback);
let name = ponse.getPathName(request);
const isFS = RegExp('^/$|^' + FS).test(name);
const gzip = true;
const p = {
request,
response,
gzip,
name,
};
if (!isFS)
return callback();
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, sendIndex(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) {
fs.readFile(getIndexPath(isDev), 'utf8', (error, template) => {
if (error)
return callback(error);
const panel = CloudFunc.buildFromJSON({
data: json,
prefix: prefix(),
template: Template,
});
const data = indexProcessing({
panel,
data: template,
});
callback(null, data);
return indexProcessing({
panel,
data: html,
});
}
@ -195,14 +173,3 @@ function hideKeysPanel(html) {
return html.replace(RegExp(from), to);
}
function check(req, res, next) {
if (!req)
throw Error('req could not be empty!');
if (!res)
throw Error('res could not be empty!');
if (typeof next !== 'function')
throw Error('next should be function!');
}

View file

@ -1,5 +1,7 @@
'use strict';
const path = require('path');
const test = require('tape');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
@ -201,6 +203,23 @@ function credentials() {
return set(reset);
}
test('cloudcmd: getIndexPath: production', (t) => {
const isDev = false;
const name = path.join(__dirname, '..', '..', 'dist', 'index.html');
t.equal(cloudcmd._getIndexPath(isDev), name);
t.end();
});
test('cloudcmd: getIndexPath: development', (t) => {
const isDev = true;
const name = path.join(__dirname, '..', '..', 'dist-dev', 'index.html');
t.equal(cloudcmd._getIndexPath(isDev), name);
t.end();
});
function cleanNodeEnv() {
const {NODE_ENV} = process.env;
process.env.NODE_ENV = '';

View file

@ -16,11 +16,6 @@ const routePath = `${rootDir}/server/route`;
const cloudcmdPath = `${rootDir}/server/cloudcmd`;
const beforePath = path.join(__dirname, '../before');
const {
_getIndexPath,
} = require(routePath);
const route = require(routePath);
const {connect} = require(beforePath);
const warp = (fn, ...a) => (...b) => fn(...b, ...a);
@ -36,25 +31,6 @@ const getStr = (url) => {
.catch(console.log);
};
test('cloudcmd: route: no args', (t) => {
t.throws(route, /req could not be empty!/, 'should throw when no args');
t.end();
});
test('cloudcmd: route: no res', (t) => {
const fn = () => route({});
t.throws(fn, /res could not be empty!/, 'should throw when no res');
t.end();
});
test('cloudcmd: route: no next', (t) => {
const fn = () => route({}, {});
t.throws(fn, /next should be function!/, 'should throw when no next');
t.end();
});
test('cloudcmd: route: buttons: no console', async (t) => {
const config = {
console: false
@ -167,22 +143,6 @@ test('cloudcmd: route: keys panel', async (t) => {
done();
});
test('cloudcmd: route: getIndexPath: production', (t) => {
const isDev = false;
const name = path.join(__dirname, '..', '..', 'dist', 'index.html');
t.equal(route._getIndexPath(isDev), name);
t.end();
});
test('cloudcmd: route: getIndexPath: development', (t) => {
const isDev = true;
const name = path.join(__dirname, '..', '..', 'dist-dev', 'index.html');
t.equal(route._getIndexPath(isDev), name);
t.end();
});
test('cloudcmd: route: file: fs', async (t) => {
const root = path.join(__dirname, '..', 'fixture', 'empty-file');
const config = {
@ -257,31 +217,6 @@ test('cloudcmd: route: realpath: error', async (t) => {
done();
});
test('cloudcmd: route: sendIndex: error', async (t) => {
const error = Error('index path error');
const {readFile} = fs;
const isDev = true;
const indexPath = _getIndexPath(isDev);
fs.readFile = (name, options, fn = options) => {
if (name === indexPath) {
fn(error);
fs.readFile = readFile;
return;
}
return readFile(name, options, fn);
};
const {port, done} = await connect();
const data = await getStr(`http://localhost:${port}`);
t.equal(data, error.message, 'should return error');
done();
t.end();
});
test('cloudcmd: route: sendIndex: encode', async (t) => {
const name = '"><svg onload=alert(3);>';
const nameEncoded = '&quot;&gt;&lt;svg&nbsp;onload=alert(3);&gt;';