UNPKG

@egodigital/egoose

Version:

Helper classes and functions for Node.js 10 or later.

348 lines 11.9 kB
"use strict"; /** * This file is part of the @egodigital/egoose distribution. * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/) * * @egodigital/egoose is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, version 3. * * @egodigital/egoose is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const fastGlob = require("fast-glob"); const fs = require("fs-extra"); const MergeDeep = require("merge-deep"); const tmp = require("tmp"); const index_1 = require("../index"); async function checkExistingFSItemByStats(path, useLSTAT, flagProvider) { useLSTAT = index_1.toBooleanSafe(useLSTAT); if (await exists(path)) { return flagProvider(await (useLSTAT ? fs.lstat : fs.stat)(path)); } return false; } function checkExistingFSItemByStatsSync(path, useLSTAT, flagProvider) { useLSTAT = index_1.toBooleanSafe(useLSTAT); if (fs.existsSync(path)) { return flagProvider((useLSTAT ? fs.lstatSync : fs.statSync)(path)); } return false; } /** * Promise version of 'fs.exists()'. * * @param {string} path The path. * * @return {Promise<boolean>} The promise that indicates if path exists or not. */ function exists(path) { return new Promise((resolve, reject) => { try { fs.exists(path, (itemExists) => { resolve(itemExists); }); } catch (e) { reject(e); } }); } exports.exists = exists; /** * Scans for files. * * @param {string|string[]} patterns One or more glob patterns. * @param {FastGlob.Options} [opts] Custom options. * * @return {Promise<string[]>} The promise with the found items. */ function glob(patterns, opts) { return fastGlob(normalizeGlobPatterns(patterns), mergeGlobOptions(opts)); } exports.glob = glob; /** * Scans for files (synchronious). * * @param {string|string[]} patterns One or more glob patterns. * @param {FastGlob.Options} [opts] Custom options. * * @return {string[]} The found items. */ function globSync(patterns, opts) { return fastGlob.sync(normalizeGlobPatterns(patterns), mergeGlobOptions(opts)); } exports.globSync = globSync; /** * Checks if a path represents an existing block device. * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'. * * @return {Promise<boolean>} The promise with the boolean that represents if path is a block device or not. */ function isBlockDevice(path, useLSTAT) { return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isBlockDevice()); } exports.isBlockDevice = isBlockDevice; /** * Checks if a path represents an existing block device (synchronous). * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'. * * @return {boolean} The boolean that represents if path is a block device or not. */ function isBlockDeviceSync(path, useLSTAT) { return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isBlockDevice()); } exports.isBlockDeviceSync = isBlockDeviceSync; /** * Checks if a path represents an existing block device. * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'. * * @return {Promise<boolean>} The promise with the boolean that represents if path is a block device or not. */ function isCharDevice(path, useLSTAT) { return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isCharacterDevice()); } exports.isCharDevice = isCharDevice; /** * Checks if a path represents an existing character device (synchronous). * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'. * * @return {boolean} The boolean that represents if path is a character device or not. */ function isCharDeviceSync(path, useLSTAT) { return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isCharacterDevice()); } exports.isCharDeviceSync = isCharDeviceSync; /** * Checks if a path represents an existing FIFO. * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'. * * @return {Promise<boolean>} The promise with the boolean that represents if path is a FIFO or not. */ function isFIFO(path, useLSTAT) { return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isFIFO()); } exports.isFIFO = isFIFO; /** * Checks if a path represents an existing FIFO (synchronous). * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'. * * @return {boolean} The boolean that represents if path is a FIFO or not. */ function isFIFOSync(path, useLSTAT) { return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isFIFO()); } exports.isFIFOSync = isFIFOSync; /** * Checks if a path represents an existing directory. * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'. * * @return {Promise<boolean>} The promise with the boolean that represents if path is a directory or not. */ function isDir(path, useLSTAT) { return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isDirectory()); } exports.isDir = isDir; /** * Checks if a path represents an existing directory (synchronous). * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'. * * @return {boolean} The boolean that represents if path is a directory or not. */ function isDirSync(path, useLSTAT) { return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isDirectory()); } exports.isDirSync = isDirSync; /** * Checks if a path represents an existing file. * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'. * * @return {Promise<boolean>} The promise with the boolean that represents if path is a file or not. */ function isFile(path, useLSTAT) { return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isFile()); } exports.isFile = isFile; /** * Checks if a path represents an existing file (synchronous). * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'. * * @return {boolean} The boolean that represents if path is a file or not. */ function isFileSync(path, useLSTAT) { return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isFile()); } exports.isFileSync = isFileSync; /** * Checks if a path represents an existing socket. * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstat()' function instead of 'fs.stat()'. * * @return {Promise<boolean>} The promise with the boolean that represents if path is a socket or not. */ function isSocket(path, useLSTAT) { return checkExistingFSItemByStats(path, useLSTAT, (stats) => stats.isSocket()); } exports.isSocket = isSocket; /** * Checks if a path represents an existing socket (synchronous). * * @param {FileSystemPath} path The path to check. * @param {boolean} [useLSTAT] Use 'fs.lstatSync()' function instead of 'fs.statSync()'. * * @return {boolean} The boolean that represents if path is a socket or not. */ function isSocketSync(path, useLSTAT) { return checkExistingFSItemByStatsSync(path, useLSTAT, (stats) => stats.isSocket()); } exports.isSocketSync = isSocketSync; /** * Checks if a path represents an existing symbolic link. * * @param {FileSystemPath} path The path to check. * * @return {Promise<boolean>} The promise with the boolean that represents if path is a symbolic link or not. */ function isSymLink(path) { return checkExistingFSItemByStats(path, true, (stats) => stats.isSymbolicLink()); } exports.isSymLink = isSymLink; /** * Checks if a path represents an existing symbolic link (synchronous). * * @param {FileSystemPath} path The path to check. * * @return {boolean} The boolean that represents if path is a symbolic link or not. */ function isSymLinkSync(path) { return checkExistingFSItemByStatsSync(path, true, (stats) => stats.isSymbolicLink()); } exports.isSymLinkSync = isSymLinkSync; function mergeGlobOptions(opts) { const DEFAULT_OPTS = { absolute: true, dot: true, followSymbolicLinks: true, markDirectories: false, onlyDirectories: false, onlyFiles: true, stats: false, unique: true, }; return MergeDeep(DEFAULT_OPTS, opts); } function normalizeGlobPatterns(patterns) { return index_1.asArray(patterns).map(p => index_1.toStringSafe(p)) .filter(p => '' !== p.trim()); } /** * Executes an action for a temp file. * * @param {Function} action The action to invoke. * @param {TempFileOptions} [opts] Custom options. * * @return {Promise<TResult>} The promise with the result of the action. */ function tempFile(action, opts) { if (_.isNil(opts)) { opts = {}; } return new Promise((resolve, reject) => { try { tmp.tmpName(toSimpleTempOptions(opts), (err, tmpFile) => { if (err) { reject(err); } else { const TRY_DELETE_TEMP_FILE = () => { if (!index_1.toBooleanSafe(opts.keep)) { try { fs.unlinkSync(tmpFile); } catch { } } }; try { Promise.resolve(action(tmpFile)).then((res) => { TRY_DELETE_TEMP_FILE(); resolve(res); }).catch((err) => { TRY_DELETE_TEMP_FILE(); reject(err); }); } catch (e) { reject(e); } } }); } catch (e) { reject(e); } }); } exports.tempFile = tempFile; /** * Executes an action for a temp file (sync). * * @param {Function} action The action to invoke. * @param {TempFileOptions} [opts] Custom options. * * @return {TResult} The result of the action. */ function tempFileSync(action, opts) { if (_.isNil(opts)) { opts = {}; } const TEMP_FILE = tmp.tmpNameSync(toSimpleTempOptions(opts)); try { return action(TEMP_FILE); } finally { if (!index_1.toBooleanSafe(opts.keep)) { try { fs.unlinkSync(TEMP_FILE); } catch { } } } } exports.tempFileSync = tempFileSync; function toSimpleTempOptions(opts) { return { dir: _.isNil(opts.dir) ? undefined : index_1.toStringSafe(opts.dir), keep: true, prefix: index_1.toStringSafe(opts.prefix), suffix: index_1.toStringSafe(opts.suffix), }; } //# sourceMappingURL=index.js.map