mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-19 09:24:51 +00:00
chore(config) es2015-ify
This commit is contained in:
parent
8e3e40d790
commit
272c5b62c2
1 changed files with 83 additions and 91 deletions
|
|
@ -1,53 +1,50 @@
|
|||
'use strict';
|
||||
|
||||
var DIR_SERVER = __dirname + '/',
|
||||
DIR_COMMON = DIR_SERVER + '../../common/',
|
||||
DIR = DIR_SERVER + '../../',
|
||||
const DIR_SERVER = __dirname + '/';
|
||||
const DIR_COMMON = DIR_SERVER + '../../common/';
|
||||
const DIR = DIR_SERVER + '../../';
|
||||
|
||||
const path = require('path');
|
||||
|
||||
const exit = require(DIR_SERVER + 'exit');
|
||||
const CloudFunc = require(DIR_COMMON + 'cloudfunc');
|
||||
|
||||
const pullout = require('pullout/legacy');
|
||||
const ponse = require('ponse');
|
||||
const jonny = require('jonny');
|
||||
const readjson = require('readjson');
|
||||
const writejson = require('writejson');
|
||||
const tryCatch = require('try-catch');
|
||||
const exec = require('execon');
|
||||
const criton = require('criton');
|
||||
const HOME = require('os-homedir')();
|
||||
|
||||
const apiURL = CloudFunc.apiURL;
|
||||
|
||||
const ConfigPath = path.join(DIR, 'json/config.json');
|
||||
const ConfigHome = path.join(HOME, '.cloudcmd.json');
|
||||
|
||||
let config;
|
||||
let error = tryCatch(() => {
|
||||
config = readjson.sync(ConfigHome);
|
||||
});
|
||||
|
||||
if (error) {
|
||||
if (error.code !== 'ENOENT')
|
||||
console.error('cloudcmd --config ~/.cloudcmd.json:', error.message);
|
||||
|
||||
path = require('path'),
|
||||
|
||||
exit = require(DIR_SERVER + 'exit'),
|
||||
CloudFunc = require(DIR_COMMON + 'cloudfunc'),
|
||||
|
||||
pullout = require('pullout/legacy'),
|
||||
ponse = require('ponse'),
|
||||
jonny = require('jonny'),
|
||||
readjson = require('readjson'),
|
||||
writejson = require('writejson'),
|
||||
tryCatch = require('try-catch'),
|
||||
exec = require('execon'),
|
||||
criton = require('criton'),
|
||||
|
||||
HOME = require('os-homedir')(),
|
||||
|
||||
apiURL = CloudFunc.apiURL,
|
||||
|
||||
ConfigPath = path.join(DIR, 'json/config.json'),
|
||||
ConfigHome = path.join(HOME, '.cloudcmd.json'),
|
||||
|
||||
error,
|
||||
config;
|
||||
|
||||
error = tryCatch(function() {
|
||||
config = readjson.sync(ConfigHome);
|
||||
error = tryCatch(() => {
|
||||
config = readjson.sync(ConfigPath);
|
||||
});
|
||||
|
||||
if (error) {
|
||||
if (error.code !== 'ENOENT')
|
||||
console.error('cloudcmd --config ~/.cloudcmd.json:', error.message);
|
||||
|
||||
error = tryCatch(function() {
|
||||
config = readjson.sync(ConfigPath);
|
||||
});
|
||||
|
||||
if (error)
|
||||
exit('cloudcmd --config', ConfigPath + ':', error.message);
|
||||
}
|
||||
if (error)
|
||||
exit('cloudcmd --config', ConfigPath + ':', error.message);
|
||||
}
|
||||
|
||||
module.exports = manage;
|
||||
module.exports.save = save;
|
||||
module.exports.middle = middle;
|
||||
module.exports.listen = function(socket, authCheck) {
|
||||
module.exports.listen = (socket, authCheck) => {
|
||||
check(socket, authCheck);
|
||||
|
||||
if (!manage('configDialog'))
|
||||
|
|
@ -73,13 +70,13 @@ function save(callback) {
|
|||
}
|
||||
|
||||
function listen(sock, authCheck) {
|
||||
var prefix = manage('prefix');
|
||||
const prefix = manage('prefix');
|
||||
|
||||
sock.of(prefix + '/config')
|
||||
.on('connection', function(socket) {
|
||||
var connect = exec.with(connection, socket);
|
||||
.on('connection', (socket) => {
|
||||
const connect = exec.with(connection, socket);
|
||||
|
||||
exec.if(!manage('auth'), connect, function(fn) {
|
||||
exec.if(!manage('auth'), connect, (fn) => {
|
||||
authCheck(socket, fn);
|
||||
});
|
||||
});
|
||||
|
|
@ -88,57 +85,52 @@ function listen(sock, authCheck) {
|
|||
function connection(socket) {
|
||||
socket.emit('config', config);
|
||||
|
||||
socket.on('message', function(json) {
|
||||
var data;
|
||||
socket.on('message', (json) => {
|
||||
if (typeof json !== 'object')
|
||||
return socket.emit('err', 'Error: Wrong data type!');
|
||||
|
||||
cryptoPass(json);
|
||||
|
||||
if (typeof json !== 'object') {
|
||||
socket.emit('err', 'Error: Wrong data type!');
|
||||
} else {
|
||||
cryptoPass(json);
|
||||
save((error) => {
|
||||
if (error)
|
||||
return socket.emit('err', error.message);
|
||||
|
||||
data = traverse(json);
|
||||
const data = traverse(json);
|
||||
|
||||
save(function(error) {
|
||||
if (error) {
|
||||
socket.emit('err', error.message);
|
||||
} else {
|
||||
socket.broadcast.send(json);
|
||||
socket.send(json);
|
||||
socket.emit('log', data);
|
||||
}
|
||||
});
|
||||
}
|
||||
socket.broadcast.send(json);
|
||||
socket.send(json);
|
||||
socket.emit('log', data);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function middle(req, res, next) {
|
||||
var noConfigDialog = !manage('configDialog');
|
||||
const noConfigDialog = !manage('configDialog');
|
||||
|
||||
if (req.url !== apiURL + '/config') {
|
||||
if (req.url !== apiURL + '/config')
|
||||
return next();
|
||||
|
||||
switch(req.method) {
|
||||
case 'GET':
|
||||
get(req, res, next);
|
||||
break;
|
||||
|
||||
case 'PATCH':
|
||||
if (noConfigDialog)
|
||||
return res
|
||||
.status(404)
|
||||
.send('Config is disabled');
|
||||
|
||||
patch(req, res, next);
|
||||
break;
|
||||
|
||||
default:
|
||||
next();
|
||||
} else {
|
||||
switch(req.method) {
|
||||
case 'GET':
|
||||
get(req, res, next);
|
||||
break;
|
||||
|
||||
case 'PATCH':
|
||||
if (noConfigDialog)
|
||||
return res
|
||||
.status(404)
|
||||
.send('Config is disabled');
|
||||
|
||||
patch(req, res, next);
|
||||
break;
|
||||
|
||||
default:
|
||||
next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function get(req, res) {
|
||||
var data = jonny.stringify(config);
|
||||
const data = jonny.stringify(config);
|
||||
|
||||
ponse.send(data, {
|
||||
name : 'config.json',
|
||||
|
|
@ -149,26 +141,26 @@ function get(req, res) {
|
|||
}
|
||||
|
||||
function patch(req, res, callback) {
|
||||
var options = {
|
||||
const options = {
|
||||
name : 'config.json',
|
||||
request : req,
|
||||
response: res,
|
||||
cache : false
|
||||
};
|
||||
|
||||
pullout(req, 'string', function(error, body) {
|
||||
var json = jonny.parse(body) || {};
|
||||
pullout(req, 'string', (error, body) => {
|
||||
const json = jonny.parse(body) || {};
|
||||
|
||||
if (error)
|
||||
return callback(error);
|
||||
|
||||
cryptoPass(json);
|
||||
|
||||
save(function(error) {
|
||||
save((error) => {
|
||||
if (error)
|
||||
return ponse.sendError(error, options);
|
||||
|
||||
var data = traverse(json);
|
||||
const data = traverse(json);
|
||||
|
||||
ponse.send(data, options);
|
||||
});
|
||||
|
|
@ -176,9 +168,9 @@ function patch(req, res, callback) {
|
|||
}
|
||||
|
||||
function traverse(json) {
|
||||
var data;
|
||||
let data;
|
||||
|
||||
Object.keys(json).forEach(function(name) {
|
||||
Object.keys(json).forEach((name) => {
|
||||
data = CloudFunc.formatMsg('config', name);
|
||||
manage(name, json[name]);
|
||||
});
|
||||
|
|
@ -187,7 +179,7 @@ function traverse(json) {
|
|||
}
|
||||
|
||||
function cryptoPass(json) {
|
||||
var algo = manage('algo');
|
||||
const algo = manage('algo');
|
||||
|
||||
if (json && json.password)
|
||||
json.password = criton(json.password, algo);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue