UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

120 lines (119 loc) 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dirExists = dirExists; exports.fileExists = fileExists; exports.createDirSafely = createDirSafely; exports.createFileSafely = createFileSafely; exports.createLinkfileSafely = createLinkfileSafely; exports.removeLinkfileSafely = removeLinkfileSafely; exports.createSymlinkSafely = createSymlinkSafely; const fs = require("fs"); const path = require("path"); /** * Check if a directory exists. */ function dirExists(dir) { try { const stats = fs.lstatSync(dir); return stats.isDirectory() || stats.isSymbolicLink(); } catch (e) { return false; } } /** * Check if a file exists. */ function fileExists(file) { try { const stats = fs.lstatSync(file); return stats.isFile() || stats.isSymbolicLink(); } catch (e) { return false; } } /** * Create a directory, only if it doesn't exist yet. * Throws an error if the directory already exists, or encounters a filesystem problem. */ function createDirSafely(dir) { if (dirExists(dir)) { const error = new Error(`directory '${path.normalize(dir)}' already exists`); // @ts-ignore error.code = "EEXIST"; throw error; } else { fs.mkdirSync(path.normalize(dir), "0766"); return `created directory '${path.normalize(dir)}'`; } } /** * Create a file, only if it doesn't exist yet. * Throws an error if the file already exists, or encounters a filesystem problem. */ function createFileSafely(file, data, overwrite = false) { if (fileExists(file) && !overwrite) { const error = new Error(`file '${path.normalize(file)}' already exists`); // @ts-ignore error.code = "EEXIST"; throw error; } else { let message = `wrote file '${path.normalize(file)}'`; if (overwrite && fileExists(file)) { message = ` - overwritten file '${path.normalize(file)}'`; } fs.writeFileSync(path.normalize(file), data); return message; } } /** * Create an Actionhero LinkFile, only if it doesn't exist yet. * Throws an error if the file already exists, or encounters a filesystem problem. */ function createLinkfileSafely(filePath, type) { if (fileExists(filePath)) { const error = new Error(`link file '${filePath}' already exists`); // @ts-ignore error.code = "EEXIST"; throw error; } else { fs.writeFileSync(filePath, type); return `creating linkfile '${filePath}'`; } } /** * Remove an Actionhero LinkFile, only if it exists. * Throws an error if the file does not exist, or encounters a filesystem problem. */ function removeLinkfileSafely(filePath) { if (!fileExists(filePath)) { const error = new Error(`link file '${filePath}' doesn't exist`); // @ts-ignore error.code = "ENOEXIST"; throw error; } else { fs.unlinkSync(filePath); return `removing linkfile '${filePath}'`; } } /** * Create a system symbolic link. * Throws an error if it encounters a filesystem problem. */ function createSymlinkSafely(destination, source) { if (dirExists(destination)) { const error = new Error(`symbolic link '${destination}' already exists`); // @ts-ignore error.code = "EEXIST"; throw error; } else { fs.symlinkSync(source, destination, "dir"); return `creating symbolic link '${destination}' => '${source}'`; } }