-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
53 lines (47 loc) · 1.49 KB
/
utils.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
const fs = require('fs');
const path = require('path');
/**
* Retrieve file paths from a given folder and its subfolders
* @param folderPath
* @return {String[]}
*/
const getFilePaths = (folderPath) => {
const entryPaths = fs.readdirSync(folderPath).map(entry => path.join(folderPath, entry));
const filePaths = entryPaths.filter(entryPath => fs.statSync(entryPath).isFile());
const dirPaths = entryPaths.filter(entryPath => !filePaths.includes(entryPath));
const dirFiles = dirPaths.reduce((prev, curr) => prev.concat(getFilePaths(curr)), []);
return [...filePaths, ...dirFiles];
};
/**
* Moves given file to target-folder
* @return {Promise<VoidFunction>}
*/
const moveFile = (file, targetFolder, prefix) => {
const [fileName] = file.split('/').splice(-1);
const newPath = `${targetFolder}/${prefix ? prefix : ''}${fileName}`;
return new Promise(((resolve, reject) => fs.rename(file, newPath, (err) => err ? reject(err) : resolve())));
};
/**
* Ensures the given path points to an existing folder
* @param path
* @param mkdir if exist returns false, create a folder at path
*/
const ensureExistingFolder = (path, mkdir) => {
try {
if (fs.existsSync(path)) {
return fs.lstatSync(path).isDirectory();
}
if (mkdir) {
fs.mkdirSync(path);
return true;
}
} catch (e) {
console.error(e);
}
return false;
};
module.exports = {
getFilePaths,
moveFile,
ensureExistingFolder,
};