@lvce-editor/file-watcher-process 4.13.0 → 4.14.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 +35 -6
  2. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { fileURLToPath, pathToFileURL } from 'node:url';
2
2
  import * as fs from 'node:fs/promises';
3
+ import { readFile } from 'node:fs/promises';
3
4
  import { FSWatcher } from 'chokidar';
4
- import { join } from 'node:path';
5
+ import ignore from 'ignore';
6
+ import { join, relative } from 'node:path';
5
7
 
6
8
  const handleUncaughtExceptionMonitor = error => {
7
9
  console.error(`[file-watcher-process] Uncaught Exception: ${error}`);
@@ -1388,19 +1390,45 @@ const normalizeEvent2 = (id, eventName, path) => {
1388
1390
  };
1389
1391
  };
1390
1392
 
1393
+ const createIgnore = ignore;
1391
1394
  const errorCallback = error => {
1392
1395
  console.error(`[file-watcher-process] ${error}`);
1393
1396
  };
1394
- const createIgnored = exclude => {
1397
+ const loadGitIgnore = async (root, enabled) => {
1398
+ try {
1399
+ const content = await readFile(`${root}/.gitignore`, 'utf8');
1400
+ return createIgnore().add(content);
1401
+ } catch {
1402
+ return undefined;
1403
+ }
1404
+ };
1405
+ const createExcludedNamesFilter = exclude => {
1395
1406
  const excludedNames = new Set(exclude);
1396
- return path => {
1397
- return path.split(/[\\/]/).some(part => excludedNames.has(part));
1407
+ return path => path.split(/[\\/]/).some(part => excludedNames.has(part));
1408
+ };
1409
+ const createIgnored = async (root, exclude, useGitIgnore) => {
1410
+ const isExcludedName = createExcludedNamesFilter(exclude);
1411
+ const gitIgnore = await loadGitIgnore(root);
1412
+ return (path, stats) => {
1413
+ if (isExcludedName(path)) {
1414
+ return true;
1415
+ }
1416
+ if (!gitIgnore) {
1417
+ return false;
1418
+ }
1419
+ const relativePath = relative(root, path).replaceAll('\\', '/');
1420
+ if (!relativePath || relativePath.startsWith('../')) {
1421
+ return false;
1422
+ }
1423
+ const candidate = stats?.isDirectory() ? `${relativePath}/` : relativePath;
1424
+ return gitIgnore.ignores(candidate);
1398
1425
  };
1399
1426
  };
1400
1427
  const watchFolders = async ({
1401
1428
  exclude,
1402
1429
  id,
1403
- roots
1430
+ roots,
1431
+ useGitIgnore = false
1404
1432
  }) => {
1405
1433
  const callBackInternal = async (eventName, path, stats) => {
1406
1434
  const event = normalizeEvent2(id, eventName, path);
@@ -1418,8 +1446,9 @@ const watchFolders = async ({
1418
1446
  try {
1419
1447
  for (const root of roots) {
1420
1448
  const path = fileURLToPath(root);
1449
+ const ignored = useGitIgnore ? await createIgnored(path, exclude, true) : createExcludedNamesFilter(exclude);
1421
1450
  const watcher = new FSWatcher({
1422
- ignored: createIgnored(exclude),
1451
+ ignored,
1423
1452
  ignoreInitial: true,
1424
1453
  ignorePermissionErrors: true
1425
1454
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-watcher-process",
3
- "version": "4.13.0",
3
+ "version": "4.14.0",
4
4
  "description": "File Watcher Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",
@@ -19,7 +19,8 @@
19
19
  },
20
20
  "dependencies": {
21
21
  "@lvce-editor/assert": "^1.5.1",
22
- "chokidar": "^5.0.0"
22
+ "chokidar": "^5.0.0",
23
+ "ignore": "^5.3.2"
23
24
  },
24
25
  "engines": {
25
26
  "node": ">=22"