-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
169 lines (135 loc) · 3.93 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
import osjs from 'osjs';
import {EventEmitter} from '@osjs/event-emitter';
import {name as applicationName} from './metadata.json';
import {Terminal} from 'xterm';
import * as fit from 'xterm/lib/addons/fit/fit';
import * as argv from 'argv-parse';
import * as commands from './commands.js';
import './index.scss'
import './xterm.scss'
Terminal.applyAddon(fit);
class Shell extends EventEmitter {
constructor(core) {
super('Shell');
this.core = core;
this.xterm = new Terminal();
this.cwd = '/';
this.input = '';
this.history = [];
}
init($root) {
this.xterm.addDisposableListener('key', (...args) => {
this.emit('key', ...args);
this.onKeyDown(...args);
});
this.xterm.addDisposableListener('paste', data => {
this.xterm.write(data);
});
this.xterm.open($root);
this.exec('banner');
//this.prompt();
}
exec(input) {
this.history.push(input);
const done = (err) => {
this.xterm.writeln('');
if (err) {
console.warn(err);
try {
this.xterm.writeln(JSON.stringify(err));
} catch (e) {
this.xterm.writeln(err);
}
}
setTimeout(() => this.prompt(), 10);
};
this.xterm.writeln('');
return this._exec(input)
.then(result => done(null, result))
.catch(done);
}
_exec(input) {
const [cmd, ...args] = input.split(' ');
const context = this._context(input);
context.on('data', s => this.xterm.write(s));
if (commands[cmd]) {
const a = argv({}, args);
try {
const result = commands[cmd](context, this.core, this.xterm)(a, input);
if (result instanceof Promise) {
return result;
}
return new Promise((resolve, reject) => {
context.on('close', () => resolve());
context.on('error', err => reject(err));
});
} catch (e) {
return Promise.reject(e);
}
}
return Promise.reject(new Error(`Command not found: ${input}`));
}
_context(input) {
const ctx = new EventEmitter(input);
return {
input,
cwd: this.cwd,
on: (...args) => ctx.on(...args),
emit: (...args) => ctx.emit(...args),
close: (err) => err ? ctx.emit('error', err) : ctx.emit('close'),
write: (...args) => this.xterm.write(...args),
writeln: (...args) => this.xterm.writeln(...args),
clear: () => this.xterm.clear()
};
}
prompt() {
const user = this.core.make('osjs/auth').user();
const version = this.core.config('version') || 'latest';
this.xterm.writeln('');
this.xterm.write(`[48;5;61m${user.username}@osjs-${version}:${this.cwd} > [0m `);
}
fit() {
this.xterm.fit();
}
onKeyDown(key, ev) {
const printable = !ev.altKey && !ev.altGraphKey && !ev.ctrlKey && !ev.metaKey;
if (ev.keyCode === 13) {
if (this.input.length > 0) {
this.exec(this.input);
} else {
this.prompt();
}
this.input = '';
} else if (ev.keyCode === 8) {
if (this.input.length > 0) {
this.xterm.write('\b \b');
this.input = this.input.substring(0, this.input.length - 1);
}
} else if ([37, 38, 39, 40].indexOf(ev.keyCode) !== -1) {
// TODO
} else if (printable) {
this.input += key;
this.xterm.write(key);
}
}
}
const register = (core, args, options, metadata) => {
const proc = core.make('osjs/application', {args, options, metadata});
proc.createWindow({
id: 'TerminalWindow',
title: metadata.title.en_EN,
dimension: {width: 800, height: 700}
})
.on('destroy', () => proc.destroy())
.render(($content, win) => {
const shell = new Shell(core);
const fit = () => setTimeout(() => shell.fit(), 10);
win.on('resized', fit);
win.on('maximize', fit);
win.on('restore', fit);
win.on('render', fit);
shell.init($content);
});
return proc;
};
osjs.register(applicationName, register);