@lvce-editor/file-system-process 2.0.0 → 2.2.0

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.
Files changed (2) hide show
  1. package/dist/index.js +348 -303
  2. package/package.json +1 -5
package/dist/index.js CHANGED
@@ -108,7 +108,6 @@ class VError extends Error {
108
108
  }
109
109
  }
110
110
 
111
- // @ts-ignore
112
111
  const getDisplayPath = path => {
113
112
  if (path === '') {
114
113
  return '<empty string>';
@@ -116,10 +115,8 @@ const getDisplayPath = path => {
116
115
  return path;
117
116
  };
118
117
  class FileNotFoundError extends VError {
119
- // @ts-ignore
120
118
  constructor(path) {
121
119
  super(`File not found: '${getDisplayPath(path)}'`);
122
- // @ts-ignore
123
120
  this.code = ENOENT;
124
121
  }
125
122
  }
@@ -133,10 +130,6 @@ const Socket = 8;
133
130
  const Symlink = 9;
134
131
  const Unknown = 12;
135
132
 
136
- /**
137
- * @param {import('fs').Dirent|import('fs').StatsBase} dirent
138
- */
139
- // @ts-ignore
140
133
  const getDirentType = dirent => {
141
134
  if (dirent.isFile()) {
142
135
  return File;
@@ -200,13 +193,259 @@ const isEnoentError = error => {
200
193
  return isEnoentErrorLinux(error) || isEnoentErrorWindows(error);
201
194
  };
202
195
 
196
+ const rpcs = Object.create(null);
197
+ const set$f = (id, rpc) => {
198
+ rpcs[id] = rpc;
199
+ };
200
+ const get$1 = id => {
201
+ return rpcs[id];
202
+ };
203
+
204
+ /* eslint-disable @typescript-eslint/explicit-function-return-type */
205
+
206
+ const create$6 = rpcId => {
207
+ return {
208
+ // @ts-ignore
209
+ invoke(method, ...params) {
210
+ const rpc = get$1(rpcId);
211
+ // @ts-ignore
212
+ return rpc.invoke(method, ...params);
213
+ },
214
+ // @ts-ignore
215
+ invokeAndTransfer(method, ...params) {
216
+ const rpc = get$1(rpcId);
217
+ // @ts-ignore
218
+ return rpc.invokeAndTransfer(method, ...params);
219
+ },
220
+ set(rpc) {
221
+ set$f(rpcId, rpc);
222
+ },
223
+ async dispose() {
224
+ const rpc = get$1(rpcId);
225
+ await rpc.dispose();
226
+ }
227
+ };
228
+ };
229
+ const MainProcess$1 = -5;
230
+ const {
231
+ invoke: invoke$6,
232
+ set: set$6} = create$6(MainProcess$1);
233
+ const MainProcess = {
234
+ __proto__: null,
235
+ invoke: invoke$6,
236
+ set: set$6
237
+ };
238
+
239
+ const {
240
+ invoke: invoke$1,
241
+ set: set$1
242
+ } = MainProcess;
243
+
244
+ const trash$2 = async path => {
245
+ if (!existsSync(path)) {
246
+ return;
247
+ }
248
+ // @ts-ignore
249
+ await invoke$1('Trash.trash', path);
250
+ };
251
+
252
+ // @ts-ignore
253
+ const trash$1 = async path => {
254
+ await _trash(path);
255
+ };
256
+
257
+ const getTrashFn = isElectron => {
258
+ if (isElectron) {
259
+ return trash$2;
260
+ }
261
+ return trash$1;
262
+ };
263
+
203
264
  const getElectronRunAsNode = () => {
204
265
  return process.env.ELECTRON_RUN_AS_NODE;
205
266
  };
206
- process;
207
267
 
208
268
  const isElectron = Boolean(getElectronRunAsNode()) || 'electron' in process.versions;
209
269
 
270
+ const trash = async path => {
271
+ try {
272
+ const fn = getTrashFn(isElectron);
273
+ await fn(path);
274
+ } catch (error) {
275
+ throw new VError(error, 'Failed to move item to trash');
276
+ }
277
+ };
278
+
279
+ // TODO lazyload chokidar and trash (but doesn't work currently because of bug with jest)
280
+ const assertUri = uri => {
281
+ if (!uri.startsWith('file://')) {
282
+ throw new Error(`path must be a valid file uri`);
283
+ }
284
+ };
285
+ const copy = async (sourceUri, targetUri) => {
286
+ try {
287
+ assertUri(sourceUri);
288
+ assertUri(targetUri);
289
+ const source = fileURLToPath(sourceUri);
290
+ const target = fileURLToPath(targetUri);
291
+ await fs.cp(source, target, {
292
+ recursive: true
293
+ });
294
+ } catch (error) {
295
+ if (error && error.message && error.message.startsWith('Invalid src or dest: cp returned EINVAL (src and dest cannot be the same)')) {
296
+ throw new VError(`Failed to copy "${sourceUri}" to "${targetUri}": src and dest cannot be the same`);
297
+ }
298
+ throw new VError(error, `Failed to copy "${sourceUri}" to "${targetUri}"`);
299
+ }
300
+ };
301
+ const readFile = async (uri, encoding = Utf8) => {
302
+ try {
303
+ string(uri);
304
+ assertUri(uri);
305
+ const path = fileURLToPath(uri);
306
+ const content = await fs.readFile(path, encoding);
307
+ return content;
308
+ } catch (error) {
309
+ if (isEnoentError(error)) {
310
+ throw new FileNotFoundError(uri);
311
+ }
312
+ throw new VError(error, `Failed to read file "${uri}"`);
313
+ }
314
+ };
315
+ const writeFile = async (uri, content, encoding = Utf8) => {
316
+ try {
317
+ assertUri(uri);
318
+ string(uri);
319
+ string(content);
320
+ const path = fileURLToPath(uri);
321
+ // queue would be more correct for concurrent writes but also slower
322
+ // Queue.add(`writeFile/${path}`, () =>
323
+ await fs.writeFile(path, content, encoding);
324
+ } catch (error) {
325
+ if (isEnoentError(error)) {
326
+ throw new FileNotFoundError(uri);
327
+ }
328
+ throw new VError(error, `Failed to write to file "${uri}"`);
329
+ }
330
+ };
331
+ const isOkayToRemove = path => {
332
+ if (path === '/') {
333
+ return false;
334
+ }
335
+ if (path === '~') {
336
+ return false;
337
+ }
338
+ if (path === os.homedir()) {
339
+ return false;
340
+ }
341
+ return true;
342
+ };
343
+ const remove$1 = async uri => {
344
+ assertUri(uri);
345
+ const path = fileURLToPath(uri);
346
+ if (!isOkayToRemove(path)) {
347
+ console.warn('not removing path');
348
+ return;
349
+ }
350
+
351
+ // TODO lazyload trash (doesn't work currently because of bug with jest)
352
+ // const { trash } = await import('../../wrap/trash.js')
353
+ try {
354
+ await trash(path);
355
+ } catch (error) {
356
+ throw new VError(error, `Failed to remove "${uri}"`);
357
+ }
358
+ };
359
+ const toPrettyDirent = dirent => {
360
+ return {
361
+ name: dirent.name,
362
+ type: getDirentType(dirent)
363
+ };
364
+ };
365
+ const readDirWithFileTypes = async uri => {
366
+ try {
367
+ assertUri(uri);
368
+ const path = fileURLToPath(uri);
369
+ const dirents = await fs.readdir(path, {
370
+ withFileTypes: true
371
+ });
372
+ const prettyDirents = dirents.map(toPrettyDirent);
373
+ return prettyDirents;
374
+ } catch (error) {
375
+ throw new VError(error, `Failed to read directory "${uri}"`);
376
+ }
377
+ };
378
+ const mkdir = async uri => {
379
+ try {
380
+ assertUri(uri);
381
+ const path = fileURLToPath(uri);
382
+ await fs.mkdir(path, {
383
+ recursive: true
384
+ });
385
+ } catch (error) {
386
+ throw new VError(error, `Failed to create directory "${uri}"`);
387
+ }
388
+ };
389
+ const fallbackRename = async (oldUri, newUri) => {
390
+ try {
391
+ const oldPath = fileURLToPath(oldUri);
392
+ const newPath = fileURLToPath(newUri);
393
+ await fs.cp(oldPath, newPath, {
394
+ recursive: true
395
+ });
396
+ await fs.rm(oldPath, {
397
+ recursive: true
398
+ });
399
+ } catch (error) {
400
+ throw new VError(error, `Failed to rename "${oldUri}" to "${newUri}"`);
401
+ }
402
+ };
403
+ const rename = async (oldUri, newUri) => {
404
+ try {
405
+ const oldPath = fileURLToPath(oldUri);
406
+ const newPath = fileURLToPath(newUri);
407
+ await fs.rename(oldPath, newPath);
408
+ } catch (error) {
409
+ if (error && error.code === EXDEV) {
410
+ return fallbackRename(oldUri, newUri);
411
+ }
412
+ throw new VError(error, `Failed to rename "${oldUri}" to "${newUri}"`);
413
+ }
414
+ };
415
+ const getPathSeparator = () => {
416
+ return '/';
417
+ };
418
+ const stat = async uri => {
419
+ const path = fileURLToPath(uri);
420
+ const stats = await fs.stat(path);
421
+ const type = getDirentType(stats);
422
+ return type;
423
+ };
424
+ const chmod = async (uri, permissions) => {
425
+ const path = fileURLToPath(uri);
426
+ await fs.chmod(path, permissions);
427
+ };
428
+ const readJson = async uri => {
429
+ try {
430
+ string(uri);
431
+ assertUri(uri);
432
+ const path = fileURLToPath(uri);
433
+ const content = await fs.readFile(path, 'utf8');
434
+ const parsed = JSON.parse(content);
435
+ return parsed;
436
+ } catch (error) {
437
+ if (isEnoentError(error)) {
438
+ throw new FileNotFoundError(uri);
439
+ }
440
+ throw new VError(error, `Failed to read file as json "${uri}"`);
441
+ }
442
+ };
443
+ const getFolderSize = async uri => {
444
+ const path = fileURLToPath(uri);
445
+ const total = await getFolderSizeInternal(path);
446
+ return total;
447
+ };
448
+
210
449
  const isMessagePort = value => {
211
450
  return value && value instanceof MessagePort;
212
451
  };
@@ -629,7 +868,7 @@ const listen$3 = async () => {
629
868
  parentPort
630
869
  } = await import('node:worker_threads');
631
870
  if (!parentPort) {
632
- throw new IpcError('parentPort is required');
871
+ throw new IpcError('parentPort is required for node worker threads ipc');
633
872
  }
634
873
  return parentPort;
635
874
  };
@@ -764,8 +1003,6 @@ const handleUpgrade$1 = async (...args) => {
764
1003
  // @ts-ignore
765
1004
  return module.handleUpgrade(...args);
766
1005
  };
767
-
768
- // @ts-ignore
769
1006
  const listen$1$1 = async ({
770
1007
  request,
771
1008
  handle
@@ -786,8 +1023,6 @@ const listen$1$1 = async ({
786
1023
  const signal$4 = webSocket => {
787
1024
  webSocket.resume();
788
1025
  };
789
-
790
- // @ts-ignore
791
1026
  const wrap$9 = webSocket => {
792
1027
  return {
793
1028
  webSocket,
@@ -856,7 +1091,7 @@ const set = (id, fn) => {
856
1091
  const get = id => {
857
1092
  return callbacks[id];
858
1093
  };
859
- const remove$1 = id => {
1094
+ const remove = id => {
860
1095
  delete callbacks[id];
861
1096
  };
862
1097
  let id = 0;
@@ -942,6 +1177,16 @@ const constructError = (message, type, name) => {
942
1177
  }
943
1178
  return new ErrorConstructor(message);
944
1179
  };
1180
+ const joinLines = lines => {
1181
+ return lines.join(NewLine);
1182
+ };
1183
+ const splitLines = lines => {
1184
+ return lines.split(NewLine);
1185
+ };
1186
+ const getCurrentStack = () => {
1187
+ const currentStack = joinLines(splitLines(new Error().stack || '').slice(2));
1188
+ return currentStack;
1189
+ };
945
1190
  const getNewLineIndex = (string, startIndex = undefined) => {
946
1191
  return string.indexOf(NewLine, startIndex);
947
1192
  };
@@ -952,19 +1197,16 @@ const getParentStack = error => {
952
1197
  }
953
1198
  return parentStack;
954
1199
  };
955
- const joinLines = lines => {
956
- return lines.join(NewLine);
957
- };
958
1200
  const MethodNotFound = -32601;
959
1201
  const Custom = -32001;
960
- const splitLines = lines => {
961
- return lines.split(NewLine);
962
- };
963
1202
  const restoreJsonRpcError = error => {
1203
+ const currentStack = getCurrentStack();
964
1204
  if (error && error instanceof Error) {
1205
+ if (typeof error.stack === 'string') {
1206
+ error.stack = error.stack + NewLine + currentStack;
1207
+ }
965
1208
  return error;
966
1209
  }
967
- const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
968
1210
  if (error && error.code && error.code === MethodNotFound) {
969
1211
  const restoredError = new JsonRpcError(error.message);
970
1212
  const parentStack = getParentStack(error);
@@ -1033,7 +1275,7 @@ const resolve = (id, response) => {
1033
1275
  return;
1034
1276
  }
1035
1277
  fn(response);
1036
- remove$1(id);
1278
+ remove(id);
1037
1279
  };
1038
1280
  const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
1039
1281
  const getErrorType = prettyError => {
@@ -1045,6 +1287,17 @@ const getErrorType = prettyError => {
1045
1287
  }
1046
1288
  return undefined;
1047
1289
  };
1290
+ const isAlreadyStack = line => {
1291
+ return line.trim().startsWith('at ');
1292
+ };
1293
+ const getStack = prettyError => {
1294
+ const stackString = prettyError.stack || '';
1295
+ const newLineIndex = stackString.indexOf('\n');
1296
+ if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
1297
+ return stackString.slice(newLineIndex + 1);
1298
+ }
1299
+ return stackString;
1300
+ };
1048
1301
  const getErrorProperty = (error, prettyError) => {
1049
1302
  if (error && error.code === E_COMMAND_NOT_FOUND) {
1050
1303
  return {
@@ -1057,7 +1310,7 @@ const getErrorProperty = (error, prettyError) => {
1057
1310
  code: Custom,
1058
1311
  message: prettyError.message,
1059
1312
  data: {
1060
- stack: prettyError.stack,
1313
+ stack: getStack(prettyError),
1061
1314
  codeFrame: prettyError.codeFrame,
1062
1315
  type: getErrorType(prettyError),
1063
1316
  code: prettyError.code,
@@ -1065,18 +1318,18 @@ const getErrorProperty = (error, prettyError) => {
1065
1318
  }
1066
1319
  };
1067
1320
  };
1068
- const create$1 = (message, error) => {
1321
+ const create$1 = (id, error) => {
1069
1322
  return {
1070
1323
  jsonrpc: Two,
1071
- id: message.id,
1324
+ id,
1072
1325
  error
1073
1326
  };
1074
1327
  };
1075
- const getErrorResponse = (message, error, preparePrettyError, logError) => {
1328
+ const getErrorResponse = (id, error, preparePrettyError, logError) => {
1076
1329
  const prettyError = preparePrettyError(error);
1077
1330
  logError(error, prettyError);
1078
1331
  const errorProperty = getErrorProperty(error, prettyError);
1079
- return create$1(message, errorProperty);
1332
+ return create$1(id, errorProperty);
1080
1333
  };
1081
1334
  const create = (message, result) => {
1082
1335
  return {
@@ -1089,12 +1342,27 @@ const getSuccessResponse = (message, result) => {
1089
1342
  const resultProperty = result ?? null;
1090
1343
  return create(message, resultProperty);
1091
1344
  };
1345
+ const getErrorResponseSimple = (id, error) => {
1346
+ return {
1347
+ jsonrpc: Two,
1348
+ id,
1349
+ error: {
1350
+ code: Custom,
1351
+ // @ts-ignore
1352
+ message: error.message,
1353
+ data: error
1354
+ }
1355
+ };
1356
+ };
1092
1357
  const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
1093
1358
  try {
1094
1359
  const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
1095
1360
  return getSuccessResponse(message, result);
1096
1361
  } catch (error) {
1097
- return getErrorResponse(message, error, preparePrettyError, logError);
1362
+ if (ipc.canUseSimpleErrorResponse) {
1363
+ return getErrorResponseSimple(message.id, error);
1364
+ }
1365
+ return getErrorResponse(message.id, error, preparePrettyError, logError);
1098
1366
  }
1099
1367
  };
1100
1368
  const defaultPreparePrettyError = error => {
@@ -1149,7 +1417,7 @@ const handleJsonRpcMessage = async (...args) => {
1149
1417
  try {
1150
1418
  ipc.send(response);
1151
1419
  } catch (error) {
1152
- const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
1420
+ const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
1153
1421
  ipc.send(errorResponse);
1154
1422
  }
1155
1423
  return;
@@ -1180,7 +1448,7 @@ const send = (transport, method, ...params) => {
1180
1448
  const message = create$4$1(method, params);
1181
1449
  transport.send(message);
1182
1450
  };
1183
- const invoke$1 = (ipc, method, ...params) => {
1451
+ const invoke = (ipc, method, ...params) => {
1184
1452
  return invokeHelper(ipc, method, params, false);
1185
1453
  };
1186
1454
  const invokeAndTransfer = (ipc, method, ...params) => {
@@ -1213,7 +1481,7 @@ const createRpc = ipc => {
1213
1481
  send(ipc, method, ...params);
1214
1482
  },
1215
1483
  invoke(method, ...params) {
1216
- return invoke$1(ipc, method, ...params);
1484
+ return invoke(ipc, method, ...params);
1217
1485
  },
1218
1486
  invokeAndTransfer(method, ...params) {
1219
1487
  return invokeAndTransfer(ipc, method, ...params);
@@ -1254,24 +1522,28 @@ const listen$2 = async (module, options) => {
1254
1522
  const ipc = module.wrap(rawIpc);
1255
1523
  return ipc;
1256
1524
  };
1257
- const create$c = async ({
1525
+ const create$e = async ({
1258
1526
  commandMap,
1259
- messagePort
1527
+ messagePort,
1528
+ requiresSocket
1260
1529
  }) => {
1261
1530
  // TODO create a commandMap per rpc instance
1262
1531
  register(commandMap);
1263
1532
  const ipc = await listen$2(IpcChildWithElectronMessagePort$1, {
1264
1533
  messagePort
1265
1534
  });
1535
+ if (requiresSocket) {
1536
+ ipc.requiresSocket = requiresSocket;
1537
+ }
1266
1538
  handleIpc(ipc);
1267
1539
  const rpc = createRpc(ipc);
1268
1540
  return rpc;
1269
1541
  };
1270
1542
  const ElectronMessagePortRpcClient = {
1271
1543
  __proto__: null,
1272
- create: create$c
1544
+ create: create$e
1273
1545
  };
1274
- const create$b = async ({
1546
+ const create$d = async ({
1275
1547
  commandMap
1276
1548
  }) => {
1277
1549
  // TODO create a commandMap per rpc instance
@@ -1283,7 +1555,7 @@ const create$b = async ({
1283
1555
  };
1284
1556
  const ElectronUtilityProcessRpcClient = {
1285
1557
  __proto__: null,
1286
- create: create$b
1558
+ create: create$d
1287
1559
  };
1288
1560
  const create$7 = async ({
1289
1561
  commandMap
@@ -1302,7 +1574,8 @@ const NodeForkedProcessRpcClient = {
1302
1574
  const create$5 = async ({
1303
1575
  commandMap,
1304
1576
  request,
1305
- handle
1577
+ handle,
1578
+ requiresSocket
1306
1579
  }) => {
1307
1580
  // TODO create a commandMap per rpc instance
1308
1581
  register(commandMap);
@@ -1310,6 +1583,9 @@ const create$5 = async ({
1310
1583
  request,
1311
1584
  handle
1312
1585
  });
1586
+ if (requiresSocket) {
1587
+ ipc.requiresSocket = requiresSocket;
1588
+ }
1313
1589
  handleIpc(ipc);
1314
1590
  const rpc = createRpc(ipc);
1315
1591
  return rpc;
@@ -1354,15 +1630,15 @@ const Auto = argv => {
1354
1630
  const getModule = method => {
1355
1631
  switch (method) {
1356
1632
  case NodeForkedProcess:
1357
- return NodeForkedProcessRpcClient;
1633
+ return NodeForkedProcessRpcClient.create;
1358
1634
  case NodeWorker:
1359
- return NodeWorkerRpcClient;
1635
+ return NodeWorkerRpcClient.create;
1360
1636
  case ElectronUtilityProcess:
1361
- return ElectronUtilityProcessRpcClient;
1637
+ return ElectronUtilityProcessRpcClient.create;
1362
1638
  case ElectronMessagePort:
1363
- return ElectronMessagePortRpcClient;
1639
+ return ElectronMessagePortRpcClient.create;
1364
1640
  case WebSocket:
1365
- return NodeWebSocketRpcClient;
1641
+ return NodeWebSocketRpcClient.create;
1366
1642
  default:
1367
1643
  throw new Error('unexpected ipc type');
1368
1644
  }
@@ -1372,268 +1648,17 @@ const listen$1 = async ({
1372
1648
  method,
1373
1649
  ...params
1374
1650
  }) => {
1375
- const module = getModule(method);
1376
- const rpc = await module.create(params);
1651
+ const create = getModule(method);
1652
+ const rpc = await create(params);
1377
1653
  return rpc;
1378
1654
  };
1379
1655
 
1380
- // TODO add tests for this
1381
-
1382
- // TODO handle structure: one shared process multiple extension hosts
1383
-
1384
- // TODO pass socket / port handle also in electron
1385
-
1386
- const state = {
1387
- ipc: undefined
1388
- };
1389
- const invoke = (method, ...params) => {
1390
- return invoke$1(state.ipc, method, ...params);
1391
- };
1392
-
1393
- // @ts-ignore
1394
- const trash$2 = async path => {
1395
- if (!existsSync(path)) {
1396
- return;
1397
- }
1398
- await invoke('Trash.trash', path);
1399
- };
1400
-
1401
- // @ts-ignore
1402
- const trash$1 = async path => {
1403
- await _trash(path);
1404
- };
1405
-
1406
- const getFn = () => {
1407
- if (isElectron) {
1408
- return trash$2;
1409
- }
1410
- return trash$1;
1411
- };
1412
-
1413
- // @ts-ignore
1414
- const trash = async path => {
1415
- try {
1416
- const fn = getFn();
1417
- await fn(path);
1418
- } catch (error) {
1419
- throw new VError(error, 'Failed to move item to trash');
1420
- }
1421
- };
1422
-
1423
- // TODO lazyload chokidar and trash (but doesn't work currently because of bug with jest)
1424
-
1425
- // @ts-ignore
1426
- const assertUri = uri => {
1427
- if (!uri.startsWith('file://')) {
1428
- throw new Error(`path must be a valid file uri`);
1429
- }
1430
- };
1431
- const copy = async (sourceUri, targetUri) => {
1432
- try {
1433
- assertUri(sourceUri);
1434
- assertUri(targetUri);
1435
- const source = fileURLToPath(sourceUri);
1436
- const target = fileURLToPath(targetUri);
1437
- await fs.cp(source, target, {
1438
- recursive: true
1439
- });
1440
- } catch (error) {
1441
- if (error && error.message && error.message.startsWith('Invalid src or dest: cp returned EINVAL (src and dest cannot be the same)')) {
1442
- throw new VError(`Failed to copy "${sourceUri}" to "${targetUri}": src and dest cannot be the same`);
1443
- }
1444
- throw new VError(error, `Failed to copy "${sourceUri}" to "${targetUri}"`);
1445
- }
1446
- };
1447
-
1448
- /**
1449
- *
1450
- * @param {string} uri
1451
- * @param {BufferEncoding} encoding
1452
- * @returns
1453
- */
1454
- // @ts-ignore
1455
- const readFile = async (uri, encoding = Utf8) => {
1456
- try {
1457
- string(uri);
1458
- assertUri(uri);
1459
- const path = fileURLToPath(uri);
1460
- // @ts-ignore
1461
- const content = await fs.readFile(path, encoding);
1462
- return content;
1463
- } catch (error) {
1464
- if (isEnoentError(error)) {
1465
- throw new FileNotFoundError(uri);
1466
- }
1467
- throw new VError(error, `Failed to read file "${uri}"`);
1468
- }
1469
- };
1470
-
1471
- /**
1472
- *
1473
- * @param {string} uri
1474
- * @param {string} content
1475
- * @param {BufferEncoding} encoding
1476
- */
1477
- // @ts-ignore
1478
- const writeFile = async (uri, content, encoding = Utf8) => {
1479
- try {
1480
- assertUri(uri);
1481
- string(uri);
1482
- string(content);
1483
- const path = fileURLToPath(uri);
1484
- // queue would be more correct for concurrent writes but also slower
1485
- // Queue.add(`writeFile/${path}`, () =>
1486
- // @ts-ignore
1487
- await fs.writeFile(path, content, encoding);
1488
- } catch (error) {
1489
- if (isEnoentError(error)) {
1490
- throw new FileNotFoundError(uri);
1491
- }
1492
- throw new VError(error, `Failed to write to file "${uri}"`);
1493
- }
1494
- };
1495
-
1496
- // @ts-ignore
1497
- const isOkayToRemove = path => {
1498
- if (path === '/') {
1499
- return false;
1500
- }
1501
- if (path === '~') {
1502
- return false;
1503
- }
1504
- if (path === os.homedir()) {
1505
- return false;
1506
- }
1507
- return true;
1508
- };
1509
-
1510
- // @ts-ignore
1511
- const remove = async uri => {
1512
- assertUri(uri);
1513
- const path = fileURLToPath(uri);
1514
- if (!isOkayToRemove(path)) {
1515
- console.warn('not removing path');
1516
- return;
1517
- }
1518
-
1519
- // TODO lazyload trash (doesn't work currently because of bug with jest)
1520
- // const { trash } = await import('../../wrap/trash.js')
1521
- try {
1522
- await trash(path);
1523
- } catch (error) {
1524
- throw new VError(error, `Failed to remove "${uri}"`);
1525
- }
1526
- };
1527
-
1528
- /**
1529
- * @param {import('fs').Dirent} dirent
1530
- */
1531
- // @ts-ignore
1532
- const toPrettyDirent = dirent => {
1533
- return {
1534
- name: dirent.name,
1535
- type: getDirentType(dirent)
1536
- };
1537
- };
1538
-
1539
- // @ts-ignore
1540
- const readDirWithFileTypes = async uri => {
1541
- try {
1542
- assertUri(uri);
1543
- const path = fileURLToPath(uri);
1544
- const dirents = await fs.readdir(path, {
1545
- withFileTypes: true
1546
- });
1547
- const prettyDirents = dirents.map(toPrettyDirent);
1548
- return prettyDirents;
1549
- } catch (error) {
1550
- throw new VError(error, `Failed to read directory "${uri}"`);
1551
- }
1552
- };
1553
-
1554
- // @ts-ignore
1555
- const mkdir = async uri => {
1556
- try {
1557
- assertUri(uri);
1558
- const path = fileURLToPath(uri);
1559
- await fs.mkdir(path, {
1560
- recursive: true
1561
- });
1562
- } catch (error) {
1563
- throw new VError(error, `Failed to create directory "${uri}"`);
1564
- }
1565
- };
1566
-
1567
- // @ts-ignore
1568
- const fallbackRename = async (oldUri, newUri) => {
1569
- try {
1570
- const oldPath = fileURLToPath(oldUri);
1571
- const newPath = fileURLToPath(newUri);
1572
- await fs.cp(oldPath, newPath, {
1573
- recursive: true
1574
- });
1575
- await fs.rm(oldPath, {
1576
- recursive: true
1577
- });
1578
- } catch (error) {
1579
- throw new VError(error, `Failed to rename "${oldUri}" to "${newUri}"`);
1580
- }
1581
- };
1582
-
1583
- // @ts-ignore
1584
- const rename = async (oldUri, newUri) => {
1585
- try {
1586
- const oldPath = fileURLToPath(oldUri);
1587
- const newPath = fileURLToPath(newUri);
1588
- await fs.rename(oldPath, newPath);
1589
- } catch (error) {
1590
- if (error && error.code === EXDEV) {
1591
- return fallbackRename(oldUri, newUri);
1592
- }
1593
- throw new VError(error, `Failed to rename "${oldUri}" to "${newUri}"`);
1594
- }
1595
- };
1596
- const getPathSeparator = () => {
1597
- return '/';
1598
- };
1599
-
1600
- // TODO handle error
1601
- // @ts-ignore
1602
- const stat = async uri => {
1603
- const path = fileURLToPath(uri);
1604
- const stats = await fs.stat(path);
1605
- const type = getDirentType(stats);
1606
- return type;
1607
- };
1608
-
1609
- // @ts-ignore
1610
- const chmod = async (uri, permissions) => {
1611
- const path = fileURLToPath(uri);
1612
- await fs.chmod(path, permissions);
1613
- };
1614
-
1615
- // @ts-ignore
1616
- const readJson = async uri => {
1617
- try {
1618
- string(uri);
1619
- assertUri(uri);
1620
- const path = fileURLToPath(uri);
1621
- const content = await fs.readFile(path, 'utf8');
1622
- const parsed = JSON.parse(content);
1623
- return parsed;
1624
- } catch (error) {
1625
- if (isEnoentError(error)) {
1626
- throw new FileNotFoundError(uri);
1627
- }
1628
- throw new VError(error, `Failed to read file as json "${uri}"`);
1629
- }
1630
- };
1631
-
1632
- // @ts-ignore
1633
- const getFolderSize = async uri => {
1634
- const path = fileURLToPath(uri);
1635
- const total = await getFolderSizeInternal(path);
1636
- return total;
1656
+ const handleElectronMessagePort = async (messagePort, ...params) => {
1657
+ object(messagePort);
1658
+ await listen$1({
1659
+ method: ElectronMessagePort,
1660
+ messagePort
1661
+ });
1637
1662
  };
1638
1663
 
1639
1664
  const handleWebSocket = async (handle, request) => {
@@ -1646,6 +1671,24 @@ const handleWebSocket = async (handle, request) => {
1646
1671
  });
1647
1672
  };
1648
1673
 
1674
+ const createMainProcessRpc = async port => {
1675
+ try {
1676
+ const rpc = await ElectronMessagePortRpcClient.create({
1677
+ commandMap: {},
1678
+ messagePort: port
1679
+ });
1680
+ return rpc;
1681
+ } catch (error) {
1682
+ throw new VError(error, `Failed to create main process rpc`);
1683
+ }
1684
+ };
1685
+
1686
+ const initialize = async port => {
1687
+ // TODO only connect to main process if platform is electron else not
1688
+ const rpc = await createMainProcessRpc(port);
1689
+ set$1(rpc);
1690
+ };
1691
+
1649
1692
  const commandMap = {
1650
1693
  'FileSystem.chmod': chmod,
1651
1694
  'FileSystem.copy': copy,
@@ -1655,11 +1698,13 @@ const commandMap = {
1655
1698
  'FileSystem.readDirWithFileTypes': readDirWithFileTypes,
1656
1699
  'FileSystem.readFile': readFile,
1657
1700
  'FileSystem.readJson': readJson,
1658
- 'FileSystem.remove': remove,
1701
+ 'FileSystem.remove': remove$1,
1659
1702
  'FileSystem.rename': rename,
1660
1703
  'FileSystem.stat': stat,
1661
1704
  'FileSystem.writeFile': writeFile,
1662
- 'HandleWebSocket.handleWebSocket': handleWebSocket
1705
+ 'HandleElectronMessagePort.handleElectronMessagePort': handleElectronMessagePort,
1706
+ 'HandleWebSocket.handleWebSocket': handleWebSocket,
1707
+ 'Initialize.initialize': initialize
1663
1708
  };
1664
1709
 
1665
1710
  const listen = async argv => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-system-process",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "description": "File System Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",
@@ -18,10 +18,6 @@
18
18
  "file-system-process": "bin/fileSystemProcess.js"
19
19
  },
20
20
  "dependencies": {
21
- "execa": "^9.5.2",
22
- "open": "^10.1.2",
23
- "tail": "^2.2.6",
24
- "tmp-promise": "^3.0.3",
25
21
  "trash": "^9.0.0"
26
22
  },
27
23
  "engines": {