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
package/dist/index.js
CHANGED
|
@@ -1,38 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
3
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
4
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
5
|
};
|
|
15
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
const express_1 = __importDefault(require("express"));
|
|
17
|
-
const http_1 = __importDefault(require("http"));
|
|
18
|
-
const socket_io_1 = require("socket.io");
|
|
19
|
-
const chokidar_1 = __importDefault(require("chokidar"));
|
|
20
|
-
const path_1 = __importDefault(require("path"));
|
|
21
|
-
const fs_1 = __importDefault(require("fs"));
|
|
22
|
-
const ip_1 = __importDefault(require("ip"));
|
|
23
7
|
const yargs_1 = __importDefault(require("yargs/yargs"));
|
|
24
8
|
const helpers_1 = require("yargs/helpers");
|
|
25
|
-
const
|
|
26
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
27
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
28
|
-
const child_process_1 = __importDefault(require("child_process"));
|
|
29
|
-
const os_1 = __importDefault(require("os"));
|
|
30
|
-
const qrcode_terminal_1 = __importDefault(require("qrcode-terminal"));
|
|
31
|
-
const app = (0, express_1.default)();
|
|
32
|
-
const server = http_1.default.createServer(app);
|
|
33
|
-
const io = new socket_io_1.Server(server);
|
|
34
|
-
const bonjour = (0, bonjour_1.default)();
|
|
35
|
-
const getPath = (filename) => path_1.default.join(process.cwd(), filename);
|
|
9
|
+
const start_server_1 = require("./start_server");
|
|
36
10
|
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
|
37
11
|
.command('start', 'Start the scripting-cli server', (yargs) => {
|
|
38
12
|
return yargs.options({
|
|
@@ -48,349 +22,13 @@ const getPath = (filename) => path_1.default.join(process.cwd(), filename);
|
|
|
48
22
|
["$0 start -p=8000", "Start server and listen on 8000"]
|
|
49
23
|
]);
|
|
50
24
|
}, (argv) => {
|
|
51
|
-
startServer(argv.port);
|
|
25
|
+
(0, start_server_1.startServer)(argv.port);
|
|
52
26
|
})
|
|
53
27
|
.option('help', {
|
|
54
28
|
alias: 'h',
|
|
55
29
|
describe: 'Show help',
|
|
56
30
|
})
|
|
57
31
|
.parse();
|
|
58
|
-
function md5(content) {
|
|
59
|
-
return crypto_1.default
|
|
60
|
-
.createHash('md5')
|
|
61
|
-
.update(content)
|
|
62
|
-
.digest('hex');
|
|
63
|
-
}
|
|
64
|
-
function startServer(port) {
|
|
65
|
-
// Function to create tsconfig.json
|
|
66
|
-
const createTsConfig = () => {
|
|
67
|
-
const tsconfigContent = `{
|
|
68
|
-
"compilerOptions": {
|
|
69
|
-
"target": "ESNext",
|
|
70
|
-
"lib": [
|
|
71
|
-
"esnext"
|
|
72
|
-
],
|
|
73
|
-
"allowJs": true,
|
|
74
|
-
"strict": true,
|
|
75
|
-
"esModuleInterop": true,
|
|
76
|
-
"allowSyntheticDefaultImports": true,
|
|
77
|
-
"forceConsistentCasingInFileNames": true,
|
|
78
|
-
"noFallthroughCasesInSwitch": true,
|
|
79
|
-
"module": "CommonJS",
|
|
80
|
-
"resolveJsonModule": true,
|
|
81
|
-
"skipLibCheck": true,
|
|
82
|
-
"jsx": "react",
|
|
83
|
-
"jsxFactory": "createElement",
|
|
84
|
-
"jsxFragmentFactory": "Fragment",
|
|
85
|
-
"paths": {
|
|
86
|
-
"scripting": [
|
|
87
|
-
"./dts/scripting.d.ts"
|
|
88
|
-
]
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
"include": [
|
|
92
|
-
"./dts/global.d.ts"
|
|
93
|
-
]
|
|
94
|
-
}`;
|
|
95
|
-
const filePath = getPath('tsconfig.json');
|
|
96
|
-
ensureDirectoryExistence(filePath);
|
|
97
|
-
if (!fs_1.default.existsSync(filePath)) {
|
|
98
|
-
fs_1.default.writeFileSync(filePath, tsconfigContent);
|
|
99
|
-
console.log(chalk_1.default.green('tsconfig.json created.'));
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
console.log(chalk_1.default.gray('tsconfig.json already exists.'));
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
const createVSCodeSettings = () => {
|
|
106
|
-
const settingsContent = `{
|
|
107
|
-
"typescript.format.semicolons": "remove",
|
|
108
|
-
"typescript.preferences.jsxAttributeCompletionStyle": "braces",
|
|
109
|
-
}`;
|
|
110
|
-
const filePath = getPath('.vscode/settings.json');
|
|
111
|
-
ensureDirectoryExistence(filePath);
|
|
112
|
-
if (!fs_1.default.existsSync(filePath)) {
|
|
113
|
-
fs_1.default.writeFileSync(filePath, settingsContent);
|
|
114
|
-
console.log(chalk_1.default.green('.vscode/settings.json created.'));
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
console.log(chalk_1.default.gray('.vscode/settings.json already exists.'));
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
const ensureDirectoryExistence = (filePath) => {
|
|
121
|
-
const dirname = path_1.default.dirname(filePath);
|
|
122
|
-
if (!fs_1.default.existsSync(dirname)) {
|
|
123
|
-
fs_1.default.mkdirSync(dirname, { recursive: true });
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
const writeDtsFiles = (files) => __awaiter(this, void 0, void 0, function* () {
|
|
127
|
-
yield Promise.all(Object.entries(files).map((_a) => __awaiter(this, [_a], void 0, function* ([filename, content]) {
|
|
128
|
-
const filePath = getPath(`dts/${filename}`);
|
|
129
|
-
ensureDirectoryExistence(filePath);
|
|
130
|
-
yield fs_1.default
|
|
131
|
-
.promises
|
|
132
|
-
.writeFile(filePath, content)
|
|
133
|
-
.catch((err) => {
|
|
134
|
-
console.log(chalk_1.default.red(`Error writing file: ${err}`));
|
|
135
|
-
});
|
|
136
|
-
})));
|
|
137
|
-
});
|
|
138
|
-
const tryOpenFileInVSCode = (filePath) => {
|
|
139
|
-
let cmd = `code "${filePath}"`;
|
|
140
|
-
if (os_1.default.platform() === "win32") {
|
|
141
|
-
cmd = `cmd.exe /c ${cmd}`;
|
|
142
|
-
}
|
|
143
|
-
else if (os_1.default.platform() === "linux") {
|
|
144
|
-
const shell = process.env["SHELL"];
|
|
145
|
-
cmd = `${shell} -c ${cmd}`;
|
|
146
|
-
}
|
|
147
|
-
else {
|
|
148
|
-
cmd = `"/Applications/Visual Studio Code.app/Contents/MacOS/Electron" "${filePath}"`;
|
|
149
|
-
}
|
|
150
|
-
child_process_1.default.execSync(cmd);
|
|
151
|
-
};
|
|
152
|
-
const PORT = port !== null && port !== void 0 ? port : 3000;
|
|
153
|
-
io.on('connection', (socket) => {
|
|
154
|
-
console.log(chalk_1.default.blue(`Client [${socket.id}] connected`));
|
|
155
|
-
let watcher = null;
|
|
156
|
-
let scriptName = "";
|
|
157
|
-
const createWatcher = (scriptName) => {
|
|
158
|
-
const scriptDir = getPath(scriptName);
|
|
159
|
-
watcher = chokidar_1.default.watch(scriptDir, {
|
|
160
|
-
ignored: /(^|[\/\\])\../,
|
|
161
|
-
persistent: true,
|
|
162
|
-
});
|
|
163
|
-
watcher
|
|
164
|
-
.on('change', (filePath) => {
|
|
165
|
-
if (filePath.match(/\.(js(x)?|ts(x)?)$/)) {
|
|
166
|
-
// send file content md5 hash to client
|
|
167
|
-
// const content = fs.readFileSync(filePath, 'utf-8');
|
|
168
|
-
fs_1.default.readFile(filePath, 'utf-8', (err, content) => {
|
|
169
|
-
if (err) {
|
|
170
|
-
console.log(chalk_1.default.red(`Error reading file: ${err}`));
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
const hash = md5(content);
|
|
174
|
-
const relativePath = path_1.default.relative(scriptDir, filePath);
|
|
175
|
-
console.log(`[${chalk_1.default.bold.blue(scriptName)}] File changed: [${hash}]${relativePath}`);
|
|
176
|
-
socket.emit('fileChange', {
|
|
177
|
-
scriptName,
|
|
178
|
-
filePath: relativePath,
|
|
179
|
-
hash,
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
// TODO, add support for other file types
|
|
184
|
-
})
|
|
185
|
-
.on('unlink', (filePath) => {
|
|
186
|
-
const relativePath = path_1.default.relative(scriptDir, filePath);
|
|
187
|
-
socket.emit('deleteFile', {
|
|
188
|
-
scriptName,
|
|
189
|
-
filePath: relativePath,
|
|
190
|
-
});
|
|
191
|
-
})
|
|
192
|
-
.on('unlinkDir', (filePath) => {
|
|
193
|
-
const relativePath = path_1.default.relative(scriptDir, filePath);
|
|
194
|
-
socket.emit('deleteFile', {
|
|
195
|
-
scriptName,
|
|
196
|
-
filePath: relativePath,
|
|
197
|
-
});
|
|
198
|
-
})
|
|
199
|
-
.on('error', error => console.log(chalk_1.default.bgRed(`Watcher error: ${error}`)))
|
|
200
|
-
.on('ready', () => console.log(`[${chalk_1.default.bold.blue(scriptName)}] Watching files for changes`));
|
|
201
|
-
};
|
|
202
|
-
socket.on('syncScriptFromClient', (data, ack) => __awaiter(this, void 0, void 0, function* () {
|
|
203
|
-
try {
|
|
204
|
-
if (data.scriptName === scriptName) {
|
|
205
|
-
ack({
|
|
206
|
-
error: `Script [${data.scriptName}] is already initialized`,
|
|
207
|
-
});
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
if (scriptName != null) {
|
|
211
|
-
console.log(`Received new script, closing previous watcher`);
|
|
212
|
-
watcher === null || watcher === void 0 ? void 0 : watcher.close();
|
|
213
|
-
watcher = null;
|
|
214
|
-
}
|
|
215
|
-
yield writeDtsFiles({
|
|
216
|
-
"global.d.ts": data["global.d.ts"],
|
|
217
|
-
"scripting.d.ts": data["scripting.d.ts"],
|
|
218
|
-
});
|
|
219
|
-
scriptName = data.scriptName;
|
|
220
|
-
const scriptDir = getPath(data.scriptName);
|
|
221
|
-
const tsconfig = require(getPath('tsconfig.json'));
|
|
222
|
-
if (!tsconfig.include.includes(scriptName)) {
|
|
223
|
-
tsconfig.include.push(scriptName);
|
|
224
|
-
yield fs_1.default.promises.writeFile(getPath('tsconfig.json'), JSON.stringify(tsconfig, null, 2));
|
|
225
|
-
}
|
|
226
|
-
ensureDirectoryExistence(scriptDir);
|
|
227
|
-
yield Promise.all(Object.entries(data.scriptFiles).map((_a) => __awaiter(this, [_a], void 0, function* ([filename, content]) {
|
|
228
|
-
const filePath = path_1.default.join(scriptDir, filename);
|
|
229
|
-
ensureDirectoryExistence(filePath);
|
|
230
|
-
yield fs_1.default.promises.writeFile(filePath, content);
|
|
231
|
-
})));
|
|
232
|
-
console.log(chalk_1.default.green('global.d.ts and scripting.d.ts and other script files saved.'));
|
|
233
|
-
createWatcher(data.scriptName);
|
|
234
|
-
ack({
|
|
235
|
-
success: true,
|
|
236
|
-
});
|
|
237
|
-
// Open the entry file in VSCode after a delay
|
|
238
|
-
// to ensure the response is sent. Because the open file command
|
|
239
|
-
// may suspend the response and cause the client to not receive the ack.
|
|
240
|
-
setTimeout(() => {
|
|
241
|
-
const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
|
|
242
|
-
tryOpenFileInVSCode(entryFilePath);
|
|
243
|
-
}, 1000);
|
|
244
|
-
}
|
|
245
|
-
catch (e) {
|
|
246
|
-
console.log(chalk_1.default.red(`Error: ${e}`));
|
|
247
|
-
ack({
|
|
248
|
-
error: `Failed to init and sync script files.\n${e}`,
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
}));
|
|
252
|
-
socket.on("syncScriptFromServer", (data, ack) => __awaiter(this, void 0, void 0, function* () {
|
|
253
|
-
try {
|
|
254
|
-
if (scriptName === data.scriptName) {
|
|
255
|
-
ack({
|
|
256
|
-
error: `Script ${data.scriptName} is already initialized`,
|
|
257
|
-
});
|
|
258
|
-
return;
|
|
259
|
-
}
|
|
260
|
-
if (scriptName != null) {
|
|
261
|
-
console.log(`Received new script [${chalk_1.default.bold.blue(data.scriptName)}], closing previous watcher`);
|
|
262
|
-
watcher === null || watcher === void 0 ? void 0 : watcher.close();
|
|
263
|
-
watcher = null;
|
|
264
|
-
}
|
|
265
|
-
yield writeDtsFiles({
|
|
266
|
-
"global.d.ts": data["global.d.ts"],
|
|
267
|
-
"scripting.d.ts": data["scripting.d.ts"],
|
|
268
|
-
});
|
|
269
|
-
const scriptDir = getPath(data.scriptName);
|
|
270
|
-
if (!fs_1.default.existsSync(scriptDir)) {
|
|
271
|
-
ack({
|
|
272
|
-
error: `Script directory ${scriptDir} does not exist`,
|
|
273
|
-
});
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
const scriptFiles = {};
|
|
277
|
-
const readDir = (dir) => __awaiter(this, void 0, void 0, function* () {
|
|
278
|
-
const files = yield fs_1.default.promises.readdir(dir, { withFileTypes: true });
|
|
279
|
-
yield Promise.all(files.map((file) => __awaiter(this, void 0, void 0, function* () {
|
|
280
|
-
if (file.isFile()) {
|
|
281
|
-
const filePath = path_1.default.join(dir, file.name);
|
|
282
|
-
const relativePath = path_1.default.relative(scriptDir, filePath);
|
|
283
|
-
const content = yield fs_1.default.promises.readFile(filePath, 'utf-8').catch((err) => {
|
|
284
|
-
console.log(chalk_1.default.red(`Error reading file: ${err}`));
|
|
285
|
-
});
|
|
286
|
-
if (typeof content === "string") {
|
|
287
|
-
scriptFiles[relativePath] = {
|
|
288
|
-
hash: md5(content),
|
|
289
|
-
content,
|
|
290
|
-
};
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
else if (file.isDirectory()) {
|
|
294
|
-
yield readDir(path_1.default.join(dir, file.name));
|
|
295
|
-
}
|
|
296
|
-
})));
|
|
297
|
-
});
|
|
298
|
-
yield readDir(scriptDir);
|
|
299
|
-
scriptName = data.scriptName;
|
|
300
|
-
ack({
|
|
301
|
-
scriptFiles,
|
|
302
|
-
});
|
|
303
|
-
createWatcher(data.scriptName);
|
|
304
|
-
// Open the entry file in VSCode after a delay
|
|
305
|
-
// to ensure the response is sent. Because the open file command
|
|
306
|
-
// may suspend the response and cause the client to not receive the ack.
|
|
307
|
-
setTimeout(() => {
|
|
308
|
-
const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
|
|
309
|
-
tryOpenFileInVSCode(entryFilePath);
|
|
310
|
-
}, 1000);
|
|
311
|
-
}
|
|
312
|
-
catch (e) {
|
|
313
|
-
ack({
|
|
314
|
-
error: `Failed to init and pull script files.\n${e}`,
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
}));
|
|
318
|
-
socket.on('stopDebugScript', (data, ack) => {
|
|
319
|
-
if (data === scriptName) {
|
|
320
|
-
console.log(`[${chalk_1.default.bold.blue(scriptName)}] Stop debug script`);
|
|
321
|
-
watcher === null || watcher === void 0 ? void 0 : watcher.close();
|
|
322
|
-
watcher = null;
|
|
323
|
-
scriptName = "";
|
|
324
|
-
ack(true);
|
|
325
|
-
}
|
|
326
|
-
else {
|
|
327
|
-
ack(false);
|
|
328
|
-
}
|
|
329
|
-
});
|
|
330
|
-
socket.on("getFileContent", (data, ack) => __awaiter(this, void 0, void 0, function* () {
|
|
331
|
-
if (scriptName !== data.scriptName) {
|
|
332
|
-
ack({
|
|
333
|
-
error: `Script ${data.scriptName} is not initialized`,
|
|
334
|
-
});
|
|
335
|
-
return;
|
|
336
|
-
}
|
|
337
|
-
const filePath = path_1.default.join(getPath(data.scriptName), data.relativePath);
|
|
338
|
-
try {
|
|
339
|
-
const content = yield fs_1.default.promises.readFile(filePath, 'utf-8');
|
|
340
|
-
ack({
|
|
341
|
-
content,
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
catch (e) {
|
|
345
|
-
ack({
|
|
346
|
-
error: `Failed to get file content.\n${e}`,
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
}));
|
|
350
|
-
socket.on("log", (data) => {
|
|
351
|
-
if (data.scriptName !== scriptName) {
|
|
352
|
-
return;
|
|
353
|
-
}
|
|
354
|
-
console.log(`[${chalk_1.default.bold.blue(scriptName)}][${data.isError ? chalk_1.default.red("ERROR") : "LOG"}][${new Date().toLocaleTimeString()}] ${data.content}`);
|
|
355
|
-
});
|
|
356
|
-
socket.on("openFile", (data) => __awaiter(this, void 0, void 0, function* () {
|
|
357
|
-
if (data.scriptName !== scriptName) {
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
360
|
-
const filePath = path_1.default.join(getPath(scriptName), data.relativePath);
|
|
361
|
-
if (fs_1.default.existsSync(filePath)) {
|
|
362
|
-
tryOpenFileInVSCode(filePath);
|
|
363
|
-
}
|
|
364
|
-
}));
|
|
365
|
-
socket.on('disconnect', () => {
|
|
366
|
-
console.log(chalk_1.default.gray(`Client [${socket.id}] disconnected`));
|
|
367
|
-
watcher === null || watcher === void 0 ? void 0 : watcher.close();
|
|
368
|
-
watcher = null;
|
|
369
|
-
});
|
|
370
|
-
const serverScriptNames = fs_1.default.readdirSync(process.cwd())
|
|
371
|
-
.filter((filename) => fs_1.default.statSync(filename).isDirectory()
|
|
372
|
-
&&
|
|
373
|
-
filename !== '.vscode');
|
|
374
|
-
// console.log(serverScriptNames)
|
|
375
|
-
socket.emit("serverScripts", serverScriptNames);
|
|
376
|
-
});
|
|
377
|
-
server.listen(PORT, () => {
|
|
378
|
-
createTsConfig(); // Create tsconfig.json when the server starts
|
|
379
|
-
createVSCodeSettings(); // Create vscode settings.json when the server starts
|
|
380
|
-
const ipAddress = ip_1.default.address();
|
|
381
|
-
const address = `http://${ipAddress}:${PORT}`;
|
|
382
|
-
console.log(`Server listening on ${chalk_1.default.bold.blue(address)}\nYou can select this server in the Scripting app to connect.\n`);
|
|
383
|
-
console.log(`Alternatively, you can ${chalk_1.default.green.bold("use the Scripting app to scan")} the QR code and connect: `);
|
|
384
|
-
qrcode_terminal_1.default.generate(address, { small: true });
|
|
385
|
-
bonjour.publish({ name: 'Scripting-service', type: 'http', port: PORT });
|
|
386
|
-
process.on('SIGINT', () => {
|
|
387
|
-
bonjour.unpublishAll(() => {
|
|
388
|
-
console.log('Server stopped');
|
|
389
|
-
process.exit();
|
|
390
|
-
});
|
|
391
|
-
});
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
32
|
// if (require.main === module) {
|
|
395
33
|
// startServer();
|
|
396
34
|
// }
|
|
@@ -0,0 +1,48 @@
|
|
|
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.startServer = startServer;
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const http_1 = __importDefault(require("http"));
|
|
9
|
+
const socket_io_1 = require("socket.io");
|
|
10
|
+
const ip_1 = __importDefault(require("ip"));
|
|
11
|
+
const bonjour_1 = __importDefault(require("bonjour"));
|
|
12
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
13
|
+
const qrcode_terminal_1 = __importDefault(require("qrcode-terminal"));
|
|
14
|
+
const util_1 = require("./util");
|
|
15
|
+
const controller_1 = require("./controller");
|
|
16
|
+
const http_router_1 = require("./http_router");
|
|
17
|
+
const app = (0, express_1.default)();
|
|
18
|
+
const server = http_1.default.createServer(app);
|
|
19
|
+
const io = new socket_io_1.Server(server);
|
|
20
|
+
const bonjour = (0, bonjour_1.default)();
|
|
21
|
+
function startServer(port) {
|
|
22
|
+
const PORT = port !== null && port !== void 0 ? port : 3000;
|
|
23
|
+
(0, http_router_1.initHttpRouter)(app);
|
|
24
|
+
io.on('connection', (socket) => {
|
|
25
|
+
controller_1.Controller.create(socket);
|
|
26
|
+
console.log(chalk_1.default.blue(`Client [${socket.id}] connected`));
|
|
27
|
+
});
|
|
28
|
+
server.listen(PORT, () => {
|
|
29
|
+
(0, util_1.createTsConfig)(); // Create tsconfig.json when the server starts
|
|
30
|
+
(0, util_1.createVSCodeSettings)(); // Create vscode settings.json when the server starts
|
|
31
|
+
const ipAddress = ip_1.default.address();
|
|
32
|
+
const address = `http://${ipAddress}:${PORT}`;
|
|
33
|
+
console.log(`Server listening on ${chalk_1.default.bold.blue(address)}\nYou can select this server in the Scripting app to connect.\n`);
|
|
34
|
+
console.log(`Alternatively, you can ${chalk_1.default.green.bold("use the Scripting app to scan")} the QR code and connect: `);
|
|
35
|
+
qrcode_terminal_1.default.generate(address, { small: true });
|
|
36
|
+
bonjour.publish({
|
|
37
|
+
name: 'Scripting-service',
|
|
38
|
+
type: 'http',
|
|
39
|
+
port: PORT
|
|
40
|
+
});
|
|
41
|
+
process.on('SIGINT', () => {
|
|
42
|
+
bonjour.unpublishAll(() => {
|
|
43
|
+
console.log('Server stopped');
|
|
44
|
+
process.exit();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
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.md5 = md5;
|
|
16
|
+
exports.getPath = getPath;
|
|
17
|
+
exports.tryOpenFileInVSCode = tryOpenFileInVSCode;
|
|
18
|
+
exports.createTsConfig = createTsConfig;
|
|
19
|
+
exports.createVSCodeSettings = createVSCodeSettings;
|
|
20
|
+
exports.ensureDirectoryExistence = ensureDirectoryExistence;
|
|
21
|
+
exports.writeDtsFiles = writeDtsFiles;
|
|
22
|
+
exports.getRelativePath = getRelativePath;
|
|
23
|
+
const path_1 = __importDefault(require("path"));
|
|
24
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
25
|
+
const os_1 = __importDefault(require("os"));
|
|
26
|
+
const child_process_1 = __importDefault(require("child_process"));
|
|
27
|
+
const fs_1 = __importDefault(require("fs"));
|
|
28
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
29
|
+
function md5(content) {
|
|
30
|
+
return crypto_1.default
|
|
31
|
+
.createHash('md5')
|
|
32
|
+
.update(content)
|
|
33
|
+
.digest('hex');
|
|
34
|
+
}
|
|
35
|
+
function getPath(filename) {
|
|
36
|
+
return path_1.default.join(process.cwd(), filename);
|
|
37
|
+
}
|
|
38
|
+
function tryOpenFileInVSCode(filePath) {
|
|
39
|
+
let cmd = `code "${filePath}"`;
|
|
40
|
+
if (os_1.default.platform() === "win32") {
|
|
41
|
+
cmd = `cmd.exe /c ${cmd}`;
|
|
42
|
+
}
|
|
43
|
+
else if (os_1.default.platform() === "linux") {
|
|
44
|
+
const shell = process.env["SHELL"];
|
|
45
|
+
cmd = `${shell} -c ${cmd}`;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
cmd = `"/Applications/Visual Studio Code.app/Contents/MacOS/Electron" "${filePath}"`;
|
|
49
|
+
}
|
|
50
|
+
child_process_1.default.execSync(cmd);
|
|
51
|
+
}
|
|
52
|
+
// Function to create tsconfig.json
|
|
53
|
+
function createTsConfig() {
|
|
54
|
+
const tsconfigContent = `{
|
|
55
|
+
"compilerOptions": {
|
|
56
|
+
"target": "ESNext",
|
|
57
|
+
"lib": [
|
|
58
|
+
"esnext"
|
|
59
|
+
],
|
|
60
|
+
"allowJs": true,
|
|
61
|
+
"strict": true,
|
|
62
|
+
"esModuleInterop": true,
|
|
63
|
+
"allowSyntheticDefaultImports": true,
|
|
64
|
+
"forceConsistentCasingInFileNames": true,
|
|
65
|
+
"noFallthroughCasesInSwitch": true,
|
|
66
|
+
"module": "CommonJS",
|
|
67
|
+
"resolveJsonModule": true,
|
|
68
|
+
"skipLibCheck": true,
|
|
69
|
+
"jsx": "react",
|
|
70
|
+
"jsxFactory": "createElement",
|
|
71
|
+
"jsxFragmentFactory": "Fragment",
|
|
72
|
+
"paths": {
|
|
73
|
+
"scripting": [
|
|
74
|
+
"./dts/scripting.d.ts"
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"include": [
|
|
79
|
+
"./dts/global.d.ts"
|
|
80
|
+
]
|
|
81
|
+
}`;
|
|
82
|
+
const filePath = getPath('tsconfig.json');
|
|
83
|
+
ensureDirectoryExistence(filePath);
|
|
84
|
+
if (!fs_1.default.existsSync(filePath)) {
|
|
85
|
+
fs_1.default.writeFileSync(filePath, tsconfigContent);
|
|
86
|
+
console.log(chalk_1.default.green('tsconfig.json created.'));
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
console.log(chalk_1.default.gray('tsconfig.json already exists.'));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function createVSCodeSettings() {
|
|
93
|
+
const settingsContent = `{
|
|
94
|
+
"typescript.format.semicolons": "remove",
|
|
95
|
+
"typescript.preferences.jsxAttributeCompletionStyle": "braces",
|
|
96
|
+
}`;
|
|
97
|
+
const filePath = getPath('.vscode/settings.json');
|
|
98
|
+
ensureDirectoryExistence(filePath);
|
|
99
|
+
if (!fs_1.default.existsSync(filePath)) {
|
|
100
|
+
fs_1.default.writeFileSync(filePath, settingsContent);
|
|
101
|
+
console.log(chalk_1.default.green('.vscode/settings.json created.'));
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
console.log(chalk_1.default.gray('.vscode/settings.json already exists.'));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function ensureDirectoryExistence(filePath) {
|
|
108
|
+
const dirname = path_1.default.dirname(filePath);
|
|
109
|
+
if (!fs_1.default.existsSync(dirname)) {
|
|
110
|
+
fs_1.default.mkdirSync(dirname, { recursive: true });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function writeDtsFiles(files) {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
yield Promise.all(Object.entries(files).map((_a) => __awaiter(this, [_a], void 0, function* ([filename, content]) {
|
|
116
|
+
const filePath = getPath(`dts/${filename}`);
|
|
117
|
+
ensureDirectoryExistence(filePath);
|
|
118
|
+
yield fs_1.default
|
|
119
|
+
.promises
|
|
120
|
+
.writeFile(filePath, content)
|
|
121
|
+
.catch((err) => {
|
|
122
|
+
console.log(chalk_1.default.red(`Error writing file: ${err}`));
|
|
123
|
+
});
|
|
124
|
+
})));
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function getRelativePath(from, to) {
|
|
128
|
+
const relativePath = path_1.default.relative(from, to);
|
|
129
|
+
return relativePath.replace("\\", "/");
|
|
130
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "scripting-cli",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
4
4
|
"description": "This plugin integrates the desktop editor synchronization feature of the Scripting app.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"scripting-cli": "
|
|
7
|
+
"scripting-cli": "dist/index.js"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "tsc",
|