@lvce-editor/file-system-process 3.5.0 → 3.7.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 +449 -451
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -216,16 +216,18 @@ const isEnoentError = error => {
216
216
  return isEnoentErrorLinux(error) || isEnoentErrorWindows(error);
217
217
  };
218
218
 
219
+ const FileSystemProcess = 210;
220
+ const MainProcess = -5;
221
+ const SharedProcess$1 = 1;
222
+
219
223
  const rpcs = Object.create(null);
220
- const set$g = (id, rpc) => {
224
+ const set$2 = (id, rpc) => {
221
225
  rpcs[id] = rpc;
222
226
  };
223
227
  const get$1 = id => {
224
228
  return rpcs[id];
225
229
  };
226
230
 
227
- /* eslint-disable @typescript-eslint/explicit-function-return-type */
228
-
229
231
  const create$6 = rpcId => {
230
232
  return {
231
233
  // @ts-ignore
@@ -241,7 +243,7 @@ const create$6 = rpcId => {
241
243
  return rpc.invokeAndTransfer(method, ...params);
242
244
  },
243
245
  set(rpc) {
244
- set$g(rpcId, rpc);
246
+ set$2(rpcId, rpc);
245
247
  },
246
248
  async dispose() {
247
249
  const rpc = get$1(rpcId);
@@ -249,321 +251,6 @@ const create$6 = rpcId => {
249
251
  }
250
252
  };
251
253
  };
252
- const DebugWorker$1 = 55;
253
- const EditorWorker$1 = 99;
254
- const ExtensionHostWorker = 44;
255
- const MainProcess$1 = -5;
256
- const RendererWorker$1 = 1;
257
- const RendererProcess$1 = 1670;
258
- const SearchProcess$1 = 77;
259
- const SearchProcessElectron = 2;
260
- const SharedProcess$1 = 1;
261
- const SourceControlWorker = 66;
262
- const EmbedsProcess$1 = 207;
263
- const EmbedsWorker = 208;
264
- const FileSystemWorker$1 = 209;
265
- const FileSystemProcess$1 = 210;
266
- const MarkdownWorker$1 = 300;
267
- const CompletionWorker = 301;
268
- const ColorPickerWorker = 302;
269
- const SourceActionWorker = 303;
270
- const ErrorWorker$1 = 3308;
271
- const SyntaxHighlightingWorker$1 = 3309;
272
- const ClipBoardWorker$1 = 3400;
273
- const ClipBoardProcess$1 = 3401;
274
- const ExtensionDetailWorker = 3402;
275
- const ProblemsWorker = 3403;
276
- const RpcId = {
277
- __proto__: null,
278
- ClipBoardProcess: ClipBoardProcess$1,
279
- ClipBoardWorker: ClipBoardWorker$1,
280
- ColorPickerWorker,
281
- CompletionWorker,
282
- DebugWorker: DebugWorker$1,
283
- EditorWorker: EditorWorker$1,
284
- EmbedsProcess: EmbedsProcess$1,
285
- EmbedsWorker,
286
- ErrorWorker: ErrorWorker$1,
287
- ExtensionDetailWorker,
288
- ExtensionHostWorker,
289
- FileSystemProcess: FileSystemProcess$1,
290
- FileSystemWorker: FileSystemWorker$1,
291
- MainProcess: MainProcess$1,
292
- MarkdownWorker: MarkdownWorker$1,
293
- ProblemsWorker,
294
- RendererProcess: RendererProcess$1,
295
- RendererWorker: RendererWorker$1,
296
- SearchProcess: SearchProcess$1,
297
- SearchProcessElectron,
298
- SharedProcess: SharedProcess$1,
299
- SourceActionWorker,
300
- SourceControlWorker,
301
- SyntaxHighlightingWorker: SyntaxHighlightingWorker$1
302
- };
303
- const {
304
- invoke: invoke$1$1,
305
- invokeAndTransfer: invokeAndTransfer$1$1} = create$6(SharedProcess$1);
306
- const SharedProcess = {
307
- __proto__: null,
308
- invoke: invoke$1$1,
309
- invokeAndTransfer: invokeAndTransfer$1$1};
310
- const createLazyRpc = rpcId => {
311
- let rpcPromise;
312
- let factory;
313
- const createRpc = async () => {
314
- const rpc = await factory();
315
- set$g(rpcId, rpc);
316
- };
317
- const ensureRpc = async () => {
318
- if (!rpcPromise) {
319
- rpcPromise = createRpc();
320
- }
321
- await rpcPromise;
322
- };
323
- return {
324
- setFactory(value) {
325
- factory = value;
326
- },
327
- async invoke(method, ...params) {
328
- await ensureRpc();
329
- const rpc = get$1(rpcId);
330
- return rpc.invoke(method, ...params);
331
- }
332
- };
333
- };
334
-
335
- const {
336
- invoke: invoke$2,
337
- setFactory
338
- } = createLazyRpc(RpcId.MainProcess);
339
-
340
- const trash$2 = async path => {
341
- if (!existsSync(path)) {
342
- return;
343
- }
344
- // @ts-ignore
345
- await invoke$2('Trash.trash', path);
346
- };
347
-
348
- // @ts-ignore
349
- const trash$1 = async path => {
350
- await _trash(path);
351
- };
352
-
353
- const getTrashFn = isElectron => {
354
- if (isElectron) {
355
- return trash$2;
356
- }
357
- return trash$1;
358
- };
359
-
360
- const getElectronRunAsNode = () => {
361
- return process.env.ELECTRON_RUN_AS_NODE || '';
362
- };
363
-
364
- const isElectron = Boolean(getElectronRunAsNode()) || 'electron' in process.versions;
365
-
366
- const trash = async path => {
367
- if (!path || path.trim() === '') {
368
- throw new Error('Path cannot be empty');
369
- }
370
- try {
371
- const fn = getTrashFn(isElectron);
372
- await fn(path);
373
- } catch (error) {
374
- throw new VError(error, 'Failed to move item to trash');
375
- }
376
- };
377
-
378
- const copy = async (sourceUri, targetUri) => {
379
- try {
380
- assertUri(sourceUri);
381
- assertUri(targetUri);
382
- const source = fileURLToPath(sourceUri);
383
- const target = fileURLToPath(targetUri);
384
- await fs.cp(source, target, {
385
- recursive: true
386
- });
387
- } catch (error) {
388
- if (error && error.message && error.message.startsWith('Invalid src or dest: cp returned EINVAL (src and dest cannot be the same)')) {
389
- throw new VError(`Failed to copy "${sourceUri}" to "${targetUri}": src and dest cannot be the same`);
390
- }
391
- throw new VError(error, `Failed to copy "${sourceUri}" to "${targetUri}"`);
392
- }
393
- };
394
- const readFile = async (uri, encoding = Utf8) => {
395
- try {
396
- string(uri);
397
- assertUri(uri);
398
- const path = fileURLToPath(uri);
399
- const content = await fs.readFile(path, encoding);
400
- return content;
401
- } catch (error) {
402
- if (isEnoentError(error)) {
403
- throw new FileNotFoundError(uri);
404
- }
405
- throw new VError(error, `Failed to read file "${uri}"`);
406
- }
407
- };
408
- const writeFile = async (uri, content, encoding = Utf8) => {
409
- try {
410
- assertUri(uri);
411
- string(uri);
412
- string(content);
413
- const path = fileURLToPath(uri);
414
- // queue would be more correct for concurrent writes but also slower
415
- // Queue.add(`writeFile/${path}`, () =>
416
- await fs.writeFile(path, content, encoding);
417
- } catch (error) {
418
- if (isEnoentError(error)) {
419
- throw new FileNotFoundError(uri);
420
- }
421
- throw new VError(error, `Failed to write to file "${uri}"`);
422
- }
423
- };
424
- const appendFile = async (uri, content) => {
425
- try {
426
- assertUri(uri);
427
- string(uri);
428
- string(content);
429
- const path = fileURLToPath(uri);
430
- await fs.appendFile(path, content);
431
- } catch (error) {
432
- if (isEnoentError(error)) {
433
- throw new FileNotFoundError(uri);
434
- }
435
- throw new VError(error, `Failed to append to file "${uri}"`);
436
- }
437
- };
438
- const isOkayToRemove = path => {
439
- if (path === '/') {
440
- return false;
441
- }
442
- if (path === '~') {
443
- return false;
444
- }
445
- if (path === os.homedir()) {
446
- return false;
447
- }
448
- return true;
449
- };
450
- const remove$1 = async uri => {
451
- assertUri(uri);
452
- const path = fileURLToPath(uri);
453
- if (!isOkayToRemove(path)) {
454
- console.warn('not removing path');
455
- return;
456
- }
457
-
458
- // TODO lazyload trash (doesn't work currently because of bug with jest)
459
- // const { trash } = await import('../../wrap/trash.js')
460
- try {
461
- await trash(path);
462
- } catch (error) {
463
- throw new VError(error, `Failed to remove "${uri}"`);
464
- }
465
- };
466
- const toPrettyDirent = dirent => {
467
- return {
468
- name: dirent.name,
469
- type: getDirentType(dirent)
470
- };
471
- };
472
- const readDirWithFileTypes = async uri => {
473
- try {
474
- assertUri(uri);
475
- const path = fileURLToPath(uri);
476
- const dirents = await fs.readdir(path, {
477
- withFileTypes: true
478
- });
479
- const prettyDirents = dirents.map(toPrettyDirent);
480
- return prettyDirents;
481
- } catch (error) {
482
- throw new VError(error, `Failed to read directory "${uri}"`);
483
- }
484
- };
485
- const mkdir = async uri => {
486
- try {
487
- assertUri(uri);
488
- const path = fileURLToPath(uri);
489
- await fs.mkdir(path, {
490
- recursive: true
491
- });
492
- } catch (error) {
493
- throw new VError(error, `Failed to create directory "${uri}"`);
494
- }
495
- };
496
- const exists = async uri => {
497
- assertUri(uri);
498
- const path = fileURLToPath(uri);
499
- return existsSync(path);
500
- };
501
- const fallbackRename = async (oldUri, newUri) => {
502
- try {
503
- const oldPath = fileURLToPath(oldUri);
504
- const newPath = fileURLToPath(newUri);
505
- await fs.cp(oldPath, newPath, {
506
- recursive: true
507
- });
508
- await fs.rm(oldPath, {
509
- recursive: true
510
- });
511
- } catch (error) {
512
- throw new VError(error, `Failed to rename "${oldUri}" to "${newUri}"`);
513
- }
514
- };
515
- const rename = async (oldUri, newUri) => {
516
- try {
517
- const oldPath = fileURLToPath(oldUri);
518
- const newPath = fileURLToPath(newUri);
519
- await fs.rename(oldPath, newPath);
520
- } catch (error) {
521
- if (error && error.code === EXDEV) {
522
- return fallbackRename(oldUri, newUri);
523
- }
524
- throw new VError(error, `Failed to rename "${oldUri}" to "${newUri}"`);
525
- }
526
- };
527
- const getPathSeparator = () => {
528
- return '/';
529
- };
530
- const stat = async uri => {
531
- const path = fileURLToPath(uri);
532
- const stats = await fs.stat(path);
533
- const type = getDirentType(stats);
534
- return type;
535
- };
536
- const chmod = async (uri, permissions) => {
537
- const path = fileURLToPath(uri);
538
- await fs.chmod(path, permissions);
539
- };
540
- const readJson = async uri => {
541
- try {
542
- string(uri);
543
- assertUri(uri);
544
- const path = fileURLToPath(uri);
545
- const content = await fs.readFile(path, 'utf8');
546
- const parsed = JSON.parse(content);
547
- return parsed;
548
- } catch (error) {
549
- if (isEnoentError(error)) {
550
- throw new FileNotFoundError(uri);
551
- }
552
- throw new VError(error, `Failed to read file as json "${uri}"`);
553
- }
554
- };
555
- const getFolderSize = async uri => {
556
- const path = fileURLToPath(uri);
557
- const total = await getFolderSizeInternal(path);
558
- return total;
559
- };
560
-
561
- const watchFile = uri => {
562
- // TODO
563
- };
564
- const unwatchFile = uri => {
565
- // TODO
566
- };
567
254
 
568
255
  const isMessagePort = value => {
569
256
  return value && value instanceof MessagePort;
@@ -887,7 +574,7 @@ const listen$a = () => {
887
574
  parentPort
888
575
  } = process;
889
576
  if (!parentPort) {
890
- throw new Error('parent port must be defined');
577
+ throw new IpcError('parent port must be defined');
891
578
  }
892
579
  return parentPort;
893
580
  };
@@ -945,7 +632,7 @@ const getTransferrablesNode = value => {
945
632
  };
946
633
  const listen$5 = async () => {
947
634
  if (!process.send) {
948
- throw new Error('process.send must be defined');
635
+ throw new IpcError('process.send must be defined');
949
636
  }
950
637
  return process;
951
638
  };
@@ -1204,13 +891,13 @@ const create$4 = (method, params) => {
1204
891
  };
1205
892
  };
1206
893
  const callbacks = Object.create(null);
1207
- const set = (id, fn) => {
894
+ const set$1 = (id, fn) => {
1208
895
  callbacks[id] = fn;
1209
896
  };
1210
897
  const get = id => {
1211
898
  return callbacks[id];
1212
899
  };
1213
- const remove = id => {
900
+ const remove$1 = id => {
1214
901
  delete callbacks[id];
1215
902
  };
1216
903
  let id = 0;
@@ -1223,7 +910,7 @@ const registerPromise = () => {
1223
910
  resolve,
1224
911
  promise
1225
912
  } = Promise.withResolvers();
1226
- set(id, resolve);
913
+ set$1(id, resolve);
1227
914
  return {
1228
915
  id,
1229
916
  promise
@@ -1395,7 +1082,7 @@ const resolve = (id, response) => {
1395
1082
  return;
1396
1083
  }
1397
1084
  fn(response);
1398
- remove(id);
1085
+ remove$1(id);
1399
1086
  };
1400
1087
  const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
1401
1088
  const getErrorType = prettyError => {
@@ -1568,10 +1255,10 @@ const send = (transport, method, ...params) => {
1568
1255
  const message = create$4(method, params);
1569
1256
  transport.send(message);
1570
1257
  };
1571
- const invoke$1 = (ipc, method, ...params) => {
1258
+ const invoke$3 = (ipc, method, ...params) => {
1572
1259
  return invokeHelper(ipc, method, params, false);
1573
1260
  };
1574
- const invokeAndTransfer$1 = (ipc, method, ...params) => {
1261
+ const invokeAndTransfer$2 = (ipc, method, ...params) => {
1575
1262
  return invokeHelper(ipc, method, params, true);
1576
1263
  };
1577
1264
 
@@ -1593,146 +1280,453 @@ const execute = (command, ...args) => {
1593
1280
  if (!fn) {
1594
1281
  throw new CommandNotFoundError(command);
1595
1282
  }
1596
- return fn(...args);
1283
+ return fn(...args);
1284
+ };
1285
+
1286
+ const createRpc = ipc => {
1287
+ const rpc = {
1288
+ // @ts-ignore
1289
+ ipc,
1290
+ /**
1291
+ * @deprecated
1292
+ */
1293
+ send(method, ...params) {
1294
+ send(ipc, method, ...params);
1295
+ },
1296
+ invoke(method, ...params) {
1297
+ return invoke$3(ipc, method, ...params);
1298
+ },
1299
+ invokeAndTransfer(method, ...params) {
1300
+ return invokeAndTransfer$2(ipc, method, ...params);
1301
+ },
1302
+ async dispose() {
1303
+ await ipc?.dispose();
1304
+ }
1305
+ };
1306
+ return rpc;
1307
+ };
1308
+ const requiresSocket$1 = () => {
1309
+ return false;
1310
+ };
1311
+ const preparePrettyError = error => {
1312
+ return error;
1313
+ };
1314
+ const logError = () => {
1315
+ // handled by renderer worker
1316
+ };
1317
+ const handleMessage = event => {
1318
+ const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket$1;
1319
+ const actualExecute = event?.target?.execute || execute;
1320
+ return handleJsonRpcMessage(event.target, event.data, actualExecute, resolve, preparePrettyError, logError, actualRequiresSocket);
1321
+ };
1322
+ const handleIpc = ipc => {
1323
+ if ('addEventListener' in ipc) {
1324
+ ipc.addEventListener('message', handleMessage);
1325
+ } else if ('on' in ipc) {
1326
+ // deprecated
1327
+ ipc.on('message', handleMessage);
1328
+ }
1329
+ };
1330
+ const listen$2 = async (module, options) => {
1331
+ const rawIpc = await module.listen(options);
1332
+ if (module.signal) {
1333
+ module.signal(rawIpc);
1334
+ }
1335
+ const ipc = module.wrap(rawIpc);
1336
+ return ipc;
1337
+ };
1338
+ const create$k = async ({
1339
+ commandMap,
1340
+ messagePort,
1341
+ requiresSocket
1342
+ }) => {
1343
+ // TODO create a commandMap per rpc instance
1344
+ register(commandMap);
1345
+ const ipc = await listen$2(IpcChildWithElectronMessagePort$1, {
1346
+ messagePort
1347
+ });
1348
+ if (requiresSocket) {
1349
+ ipc.requiresSocket = requiresSocket;
1350
+ }
1351
+ handleIpc(ipc);
1352
+ const rpc = createRpc(ipc);
1353
+ return rpc;
1354
+ };
1355
+ const ElectronMessagePortRpcClient = {
1356
+ __proto__: null,
1357
+ create: create$k
1358
+ };
1359
+ const create$j = async ({
1360
+ commandMap
1361
+ }) => {
1362
+ // TODO create a commandMap per rpc instance
1363
+ register(commandMap);
1364
+ const ipc = await listen$2(IpcChildWithElectronUtilityProcess$1);
1365
+ handleIpc(ipc);
1366
+ const rpc = createRpc(ipc);
1367
+ return rpc;
1368
+ };
1369
+ const ElectronUtilityProcessRpcClient = {
1370
+ __proto__: null,
1371
+ create: create$j
1372
+ };
1373
+ const create$a = async ({
1374
+ commandMap
1375
+ }) => {
1376
+ // TODO create a commandMap per rpc instance
1377
+ register(commandMap);
1378
+ const ipc = await listen$2(IpcChildWithNodeForkedProcess$1);
1379
+ handleIpc(ipc);
1380
+ const rpc = createRpc(ipc);
1381
+ return rpc;
1382
+ };
1383
+ const NodeForkedProcessRpcClient = {
1384
+ __proto__: null,
1385
+ create: create$a
1386
+ };
1387
+ const create$8 = async ({
1388
+ commandMap,
1389
+ request,
1390
+ handle,
1391
+ requiresSocket
1392
+ }) => {
1393
+ // TODO create a commandMap per rpc instance
1394
+ register(commandMap);
1395
+ const ipc = await listen$2(IpcChildWithWebSocket, {
1396
+ request,
1397
+ handle
1398
+ });
1399
+ if (requiresSocket) {
1400
+ ipc.requiresSocket = requiresSocket;
1401
+ }
1402
+ handleIpc(ipc);
1403
+ const rpc = createRpc(ipc);
1404
+ return rpc;
1405
+ };
1406
+ const NodeWebSocketRpcClient = {
1407
+ __proto__: null,
1408
+ create: create$8
1409
+ };
1410
+ const create$7 = async ({
1411
+ commandMap
1412
+ }) => {
1413
+ // TODO create a commandMap per rpc instance
1414
+ register(commandMap);
1415
+ const ipc = await listen$2(IpcChildWithNodeWorker$1);
1416
+ handleIpc(ipc);
1417
+ const rpc = createRpc(ipc);
1418
+ return rpc;
1419
+ };
1420
+ const NodeWorkerRpcClient = {
1421
+ __proto__: null,
1422
+ create: create$7
1423
+ };
1424
+
1425
+ const {
1426
+ invoke: invoke$2,
1427
+ invokeAndTransfer: invokeAndTransfer$1,
1428
+ set,
1429
+ dispose
1430
+ } = create$6(SharedProcess$1);
1431
+
1432
+ const SharedProcess = {
1433
+ __proto__: null,
1434
+ dispose,
1435
+ invoke: invoke$2,
1436
+ invokeAndTransfer: invokeAndTransfer$1,
1437
+ set
1438
+ };
1439
+
1440
+ const createLazyRpc = rpcId => {
1441
+ let rpcPromise;
1442
+ let factory;
1443
+ const createRpc = async () => {
1444
+ const rpc = await factory();
1445
+ set$2(rpcId, rpc);
1446
+ };
1447
+ const ensureRpc = async () => {
1448
+ if (!rpcPromise) {
1449
+ rpcPromise = createRpc();
1450
+ }
1451
+ await rpcPromise;
1452
+ };
1453
+ return {
1454
+ setFactory(value) {
1455
+ factory = value;
1456
+ },
1457
+ async invoke(method, ...params) {
1458
+ await ensureRpc();
1459
+ const rpc = get$1(rpcId);
1460
+ return rpc.invoke(method, ...params);
1461
+ }
1462
+ };
1463
+ };
1464
+
1465
+ const {
1466
+ invoke: invoke$1,
1467
+ setFactory
1468
+ } = createLazyRpc(MainProcess);
1469
+
1470
+ const trash$2 = async path => {
1471
+ if (!existsSync(path)) {
1472
+ return;
1473
+ }
1474
+ // @ts-ignore
1475
+ await invoke$1('Trash.trash', path);
1476
+ };
1477
+
1478
+ // @ts-ignore
1479
+ const trash$1 = async path => {
1480
+ await _trash(path);
1481
+ };
1482
+
1483
+ const getTrashFn = isElectron => {
1484
+ if (isElectron) {
1485
+ return trash$2;
1486
+ }
1487
+ return trash$1;
1488
+ };
1489
+
1490
+ const getElectronRunAsNode = () => {
1491
+ return process.env.ELECTRON_RUN_AS_NODE || '';
1492
+ };
1493
+
1494
+ const isElectron = Boolean(getElectronRunAsNode()) || 'electron' in process.versions;
1495
+
1496
+ const trash = async path => {
1497
+ if (!path || path.trim() === '') {
1498
+ throw new Error('Path cannot be empty');
1499
+ }
1500
+ try {
1501
+ const fn = getTrashFn(isElectron);
1502
+ await fn(path);
1503
+ } catch (error) {
1504
+ throw new VError(error, 'Failed to move item to trash');
1505
+ }
1506
+ };
1507
+
1508
+ const copy = async (sourceUri, targetUri) => {
1509
+ try {
1510
+ assertUri(sourceUri);
1511
+ assertUri(targetUri);
1512
+ const source = fileURLToPath(sourceUri);
1513
+ const target = fileURLToPath(targetUri);
1514
+ await fs.cp(source, target, {
1515
+ recursive: true
1516
+ });
1517
+ } catch (error) {
1518
+ if (error && error.message && error.message.startsWith('Invalid src or dest: cp returned EINVAL (src and dest cannot be the same)')) {
1519
+ throw new VError(`Failed to copy "${sourceUri}" to "${targetUri}": src and dest cannot be the same`);
1520
+ }
1521
+ throw new VError(error, `Failed to copy "${sourceUri}" to "${targetUri}"`);
1522
+ }
1523
+ };
1524
+ const readFile = async (uri, encoding = Utf8) => {
1525
+ try {
1526
+ string(uri);
1527
+ assertUri(uri);
1528
+ const path = fileURLToPath(uri);
1529
+ const content = await fs.readFile(path, encoding);
1530
+ return content;
1531
+ } catch (error) {
1532
+ if (isEnoentError(error)) {
1533
+ throw new FileNotFoundError(uri);
1534
+ }
1535
+ throw new VError(error, `Failed to read file "${uri}"`);
1536
+ }
1537
+ };
1538
+ const writeFile = async (uri, content, encoding = Utf8) => {
1539
+ try {
1540
+ assertUri(uri);
1541
+ string(uri);
1542
+ string(content);
1543
+ const path = fileURLToPath(uri);
1544
+ // queue would be more correct for concurrent writes but also slower
1545
+ // Queue.add(`writeFile/${path}`, () =>
1546
+ await fs.writeFile(path, content, encoding);
1547
+ } catch (error) {
1548
+ if (isEnoentError(error)) {
1549
+ throw new FileNotFoundError(uri);
1550
+ }
1551
+ throw new VError(error, `Failed to write to file "${uri}"`);
1552
+ }
1597
1553
  };
1598
-
1599
- const createRpc = ipc => {
1600
- const rpc = {
1601
- // @ts-ignore
1602
- ipc,
1603
- /**
1604
- * @deprecated
1605
- */
1606
- send(method, ...params) {
1607
- send(ipc, method, ...params);
1608
- },
1609
- invoke(method, ...params) {
1610
- return invoke$1(ipc, method, ...params);
1611
- },
1612
- invokeAndTransfer(method, ...params) {
1613
- return invokeAndTransfer$1(ipc, method, ...params);
1614
- },
1615
- async dispose() {
1616
- await ipc?.dispose();
1554
+ const appendFile = async (uri, content) => {
1555
+ try {
1556
+ assertUri(uri);
1557
+ string(uri);
1558
+ string(content);
1559
+ const path = fileURLToPath(uri);
1560
+ await fs.appendFile(path, content);
1561
+ } catch (error) {
1562
+ if (isEnoentError(error)) {
1563
+ throw new FileNotFoundError(uri);
1617
1564
  }
1618
- };
1619
- return rpc;
1565
+ throw new VError(error, `Failed to append to file "${uri}"`);
1566
+ }
1620
1567
  };
1621
- const requiresSocket = () => {
1622
- return false;
1568
+ const isOkayToRemove = path => {
1569
+ if (path === '/') {
1570
+ return false;
1571
+ }
1572
+ if (path === '~') {
1573
+ return false;
1574
+ }
1575
+ if (path === os.homedir()) {
1576
+ return false;
1577
+ }
1578
+ return true;
1623
1579
  };
1624
- const preparePrettyError = error => {
1625
- return error;
1580
+ const remove = async uri => {
1581
+ assertUri(uri);
1582
+ const path = fileURLToPath(uri);
1583
+ if (!isOkayToRemove(path)) {
1584
+ console.warn('not removing path');
1585
+ return;
1586
+ }
1587
+
1588
+ // TODO lazyload trash (doesn't work currently because of bug with jest)
1589
+ // const { trash } = await import('../../wrap/trash.js')
1590
+ try {
1591
+ await trash(path);
1592
+ } catch (error) {
1593
+ throw new VError(error, `Failed to remove "${uri}"`);
1594
+ }
1626
1595
  };
1627
- const logError = () => {
1628
- // handled by renderer worker
1596
+ const toPrettyDirent = dirent => {
1597
+ return {
1598
+ name: dirent.name,
1599
+ type: getDirentType(dirent)
1600
+ };
1629
1601
  };
1630
- const handleMessage = event => {
1631
- const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
1632
- const actualExecute = event?.target?.execute || execute;
1633
- return handleJsonRpcMessage(event.target, event.data, actualExecute, resolve, preparePrettyError, logError, actualRequiresSocket);
1602
+ const readDirWithFileTypes = async uri => {
1603
+ try {
1604
+ assertUri(uri);
1605
+ const path = fileURLToPath(uri);
1606
+ const dirents = await fs.readdir(path, {
1607
+ withFileTypes: true
1608
+ });
1609
+ const prettyDirents = dirents.map(toPrettyDirent);
1610
+ return prettyDirents;
1611
+ } catch (error) {
1612
+ throw new VError(error, `Failed to read directory "${uri}"`);
1613
+ }
1634
1614
  };
1635
- const handleIpc = ipc => {
1636
- if ('addEventListener' in ipc) {
1637
- ipc.addEventListener('message', handleMessage);
1638
- } else if ('on' in ipc) {
1639
- // deprecated
1640
- ipc.on('message', handleMessage);
1615
+ const mkdir = async uri => {
1616
+ try {
1617
+ assertUri(uri);
1618
+ const path = fileURLToPath(uri);
1619
+ await fs.mkdir(path, {
1620
+ recursive: true
1621
+ });
1622
+ } catch (error) {
1623
+ throw new VError(error, `Failed to create directory "${uri}"`);
1641
1624
  }
1642
1625
  };
1643
- const listen$2 = async (module, options) => {
1644
- const rawIpc = await module.listen(options);
1645
- if (module.signal) {
1646
- module.signal(rawIpc);
1626
+ const exists = async uri => {
1627
+ assertUri(uri);
1628
+ const path = fileURLToPath(uri);
1629
+ return existsSync(path);
1630
+ };
1631
+ const fallbackRename = async (oldUri, newUri) => {
1632
+ try {
1633
+ const oldPath = fileURLToPath(oldUri);
1634
+ const newPath = fileURLToPath(newUri);
1635
+ await fs.cp(oldPath, newPath, {
1636
+ recursive: true
1637
+ });
1638
+ await fs.rm(oldPath, {
1639
+ recursive: true
1640
+ });
1641
+ } catch (error) {
1642
+ throw new VError(error, `Failed to rename "${oldUri}" to "${newUri}"`);
1647
1643
  }
1648
- const ipc = module.wrap(rawIpc);
1649
- return ipc;
1650
1644
  };
1651
- const create$k = async ({
1652
- commandMap,
1653
- messagePort,
1654
- requiresSocket
1655
- }) => {
1656
- // TODO create a commandMap per rpc instance
1657
- register(commandMap);
1658
- const ipc = await listen$2(IpcChildWithElectronMessagePort$1, {
1659
- messagePort
1660
- });
1661
- if (requiresSocket) {
1662
- ipc.requiresSocket = requiresSocket;
1645
+ const rename = async (oldUri, newUri) => {
1646
+ try {
1647
+ const oldPath = fileURLToPath(oldUri);
1648
+ const newPath = fileURLToPath(newUri);
1649
+ await fs.rename(oldPath, newPath);
1650
+ } catch (error) {
1651
+ if (error && error.code === EXDEV) {
1652
+ return fallbackRename(oldUri, newUri);
1653
+ }
1654
+ throw new VError(error, `Failed to rename "${oldUri}" to "${newUri}"`);
1663
1655
  }
1664
- handleIpc(ipc);
1665
- const rpc = createRpc(ipc);
1666
- return rpc;
1667
1656
  };
1668
- const ElectronMessagePortRpcClient = {
1669
- __proto__: null,
1670
- create: create$k
1657
+ const getPathSeparator = () => {
1658
+ return '/';
1671
1659
  };
1672
- const create$j = async ({
1673
- commandMap
1674
- }) => {
1675
- // TODO create a commandMap per rpc instance
1676
- register(commandMap);
1677
- const ipc = await listen$2(IpcChildWithElectronUtilityProcess$1);
1678
- handleIpc(ipc);
1679
- const rpc = createRpc(ipc);
1680
- return rpc;
1660
+ const stat = async uri => {
1661
+ const path = fileURLToPath(uri);
1662
+ const stats = await fs.stat(path);
1663
+ const type = getDirentType(stats);
1664
+ return type;
1681
1665
  };
1682
- const ElectronUtilityProcessRpcClient = {
1683
- __proto__: null,
1684
- create: create$j
1666
+ const chmod = async (uri, permissions) => {
1667
+ const path = fileURLToPath(uri);
1668
+ await fs.chmod(path, permissions);
1685
1669
  };
1686
- const create$a = async ({
1687
- commandMap
1688
- }) => {
1689
- // TODO create a commandMap per rpc instance
1690
- register(commandMap);
1691
- const ipc = await listen$2(IpcChildWithNodeForkedProcess$1);
1692
- handleIpc(ipc);
1693
- const rpc = createRpc(ipc);
1694
- return rpc;
1670
+ const readJson = async uri => {
1671
+ try {
1672
+ string(uri);
1673
+ assertUri(uri);
1674
+ const path = fileURLToPath(uri);
1675
+ const content = await fs.readFile(path, 'utf8');
1676
+ const parsed = JSON.parse(content);
1677
+ return parsed;
1678
+ } catch (error) {
1679
+ if (isEnoentError(error)) {
1680
+ throw new FileNotFoundError(uri);
1681
+ }
1682
+ throw new VError(error, `Failed to read file as json "${uri}"`);
1683
+ }
1695
1684
  };
1696
- const NodeForkedProcessRpcClient = {
1697
- __proto__: null,
1698
- create: create$a
1685
+ const getFolderSize = async uri => {
1686
+ const path = fileURLToPath(uri);
1687
+ const total = await getFolderSizeInternal(path);
1688
+ return total;
1699
1689
  };
1700
- const create$8 = async ({
1701
- commandMap,
1702
- request,
1703
- handle,
1704
- requiresSocket
1705
- }) => {
1706
- // TODO create a commandMap per rpc instance
1707
- register(commandMap);
1708
- const ipc = await listen$2(IpcChildWithWebSocket, {
1709
- request,
1710
- handle
1711
- });
1712
- if (requiresSocket) {
1713
- ipc.requiresSocket = requiresSocket;
1690
+
1691
+ const fileWacherEvents = Object.create(null);
1692
+ const setupWatcher = async (watcherId, uri, onChange) => {
1693
+ try {
1694
+ const path = fileURLToPath(uri);
1695
+ // TODO
1696
+ const watcher = fs.watch(path, {});
1697
+ const watcherEvents = [];
1698
+ fileWacherEvents[watcherId] = watcherEvents;
1699
+ for await (const event of watcher) {
1700
+ watcherEvents.push(event);
1701
+ onChange();
1702
+ }
1703
+ } catch (error) {
1704
+ if (error && error.code === 'ENOENT') {
1705
+ return;
1706
+ }
1707
+ console.error(new VError(error, `Failed to setup watcher for ${uri}`));
1714
1708
  }
1715
- handleIpc(ipc);
1716
- const rpc = createRpc(ipc);
1717
- return rpc;
1718
- };
1719
- const NodeWebSocketRpcClient = {
1720
- __proto__: null,
1721
- create: create$8
1722
1709
  };
1723
- const create$7 = async ({
1724
- commandMap
1725
- }) => {
1726
- // TODO create a commandMap per rpc instance
1727
- register(commandMap);
1728
- const ipc = await listen$2(IpcChildWithNodeWorker$1);
1729
- handleIpc(ipc);
1730
- const rpc = createRpc(ipc);
1731
- return rpc;
1710
+
1711
+ // TODO when socket closes, dispose file watcher for this socket
1712
+ const watchFile = async (socket, watcherId, uri) => {
1713
+ assertUri(uri);
1714
+ fileWacherEvents[watcherId] = [];
1715
+ // TODO await promise?
1716
+ const onChange = () => {
1717
+ // TODO handle error
1718
+ socket.send({
1719
+ jsonrpc: '2.0',
1720
+ method: 'FileSystem.executeWatchCallback',
1721
+ params: [watcherId]
1722
+ });
1723
+ };
1724
+
1725
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
1726
+ setupWatcher(watcherId, uri, onChange);
1732
1727
  };
1733
- const NodeWorkerRpcClient = {
1734
- __proto__: null,
1735
- create: create$7
1728
+ const unwatchFile = uri => {
1729
+ // TODO
1736
1730
  };
1737
1731
 
1738
1732
  const NodeWorker = 1;
@@ -1786,17 +1780,21 @@ const handleElectronMessagePort = async (messagePort, rpcId) => {
1786
1780
  messagePort
1787
1781
  });
1788
1782
  if (rpcId) {
1789
- set$g(rpcId, rpc);
1783
+ set$2(rpcId, rpc);
1790
1784
  }
1791
1785
  };
1792
1786
 
1787
+ const requiresSocket = method => {
1788
+ return method === 'FileSystem.watchFile';
1789
+ };
1793
1790
  const handleWebSocket = async (handle, request) => {
1794
1791
  object(handle);
1795
1792
  object(request);
1796
1793
  await listen$1({
1797
1794
  method: WebSocket,
1798
1795
  request,
1799
- handle
1796
+ handle,
1797
+ requiresSocket
1800
1798
  });
1801
1799
  };
1802
1800
 
@@ -1817,7 +1815,7 @@ const getPortTuple$1 = async () => {
1817
1815
  const id1 = create();
1818
1816
  const id2 = create();
1819
1817
  // @ts-ignore
1820
- await invoke('TemporaryMessagePort.getPortTuple3', id1, id2, RpcId.FileSystemProcess);
1818
+ await invoke('TemporaryMessagePort.getPortTuple3', id1, id2, FileSystemProcess);
1821
1819
  const port1 = ports[id1];
1822
1820
  const port2 = ports[id2];
1823
1821
  delete ports[id1];
@@ -1855,7 +1853,7 @@ const createMainProcessRpc = async () => {
1855
1853
  } = await getPortTuple();
1856
1854
  await invokeAndTransfer(
1857
1855
  // @ts-ignore
1858
- 'TemporaryMessagePort.sendToElectron', port1, RpcId.MainProcess, RpcId.FileSystemProcess);
1856
+ 'TemporaryMessagePort.sendToElectron', port1, MainProcess, FileSystemProcess);
1859
1857
  const rpc = await ElectronMessagePortRpcClient.create({
1860
1858
  commandMap: {},
1861
1859
  messagePort: port2
@@ -1881,7 +1879,7 @@ const commandMap = {
1881
1879
  'FileSystem.readDirWithFileTypes': readDirWithFileTypes,
1882
1880
  'FileSystem.readFile': readFile,
1883
1881
  'FileSystem.readJson': readJson,
1884
- 'FileSystem.remove': remove$1,
1882
+ 'FileSystem.remove': remove,
1885
1883
  'FileSystem.rename': rename,
1886
1884
  'FileSystem.stat': stat,
1887
1885
  'FileSystem.unwatchFile': unwatchFile,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-system-process",
3
- "version": "3.5.0",
3
+ "version": "3.7.0",
4
4
  "description": "File System Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",
@@ -18,7 +18,7 @@
18
18
  "file-system-process": "bin/fileSystemProcess.js"
19
19
  },
20
20
  "dependencies": {
21
- "trash": "^9.0.0",
21
+ "trash": "^10.0.0",
22
22
  "ws": "^8.18.3"
23
23
  },
24
24
  "engines": {