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