scripting-cli 1.1.8 → 1.1.10
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/README.md +4 -0
- package/README_zh.md +4 -0
- package/dist/controller.js +181 -191
- package/dist/index.js +18 -2
- package/dist/start_server.js +4 -3
- package/dist/util.js +12 -23
- package/package.json +1 -1
- package/src/controller.ts +25 -19
- package/src/index.ts +25 -9
- package/src/start_server.ts +7 -2
- package/tsconfig.json +1 -1
package/README.md
CHANGED
|
@@ -76,6 +76,10 @@ As you write and save scripts in your desktop editor (e.g., VSCode), the updated
|
|
|
76
76
|
## Additional Information
|
|
77
77
|
|
|
78
78
|
- The default port for the service is `3000`. If that port is already in use, specify a different one using the `--port` option.
|
|
79
|
+
- Use the `--no-auto-open` option to start the development server without automatically opening the `index.tsx` or `widget.tsx` file in your default code editor. Example:
|
|
80
|
+
```bash
|
|
81
|
+
npx scripting-cli start --no-auto-open
|
|
82
|
+
```
|
|
79
83
|
- The tool is intended for users who prefer using desktop editors like VSCode and want seamless integration with the Scripting app for code synchronization and debugging.
|
|
80
84
|
|
|
81
85
|
## Troubleshooting
|
package/README_zh.md
CHANGED
|
@@ -76,6 +76,10 @@ npx scripting-cli start --port=4000
|
|
|
76
76
|
## 其他信息
|
|
77
77
|
|
|
78
78
|
- 服务的默认端口是 `3000`,如果该端口已被占用,您可以使用 `--port` 参数指定其他端口。
|
|
79
|
+
- 使用 --no-auto-open 选项可以在启动开发服务器时,避免自动打开 index.tsx 或 widget.tsx 文件。示例:
|
|
80
|
+
```bash
|
|
81
|
+
npx scripting-cli start --no-auto-open
|
|
82
|
+
```
|
|
79
83
|
- 该工具适用于喜欢使用桌面编辑器(如 VSCode)并希望与 Scripting 应用进行无缝集成的用户,以便进行代码同步和调试。
|
|
80
84
|
|
|
81
85
|
## 故障排除
|
package/dist/controller.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -19,185 +10,21 @@ const fs_1 = __importDefault(require("fs"));
|
|
|
19
10
|
const chalk_1 = __importDefault(require("chalk"));
|
|
20
11
|
const path_1 = __importDefault(require("path"));
|
|
21
12
|
class Controller {
|
|
22
|
-
static
|
|
23
|
-
|
|
13
|
+
static instances = new Map();
|
|
14
|
+
static create(socket, noAutoOpen) {
|
|
15
|
+
const ctrl = new Controller(socket, noAutoOpen);
|
|
24
16
|
Controller.instances.set(socket.id, ctrl);
|
|
25
17
|
return ctrl;
|
|
26
18
|
}
|
|
27
19
|
static get(socketId) {
|
|
28
20
|
return Controller.instances.get(socketId);
|
|
29
21
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
});
|
|
22
|
+
watcher = null;
|
|
23
|
+
scriptName = null;
|
|
24
|
+
socket = null;
|
|
25
|
+
noAutoOpen;
|
|
26
|
+
constructor(socket, noAutoOpen) {
|
|
27
|
+
this.noAutoOpen = noAutoOpen;
|
|
201
28
|
this.socket = socket;
|
|
202
29
|
socket.emit("socketId", socket.id);
|
|
203
30
|
socket.on('syncScriptFromClient', this.handleSyncScriptFromClient);
|
|
@@ -207,9 +34,8 @@ class Controller {
|
|
|
207
34
|
socket.on("log", this.handleLog);
|
|
208
35
|
socket.on("openFile", this.handleOpenFile);
|
|
209
36
|
socket.on('disconnect', (reason) => {
|
|
210
|
-
var _a;
|
|
211
37
|
console.log(chalk_1.default.gray(`Client [${socket.id}] disconnected: ${reason}`));
|
|
212
|
-
|
|
38
|
+
this.watcher?.close();
|
|
213
39
|
this.watcher = null;
|
|
214
40
|
this.scriptName = null;
|
|
215
41
|
Controller.instances.delete(socket.id);
|
|
@@ -234,7 +60,6 @@ class Controller {
|
|
|
234
60
|
// send file content md5 hash to client
|
|
235
61
|
// const content = fs.readFileSync(filePath, 'utf-8');
|
|
236
62
|
fs_1.default.readFile(filePath, 'utf-8', (err, content) => {
|
|
237
|
-
var _a;
|
|
238
63
|
if (err) {
|
|
239
64
|
console.log(chalk_1.default.red(`Error reading file: ${err}`));
|
|
240
65
|
return;
|
|
@@ -242,7 +67,7 @@ class Controller {
|
|
|
242
67
|
const hash = (0, util_1.md5)(content);
|
|
243
68
|
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
244
69
|
console.log(`[${chalk_1.default.bold.blue(scriptName)}] File changed: [${hash}]${relativePath}`);
|
|
245
|
-
|
|
70
|
+
this.socket?.emit('fileChange', {
|
|
246
71
|
scriptName,
|
|
247
72
|
filePath: relativePath,
|
|
248
73
|
hash,
|
|
@@ -252,17 +77,15 @@ class Controller {
|
|
|
252
77
|
// TODO, add support for other file types
|
|
253
78
|
})
|
|
254
79
|
.on('unlink', (filePath) => {
|
|
255
|
-
var _a;
|
|
256
80
|
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
257
|
-
|
|
81
|
+
this.socket?.emit('deleteFile', {
|
|
258
82
|
scriptName,
|
|
259
83
|
filePath: relativePath,
|
|
260
84
|
});
|
|
261
85
|
})
|
|
262
86
|
.on('unlinkDir', (filePath) => {
|
|
263
|
-
var _a;
|
|
264
87
|
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
265
|
-
|
|
88
|
+
this.socket?.emit('deleteFile', {
|
|
266
89
|
scriptName,
|
|
267
90
|
filePath: relativePath,
|
|
268
91
|
});
|
|
@@ -270,6 +93,173 @@ class Controller {
|
|
|
270
93
|
.on('error', error => console.log(chalk_1.default.bgRed(`Watcher error: ${error}`)))
|
|
271
94
|
.on('ready', () => console.log(`[${chalk_1.default.bold.blue(scriptName)}] Watching files for changes`));
|
|
272
95
|
}
|
|
96
|
+
handleSyncScriptFromClient = async (data, ack) => {
|
|
97
|
+
try {
|
|
98
|
+
if (data.scriptName === this.scriptName) {
|
|
99
|
+
ack({
|
|
100
|
+
error: `Script [${data.scriptName}] is already initialized`,
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (this.scriptName != null) {
|
|
105
|
+
console.log(`Received new script, closing previous watcher`);
|
|
106
|
+
this.watcher?.close();
|
|
107
|
+
this.watcher = null;
|
|
108
|
+
}
|
|
109
|
+
await (0, util_1.writeDtsFiles)({
|
|
110
|
+
"global.d.ts": data["global.d.ts"],
|
|
111
|
+
"scripting.d.ts": data["scripting.d.ts"],
|
|
112
|
+
});
|
|
113
|
+
this.scriptName = data.scriptName;
|
|
114
|
+
const scriptName = data.scriptName;
|
|
115
|
+
const scriptDir = (0, util_1.getPath)(data.scriptName);
|
|
116
|
+
const tsconfig = require((0, util_1.getPath)('tsconfig.json'));
|
|
117
|
+
if (!tsconfig.include.includes(scriptName)) {
|
|
118
|
+
tsconfig.include.push(scriptName);
|
|
119
|
+
await fs_1.default.promises.writeFile((0, util_1.getPath)('tsconfig.json'), JSON.stringify(tsconfig, null, 2));
|
|
120
|
+
}
|
|
121
|
+
(0, util_1.ensureDirectoryExistence)(scriptDir);
|
|
122
|
+
await Promise.all(Object.entries(data.scriptFiles).map(async ([filename, content]) => {
|
|
123
|
+
const filePath = path_1.default.join(scriptDir, filename);
|
|
124
|
+
(0, util_1.ensureDirectoryExistence)(filePath);
|
|
125
|
+
await fs_1.default.promises.writeFile(filePath, content);
|
|
126
|
+
}));
|
|
127
|
+
console.log(chalk_1.default.green('global.d.ts and scripting.d.ts and other script files saved.'));
|
|
128
|
+
this.createWatcher(data.scriptName);
|
|
129
|
+
ack({
|
|
130
|
+
success: true,
|
|
131
|
+
});
|
|
132
|
+
if (!this.noAutoOpen) {
|
|
133
|
+
// Open the entry file in VSCode after a delay
|
|
134
|
+
// to ensure the response is sent. Because the open file command
|
|
135
|
+
// may suspend the response and cause the client to not receive the ack.
|
|
136
|
+
setTimeout(() => {
|
|
137
|
+
const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
|
|
138
|
+
(0, util_1.tryOpenFileInVSCode)(entryFilePath);
|
|
139
|
+
}, 1000);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (e) {
|
|
143
|
+
console.log(chalk_1.default.red(`Error: ${e}`));
|
|
144
|
+
ack({
|
|
145
|
+
error: `Failed to init and sync script files.\n${e}`,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
handleSyncScriptFromServer = async (data, ack) => {
|
|
150
|
+
try {
|
|
151
|
+
if (this.scriptName === data.scriptName) {
|
|
152
|
+
ack({
|
|
153
|
+
error: `Script ${data.scriptName} is already initialized`,
|
|
154
|
+
});
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (this.scriptName != null) {
|
|
158
|
+
console.log(`Received new script [${chalk_1.default.bold.blue(data.scriptName)}], closing previous watcher`);
|
|
159
|
+
this.watcher?.close();
|
|
160
|
+
this.watcher = null;
|
|
161
|
+
}
|
|
162
|
+
await (0, util_1.writeDtsFiles)({
|
|
163
|
+
"global.d.ts": data["global.d.ts"],
|
|
164
|
+
"scripting.d.ts": data["scripting.d.ts"],
|
|
165
|
+
});
|
|
166
|
+
const scriptDir = (0, util_1.getPath)(data.scriptName);
|
|
167
|
+
if (!fs_1.default.existsSync(scriptDir)) {
|
|
168
|
+
ack({
|
|
169
|
+
error: `Script directory ${scriptDir} does not exist`,
|
|
170
|
+
});
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const scriptFiles = {};
|
|
174
|
+
const readDir = async (dir) => {
|
|
175
|
+
const files = await fs_1.default.promises.readdir(dir, { withFileTypes: true });
|
|
176
|
+
await Promise.all(files.map(async (file) => {
|
|
177
|
+
if (file.isFile()) {
|
|
178
|
+
const filePath = path_1.default.join(dir, file.name);
|
|
179
|
+
const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
|
|
180
|
+
const content = await fs_1.default.promises.readFile(filePath, 'utf-8').catch((err) => {
|
|
181
|
+
console.log(chalk_1.default.red(`Error reading file: ${err}`));
|
|
182
|
+
});
|
|
183
|
+
if (typeof content === "string") {
|
|
184
|
+
scriptFiles[relativePath] = {
|
|
185
|
+
hash: (0, util_1.md5)(content),
|
|
186
|
+
content,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else if (file.isDirectory()) {
|
|
191
|
+
await readDir(path_1.default.join(dir, file.name));
|
|
192
|
+
}
|
|
193
|
+
}));
|
|
194
|
+
};
|
|
195
|
+
await readDir(scriptDir);
|
|
196
|
+
this.scriptName = data.scriptName;
|
|
197
|
+
ack({
|
|
198
|
+
scriptFiles,
|
|
199
|
+
});
|
|
200
|
+
this.createWatcher(data.scriptName);
|
|
201
|
+
if (!this.noAutoOpen) {
|
|
202
|
+
// Open the entry file in VSCode after a delay
|
|
203
|
+
// to ensure the response is sent. Because the open file command
|
|
204
|
+
// may suspend the response and cause the client to not receive the ack.
|
|
205
|
+
setTimeout(() => {
|
|
206
|
+
const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
|
|
207
|
+
(0, util_1.tryOpenFileInVSCode)(entryFilePath);
|
|
208
|
+
}, 1000);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
catch (e) {
|
|
212
|
+
ack({
|
|
213
|
+
error: `Failed to init and pull script files.\n${e}`,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
handleLog = (data) => {
|
|
218
|
+
if (data.scriptName !== this.scriptName) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
console.log(`[${chalk_1.default.bold.blue(this.scriptName)}][${data.isError ? chalk_1.default.red("ERROR") : "LOG"}][${new Date().toLocaleTimeString()}] ${data.content}`);
|
|
222
|
+
};
|
|
223
|
+
handleOpenFile = async (data) => {
|
|
224
|
+
if (data.scriptName !== this.scriptName) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const filePath = path_1.default.join((0, util_1.getPath)(this.scriptName), data.relativePath);
|
|
228
|
+
if (!this.noAutoOpen && fs_1.default.existsSync(filePath)) {
|
|
229
|
+
(0, util_1.tryOpenFileInVSCode)(filePath);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
handleStopDebugScript = (data, ack) => {
|
|
233
|
+
if (data === this.scriptName) {
|
|
234
|
+
console.log(`[${chalk_1.default.bold.blue(this.scriptName)}] Stop debug script`);
|
|
235
|
+
this.watcher?.close();
|
|
236
|
+
this.watcher = null;
|
|
237
|
+
this.scriptName = null;
|
|
238
|
+
ack(true);
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
ack(false);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
handleGetFileContent = async (data, ack) => {
|
|
245
|
+
if (this.scriptName !== data.scriptName) {
|
|
246
|
+
ack({
|
|
247
|
+
error: `Script ${data.scriptName} is not initialized`,
|
|
248
|
+
});
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const filePath = path_1.default.join((0, util_1.getPath)(data.scriptName), data.relativePath);
|
|
252
|
+
try {
|
|
253
|
+
const content = await fs_1.default.promises.readFile(filePath, 'utf-8');
|
|
254
|
+
ack({
|
|
255
|
+
content,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
catch (e) {
|
|
259
|
+
ack({
|
|
260
|
+
error: `Failed to get file content.\n${e}`,
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
};
|
|
273
264
|
}
|
|
274
265
|
exports.Controller = Controller;
|
|
275
|
-
Controller.instances = new Map();
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,8 @@ const helpers_1 = require("yargs/helpers");
|
|
|
9
9
|
const start_server_1 = require("./start_server");
|
|
10
10
|
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
|
11
11
|
.command('start', 'Start the scripting-cli server', (yargs) => {
|
|
12
|
-
return yargs
|
|
12
|
+
return yargs
|
|
13
|
+
.options({
|
|
13
14
|
port: {
|
|
14
15
|
alias: ["p"],
|
|
15
16
|
describe: "The port the server listens on",
|
|
@@ -20,9 +21,24 @@ const start_server_1 = require("./start_server");
|
|
|
20
21
|
.usage("$0 start [--port=<port>]")
|
|
21
22
|
.example([
|
|
22
23
|
["$0 start -p=8000", "Start server and listen on 8000"]
|
|
24
|
+
])
|
|
25
|
+
.options({
|
|
26
|
+
"no-auto-open": {
|
|
27
|
+
type: "boolean",
|
|
28
|
+
default: true,
|
|
29
|
+
describe: "Do not open the index.tsx/widget.tsx file automatically",
|
|
30
|
+
},
|
|
31
|
+
})
|
|
32
|
+
.usage("$0 start [--no-auto-open]")
|
|
33
|
+
.example([
|
|
34
|
+
["$0 start --no-auto-open", "Start server without opening the index.tsx/widget.tsx file"],
|
|
35
|
+
["$0 start --no-auto-open -p=8000", "Start server without opening the index.tsx/widget.tsx file and listen on 8000"],
|
|
23
36
|
]);
|
|
24
37
|
}, (argv) => {
|
|
25
|
-
(0, start_server_1.startServer)(
|
|
38
|
+
(0, start_server_1.startServer)({
|
|
39
|
+
port: argv.port,
|
|
40
|
+
noAutoOpen: argv['no-auto-open'],
|
|
41
|
+
});
|
|
26
42
|
})
|
|
27
43
|
.option('help', {
|
|
28
44
|
alias: 'h',
|
package/dist/start_server.js
CHANGED
|
@@ -18,11 +18,12 @@ const app = (0, express_1.default)();
|
|
|
18
18
|
const server = http_1.default.createServer(app);
|
|
19
19
|
const io = new socket_io_1.Server(server);
|
|
20
20
|
const bonjour = (0, bonjour_1.default)();
|
|
21
|
-
function startServer(port) {
|
|
22
|
-
|
|
21
|
+
function startServer({ port, noAutoOpen }) {
|
|
22
|
+
console.log("port", port, "noAutoOpen", noAutoOpen);
|
|
23
|
+
const PORT = port ?? 3000;
|
|
23
24
|
(0, http_router_1.initHttpRouter)(app);
|
|
24
25
|
io.on('connection', (socket) => {
|
|
25
|
-
controller_1.Controller.create(socket);
|
|
26
|
+
controller_1.Controller.create(socket, noAutoOpen);
|
|
26
27
|
console.log(chalk_1.default.blue(`Client [${socket.id}] connected`));
|
|
27
28
|
});
|
|
28
29
|
server.listen(PORT, () => {
|
package/dist/util.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -110,21 +101,19 @@ function ensureDirectoryExistence(filePath) {
|
|
|
110
101
|
fs_1.default.mkdirSync(dirname, { recursive: true });
|
|
111
102
|
}
|
|
112
103
|
}
|
|
113
|
-
function writeDtsFiles(files) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
})));
|
|
125
|
-
});
|
|
104
|
+
async function writeDtsFiles(files) {
|
|
105
|
+
await Promise.all(Object.entries(files).map(async ([filename, content]) => {
|
|
106
|
+
const filePath = getPath(`dts/${filename}`);
|
|
107
|
+
ensureDirectoryExistence(filePath);
|
|
108
|
+
await fs_1.default
|
|
109
|
+
.promises
|
|
110
|
+
.writeFile(filePath, content)
|
|
111
|
+
.catch((err) => {
|
|
112
|
+
console.log(chalk_1.default.red(`Error writing file: ${err}`));
|
|
113
|
+
});
|
|
114
|
+
}));
|
|
126
115
|
}
|
|
127
116
|
function getRelativePath(from, to) {
|
|
128
117
|
const relativePath = path_1.default.relative(from, to);
|
|
129
|
-
return relativePath.
|
|
118
|
+
return relativePath.replaceAll("\\", "/");
|
|
130
119
|
}
|
package/package.json
CHANGED
package/src/controller.ts
CHANGED
|
@@ -10,8 +10,8 @@ export class Controller {
|
|
|
10
10
|
|
|
11
11
|
private static instances: Map<string, Controller> = new Map();
|
|
12
12
|
|
|
13
|
-
static create(socket: Socket) {
|
|
14
|
-
const ctrl = new Controller(socket);
|
|
13
|
+
static create(socket: Socket, noAutoOpen: boolean | undefined) {
|
|
14
|
+
const ctrl = new Controller(socket, noAutoOpen);
|
|
15
15
|
Controller.instances.set(socket.id, ctrl);
|
|
16
16
|
return ctrl;
|
|
17
17
|
}
|
|
@@ -23,8 +23,10 @@ export class Controller {
|
|
|
23
23
|
watcher: FSWatcher | null = null;
|
|
24
24
|
scriptName: string | null = null;
|
|
25
25
|
socket: Socket | null = null;
|
|
26
|
+
noAutoOpen: boolean | undefined;
|
|
26
27
|
|
|
27
|
-
constructor(socket: Socket) {
|
|
28
|
+
constructor(socket: Socket, noAutoOpen: boolean | undefined) {
|
|
29
|
+
this.noAutoOpen = noAutoOpen;
|
|
28
30
|
this.socket = socket;
|
|
29
31
|
|
|
30
32
|
socket.emit("socketId", socket.id);
|
|
@@ -169,13 +171,16 @@ export class Controller {
|
|
|
169
171
|
success: true,
|
|
170
172
|
});
|
|
171
173
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
174
|
+
if (!this.noAutoOpen) {
|
|
175
|
+
|
|
176
|
+
// Open the entry file in VSCode after a delay
|
|
177
|
+
// to ensure the response is sent. Because the open file command
|
|
178
|
+
// may suspend the response and cause the client to not receive the ack.
|
|
179
|
+
setTimeout(() => {
|
|
180
|
+
const entryFilePath = path.join(scriptDir, "index.tsx")
|
|
181
|
+
tryOpenFileInVSCode(entryFilePath);
|
|
182
|
+
}, 1000);
|
|
183
|
+
}
|
|
179
184
|
|
|
180
185
|
} catch (e) {
|
|
181
186
|
console.log(chalk.red(`Error: ${e}`));
|
|
@@ -262,14 +267,15 @@ export class Controller {
|
|
|
262
267
|
|
|
263
268
|
this.createWatcher(data.scriptName);
|
|
264
269
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
270
|
+
if (!this.noAutoOpen) {
|
|
271
|
+
// Open the entry file in VSCode after a delay
|
|
272
|
+
// to ensure the response is sent. Because the open file command
|
|
273
|
+
// may suspend the response and cause the client to not receive the ack.
|
|
274
|
+
setTimeout(() => {
|
|
275
|
+
const entryFilePath = path.join(scriptDir, "index.tsx")
|
|
276
|
+
tryOpenFileInVSCode(entryFilePath);
|
|
277
|
+
}, 1000);
|
|
278
|
+
}
|
|
273
279
|
} catch (e) {
|
|
274
280
|
ack({
|
|
275
281
|
error: `Failed to init and pull script files.\n${e}`,
|
|
@@ -299,7 +305,7 @@ export class Controller {
|
|
|
299
305
|
|
|
300
306
|
const filePath = path.join(getPath(this.scriptName), data.relativePath)
|
|
301
307
|
|
|
302
|
-
if (fs.existsSync(filePath)) {
|
|
308
|
+
if (!this.noAutoOpen && fs.existsSync(filePath)) {
|
|
303
309
|
tryOpenFileInVSCode(filePath)
|
|
304
310
|
}
|
|
305
311
|
|
package/src/index.ts
CHANGED
|
@@ -6,20 +6,36 @@ import { startServer } from './start_server';
|
|
|
6
6
|
|
|
7
7
|
yargs(hideBin(process.argv))
|
|
8
8
|
.command('start', 'Start the scripting-cli server', (yargs) => {
|
|
9
|
-
return yargs
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
return yargs
|
|
10
|
+
.options({
|
|
11
|
+
port: {
|
|
12
|
+
alias: ["p"],
|
|
13
|
+
describe: "The port the server listens on",
|
|
14
|
+
type: "number",
|
|
15
|
+
default: 3000,
|
|
16
|
+
}
|
|
17
|
+
})
|
|
17
18
|
.usage("$0 start [--port=<port>]")
|
|
18
19
|
.example([
|
|
19
20
|
["$0 start -p=8000", "Start server and listen on 8000"]
|
|
21
|
+
])
|
|
22
|
+
.options({
|
|
23
|
+
"no-auto-open": {
|
|
24
|
+
type: "boolean",
|
|
25
|
+
default: true,
|
|
26
|
+
describe: "Do not open the index.tsx/widget.tsx file automatically",
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
.usage("$0 start [--no-auto-open]")
|
|
30
|
+
.example([
|
|
31
|
+
["$0 start --no-auto-open", "Start server without opening the index.tsx/widget.tsx file"],
|
|
32
|
+
["$0 start --no-auto-open -p=8000", "Start server without opening the index.tsx/widget.tsx file and listen on 8000"],
|
|
20
33
|
]);
|
|
21
34
|
}, (argv) => {
|
|
22
|
-
startServer(
|
|
35
|
+
startServer({
|
|
36
|
+
port: argv.port,
|
|
37
|
+
noAutoOpen: argv['no-auto-open'],
|
|
38
|
+
});
|
|
23
39
|
})
|
|
24
40
|
.option('help', {
|
|
25
41
|
alias: 'h',
|
package/src/start_server.ts
CHANGED
|
@@ -15,14 +15,19 @@ const server = http.createServer(app);
|
|
|
15
15
|
const io = new SocketIOServer(server);
|
|
16
16
|
const bonjour = Bonjour();
|
|
17
17
|
|
|
18
|
-
export function startServer(port
|
|
18
|
+
export function startServer({ port, noAutoOpen }: {
|
|
19
|
+
port: number | undefined
|
|
20
|
+
noAutoOpen: boolean | undefined
|
|
21
|
+
}) {
|
|
22
|
+
|
|
23
|
+
console.log("port", port, "noAutoOpen", noAutoOpen);
|
|
19
24
|
|
|
20
25
|
const PORT = port ?? 3000;
|
|
21
26
|
|
|
22
27
|
initHttpRouter(app);
|
|
23
28
|
|
|
24
29
|
io.on('connection', (socket) => {
|
|
25
|
-
Controller.create(socket);
|
|
30
|
+
Controller.create(socket, noAutoOpen);
|
|
26
31
|
console.log(chalk.blue(`Client [${socket.id}] connected`));
|
|
27
32
|
});
|
|
28
33
|
|