-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFiles.js
60 lines (47 loc) · 1.66 KB
/
getFiles.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
51
52
53
54
55
56
57
58
59
60
import fs from "fs";
import _ from "lodash";
import {addFiles, updateFiles} from "./actions/actions.js";
import dotenv from "dotenv"
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
const testPath = join(dirname(fileURLToPath(import.meta.url)), '/testFiles/');
const PATH = process.env.FILES_PATH || testPath
dotenv.config();
export function getFiles(currentState) {
const files = fs.readdirSync(PATH)
if (currentState === [])
getInitialFiles(files)
else
checkFilesUpdate(currentState)
}
function getInitialFiles(files) {
let filesArray = []
files.forEach(file => {
filesArray.push({name: file, active: true})
})
addFiles(filesArray)
}
function checkFilesUpdate(currentState) {
let stateFiles = []
const directoryFiles = fs.readdirSync(PATH)
currentState.forEach(file => {
stateFiles.push(file.name)
})
const newFilesAdded = _.difference(directoryFiles, stateFiles)
const removedFiles = _.difference(stateFiles, directoryFiles)
const intersectingFiles = _.intersection(directoryFiles, stateFiles)
getUpdatedFiles(newFilesAdded, removedFiles, intersectingFiles)
}
function getUpdatedFiles(newFilesAdded = null, removedFiles = null, intersectingFiles) {
let newStateArray = []
newFilesAdded.forEach(file => {
newStateArray.push({name: file, active: true})
})
removedFiles.forEach(file => {
newStateArray.push({name: file, active: false})
})
intersectingFiles.forEach(file => {
newStateArray.push({name: file, active: true})
})
updateFiles(newStateArray)
}