-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathaddWatchersToParentFolders.ts
83 lines (74 loc) · 2.87 KB
/
addWatchersToParentFolders.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import fsSync, { type WatchEventType } from 'fs';
import path from 'path';
import { getUserData } from '../filesystem';
import logger from '../logger';
import getParentFolderPaths from './getParentFolderPaths';
import checkForFolderModifications from './checkForFolderModifications';
import { saveAbortController } from './controlAbortControllers';
const fileNameRegex = /^.{1,}\.\w{1,}$/;
const parentFolderWatcherFunction = async (eventType: WatchEventType, filename?: string | null) => {
if (filename) {
if (eventType === 'rename') {
// if not a filename, it should be a directory
const isADirectory = !fileNameRegex.test(filename);
if (isADirectory) {
// possible folder addition or deletion
checkForFolderModifications(filename);
}
}
} else {
logger.warn('Failed to watch parent folders because watcher sent an undefined filename', {
eventType,
filename
});
}
};
const addWatcherToParentFolder = (parentFolderPath: string) => {
try {
const abortController = new AbortController();
const watcher = fsSync.watch(
parentFolderPath,
{
signal: abortController.signal,
// TODO - recursive mode won't work on linux
recursive: true
},
(eventType, filename) => parentFolderWatcherFunction(eventType, filename)
);
logger.debug('Added watcher to a parent folder successfully.', { parentFolderPath });
watcher.addListener('error', (error) =>
logger.error(`Error occurred when watching a folder.`, { error, parentFolderPath })
);
watcher.addListener('close', () =>
logger.debug(`Successfully closed the parent folder watcher.`, { parentFolderPath })
);
saveAbortController(parentFolderPath, abortController);
} catch (error) {
logger.error(`Error occurred when watching a folder.`, { error, parentFolderPath });
}
};
/* Parent folder watchers only watch for folder modifications (not file modifications) inside the parent folder. */
const addWatchersToParentFolders = async () => {
const { musicFolders } = getUserData();
const musicFolderPaths = musicFolders.map((folder) => folder.path);
const parentFolderPaths = getParentFolderPaths(musicFolderPaths);
logger.debug(`${parentFolderPaths.length} parent folders of music folders found.`);
if (Array.isArray(parentFolderPaths) && parentFolderPaths.length > 0) {
for (const parentFolderPath of parentFolderPaths) {
try {
addWatcherToParentFolder(parentFolderPath);
} catch (error) {
logger.error(
`Failed to add watcher to '${path.basename(parentFolderPath)}' parent folder.`,
{ error, parentFolderPath }
);
}
}
return;
}
logger.warn(
`Failed to add watchers to parent folders of music folders. No parent folders found.`,
{ parentFolderPaths, musicFolderPaths }
);
};
export default addWatchersToParentFolders;