@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
108 lines (106 loc) • 2.92 kB
JavaScript
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
var chmod_x_exports = /* @__PURE__ */ require_runtime.__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 = (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;
Object.defineProperty(exports, 'chmod_x_exports', {
enumerable: true,
get: function () {
return chmod_x_exports;
}
});
exports.isExecutable = isExecutable;
exports.isExecutableSync = isExecutableSync;
exports.isWritable = isWritable;
exports.isWritableSync = isWritableSync;