mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
70 lines
1.4 KiB
JavaScript
70 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const http = require('http');
|
|
const os = require('os');
|
|
|
|
const express = require('express');
|
|
const io = require('socket.io');
|
|
const writejson = require('writejson');
|
|
const readjson = require('readjson');
|
|
|
|
const cloudcmd = require('..');
|
|
const {assign} = Object;
|
|
|
|
const pathConfig = os.homedir() + '/.cloudcmd.json';
|
|
const currentConfig = readjson.sync.try(pathConfig);
|
|
|
|
module.exports = (_config, _plugins, _fn) => {
|
|
const {config, plugins, fn} = parse(_config, _plugins, _fn);
|
|
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const after = () => {
|
|
if (currentConfig)
|
|
writejson.sync(pathConfig, currentConfig);
|
|
|
|
server.close();
|
|
};
|
|
|
|
const socket = io.listen(server);
|
|
|
|
app.use(cloudcmd({
|
|
socket,
|
|
plugins,
|
|
config: assign(defaultConfig(), config)
|
|
}));
|
|
|
|
server.listen(() => {
|
|
fn(server.address().port, after);
|
|
});
|
|
};
|
|
|
|
function defaultConfig() {
|
|
return {
|
|
auth: false,
|
|
root: __dirname
|
|
};
|
|
}
|
|
|
|
function parse(config, plugins, fn) {
|
|
if (typeof plugins === 'undefined')
|
|
return {
|
|
fn: config,
|
|
config: undefined,
|
|
plugins: undefined
|
|
}
|
|
|
|
if (typeof fn === 'undefined')
|
|
return {
|
|
config,
|
|
fn: plugins,
|
|
plugins: undefined
|
|
}
|
|
|
|
return {
|
|
config,
|
|
plugins,
|
|
fn
|
|
};
|
|
}
|
|
|