-
Notifications
You must be signed in to change notification settings - Fork 1
/
AVFileCreator.js
50 lines (40 loc) · 1.5 KB
/
AVFileCreator.js
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
import fs from 'fs';
import path from 'path';
import chokidar from 'chokidar';
export const concatenateFilesInDirectory = (directoryPath, targetFilePath) => {
console.log('directpath ', directoryPath);
const directoryCheckInterval = setInterval(() => {
if (fs.existsSync(directoryPath)) {
console.log(`Directory found: ${directoryPath}`);
// Stop checking for the directory
clearInterval(directoryCheckInterval);
// Initial concatenation of existing files
concatenateExistingFiles(directoryPath, targetFilePath);
// Watch the directory for added or changed files
const watcher = chokidar.watch(directoryPath, { ignored: /^\./, persistent: true });
watcher.on('add', filePath => {
if (!filePath.endsWith('crdownload')) {
console.log(`File ${filePath} has been added`);
setTimeout(() => {
appendFileContent(filePath, targetFilePath);
}, 1000);
}
});
}
});
};
const concatenateExistingFiles = (directoryPath, targetFilePath) => {
fs.readdir(directoryPath, (err, files) => {
if (err) throw err;
// Clear the target file before appending
fs.writeFileSync(targetFilePath, '');
files.forEach(file => {
const filePath = path.join(directoryPath, file);
appendFileContent(filePath, targetFilePath);
});
});
};
const appendFileContent = (sourceFilePath, targetFilePath) => {
const content = fs.readFileSync(sourceFilePath);
fs.appendFileSync(targetFilePath, content);
};