mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-28 18:23:35 +00:00
feature(cloudcmd) move out dist from route
This commit is contained in:
parent
bb44a14780
commit
4e99372e6f
6 changed files with 94 additions and 163 deletions
|
|
@ -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,
|
||||
|
|
|
|||
153
server/route.js
153
server/route.js
|
|
@ -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!');
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue