/
before.js
45 lines (34 loc) · 971 Bytes
/
before.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'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, fn = config) => {
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,
config: assign(defaultConfig(), config)
}));
server.listen(() => {
fn(server.address().port, after);
});
};
function defaultConfig() {
return {
auth: false,
root: __dirname
};
}