-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
227 lines (213 loc) · 6.26 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"use strict";
// eslint-disable-next-line node/no-unpublished-require
const lookupCommand = require("ember-cli/lib/cli/lookup-command");
function getServeURL(options) {
return `http${options.ssl ? "s" : ""}://${options.host || "localhost"}:${
options.port
}/cli`;
}
const UI_PATCH_ID = "PATCH_9cf61e15-5685-4308-8938-e5c991825bc6";
const path = require("path");
const fs = require("fs");
const express = require("express");
const fetch = require("node-fetch");
const https = require('https');
const httpsAgent = new https.Agent({ rejectUnauthorized: false });
let capturing = false;
let results = [];
async function executeCommand(cli, commandName, commandArgs) {
capturing = true;
try {
const command = makeCommand(cli, commandName, commandArgs);
await runCommand(command, commandArgs);
capturing = false;
} catch (e) {
results.push(e.toString());
capturing = false;
}
}
async function runCommand(command, commandArgs) {
await command.beforeRun(commandArgs);
await command.validateAndRun(commandArgs);
}
function maybeWarnEmberCliError(cli) {
if (!("env" in cli)) {
cli.ui.writeWarnLine(
`ember-fast-cli: unable to connect to ember-cli (no environment), check addon readme.`
);
return true;
} else {
return false;
}
}
function makeCommand(cli, commandName, commandArgs) {
if ("maybeMakeCommand" in cli) {
return cli.maybeMakeCommand(commandName, commandArgs);
}
if (maybeWarnEmberCliError(cli)) {
return;
}
let CurrentCommand = lookupCommand(
cli.env.commands,
commandName,
commandArgs,
{
project: cli.env.project,
ui: cli.ui,
}
);
let command = new CurrentCommand({
ui: cli.ui,
analytics: cli.analytics,
commands: cli.env.commands,
tasks: cli.env.tasks,
project: cli.env.project,
settings: cli.env.settings,
testing: cli.testing,
cli: cli,
});
return command;
}
const xtermPath = path.dirname(path.dirname(require.resolve("xterm")));
const methodsToPatch = [
// 'write',
"writeError",
// 'prependLine',
"writeDeprecateLine",
"writeLine",
"writeErrorLine",
"writeDebugLine",
"writeInfoLine",
"writeWarnLine",
];
function postData(url = "/", data = {}) {
return fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify({ data }),
agent: httpsAgent
}).then((response) => response.json());
}
module.exports = {
name: require("./package").name,
// ember-language-server inegraion
onInit(_, project) {
// eslint-disable-next-line no-unused-vars
project.executors["els.executeInEmberCLI"] = async (server, __, [cmd]) => {
try {
let cliMetaFileName = path.join(__dirname, "meta.json");
if (!fs.existsSync(cliMetaFileName)) {
server.connection.window.showErrorMessage(
"Unable to find server params, try start ember server inside project directory"
);
return;
}
let meta = JSON.parse(fs.readFileSync(cliMetaFileName, "utf8"));
if (meta && meta.endpoint) {
let results = await postData(
meta.endpoint,
(cmd || "")
.replace("ember ", "")
.trim()
.split(" ")
.map((e) => e.trim())
.filter((e) => e.length)
);
if (results.length) {
server.connection.window.showInformationMessage(results[0]);
}
}
} catch (e) {
server.connection.window.showErrorMessage(JSON.stringify(e));
}
};
},
serverMiddleware(config) {
if (!("maybeMakeCommand" in this.parent.cli)) {
if (maybeWarnEmberCliError(this.parent.cli)) {
return;
}
}
let app = config.app;
app.get("/cli", (_, res) => {
res.sendFile(path.join(__dirname, "lib", "index.html"));
});
app.get("/cli/xterm.js", (_, res) => {
res.sendFile(path.join(xtermPath, "lib", "xterm.js"));
});
app.get("/cli/xterm.css", (_, res) => {
res.sendFile(path.join(xtermPath, "css", "xterm.css"));
});
app.use("/cli", express.json());
app.post("/cli", (req, res) => {
const [command, ...commandArgs] = req.body.data;
executeCommand(this.parent.cli, command, commandArgs).then(() => {
if (results.length === 0) {
results.push(
"No output, likely the specified command " +
command +
" is invalid. For available options, see `ember help`."
);
}
res.json(results);
results = [];
});
});
let endpoint = getServeURL(config.options);
fs.writeFileSync(
path.join(__dirname, "meta.json"),
JSON.stringify({ endpoint }),
"utf8"
);
this.ui.writeLine("");
this.ui.writeLine(`[ember-fast-cli] Serving on: ${endpoint}`);
this.ui.writeLine("");
if (UI_PATCH_ID in this.ui) {
return;
}
/* eslint-disable no-console */
const originalLog = console.log;
console.log = (...args) => {
if (capturing) {
results.push(args[0]);
}
originalLog.apply(console, args);
};
methodsToPatch.forEach((methodName) => {
let originalImplementation = this.ui[methodName];
this.ui[methodName] = (...args) => {
if (capturing) {
results.push(args[0]);
}
originalImplementation.apply(this.ui, args);
};
});
const originalPrompt = this.ui.prompt;
this.ui.prompt = (...args) => {
if (capturing) {
results.push(args[0].message);
const choices = args[0].choices || [];
const maybeSkipAnswer = choices.find(({ value }) => value === "skip");
if (maybeSkipAnswer) {
results.push("Answer: " + maybeSkipAnswer.name);
return Promise.resolve({ answer: maybeSkipAnswer.value });
}
const maybeNoAnswer = choices.find(
({ key }) => key.toLowerCase() === "n"
);
if (maybeNoAnswer) {
results.push("Answer: " + maybeSkipAnswer.name);
return Promise.resolve({ answer: maybeSkipAnswer.value });
}
return Promise.reject("Ember Fast CLi: Unable to answer.");
}
return originalPrompt.apply(this.ui, args);
};
this.ui[UI_PATCH_ID] = true;
},
isEnabled() {
return true;
},
};