-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
178 lines (132 loc) · 3.99 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
const DIR_ROOT = __dirname + '/..';
const path = require('path');
const spawnify = require('spawnify');
const rendy = require('rendy');
const untildify = require('untildify');
const express = require('express');
const currify = require('currify');
const Router = express.Router;
const modules = require('../json/modules');
const rmLastSlash = (a) => a.replace(/\/$/, '') || '/';
const addLastSlash = (a) => a[a.length - 1] === '/' ? a : `${a}/`;
const modulesFn = currify(_modulesFn);
const konsoleFn = currify(_konsoleFn);
const Console = require('./console');
const isDev = process.env.NODE_ENV === 'development';
module.exports = (options) => {
options = options || {};
const router = Router();
const prefix = options.prefix || '/console';
router.route(prefix + '/*')
.get(konsoleFn(options))
.get(modulesFn(prefix, options))
.get(staticFn)
return router;
};
module.exports.listen = (socket, options) => {
if (!options) {
options = socket;
socket = null
}
const o = options;
if (!options.prefix)
options.prefix = '/console';
return Console(socket, {
server: o.server,
prefix: o.prefix,
prompt: o.prompt,
execute: o.execute || execute,
auth: o.auth,
});
}
function _modulesFn(prefix, options, req, res, next) {
if (req.url !== '/modules.json')
return next();
const urls = [];
const o = options;
let urlSocket = '';
let urlJq = prefix;
let urlJquery = prefix;
if (checkOption(o.online)) {
urls.push.apply(urls, modules.map((m) => {
return rendy(m.remote, {
version: m.version
});
}));
} else {
modules.forEach((m) => {
if (m.name === 'socket')
urlSocket = Console.getSocketPath() + '/socket.io.js';
else if (m.name === 'jquery')
urlJquery += m.local;
else
urlJq += m.local;
});
urls.push.apply(urls, [urlJquery, urlJq, urlSocket]);
}
res.type('json');
res.send(urls);
}
function checkOption(isOption) {
if (typeof isOption === 'function')
return isOption();
if (typeof isOption === 'undefined')
return true;
return isOption;
}
function _konsoleFn(options, req, res, next) {
const o = options || {};
const prefix = o.prefix || '/console';
const url = req.url
if (url.indexOf(prefix))
return next();
req.url = req.url.replace(prefix, '');
if (/^\/console\.js(\.map)?$/.test(req.url))
req.url = `/dist${req.url}`;
if (isDev)
req.url = req.url.replace(/^\/dist\//, '/dist-dev/');
next();
}
function staticFn(req, res) {
const file = path.normalize(DIR_ROOT + req.url);
res.sendFile(file);
}
function execute(socket, command, cwd) {
const cmd = command.cmd;
const env = Object.assign({}, command.env, process.env);
const spawn = spawnify(cmd, {
env,
cwd: cwd()
});
socket.on('kill', kill);
socket.on('write', write);
spawn.on('error', onError);
spawn.on('data', onData);
spawn.once('path', onPath);
spawn.once('close', onClose);
function kill() {
spawn.kill();
}
function write(data) {
spawn.write(data);
}
function onError(error) {
socket.emit('err', error.message);
}
function onData(data) {
socket.emit('data', data);
}
function onPath(path) {
socket.emit('label', rmLastSlash(path));
socket.emit('path', addLastSlash(untildify(path)));
cwd(path);
}
function onClose() {
socket.removeListener('kill', kill);
socket.removeListener('write', write);
spawn.removeListener('error', onError);
spawn.removeListener('data', onData);
socket.emit('prompt');
}
}