@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
88 lines (86 loc) • 2.16 kB
JavaScript
import { accessSync, chmodSync, constants, statSync } from "node:fs";
import { access, chmod } from "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 = 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, isExecutable, isExecutableSync, isWritable, isWritableSync };
//# sourceMappingURL=chmod-x.mjs.map