UNPKG

@a2r/fs

Version:

A2R file system library

162 lines (161 loc) 6.36 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.copyContents = exports.getFilesRecursively = exports.ensureDir = exports.emptyFolder = exports.exists = exports.rename = exports.unlink = exports.copyFile = exports.readFile = exports.readDir = exports.writeFile = exports.rimraf = exports.rmDir = exports.mkDir = void 0; const fs = __importStar(require("fs")); const util = __importStar(require("util")); const path = __importStar(require("path")); const rimraf_1 = __importDefault(require("rimraf")); /** * Create a directory */ exports.mkDir = util.promisify(fs.mkdir); /** * Delete a directory. */ exports.rmDir = util.promisify(fs.rmdir); /** * The [UNIX command](http://en.wikipedia.org/wiki/Rm_(Unix)) `rm -rf` for node */ exports.rimraf = util.promisify(rimraf_1.default); /** * Writes data to a file, replacing the file if it already exists. */ exports.writeFile = util.promisify(fs.writeFile); /** * Read a directory. */ exports.readDir = util.promisify(fs.readdir); /** * Reads the entire contents of a file. */ exports.readFile = util.promisify(fs.readFile); /** * Copies `src` to `dest`. By default, dest is overwritten if it already exists. No arguments other than a possible exception are given to the callback function. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination. */ exports.copyFile = util.promisify(fs.copyFile); /** * Deletes a name and possibly the file it refers to. */ exports.unlink = util.promisify(fs.unlink); /** * Change the name or location of a file or directory */ exports.rename = util.promisify(fs.rename); /** * Test whether or not the given path exists by checking with the file system. * @param {fs.PathLike} pathToCheck A path to a file or directory. If a URL is provided, it must use the `file:` protocol. URL support is experimental. */ const exists = (pathToCheck) => new Promise((resolve) => { fs.access(pathToCheck, fs.constants.F_OK, (err) => { if (err) { return resolve(false); } return resolve(true); }); }); exports.exists = exists; /** * Empties given folder by removing it and creating it again * @param {string} folderPath Folder path to be emptied */ const emptyFolder = async (folderPath) => { const pathExists = await exports.exists(folderPath); if (pathExists) { await exports.rimraf(folderPath); } await exports.mkDir(folderPath, { recursive: true }); }; exports.emptyFolder = emptyFolder; /** * Ensures that given dir path exists * @param {string} folderPath Path to ensure * @param {fs.MakeDirectoryOptions} options Options */ const ensureDir = async (folderPath, options, recursive = true) => { await new Promise((resolve, reject) => { fs.mkdir(folderPath, Object.assign(Object.assign({}, (options || {})), { recursive }), (err) => { if (err && err.code !== 'EEXIST') { reject(err); } else { resolve(); } }); }); }; exports.ensureDir = ensureDir; /** * Gets files recursively * @param folderPath Path to get files from * @param extName Extension names to filter files (including `.`) */ const getFilesRecursively = async (folderPath, extName) => { const contents = await exports.readDir(folderPath, { encoding: 'utf8', withFileTypes: true, }); const files = await Promise.all(contents.map(async (content) => { if (content.isDirectory()) { const folderFiles = await exports.getFilesRecursively(path.resolve(folderPath, content.name), extName); return folderFiles; } const ext = path.extname(content.name); if (!extName || !extName.length || extName.indexOf(ext) !== -1) { return [path.resolve(folderPath, content.name)]; } return []; })); return files.reduce((t, f) => [...t, ...f], []); }; exports.getFilesRecursively = getFilesRecursively; /** * Copies contents recursively from `fromPath` to `destPath` * @param {string} fromPath Source path * @param {string} destPath Destination path * @param {boolean} [hard=true] Hard mode (overwrites existing files) * @param {string} [relativePath=''] Relative path */ const copyContents = async (fromPath, destPath, hard = true, relativePath = '') => { const contentsPath = path.resolve(fromPath, relativePath); await exports.ensureDir(destPath); const contents = await exports.readDir(contentsPath, { withFileTypes: true }); await Promise.all(contents.map(async (pathInfo) => { const { name: relPath } = pathInfo; const fullRelPath = path.join(relativePath, relPath); const sourcePath = path.resolve(fromPath, fullRelPath); const targetPath = path.resolve(destPath, fullRelPath); if (pathInfo.isDirectory()) { await exports.ensureDir(targetPath); await exports.copyContents(fromPath, destPath, hard, fullRelPath); } else { const write = hard || !(await exports.exists(targetPath)); if (write) { await exports.copyFile(sourcePath, targetPath); } } })); }; exports.copyContents = copyContents;