-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (126 loc) · 3.75 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
/*global require,global,process,lively*/
const { readFileSync, appendFileSync } = require("fs");
const debugLogFile = "/tmp/lively-emacs-eval.log";
const debugLogging = false;
function debugLog(msg) {
debugLogging && appendFileSync(debugLogFile, msg + "\n");
}
function loadDependencies() {
global.io = require('socket.io-client');
global.babel = require('babel-standalone');
require('systemjs');
require('./lively.modules.js');
require('./lively-system-interface.js');
require('./lively.2lively_client.js');
}
function readVersion() {
return JSON.parse(readFileSync(`${__dirname}/package.json`)).version;
}
function connect(url = "http://localhost:9011/lively-socket.io") {
debugLog(url);
let client = (lively.l2l.client = lively.l2l.L2LClient.ensure({
url,
namespace: 'l2l',
info: {type: 'l2l emacs', location: 'inferior emacs'},
}));
return client.whenRegistered(20 * 1000).then(() => {
client.info.id = client.id;
return client;
});
}
function setupStdinReader() {
process.stdin.on('data', async data => {
try {
debugLog(`received ${data}`);
const cmd = JSON.parse(data.toString());
const result = await processCommand(cmd);
let resultJson;
try {
resultJson = JSON.stringify({result});
} catch (err) {
resultJson = JSON.stringify({error: `Error stringifing result`});
}
process.stdout.write(resultJson + '\n');
} catch (err) {
const error = {error: `Failed to process command: ${err.message || err}`};
process.stdout.write(JSON.stringify(error) + '\n');
debugLog(`ERROR when receiving data: ${err.stack}`);
}
});
process.stdin.on('error', err => {
debugLog(`Process error: ${err.stack}`)
console.error('process error', err);
process.exit(2);
});
}
const commands = {
runEval: async cmd => {
const system = cmd.peerId
? lively.systemInterface.l2lInterfaceFor(cmd.peerId, lively.l2l.client)
: lively.systemInterface.localInterface;
const env = {
format: 'esm',
targetModule: cmd.targetModule || "lively://emacs-plugin/dummy-module",
// context: {},
sourceURL: '_emacs_doit_' + Date.now(),
asString: cmd.asString,
inspect: cmd.inspect,
inspectDepth: cmd.inspectDepth,
};
const result = await system.runEval(cmd.source, env);
return result;
},
completions: cmd => {
const system = cmd.peerId
? lively.systemInterface.l2lInterfaceFor(cmd.peerId, lively.l2l.client)
: lively.systemInterface.localInterface;
const env = {
format: 'esm',
targetModule: cmd.targetModule,
context: {},
sourceURL: '_emacs_completion_' + Date.now(),
};
return system.dynamicCompletionsForPrefix(
env.targetModule,
cmd.prefix,
env,
);
},
listPeers: async _cmd => {
const result = await lively.l2l.client.listPeers();
if (result.error) {
throw result.error;
}
return result;
// return [lively.l2l.client.info].concat(result);
},
};
async function processCommand(cmd) {
const handler = commands[cmd.action];
if (typeof handler == 'function') {
return handler(cmd);
}
throw new Error(`Unknown command ${JSON.stringify(cmd)}`);
}
function commandlineArgs() {
debugLog(process.argv)
const args = {serverAddress: null};
const idx = process.argv.indexOf("--server-address");
if (idx > -1) {
args.serverAddress = process.argv[idx+1];
}
debugLog(JSON.stringify(args))
return args;
}
async function main() {
try {
loadDependencies();
const args = commandlineArgs();
await connect(args.serverAddress);
setupStdinReader();
console.log(`lively.emacs started, version ${readVersion()}`);
} catch (err) {
console.error('[l2l] failed:', err);
}
}
main();