@lvce-editor/file-watcher-process 4.3.0 → 4.5.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 +115 -25
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { fileURLToPath, pathToFileURL } from 'node:url';
2
2
  import * as fs from 'node:fs/promises';
3
3
  import { FSWatcher } from 'chokidar';
4
+ import { join } from 'node:path';
4
5
 
5
6
  const handleUncaughtExceptionMonitor = error => {
6
7
  console.error(`[file-watcher-process] Uncaught Exception: ${error}`);
@@ -15,7 +16,7 @@ class AssertionError extends Error {
15
16
  const Object$1 = 1;
16
17
  const Number = 2;
17
18
  const Array$1 = 3;
18
- const String = 4;
19
+ const String$1 = 4;
19
20
  const Boolean = 5;
20
21
  const Function = 6;
21
22
  const Null = 7;
@@ -27,7 +28,7 @@ const getType = value => {
27
28
  case 'function':
28
29
  return Function;
29
30
  case 'string':
30
- return String;
31
+ return String$1;
31
32
  case 'object':
32
33
  if (value === null) {
33
34
  return Null;
@@ -50,7 +51,7 @@ const number = value => {
50
51
  };
51
52
  const string = value => {
52
53
  const type = getType(value);
53
- if (type !== String) {
54
+ if (type !== String$1) {
54
55
  throw new AssertionError('expected value to be of type string');
55
56
  }
56
57
  };
@@ -1234,6 +1235,47 @@ const waitForWatcherToBeReady = async watcher => {
1234
1235
  await promise;
1235
1236
  };
1236
1237
 
1238
+ const success = {
1239
+ ok: true
1240
+ };
1241
+ const successWithInotifyWatchCount = inotifyWatchCount => {
1242
+ return {
1243
+ inotifyWatchCount,
1244
+ ok: true
1245
+ };
1246
+ };
1247
+ const getErrorCode = error => {
1248
+ if (error && typeof error === 'object' && 'code' in error && typeof error.code === 'string') {
1249
+ return error.code;
1250
+ }
1251
+ return undefined;
1252
+ };
1253
+ const fromError = error => {
1254
+ if (error instanceof Error) {
1255
+ const code = getErrorCode(error);
1256
+ return {
1257
+ error: {
1258
+ ...(code && {
1259
+ code
1260
+ }),
1261
+ message: error.message,
1262
+ name: error.name,
1263
+ ...(error.stack && {
1264
+ stack: error.stack
1265
+ })
1266
+ },
1267
+ ok: false
1268
+ };
1269
+ }
1270
+ return {
1271
+ error: {
1272
+ message: String(error),
1273
+ name: 'Error'
1274
+ },
1275
+ ok: false
1276
+ };
1277
+ };
1278
+
1237
1279
  const callBackInternal = async (eventName, path, stats) => {
1238
1280
  const event = normalizeEvent(eventName, path);
1239
1281
  await invoke('FileWatcher.handleChange', event);
@@ -1245,13 +1287,55 @@ const errorCallback$1 = error => {
1245
1287
  console.error(`[file-watcher-process] ${error}`);
1246
1288
  };
1247
1289
  const watchFolder = async uri => {
1248
- const path = fileURLToPath(uri);
1249
- const watcher = new FSWatcher();
1250
- watcher.on('all', callback);
1251
- watcher.on('error', errorCallback$1);
1252
- const readyPromise = waitForWatcherToBeReady(watcher);
1253
- watcher.add(path);
1254
- await readyPromise;
1290
+ let watcher;
1291
+ try {
1292
+ const path = fileURLToPath(uri);
1293
+ watcher = new FSWatcher();
1294
+ watcher.on('all', callback);
1295
+ watcher.on('error', errorCallback$1);
1296
+ const readyPromise = waitForWatcherToBeReady(watcher);
1297
+ watcher.add(path);
1298
+ await readyPromise;
1299
+ return success;
1300
+ } catch (error) {
1301
+ if (watcher) {
1302
+ await Promise.allSettled([watcher.close()]);
1303
+ }
1304
+ return fromError(error);
1305
+ }
1306
+ };
1307
+
1308
+ // cspell:disable-next-line
1309
+ const fdInfoPath = '/proc/self/fdinfo';
1310
+ const countInotifyWatches = fdInfoContents => {
1311
+ let count = 0;
1312
+ for (const content of fdInfoContents) {
1313
+ for (const line of content.split('\n')) {
1314
+ if (line.startsWith('inotify ')) {
1315
+ count++;
1316
+ }
1317
+ }
1318
+ }
1319
+ return count;
1320
+ };
1321
+ const readFdInfo = async fileName => {
1322
+ try {
1323
+ return await fs.readFile(join(fdInfoPath, fileName), 'utf8');
1324
+ } catch {
1325
+ return '';
1326
+ }
1327
+ };
1328
+ const getInotifyWatchCount = async () => {
1329
+ if (process.platform !== 'linux') {
1330
+ return undefined;
1331
+ }
1332
+ try {
1333
+ const fileNames = await fs.readdir(fdInfoPath);
1334
+ const fdInfoContents = await Promise.all(fileNames.map(readFdInfo));
1335
+ return countInotifyWatches(fdInfoContents);
1336
+ } catch {
1337
+ return undefined;
1338
+ }
1255
1339
  };
1256
1340
 
1257
1341
  const normalizeEvent2 = (id, eventName, path) => {
@@ -1283,21 +1367,22 @@ const watchFolders = async ({
1283
1367
  const callback = (eventName, path, stats) => {
1284
1368
  void callBackInternal(eventName, path);
1285
1369
  };
1286
- const watcherEntries = roots.map(root => {
1287
- const path = fileURLToPath(root);
1288
- const watcher = new FSWatcher({
1289
- ignoreInitial: true,
1290
- ignorePermissionErrors: true
1291
- });
1292
- watcher.on('error', errorCallback);
1293
- const readyPromise = waitForWatcherToBeReady(watcher);
1294
- watcher.add(path);
1295
- return {
1296
- readyPromise,
1297
- watcher
1298
- };
1299
- });
1370
+ const watcherEntries = [];
1300
1371
  try {
1372
+ for (const root of roots) {
1373
+ const path = fileURLToPath(root);
1374
+ const watcher = new FSWatcher({
1375
+ ignoreInitial: true,
1376
+ ignorePermissionErrors: true
1377
+ });
1378
+ watcher.on('error', errorCallback);
1379
+ const readyPromise = waitForWatcherToBeReady(watcher);
1380
+ watcherEntries.push({
1381
+ readyPromise,
1382
+ watcher
1383
+ });
1384
+ watcher.add(path);
1385
+ }
1301
1386
  await Promise.all(watcherEntries.map(({
1302
1387
  readyPromise
1303
1388
  }) => readyPromise));
@@ -1305,13 +1390,18 @@ const watchFolders = async ({
1305
1390
  await Promise.allSettled(watcherEntries.map(({
1306
1391
  watcher
1307
1392
  }) => watcher.close()));
1308
- throw error;
1393
+ return fromError(error);
1309
1394
  }
1310
1395
  for (const {
1311
1396
  watcher
1312
1397
  } of watcherEntries) {
1313
1398
  watcher.on('all', callback);
1314
1399
  }
1400
+ const inotifyWatchCount = await getInotifyWatchCount();
1401
+ if (inotifyWatchCount === undefined) {
1402
+ return success;
1403
+ }
1404
+ return successWithInotifyWatchCount(inotifyWatchCount);
1315
1405
  };
1316
1406
 
1317
1407
  const commandMap = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-watcher-process",
3
- "version": "4.3.0",
3
+ "version": "4.5.0",
4
4
  "description": "File Watcher Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",