scripting-cli 1.2.1 → 1.2.3
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 +59 -27
- package/dist/http_router.js +31 -0
- package/package.json +1 -1
- package/src/controller.ts +91 -46
- package/src/http_router.ts +44 -0
package/dist/controller.js
CHANGED
|
@@ -28,10 +28,10 @@ class Controller {
|
|
|
28
28
|
this.noAutoOpen = noAutoOpen;
|
|
29
29
|
this.socket = socket;
|
|
30
30
|
socket.emit("socketId", socket.id);
|
|
31
|
-
socket.on('syncScriptFromClient', this.handleSyncScriptFromClient);
|
|
32
|
-
socket.on("syncScriptFromServer", this.handleSyncScriptFromClient);
|
|
33
|
-
socket.on('stopDebugScript', this.handleStopDebugScript);
|
|
34
|
-
socket.on("getFileContent", this.handleGetFileContent);
|
|
31
|
+
// socket.on('syncScriptFromClient', this.handleSyncScriptFromClient);
|
|
32
|
+
// socket.on("syncScriptFromServer", this.handleSyncScriptFromClient);
|
|
33
|
+
// socket.on('stopDebugScript', this.handleStopDebugScript);
|
|
34
|
+
// socket.on("getFileContent", this.handleGetFileContent);
|
|
35
35
|
socket.on("log", this.handleLog);
|
|
36
36
|
socket.on("openFile", this.handleOpenFile);
|
|
37
37
|
socket.on('disconnect', (reason) => {
|
|
@@ -55,8 +55,7 @@ class Controller {
|
|
|
55
55
|
ignored: /(^|[\/\\])\../,
|
|
56
56
|
persistent: true,
|
|
57
57
|
});
|
|
58
|
-
|
|
59
|
-
.on('change', (filePath) => {
|
|
58
|
+
const updateHandler = (filePath) => {
|
|
60
59
|
if (filePath.match(/\.(js(x)?|ts(x)?|json|md|txt)$/)) {
|
|
61
60
|
// send file content md5 hash to client
|
|
62
61
|
// const content = fs.readFileSync(filePath, 'utf-8');
|
|
@@ -75,22 +74,28 @@ class Controller {
|
|
|
75
74
|
});
|
|
76
75
|
});
|
|
77
76
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
77
|
+
else {
|
|
78
|
+
// Other file types, just notify the client to reload the file
|
|
79
|
+
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
80
|
+
console.log(`[${chalk_1.default.bold.blue(scriptName)}] File changed: ${relativePath}`);
|
|
81
|
+
this.socket?.emit('otherFileChange', {
|
|
82
|
+
scriptName,
|
|
83
|
+
filePath: relativePath,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const deleteHandler = (filePath) => {
|
|
88
88
|
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
89
89
|
this.socket?.emit('deleteFile', {
|
|
90
90
|
scriptName,
|
|
91
91
|
filePath: relativePath,
|
|
92
92
|
});
|
|
93
|
-
}
|
|
93
|
+
};
|
|
94
|
+
this.watcher
|
|
95
|
+
.on('add', updateHandler)
|
|
96
|
+
.on('change', updateHandler)
|
|
97
|
+
.on('unlink', deleteHandler)
|
|
98
|
+
.on('unlinkDir', deleteHandler)
|
|
94
99
|
.on('error', error => console.log(chalk_1.default.bgRed(`Watcher error: ${error}`)))
|
|
95
100
|
.on('ready', () => console.log(`[${chalk_1.default.bold.blue(scriptName)}] Watching files for changes`));
|
|
96
101
|
}
|
|
@@ -155,8 +160,8 @@ class Controller {
|
|
|
155
160
|
this.watcher = null;
|
|
156
161
|
}
|
|
157
162
|
await (0, util_1.writeDtsFiles)({
|
|
158
|
-
globalDtsFileName: data[const_1.globalDtsFileName],
|
|
159
|
-
scriptingDtsFileName: data[const_1.scriptingDtsFileName],
|
|
163
|
+
[const_1.globalDtsFileName]: data[const_1.globalDtsFileName],
|
|
164
|
+
[const_1.scriptingDtsFileName]: data[const_1.scriptingDtsFileName],
|
|
160
165
|
});
|
|
161
166
|
const scriptDir = (0, util_1.getScriptPath)(data.scriptName);
|
|
162
167
|
if (!fs_1.default.existsSync(scriptDir)) {
|
|
@@ -166,6 +171,7 @@ class Controller {
|
|
|
166
171
|
return;
|
|
167
172
|
}
|
|
168
173
|
const scriptFiles = {};
|
|
174
|
+
const otherFiles = [];
|
|
169
175
|
const readDir = async (dir) => {
|
|
170
176
|
const files = await fs_1.default.promises.readdir(dir, { withFileTypes: true });
|
|
171
177
|
await Promise.all(files.map(async (file) => {
|
|
@@ -173,17 +179,23 @@ class Controller {
|
|
|
173
179
|
if (file.name.startsWith('.git') || file.name.startsWith('.vscode')) {
|
|
174
180
|
return;
|
|
175
181
|
}
|
|
182
|
+
// only read js, jsx, ts, tsx, json, md, txt files
|
|
176
183
|
if (file.isFile()) {
|
|
177
184
|
const filePath = path_1.default.join(dir, file.name);
|
|
178
185
|
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
186
|
+
if (file.name.match(/\.(js(x)?|ts(x)?|json|md|txt)$/)) {
|
|
187
|
+
const content = await fs_1.default.promises.readFile(filePath, 'utf-8').catch((err) => {
|
|
188
|
+
console.log(chalk_1.default.red(`Error reading file: ${err}`));
|
|
189
|
+
});
|
|
190
|
+
if (typeof content === "string") {
|
|
191
|
+
scriptFiles[relativePath] = {
|
|
192
|
+
hash: (0, util_1.md5)(content),
|
|
193
|
+
content,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
otherFiles.push(relativePath);
|
|
187
199
|
}
|
|
188
200
|
}
|
|
189
201
|
else if (file.isDirectory()) {
|
|
@@ -195,6 +207,7 @@ class Controller {
|
|
|
195
207
|
this.scriptName = data.scriptName;
|
|
196
208
|
ack({
|
|
197
209
|
scriptFiles,
|
|
210
|
+
otherFiles: otherFiles.length > 0 ? otherFiles : undefined,
|
|
198
211
|
});
|
|
199
212
|
this.createWatcher(data.scriptName);
|
|
200
213
|
if (!this.noAutoOpen) {
|
|
@@ -260,5 +273,24 @@ class Controller {
|
|
|
260
273
|
});
|
|
261
274
|
}
|
|
262
275
|
};
|
|
276
|
+
handleGetFilePath = async (data, ack) => {
|
|
277
|
+
if (this.scriptName !== data.scriptName) {
|
|
278
|
+
ack({
|
|
279
|
+
error: `Script ${data.scriptName} is not initialized`,
|
|
280
|
+
});
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
const filePath = path_1.default.join((0, util_1.getScriptPath)(data.scriptName), data.relativePath);
|
|
284
|
+
if (fs_1.default.existsSync(filePath)) {
|
|
285
|
+
ack({
|
|
286
|
+
filePath,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
ack({
|
|
291
|
+
error: `File ${filePath} does not exist`,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
};
|
|
263
295
|
}
|
|
264
296
|
exports.Controller = Controller;
|
package/dist/http_router.js
CHANGED
|
@@ -41,6 +41,36 @@ const getFileContent = (req, res) => {
|
|
|
41
41
|
});
|
|
42
42
|
});
|
|
43
43
|
};
|
|
44
|
+
const getFileData = (req, res) => {
|
|
45
|
+
const { socketId, scriptName, relativePath, } = req.query;
|
|
46
|
+
if (!socketId) {
|
|
47
|
+
console.log(chalk_1.default.red(`Socket not found: ${socketId}`));
|
|
48
|
+
}
|
|
49
|
+
if (!scriptName || !relativePath) {
|
|
50
|
+
res.json({
|
|
51
|
+
error: "Missing parameters: scriptName or relativePath."
|
|
52
|
+
});
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const ctrl = controller_1.Controller.get(socketId);
|
|
56
|
+
if (!ctrl) {
|
|
57
|
+
console.log(chalk_1.default.red(`Socket not found: ${socketId}`));
|
|
58
|
+
res.json({
|
|
59
|
+
error: "Socket not found."
|
|
60
|
+
});
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
ctrl.handleGetFilePath({
|
|
64
|
+
scriptName,
|
|
65
|
+
relativePath,
|
|
66
|
+
}, (result) => {
|
|
67
|
+
if (result.error) {
|
|
68
|
+
res.status(500).send(result.error);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
res.sendFile(result.filePath);
|
|
72
|
+
});
|
|
73
|
+
};
|
|
44
74
|
const syncScriptFromClient = (req, res) => {
|
|
45
75
|
const params = req.body;
|
|
46
76
|
const socketId = params.socketId;
|
|
@@ -90,6 +120,7 @@ function initHttpRouter(app) {
|
|
|
90
120
|
app.use(express_1.default.json({ limit: '50mb' }));
|
|
91
121
|
app.use(express_1.default.urlencoded({ extended: true }));
|
|
92
122
|
app.get("/getFileContent", getFileContent);
|
|
123
|
+
app.get("/getFileData", getFileData);
|
|
93
124
|
app.post("/syncScriptFromClient", syncScriptFromClient);
|
|
94
125
|
app.post("/syncScriptFromServer", syncScriptFromServer);
|
|
95
126
|
}
|
package/package.json
CHANGED
package/src/controller.ts
CHANGED
|
@@ -32,13 +32,13 @@ export class Controller {
|
|
|
32
32
|
|
|
33
33
|
socket.emit("socketId", socket.id);
|
|
34
34
|
|
|
35
|
-
socket.on('syncScriptFromClient', this.handleSyncScriptFromClient);
|
|
35
|
+
// socket.on('syncScriptFromClient', this.handleSyncScriptFromClient);
|
|
36
36
|
|
|
37
|
-
socket.on("syncScriptFromServer", this.handleSyncScriptFromClient);
|
|
37
|
+
// socket.on("syncScriptFromServer", this.handleSyncScriptFromClient);
|
|
38
38
|
|
|
39
|
-
socket.on('stopDebugScript', this.handleStopDebugScript);
|
|
39
|
+
// socket.on('stopDebugScript', this.handleStopDebugScript);
|
|
40
40
|
|
|
41
|
-
socket.on("getFileContent", this.handleGetFileContent);
|
|
41
|
+
// socket.on("getFileContent", this.handleGetFileContent);
|
|
42
42
|
|
|
43
43
|
socket.on("log", this.handleLog);
|
|
44
44
|
|
|
@@ -70,44 +70,52 @@ export class Controller {
|
|
|
70
70
|
persistent: true,
|
|
71
71
|
});
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
hash,
|
|
92
|
-
});
|
|
73
|
+
const updateHandler = (filePath: string) => {
|
|
74
|
+
if (filePath.match(/\.(js(x)?|ts(x)?|json|md|txt)$/)) {
|
|
75
|
+
// send file content md5 hash to client
|
|
76
|
+
// const content = fs.readFileSync(filePath, 'utf-8');
|
|
77
|
+
fs.readFile(filePath, 'utf-8', (err, content) => {
|
|
78
|
+
if (err) {
|
|
79
|
+
console.log(chalk.red(`Error reading file: ${err}`));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const hash = md5(content);
|
|
83
|
+
const relativePath = getRelativePath(scriptDir, filePath);
|
|
84
|
+
console.log(
|
|
85
|
+
`[${chalk.bold.blue(scriptName)}] File changed: [${hash}]${relativePath}`
|
|
86
|
+
)
|
|
87
|
+
this.socket?.emit('fileChange', {
|
|
88
|
+
scriptName,
|
|
89
|
+
filePath: relativePath,
|
|
90
|
+
hash,
|
|
93
91
|
});
|
|
94
|
-
}
|
|
95
|
-
// TODO, add support for other file types
|
|
96
|
-
})
|
|
97
|
-
.on('unlink', (filePath) => {
|
|
98
|
-
const relativePath = getRelativePath(scriptDir, filePath);
|
|
99
|
-
this.socket?.emit('deleteFile', {
|
|
100
|
-
scriptName,
|
|
101
|
-
filePath: relativePath,
|
|
102
92
|
});
|
|
103
|
-
}
|
|
104
|
-
|
|
93
|
+
} else {
|
|
94
|
+
// Other file types, just notify the client to reload the file
|
|
105
95
|
const relativePath = getRelativePath(scriptDir, filePath);
|
|
106
|
-
|
|
96
|
+
console.log(
|
|
97
|
+
`[${chalk.bold.blue(scriptName)}] File changed: ${relativePath}`
|
|
98
|
+
)
|
|
99
|
+
this.socket?.emit('otherFileChange', {
|
|
107
100
|
scriptName,
|
|
108
101
|
filePath: relativePath,
|
|
109
102
|
});
|
|
110
|
-
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const deleteHandler = (filePath: string) => {
|
|
107
|
+
const relativePath = getRelativePath(scriptDir, filePath);
|
|
108
|
+
this.socket?.emit('deleteFile', {
|
|
109
|
+
scriptName,
|
|
110
|
+
filePath: relativePath,
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
this.watcher
|
|
115
|
+
.on('add', updateHandler)
|
|
116
|
+
.on('change', updateHandler)
|
|
117
|
+
.on('unlink', deleteHandler)
|
|
118
|
+
.on('unlinkDir', deleteHandler)
|
|
111
119
|
.on('error', error => console.log(chalk.bgRed(`Watcher error: ${error}`)))
|
|
112
120
|
.on('ready', () => console.log(`[${chalk.bold.blue(scriptName)}] Watching files for changes`));
|
|
113
121
|
}
|
|
@@ -193,6 +201,7 @@ export class Controller {
|
|
|
193
201
|
hash: string
|
|
194
202
|
content: string
|
|
195
203
|
}>
|
|
204
|
+
otherFiles?: string[]
|
|
196
205
|
}) => void) => {
|
|
197
206
|
try {
|
|
198
207
|
if (this.scriptName === data.scriptName) {
|
|
@@ -208,8 +217,8 @@ export class Controller {
|
|
|
208
217
|
}
|
|
209
218
|
|
|
210
219
|
await writeDtsFiles({
|
|
211
|
-
globalDtsFileName: data[globalDtsFileName],
|
|
212
|
-
scriptingDtsFileName: data[scriptingDtsFileName],
|
|
220
|
+
[globalDtsFileName]: data[globalDtsFileName],
|
|
221
|
+
[scriptingDtsFileName]: data[scriptingDtsFileName],
|
|
213
222
|
});
|
|
214
223
|
|
|
215
224
|
const scriptDir = getScriptPath(data.scriptName);
|
|
@@ -226,6 +235,8 @@ export class Controller {
|
|
|
226
235
|
content: string
|
|
227
236
|
}> = {};
|
|
228
237
|
|
|
238
|
+
const otherFiles: string[] = [];
|
|
239
|
+
|
|
229
240
|
const readDir = async (dir: string) => {
|
|
230
241
|
const files = await fs.promises.readdir(dir, { withFileTypes: true });
|
|
231
242
|
await Promise.all(
|
|
@@ -234,17 +245,24 @@ export class Controller {
|
|
|
234
245
|
if (file.name.startsWith('.git') || file.name.startsWith('.vscode')) {
|
|
235
246
|
return;
|
|
236
247
|
}
|
|
248
|
+
|
|
249
|
+
// only read js, jsx, ts, tsx, json, md, txt files
|
|
237
250
|
if (file.isFile()) {
|
|
238
251
|
const filePath = path.join(dir, file.name);
|
|
239
252
|
const relativePath = getRelativePath(scriptDir, filePath);
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
253
|
+
|
|
254
|
+
if (file.name.match(/\.(js(x)?|ts(x)?|json|md|txt)$/)) {
|
|
255
|
+
const content = await fs.promises.readFile(filePath, 'utf-8').catch((err) => {
|
|
256
|
+
console.log(chalk.red(`Error reading file: ${err}`));
|
|
257
|
+
});
|
|
258
|
+
if (typeof content === "string") {
|
|
259
|
+
scriptFiles[relativePath] = {
|
|
260
|
+
hash: md5(content),
|
|
261
|
+
content,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
} else {
|
|
265
|
+
otherFiles.push(relativePath);
|
|
248
266
|
}
|
|
249
267
|
} else if (file.isDirectory()) {
|
|
250
268
|
await readDir(path.join(dir, file.name));
|
|
@@ -259,6 +277,7 @@ export class Controller {
|
|
|
259
277
|
|
|
260
278
|
ack({
|
|
261
279
|
scriptFiles,
|
|
280
|
+
otherFiles: otherFiles.length > 0 ? otherFiles : undefined,
|
|
262
281
|
});
|
|
263
282
|
|
|
264
283
|
this.createWatcher(data.scriptName);
|
|
@@ -345,4 +364,30 @@ export class Controller {
|
|
|
345
364
|
});
|
|
346
365
|
}
|
|
347
366
|
}
|
|
367
|
+
|
|
368
|
+
handleGetFilePath = async (data: {
|
|
369
|
+
scriptName: string,
|
|
370
|
+
relativePath: string
|
|
371
|
+
}, ack: (result: {
|
|
372
|
+
error?: string,
|
|
373
|
+
filePath?: string
|
|
374
|
+
}) => void) => {
|
|
375
|
+
if (this.scriptName !== data.scriptName) {
|
|
376
|
+
ack({
|
|
377
|
+
error: `Script ${data.scriptName} is not initialized`,
|
|
378
|
+
});
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const filePath = path.join(getScriptPath(data.scriptName), data.relativePath);
|
|
383
|
+
if (fs.existsSync(filePath)) {
|
|
384
|
+
ack({
|
|
385
|
+
filePath,
|
|
386
|
+
});
|
|
387
|
+
} else {
|
|
388
|
+
ack({
|
|
389
|
+
error: `File ${filePath} does not exist`,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
}
|
|
348
393
|
}
|
package/src/http_router.ts
CHANGED
|
@@ -49,6 +49,49 @@ const getFileContent: express.RequestHandler = (req, res) => {
|
|
|
49
49
|
})
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
const getFileData: express.RequestHandler = (req, res) => {
|
|
53
|
+
const {
|
|
54
|
+
socketId,
|
|
55
|
+
scriptName,
|
|
56
|
+
relativePath,
|
|
57
|
+
} = req.query as {
|
|
58
|
+
scriptName: string;
|
|
59
|
+
socketId: string;
|
|
60
|
+
relativePath: string;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (!socketId) {
|
|
64
|
+
console.log(chalk.red(`Socket not found: ${socketId}`));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!scriptName || !relativePath) {
|
|
68
|
+
res.json({
|
|
69
|
+
error: "Missing parameters: scriptName or relativePath."
|
|
70
|
+
});
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const ctrl = Controller.get(socketId);
|
|
75
|
+
if (!ctrl) {
|
|
76
|
+
console.log(chalk.red(`Socket not found: ${socketId}`));
|
|
77
|
+
res.json({
|
|
78
|
+
error: "Socket not found."
|
|
79
|
+
});
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
ctrl.handleGetFilePath({
|
|
84
|
+
scriptName,
|
|
85
|
+
relativePath,
|
|
86
|
+
}, (result: any) => {
|
|
87
|
+
if (result.error) {
|
|
88
|
+
res.status(500).send(result.error);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
res.sendFile(result.filePath);
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
52
95
|
const syncScriptFromClient: express.RequestHandler = (req, res) => {
|
|
53
96
|
const params = req.body as {
|
|
54
97
|
socketId: string;
|
|
@@ -119,6 +162,7 @@ export function initHttpRouter(app: express.Express) {
|
|
|
119
162
|
app.use(express.json({ limit: '50mb' }));
|
|
120
163
|
app.use(express.urlencoded({ extended: true }));
|
|
121
164
|
app.get("/getFileContent", getFileContent);
|
|
165
|
+
app.get("/getFileData", getFileData);
|
|
122
166
|
app.post("/syncScriptFromClient", syncScriptFromClient);
|
|
123
167
|
app.post("/syncScriptFromServer", syncScriptFromServer);
|
|
124
168
|
}
|