UNPKG

@stryke/fs

Version:

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

97 lines (95 loc) 2.5 kB
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs"; import { accessSync, chmodSync, constants, statSync } from "node:fs"; import { access, chmod } from "node:fs/promises"; //#region src/chmod-x.ts var chmod_x_exports = /* @__PURE__ */ __exportAll({ chmodX: () => chmodX, chmodXSync: () => chmodXSync, isExecutable: () => isExecutable, isExecutableSync: () => isExecutableSync, isWritable: () => isWritable, isWritableSync: () => isWritableSync }); /** * 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 = statSync(file); const newMode = s.mode | 73; if (s.mode === newMode) return; 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 = statSync(file); const newMode = s.mode | 73; if (s.mode === newMode) return; return 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 access(filename, 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 { accessSync(filename, 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 access(filename, 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 { accessSync(filename, constants.X_OK); return true; } catch { return false; } } //#endregion export { chmodX, chmodXSync, chmod_x_exports, isExecutable, isExecutableSync, isWritable, isWritableSync }; //# sourceMappingURL=chmod-x.mjs.map