UNPKG

@stryke/fs

Version:

A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.

94 lines (92 loc) 2.52 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); let node_fs = require("node:fs"); let node_fs_promises = require("node:fs/promises"); //#region src/chmod-x.ts /** * Adds execute permissions to a file * * @param file - The file to add execute permissions to */ function chmodXSync(file) { if (process.platform === "win32") return; const s = (0, node_fs.statSync)(file); const newMode = s.mode | 73; if (s.mode === newMode) return; (0, node_fs.chmodSync)(file, newMode.toString(8).slice(-3)); } /** * Adds execute permissions to a file * * @param file - The file to add execute permissions to */ async function chmodX(file) { if (process.platform === "win32") return; const s = (0, node_fs.statSync)(file); const newMode = s.mode | 73; if (s.mode === newMode) return; return (0, node_fs_promises.chmod)(file, newMode.toString(8).slice(-3)); } /** * Checks the write permission of a file * * @param filename - The file to check the permission of * @returns A promise that resolves to true if the file is writable, false otherwise */ async function isWritable(filename) { try { await (0, node_fs_promises.access)(filename, node_fs.constants.W_OK); return true; } catch { return false; } } /** * Checks the write permission of a file * * @param filename - The file to check the permission of * @returns True if the file is writable, false otherwise */ function isWritableSync(filename) { try { (0, node_fs.accessSync)(filename, node_fs.constants.W_OK); return true; } catch { return false; } } /** * Checks the execute permission of a file * * @param filename - The file to check the permission of * @returns A promise that resolves to true if the file is executable, false otherwise */ async function isExecutable(filename) { try { await (0, node_fs_promises.access)(filename, node_fs.constants.X_OK); return true; } catch { return false; } } /** * Checks the execute permission of a file * * @param filename - The file to check the permission of * @returns True if the file is executable, false otherwise */ function isExecutableSync(filename) { try { (0, node_fs.accessSync)(filename, node_fs.constants.X_OK); return true; } catch { return false; } } //#endregion exports.chmodX = chmodX; exports.chmodXSync = chmodXSync; exports.isExecutable = isExecutable; exports.isExecutableSync = isExecutableSync; exports.isWritable = isWritable; exports.isWritableSync = isWritableSync;