scripting-cli 1.1.7 → 1.1.9

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.
@@ -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,6 +10,7 @@ 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 {
13
+ static instances = new Map();
22
14
  static create(socket) {
23
15
  const ctrl = new Controller(socket);
24
16
  Controller.instances.set(socket.id, ctrl);
@@ -27,177 +19,10 @@ class Controller {
27
19
  static get(socketId) {
28
20
  return Controller.instances.get(socketId);
29
21
  }
22
+ watcher = null;
23
+ scriptName = null;
24
+ socket = null;
30
25
  constructor(socket) {
31
- this.watcher = null;
32
- this.scriptName = null;
33
- this.socket = null;
34
- this.handleSyncScriptFromClient = (data, ack) => __awaiter(this, void 0, void 0, function* () {
35
- var _a;
36
- try {
37
- if (data.scriptName === this.scriptName) {
38
- ack({
39
- error: `Script [${data.scriptName}] is already initialized`,
40
- });
41
- return;
42
- }
43
- if (this.scriptName != null) {
44
- console.log(`Received new script, closing previous watcher`);
45
- (_a = this.watcher) === null || _a === void 0 ? void 0 : _a.close();
46
- this.watcher = null;
47
- }
48
- yield (0, util_1.writeDtsFiles)({
49
- "global.d.ts": data["global.d.ts"],
50
- "scripting.d.ts": data["scripting.d.ts"],
51
- });
52
- this.scriptName = data.scriptName;
53
- const scriptName = data.scriptName;
54
- const scriptDir = (0, util_1.getPath)(data.scriptName);
55
- const tsconfig = require((0, util_1.getPath)('tsconfig.json'));
56
- if (!tsconfig.include.includes(scriptName)) {
57
- tsconfig.include.push(scriptName);
58
- yield fs_1.default.promises.writeFile((0, util_1.getPath)('tsconfig.json'), JSON.stringify(tsconfig, null, 2));
59
- }
60
- (0, util_1.ensureDirectoryExistence)(scriptDir);
61
- yield Promise.all(Object.entries(data.scriptFiles).map((_a) => __awaiter(this, [_a], void 0, function* ([filename, content]) {
62
- const filePath = path_1.default.join(scriptDir, filename);
63
- (0, util_1.ensureDirectoryExistence)(filePath);
64
- yield fs_1.default.promises.writeFile(filePath, content);
65
- })));
66
- console.log(chalk_1.default.green('global.d.ts and scripting.d.ts and other script files saved.'));
67
- this.createWatcher(data.scriptName);
68
- ack({
69
- success: true,
70
- });
71
- // Open the entry file in VSCode after a delay
72
- // to ensure the response is sent. Because the open file command
73
- // may suspend the response and cause the client to not receive the ack.
74
- setTimeout(() => {
75
- const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
76
- (0, util_1.tryOpenFileInVSCode)(entryFilePath);
77
- }, 1000);
78
- }
79
- catch (e) {
80
- console.log(chalk_1.default.red(`Error: ${e}`));
81
- ack({
82
- error: `Failed to init and sync script files.\n${e}`,
83
- });
84
- }
85
- });
86
- this.handleSyncScriptFromServer = (data, ack) => __awaiter(this, void 0, void 0, function* () {
87
- var _a;
88
- try {
89
- if (this.scriptName === data.scriptName) {
90
- ack({
91
- error: `Script ${data.scriptName} is already initialized`,
92
- });
93
- return;
94
- }
95
- if (this.scriptName != null) {
96
- console.log(`Received new script [${chalk_1.default.bold.blue(data.scriptName)}], closing previous watcher`);
97
- (_a = this.watcher) === null || _a === void 0 ? void 0 : _a.close();
98
- this.watcher = null;
99
- }
100
- yield (0, util_1.writeDtsFiles)({
101
- "global.d.ts": data["global.d.ts"],
102
- "scripting.d.ts": data["scripting.d.ts"],
103
- });
104
- const scriptDir = (0, util_1.getPath)(data.scriptName);
105
- if (!fs_1.default.existsSync(scriptDir)) {
106
- ack({
107
- error: `Script directory ${scriptDir} does not exist`,
108
- });
109
- return;
110
- }
111
- const scriptFiles = {};
112
- const readDir = (dir) => __awaiter(this, void 0, void 0, function* () {
113
- const files = yield fs_1.default.promises.readdir(dir, { withFileTypes: true });
114
- yield Promise.all(files.map((file) => __awaiter(this, void 0, void 0, function* () {
115
- if (file.isFile()) {
116
- const filePath = path_1.default.join(dir, file.name);
117
- const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
118
- const content = yield fs_1.default.promises.readFile(filePath, 'utf-8').catch((err) => {
119
- console.log(chalk_1.default.red(`Error reading file: ${err}`));
120
- });
121
- if (typeof content === "string") {
122
- scriptFiles[relativePath] = {
123
- hash: (0, util_1.md5)(content),
124
- content,
125
- };
126
- }
127
- }
128
- else if (file.isDirectory()) {
129
- yield readDir(path_1.default.join(dir, file.name));
130
- }
131
- })));
132
- });
133
- yield readDir(scriptDir);
134
- this.scriptName = data.scriptName;
135
- ack({
136
- scriptFiles,
137
- });
138
- this.createWatcher(data.scriptName);
139
- // Open the entry file in VSCode after a delay
140
- // to ensure the response is sent. Because the open file command
141
- // may suspend the response and cause the client to not receive the ack.
142
- setTimeout(() => {
143
- const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
144
- (0, util_1.tryOpenFileInVSCode)(entryFilePath);
145
- }, 1000);
146
- }
147
- catch (e) {
148
- ack({
149
- error: `Failed to init and pull script files.\n${e}`,
150
- });
151
- }
152
- });
153
- this.handleLog = (data) => {
154
- if (data.scriptName !== this.scriptName) {
155
- return;
156
- }
157
- console.log(`[${chalk_1.default.bold.blue(this.scriptName)}][${data.isError ? chalk_1.default.red("ERROR") : "LOG"}][${new Date().toLocaleTimeString()}] ${data.content}`);
158
- };
159
- this.handleOpenFile = (data) => __awaiter(this, void 0, void 0, function* () {
160
- if (data.scriptName !== this.scriptName) {
161
- return;
162
- }
163
- const filePath = path_1.default.join((0, util_1.getPath)(this.scriptName), data.relativePath);
164
- if (fs_1.default.existsSync(filePath)) {
165
- (0, util_1.tryOpenFileInVSCode)(filePath);
166
- }
167
- });
168
- this.handleStopDebugScript = (data, ack) => {
169
- var _a;
170
- if (data === this.scriptName) {
171
- console.log(`[${chalk_1.default.bold.blue(this.scriptName)}] Stop debug script`);
172
- (_a = this.watcher) === null || _a === void 0 ? void 0 : _a.close();
173
- this.watcher = null;
174
- this.scriptName = null;
175
- ack(true);
176
- }
177
- else {
178
- ack(false);
179
- }
180
- };
181
- this.handleGetFileContent = (data, ack) => __awaiter(this, void 0, void 0, function* () {
182
- if (this.scriptName !== data.scriptName) {
183
- ack({
184
- error: `Script ${data.scriptName} is not initialized`,
185
- });
186
- return;
187
- }
188
- const filePath = path_1.default.join((0, util_1.getPath)(data.scriptName), data.relativePath);
189
- try {
190
- const content = yield fs_1.default.promises.readFile(filePath, 'utf-8');
191
- ack({
192
- content,
193
- });
194
- }
195
- catch (e) {
196
- ack({
197
- error: `Failed to get file content.\n${e}`,
198
- });
199
- }
200
- });
201
26
  this.socket = socket;
202
27
  socket.emit("socketId", socket.id);
203
28
  socket.on('syncScriptFromClient', this.handleSyncScriptFromClient);
@@ -207,9 +32,8 @@ class Controller {
207
32
  socket.on("log", this.handleLog);
208
33
  socket.on("openFile", this.handleOpenFile);
209
34
  socket.on('disconnect', (reason) => {
210
- var _a;
211
35
  console.log(chalk_1.default.gray(`Client [${socket.id}] disconnected: ${reason}`));
212
- (_a = this.watcher) === null || _a === void 0 ? void 0 : _a.close();
36
+ this.watcher?.close();
213
37
  this.watcher = null;
214
38
  this.scriptName = null;
215
39
  Controller.instances.delete(socket.id);
@@ -234,7 +58,6 @@ class Controller {
234
58
  // send file content md5 hash to client
235
59
  // const content = fs.readFileSync(filePath, 'utf-8');
236
60
  fs_1.default.readFile(filePath, 'utf-8', (err, content) => {
237
- var _a;
238
61
  if (err) {
239
62
  console.log(chalk_1.default.red(`Error reading file: ${err}`));
240
63
  return;
@@ -242,7 +65,7 @@ class Controller {
242
65
  const hash = (0, util_1.md5)(content);
243
66
  const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
244
67
  console.log(`[${chalk_1.default.bold.blue(scriptName)}] File changed: [${hash}]${relativePath}`);
245
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit('fileChange', {
68
+ this.socket?.emit('fileChange', {
246
69
  scriptName,
247
70
  filePath: relativePath,
248
71
  hash,
@@ -252,17 +75,15 @@ class Controller {
252
75
  // TODO, add support for other file types
253
76
  })
254
77
  .on('unlink', (filePath) => {
255
- var _a;
256
78
  const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
257
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit('deleteFile', {
79
+ this.socket?.emit('deleteFile', {
258
80
  scriptName,
259
81
  filePath: relativePath,
260
82
  });
261
83
  })
262
84
  .on('unlinkDir', (filePath) => {
263
- var _a;
264
85
  const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
265
- (_a = this.socket) === null || _a === void 0 ? void 0 : _a.emit('deleteFile', {
86
+ this.socket?.emit('deleteFile', {
266
87
  scriptName,
267
88
  filePath: relativePath,
268
89
  });
@@ -270,6 +91,169 @@ class Controller {
270
91
  .on('error', error => console.log(chalk_1.default.bgRed(`Watcher error: ${error}`)))
271
92
  .on('ready', () => console.log(`[${chalk_1.default.bold.blue(scriptName)}] Watching files for changes`));
272
93
  }
94
+ handleSyncScriptFromClient = async (data, ack) => {
95
+ try {
96
+ if (data.scriptName === this.scriptName) {
97
+ ack({
98
+ error: `Script [${data.scriptName}] is already initialized`,
99
+ });
100
+ return;
101
+ }
102
+ if (this.scriptName != null) {
103
+ console.log(`Received new script, closing previous watcher`);
104
+ this.watcher?.close();
105
+ this.watcher = null;
106
+ }
107
+ await (0, util_1.writeDtsFiles)({
108
+ "global.d.ts": data["global.d.ts"],
109
+ "scripting.d.ts": data["scripting.d.ts"],
110
+ });
111
+ this.scriptName = data.scriptName;
112
+ const scriptName = data.scriptName;
113
+ const scriptDir = (0, util_1.getPath)(data.scriptName);
114
+ const tsconfig = require((0, util_1.getPath)('tsconfig.json'));
115
+ if (!tsconfig.include.includes(scriptName)) {
116
+ tsconfig.include.push(scriptName);
117
+ await fs_1.default.promises.writeFile((0, util_1.getPath)('tsconfig.json'), JSON.stringify(tsconfig, null, 2));
118
+ }
119
+ (0, util_1.ensureDirectoryExistence)(scriptDir);
120
+ await Promise.all(Object.entries(data.scriptFiles).map(async ([filename, content]) => {
121
+ const filePath = path_1.default.join(scriptDir, filename);
122
+ (0, util_1.ensureDirectoryExistence)(filePath);
123
+ await fs_1.default.promises.writeFile(filePath, content);
124
+ }));
125
+ console.log(chalk_1.default.green('global.d.ts and scripting.d.ts and other script files saved.'));
126
+ this.createWatcher(data.scriptName);
127
+ ack({
128
+ success: true,
129
+ });
130
+ // Open the entry file in VSCode after a delay
131
+ // to ensure the response is sent. Because the open file command
132
+ // may suspend the response and cause the client to not receive the ack.
133
+ setTimeout(() => {
134
+ const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
135
+ (0, util_1.tryOpenFileInVSCode)(entryFilePath);
136
+ }, 1000);
137
+ }
138
+ catch (e) {
139
+ console.log(chalk_1.default.red(`Error: ${e}`));
140
+ ack({
141
+ error: `Failed to init and sync script files.\n${e}`,
142
+ });
143
+ }
144
+ };
145
+ handleSyncScriptFromServer = async (data, ack) => {
146
+ try {
147
+ if (this.scriptName === data.scriptName) {
148
+ ack({
149
+ error: `Script ${data.scriptName} is already initialized`,
150
+ });
151
+ return;
152
+ }
153
+ if (this.scriptName != null) {
154
+ console.log(`Received new script [${chalk_1.default.bold.blue(data.scriptName)}], closing previous watcher`);
155
+ this.watcher?.close();
156
+ this.watcher = null;
157
+ }
158
+ await (0, util_1.writeDtsFiles)({
159
+ "global.d.ts": data["global.d.ts"],
160
+ "scripting.d.ts": data["scripting.d.ts"],
161
+ });
162
+ const scriptDir = (0, util_1.getPath)(data.scriptName);
163
+ if (!fs_1.default.existsSync(scriptDir)) {
164
+ ack({
165
+ error: `Script directory ${scriptDir} does not exist`,
166
+ });
167
+ return;
168
+ }
169
+ const scriptFiles = {};
170
+ const readDir = async (dir) => {
171
+ const files = await fs_1.default.promises.readdir(dir, { withFileTypes: true });
172
+ await Promise.all(files.map(async (file) => {
173
+ if (file.isFile()) {
174
+ const filePath = path_1.default.join(dir, file.name);
175
+ const relativePath = (0, util_1.getRelativePath)(scriptDir, filePath);
176
+ const content = await fs_1.default.promises.readFile(filePath, 'utf-8').catch((err) => {
177
+ console.log(chalk_1.default.red(`Error reading file: ${err}`));
178
+ });
179
+ if (typeof content === "string") {
180
+ scriptFiles[relativePath] = {
181
+ hash: (0, util_1.md5)(content),
182
+ content,
183
+ };
184
+ }
185
+ }
186
+ else if (file.isDirectory()) {
187
+ await readDir(path_1.default.join(dir, file.name));
188
+ }
189
+ }));
190
+ };
191
+ await readDir(scriptDir);
192
+ this.scriptName = data.scriptName;
193
+ ack({
194
+ scriptFiles,
195
+ });
196
+ this.createWatcher(data.scriptName);
197
+ // Open the entry file in VSCode after a delay
198
+ // to ensure the response is sent. Because the open file command
199
+ // may suspend the response and cause the client to not receive the ack.
200
+ setTimeout(() => {
201
+ const entryFilePath = path_1.default.join(scriptDir, "index.tsx");
202
+ (0, util_1.tryOpenFileInVSCode)(entryFilePath);
203
+ }, 1000);
204
+ }
205
+ catch (e) {
206
+ ack({
207
+ error: `Failed to init and pull script files.\n${e}`,
208
+ });
209
+ }
210
+ };
211
+ handleLog = (data) => {
212
+ if (data.scriptName !== this.scriptName) {
213
+ return;
214
+ }
215
+ console.log(`[${chalk_1.default.bold.blue(this.scriptName)}][${data.isError ? chalk_1.default.red("ERROR") : "LOG"}][${new Date().toLocaleTimeString()}] ${data.content}`);
216
+ };
217
+ handleOpenFile = async (data) => {
218
+ if (data.scriptName !== this.scriptName) {
219
+ return;
220
+ }
221
+ const filePath = path_1.default.join((0, util_1.getPath)(this.scriptName), data.relativePath);
222
+ if (fs_1.default.existsSync(filePath)) {
223
+ (0, util_1.tryOpenFileInVSCode)(filePath);
224
+ }
225
+ };
226
+ handleStopDebugScript = (data, ack) => {
227
+ if (data === this.scriptName) {
228
+ console.log(`[${chalk_1.default.bold.blue(this.scriptName)}] Stop debug script`);
229
+ this.watcher?.close();
230
+ this.watcher = null;
231
+ this.scriptName = null;
232
+ ack(true);
233
+ }
234
+ else {
235
+ ack(false);
236
+ }
237
+ };
238
+ handleGetFileContent = async (data, ack) => {
239
+ if (this.scriptName !== data.scriptName) {
240
+ ack({
241
+ error: `Script ${data.scriptName} is not initialized`,
242
+ });
243
+ return;
244
+ }
245
+ const filePath = path_1.default.join((0, util_1.getPath)(data.scriptName), data.relativePath);
246
+ try {
247
+ const content = await fs_1.default.promises.readFile(filePath, 'utf-8');
248
+ ack({
249
+ content,
250
+ });
251
+ }
252
+ catch (e) {
253
+ ack({
254
+ error: `Failed to get file content.\n${e}`,
255
+ });
256
+ }
257
+ };
273
258
  }
274
259
  exports.Controller = Controller;
275
- Controller.instances = new Map();
@@ -19,7 +19,7 @@ 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
21
  function startServer(port) {
22
- const PORT = port !== null && port !== void 0 ? port : 3000;
22
+ const PORT = port ?? 3000;
23
23
  (0, http_router_1.initHttpRouter)(app);
24
24
  io.on('connection', (socket) => {
25
25
  controller_1.Controller.create(socket);
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
- 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
- });
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.replace("\\", "/");
118
+ return relativePath.replaceAll("\\", "/");
130
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scripting-cli",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "This plugin integrates the desktop editor synchronization feature of the Scripting app.",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
package/src/util.ts CHANGED
@@ -114,5 +114,5 @@ export async function writeDtsFiles(files: Record<string, string>) {
114
114
 
115
115
  export function getRelativePath(from: string, to: string) {
116
116
  const relativePath = path.relative(from, to);
117
- return relativePath.replace("\\", "/");
118
- }
117
+ return relativePath.replaceAll("\\", "/");
118
+ }
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es2015",
3
+ "target": "ESNext",
4
4
  "module": "commonjs",
5
5
  "declaration": false,
6
6
  "outDir": "./dist",