jam-my-stack/src/file-utils.js

25 lines
685 B
JavaScript
Raw Normal View History

const { existsSync, mkdirSync } = require("fs");
2021-03-04 21:06:28 +01:00
const { resolve } = require('path');
const fs = require('fs').promises;
2021-03-04 21:06:28 +01:00
// https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
async function getFiles(dir) {
const dirents = await fs.readdir(dir, { withFileTypes: true });
const files = await Promise.all(dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
}));
return Array.prototype.concat(...files);
}
function createIfNotExists(dir) {
if(!existsSync(dir)) {
mkdirSync(dir, { recursive: true })
}
}
2021-03-04 21:06:28 +01:00
module.exports = {
getFiles,
createIfNotExists
2021-03-04 21:06:28 +01:00
}