-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSystem.js
More file actions
34 lines (30 loc) · 1.41 KB
/
fileSystem.js
File metadata and controls
34 lines (30 loc) · 1.41 KB
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
const fs = require('fs');
const path = require('path');
// TODO PE; 2018-08-20; переименовать?
function getFileInDir(directoryPath, extension, filePaths) {
filePaths = filePaths || [];
// TODO Anonymous Developer; 2016-03-17; Необходимо переписать этот код и использовать асинхронные версии функций для чтения из файла
const fileNames = fs.readdirSync(directoryPath);
for (const fileName of fileNames) {
// TODO WinDev; ; Убедиться, что будет работать под Windows.
const filePath = directoryPath + '/' + fileName;
if (fs.statSync(filePath).isDirectory()) {
getFileInDir(filePath, filePaths);
} else if (filePath.endsWith(`.${extension}`)) {
filePaths.push(filePath);
}
}
return filePaths;
}
function readFile(filePath) {
return fs.readFileSync(filePath, 'utf8'); // TODO Veronika; 2018-08-16; сделать кодировку настраиваемой
}
// TODO Digi; 2018-09-21; Добавить функцию getFileName, которая по пути файла будет возвращать его имя. Воспользоваться модулем path из Node.js
function getFileName(filePath) {
return path.basename(filePath).toString();
}
module.exports = {
getFileInDir,
readFile,
getFileName,
};