2020-03-21 15:46:10 +05:45
|
|
|
const fs = require('fs');
|
2020-12-20 01:43:36 +02:00
|
|
|
const flatten = require('lodash/flatten');
|
2020-03-21 15:46:10 +05:45
|
|
|
const os = require('os');
|
|
|
|
const path = require('path');
|
|
|
|
const { promisify } = require('util');
|
|
|
|
|
2020-03-24 20:35:47 +05:45
|
|
|
// Promisify common fs functions.
|
2020-03-21 15:46:10 +05:45
|
|
|
const stat = promisify(fs.stat);
|
2020-03-24 20:35:47 +05:45
|
|
|
const readFile = promisify(fs.readFile);
|
|
|
|
const writeFile = promisify(fs.writeFile);
|
|
|
|
const readdir = promisify(fs.readdir);
|
2020-10-05 23:58:22 +03:00
|
|
|
const mkdir = promisify(fs.mkdir);
|
2020-03-21 15:46:10 +05:45
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a temporary directory and returns it path.
|
|
|
|
*
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
*/
|
|
|
|
function createTemp() {
|
|
|
|
return promisify(fs.mkdtemp)(`${os.tmpdir()}${path.sep}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures the given path exists.
|
|
|
|
* - If the path already exist, it's fine - it does nothing.
|
|
|
|
* - If the path doesn't exist, it will create it.
|
|
|
|
*
|
|
|
|
* @param {string} path
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
function ensureDirectoryExists(dir) {
|
2020-10-05 23:58:22 +03:00
|
|
|
return stat(dir).catch(() => mkdir(dir, { recursive: true }));
|
2020-03-21 15:46:10 +05:45
|
|
|
}
|
|
|
|
|
2020-08-08 14:21:43 +02:00
|
|
|
/**
|
2020-08-10 15:10:37 +02:00
|
|
|
* Read a directory,
|
|
|
|
* sorting folders and files by alphabetically order.
|
|
|
|
* Can be browsed recursively.
|
2020-08-08 14:21:43 +02:00
|
|
|
*
|
|
|
|
* @param {string} dir
|
2020-08-10 15:10:37 +02:00
|
|
|
* The directory to analyse
|
|
|
|
*
|
|
|
|
* @param {boolean} recursive
|
|
|
|
* Browse directory recursively
|
2020-08-08 14:21:43 +02:00
|
|
|
*
|
|
|
|
* @returns {Promise<[string]>}
|
2020-08-10 15:10:37 +02:00
|
|
|
* All found files, concatenated to the current dir
|
2020-08-08 14:21:43 +02:00
|
|
|
*/
|
2020-08-10 15:10:37 +02:00
|
|
|
async function getFilepathsInFolder(dir, recursive = false) {
|
2020-08-08 14:21:43 +02:00
|
|
|
const pathsList = await readdir(dir);
|
2020-12-11 20:18:03 +09:00
|
|
|
return flatten(
|
|
|
|
await Promise.all(
|
|
|
|
pathsList.sort().map(async (currentPath) => {
|
|
|
|
const currentFile = path.resolve(dir, currentPath);
|
|
|
|
const statFile = await stat(currentFile);
|
|
|
|
if (statFile && statFile.isDirectory()) {
|
|
|
|
if (recursive) {
|
|
|
|
return await getFilepathsInFolder(currentFile, true);
|
|
|
|
}
|
|
|
|
return [];
|
2020-08-10 15:10:37 +02:00
|
|
|
}
|
2020-12-11 20:18:03 +09:00
|
|
|
return [currentFile];
|
|
|
|
})
|
|
|
|
)
|
2020-08-08 14:21:43 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-21 15:46:10 +05:45
|
|
|
module.exports = {
|
|
|
|
stat,
|
2020-03-24 20:35:47 +05:45
|
|
|
readdir,
|
|
|
|
readFile,
|
|
|
|
writeFile,
|
2020-03-21 15:46:10 +05:45
|
|
|
createTemp,
|
|
|
|
ensureDirectoryExists,
|
2020-08-10 15:10:37 +02:00
|
|
|
getFilepathsInFolder,
|
2020-03-21 15:46:10 +05:45
|
|
|
};
|