scripting-cli 1.1.4 → 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/controller.js +275 -0
- package/dist/http_router.js +95 -0
- package/dist/index.js +2 -364
- package/dist/start_server.js +48 -0
- package/dist/util.js +130 -0
- package/package.json +2 -2
- package/src/controller.ts +346 -0
- package/src/http_router.ts +124 -0
- package/src/index.ts +2 -442
- package/src/start_server.ts +55 -0
- package/src/util.ts +118 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.Controller = void 0;
|
|
16
|
+
const chokidar_1 = __importDefault(require("chokidar"));
|
|
17
|
+
const util_1 = require("./util");
|
|
18
|
+
const fs_1 = __importDefault(require("fs"));
|
|
19
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
20
|
+
const path_1 = __importDefault(require("path"));
|
|
21
|
+
class Controller {
|
|
22
|
+
static create(socket) {
|
|
23
|
+
const ctrl = new Controller(socket);
|
|
24
|
+
Controller.instances.set(socket.id, ctrl);
|
|
25
|
+
return ctrl;
|
|
26
|
+
}
|
|
27
|
+
static get(socketId) {
|
|
28
|
+
return Controller.instances.get(socketId);
|
|
29
|
+
}
|
|
30
|
+
constructor(socket) {
|
|
31
|
+
this.watcher = null;
|
|
32
|
+
this.scriptName = null;
|
|
33
|
+
this.socket = null;
|
|
34
|
+
this.handleSyncScriptFromClient = (data, ack) => __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
var _a;
|
|
36
|
+
try {
|
|
37
|
+
if (data.scriptName === this.scriptName) {
|
|
38
|
+
ack({
|
|
39
|
+
error: `Script [${data.scriptName}] is already initialized`,
|
|
40
|
+
});
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (this.scriptName != null) {
|
|
44
|
+
console.log(`Received new script, closing previous watcher`);
|
|
45
|
+
(_a = this.watcher) === null || _a === void 0 ? void 0 : _a.close();
|
|
46
|
+
this.watcher = null;
|
|
47
|
+
}
|
|
48
|
+
yield (0, util_1.writeDtsFiles)({
|
|
49
|
+
"global.d.ts": data["global.d.ts"],
|
|
50
|
+
"scripting.d.ts": data["scripting.d.ts"],
|
|
51
|
+
});
|
|
52
|
+
this.scriptName = data.scriptName;
|
|
53
|
+
const scriptName = data.scriptName;
|
|
54
|
+
const scriptDir = (0, util_1.getPath)(data.scriptName);
|
|
55
|
+
const tsconfig = require((0, util_1.getPath)('tsconfig.json'));
|
|
56
|
+
if (!tsconfig.include.includes(scriptName)) {
|
|
57
|
+
tsconfig.include.push(scriptName);
|
|
58
|
+
yield fs_1.default.promises.writeFile((0, util_1.getPath)('tsconfig.json'), JSON.stringify(tsconfig, null, 2));
|
|
59
|
+
}
|
|
60
|
+
(0, util_1.ensureDirectoryExistence)(scriptDir);
|
|
61
|
+
yield Promise.all(Object.entries(data.scriptFiles).map((_a) => __awaiter(this, [_a], void 0, function* ([filename, content]) {
|
|
62
|
+
const filePath = path_1.default.join(scriptDir, filename);
|
|
63
|
+
(0, util_1.ensureDirectoryExistence)(filePath);
|
|
64
|
+
yield fs_1.default.promises.writeFile(filePath, content);
|
|
65
|
+
})));
|
|
66
|
+
console.log(chalk_1.default.green('global.d.ts and scripting.d.ts and other script files saved.'));
|
|
67
|
+
this.createWatcher(data.scriptName);
|
|
68
|
+
ack({
|
|
69
|
+
success: true,
|
|
70
|
+
});
|
|
71
|
+
// Open the entry file in VSCode after a delay
|
|
72
|
+
// to ensure the response is sent. Because the open file command
|
|
73
|
+
// may suspend the response and cause the client to not receive the ack.
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
|
|
76
|
+
(0, util_1.tryOpenFileInVSCode)(entryFilePath);
|
|
77
|
+
}, 1000);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
console.log(chalk_1.default.red(`Error: ${e}`));
|
|
81
|
+
ack({
|
|
82
|
+
error: `Failed to init and sync script files.\n${e}`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
this.handleSyncScriptFromServer = (data, ack) => __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
var _a;
|
|
88
|
+
try {
|
|
89
|
+
if (this.scriptName === data.scriptName) {
|
|
90
|
+
ack({
|
|
91
|
+
error: `Script ${data.scriptName} is already initialized`,
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (this.scriptName != null) {
|
|
96
|
+
console.log(`Received new script [${chalk_1.default.bold.blue(data.scriptName)}], closing previous watcher`);
|
|
97
|
+
(_a = this.watcher) === null || _a === void 0 ? void 0 : _a.close();
|
|
98
|
+
this.watcher = null;
|
|
99
|
+
}
|
|
100
|
+
yield (0, util_1.writeDtsFiles)({
|
|
101
|
+
"global.d.ts": data["global.d.ts"],
|
|
102
|
+
"scripting.d.ts": data["scripting.d.ts"],
|
|
103
|
+
});
|
|
104
|
+
const scriptDir = (0, util_1.getPath)(data.scriptName);
|
|
105
|
+
if (!fs_1.default.existsSync(scriptDir)) {
|
|
106
|
+
ack({
|
|
107
|
+
error: `Script directory ${scriptDir} does not exist`,
|
|
108
|
+
});
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const scriptFiles = {};
|
|
112
|
+
const readDir = (dir) => __awaiter(this, void 0, void 0, function* () {
|
|
113
|
+
const files = yield fs_1.default.promises.readdir(dir, { withFileTypes: true });
|
|
114
|
+
yield Promise.all(files.map((file) => __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
if (file.isFile()) {
|
|
116
|
+
const filePath = path_1.default.join(dir, file.name);
|
|
117
|
+
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
118
|
+
const content = yield fs_1.default.promises.readFile(filePath, 'utf-8').catch((err) => {
|
|
119
|
+
console.log(chalk_1.default.red(`Error reading file: ${err}`));
|
|
120
|
+
});
|
|
121
|
+
if (typeof content === "string") {
|
|
122
|
+
scriptFiles[relativePath] = {
|
|
123
|
+
hash: (0, util_1.md5)(content),
|
|
124
|
+
content,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else if (file.isDirectory()) {
|
|
129
|
+
yield readDir(path_1.default.join(dir, file.name));
|
|
130
|
+
}
|
|
131
|
+
})));
|
|
132
|
+
});
|
|
133
|
+
yield readDir(scriptDir);
|
|
134
|
+
this.scriptName = data.scriptName;
|
|
135
|
+
ack({
|
|
136
|
+
scriptFiles,
|
|
137
|
+
});
|
|
138
|
+
this.createWatcher(data.scriptName);
|
|
139
|
+
// Open the entry file in VSCode after a delay
|
|
140
|
+
// to ensure the response is sent. Because the open file command
|
|
141
|
+
// may suspend the response and cause the client to not receive the ack.
|
|
142
|
+
setTimeout(() => {
|
|
143
|
+
const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
|
|
144
|
+
(0, util_1.tryOpenFileInVSCode)(entryFilePath);
|
|
145
|
+
}, 1000);
|
|
146
|
+
}
|
|
147
|
+
catch (e) {
|
|
148
|
+
ack({
|
|
149
|
+
error: `Failed to init and pull script files.\n${e}`,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
this.handleLog = (data) => {
|
|
154
|
+
if (data.scriptName !== this.scriptName) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
console.log(`[${chalk_1.default.bold.blue(this.scriptName)}][${data.isError ? chalk_1.default.red("ERROR") : "LOG"}][${new Date().toLocaleTimeString()}] ${data.content}`);
|
|
158
|
+
};
|
|
159
|
+
this.handleOpenFile = (data) => __awaiter(this, void 0, void 0, function* () {
|
|
160
|
+
if (data.scriptName !== this.scriptName) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const filePath = path_1.default.join((0, util_1.getPath)(this.scriptName), data.relativePath);
|
|
164
|
+
if (fs_1.default.existsSync(filePath)) {
|
|
165
|
+
(0, util_1.tryOpenFileInVSCode)(filePath);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
this.handleStopDebugScript = (data, ack) => {
|
|
169
|
+
var _a;
|
|
170
|
+
if (data === this.scriptName) {
|
|
171
|
+
console.log(`[${chalk_1.default.bold.blue(this.scriptName)}] Stop debug script`);
|
|
172
|
+
(_a = this.watcher) === null || _a === void 0 ? void 0 : _a.close();
|
|
173
|
+
this.watcher = null;
|
|
174
|
+
this.scriptName = null;
|
|
175
|
+
ack(true);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
ack(false);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
this.handleGetFileContent = (data, ack) => __awaiter(this, void 0, void 0, function* () {
|
|
182
|
+
if (this.scriptName !== data.scriptName) {
|
|
183
|
+
ack({
|
|
184
|
+
error: `Script ${data.scriptName} is not initialized`,
|
|
185
|
+
});
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const filePath = path_1.default.join((0, util_1.getPath)(data.scriptName), data.relativePath);
|
|
189
|
+
try {
|
|
190
|
+
const content = yield fs_1.default.promises.readFile(filePath, 'utf-8');
|
|
191
|
+
ack({
|
|
192
|
+
content,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
catch (e) {
|
|
196
|
+
ack({
|
|
197
|
+
error: `Failed to get file content.\n${e}`,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
this.socket = socket;
|
|
202
|
+
socket.emit("socketId", socket.id);
|
|
203
|
+
socket.on('syncScriptFromClient', this.handleSyncScriptFromClient);
|
|
204
|
+
socket.on("syncScriptFromServer", this.handleSyncScriptFromClient);
|
|
205
|
+
socket.on('stopDebugScript', this.handleStopDebugScript);
|
|
206
|
+
socket.on("getFileContent", this.handleGetFileContent);
|
|
207
|
+
socket.on("log", this.handleLog);
|
|
208
|
+
socket.on("openFile", this.handleOpenFile);
|
|
209
|
+
socket.on('disconnect', (reason) => {
|
|
210
|
+
var _a;
|
|
211
|
+
console.log(chalk_1.default.gray(`Client [${socket.id}] disconnected: ${reason}`));
|
|
212
|
+
(_a = this.watcher) === null || _a === void 0 ? void 0 : _a.close();
|
|
213
|
+
this.watcher = null;
|
|
214
|
+
this.scriptName = null;
|
|
215
|
+
Controller.instances.delete(socket.id);
|
|
216
|
+
socket.removeAllListeners();
|
|
217
|
+
});
|
|
218
|
+
fs_1.default.promises.readdir(process.cwd()).then((files) => {
|
|
219
|
+
const serverScriptNames = files.filter((filename) => fs_1.default.statSync(filename).isDirectory()
|
|
220
|
+
&& filename !== '.vscode'
|
|
221
|
+
&& filename !== 'dts');
|
|
222
|
+
socket.emit("serverScripts", serverScriptNames);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
createWatcher(scriptName) {
|
|
226
|
+
const scriptDir = (0, util_1.getPath)(scriptName);
|
|
227
|
+
this.watcher = chokidar_1.default.watch(scriptDir, {
|
|
228
|
+
ignored: /(^|[\/\\])\../,
|
|
229
|
+
persistent: true,
|
|
230
|
+
});
|
|
231
|
+
this.watcher
|
|
232
|
+
.on('change', (filePath) => {
|
|
233
|
+
if (filePath.match(/\.(js(x)?|ts(x)?|json|md|txt)$/)) {
|
|
234
|
+
// send file content md5 hash to client
|
|
235
|
+
// const content = fs.readFileSync(filePath, 'utf-8');
|
|
236
|
+
fs_1.default.readFile(filePath, 'utf-8', (err, content) => {
|
|
237
|
+
var _a;
|
|
238
|
+
if (err) {
|
|
239
|
+
console.log(chalk_1.default.red(`Error reading file: ${err}`));
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const hash = (0, util_1.md5)(content);
|
|
243
|
+
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
244
|
+
console.log(`[${chalk_1.default.bold.blue(scriptName)}] File changed: [${hash}]${relativePath}`);
|
|
245
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit('fileChange', {
|
|
246
|
+
scriptName,
|
|
247
|
+
filePath: relativePath,
|
|
248
|
+
hash,
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
// TODO, add support for other file types
|
|
253
|
+
})
|
|
254
|
+
.on('unlink', (filePath) => {
|
|
255
|
+
var _a;
|
|
256
|
+
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
257
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit('deleteFile', {
|
|
258
|
+
scriptName,
|
|
259
|
+
filePath: relativePath,
|
|
260
|
+
});
|
|
261
|
+
})
|
|
262
|
+
.on('unlinkDir', (filePath) => {
|
|
263
|
+
var _a;
|
|
264
|
+
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
265
|
+
(_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit('deleteFile', {
|
|
266
|
+
scriptName,
|
|
267
|
+
filePath: relativePath,
|
|
268
|
+
});
|
|
269
|
+
})
|
|
270
|
+
.on('error', error => console.log(chalk_1.default.bgRed(`Watcher error: ${error}`)))
|
|
271
|
+
.on('ready', () => console.log(`[${chalk_1.default.bold.blue(scriptName)}] Watching files for changes`));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
exports.Controller = Controller;
|
|
275
|
+
Controller.instances = new Map();
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.initHttpRouter = initHttpRouter;
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const controller_1 = require("./controller");
|
|
9
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
10
|
+
const getFileContent = (req, res) => {
|
|
11
|
+
const { socketId, scriptName, relativePath, } = req.query;
|
|
12
|
+
if (!socketId) {
|
|
13
|
+
console.log(chalk_1.default.red(`Socket not found: ${socketId}`));
|
|
14
|
+
}
|
|
15
|
+
if (!scriptName || !relativePath) {
|
|
16
|
+
res.json({
|
|
17
|
+
error: "Missing parameters: scriptName or relativePath."
|
|
18
|
+
});
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const ctrl = controller_1.Controller.get(socketId);
|
|
22
|
+
if (!ctrl) {
|
|
23
|
+
console.log(chalk_1.default.red(`Socket not found: ${socketId}`));
|
|
24
|
+
res.json({
|
|
25
|
+
error: "Socket not found."
|
|
26
|
+
});
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
ctrl.handleGetFileContent({
|
|
30
|
+
scriptName,
|
|
31
|
+
relativePath,
|
|
32
|
+
}, (result) => {
|
|
33
|
+
if (result.error) {
|
|
34
|
+
res.json({
|
|
35
|
+
error: result.error
|
|
36
|
+
});
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
res.json({
|
|
40
|
+
content: result.content
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
const syncScriptFromClient = (req, res) => {
|
|
45
|
+
const params = req.body;
|
|
46
|
+
const socketId = params.socketId;
|
|
47
|
+
const ctrl = controller_1.Controller.get(socketId);
|
|
48
|
+
if (!ctrl) {
|
|
49
|
+
console.log(chalk_1.default.red(`Socket not found: ${socketId}.`));
|
|
50
|
+
res.json({
|
|
51
|
+
error: "Socket not found."
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
ctrl.handleSyncScriptFromClient(params, (result) => {
|
|
56
|
+
if (result.error) {
|
|
57
|
+
res.json({
|
|
58
|
+
error: result.error
|
|
59
|
+
});
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
res.json({
|
|
63
|
+
success: true
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
const syncScriptFromServer = (req, res) => {
|
|
68
|
+
const params = req.body;
|
|
69
|
+
const socketId = params.socketId;
|
|
70
|
+
const ctrl = controller_1.Controller.get(socketId);
|
|
71
|
+
if (!ctrl) {
|
|
72
|
+
res.json({
|
|
73
|
+
error: "Socket not found."
|
|
74
|
+
});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
ctrl.handleSyncScriptFromServer(params, (result) => {
|
|
78
|
+
if (result.error) {
|
|
79
|
+
res.json({
|
|
80
|
+
error: result.error
|
|
81
|
+
});
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
res.json({
|
|
85
|
+
scriptFiles: result.scriptFiles
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
function initHttpRouter(app) {
|
|
90
|
+
app.use(express_1.default.json({ limit: '50mb' }));
|
|
91
|
+
app.use(express_1.default.urlencoded({ extended: true }));
|
|
92
|
+
app.get("/getFileContent", getFileContent);
|
|
93
|
+
app.post("/syncScriptFromClient", syncScriptFromClient);
|
|
94
|
+
app.post("/syncScriptFromServer", syncScriptFromServer);
|
|
95
|
+
}
|