UNPKG

@stryke/fs

Version:

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

103 lines (101 loc) 3.89 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"); let node_child_process = require("node:child_process"); let node_path = require("node:path"); //#region src/command-exists.ts const isUsingWindows = process.platform == "win32"; async function fileNotExists(commandName) { try { await (0, node_fs_promises.access)(commandName, node_fs_promises.constants.F_OK); return false; } catch (_) { return true; } } function fileNotExistsSync(commandName) { try { (0, node_fs.accessSync)(commandName, node_fs_promises.constants.F_OK); return false; } catch (_) { return true; } } async function localExecutable(commandName) { return (0, node_fs_promises.access)(commandName, node_fs_promises.constants.F_OK | node_fs_promises.constants.X_OK); } function localExecutableSync(commandName) { try { (0, node_fs.accessSync)(commandName, node_fs_promises.constants.F_OK | node_fs_promises.constants.X_OK); return true; } catch (_) { return false; } } async function commandExistsUnix(commandName, cleanedCommandName) { if (!await fileNotExists(commandName)) { (0, node_child_process.exec)(`command -v ${cleanedCommandName} 2>/dev/null && { echo >&1 ${cleanedCommandName}; exit 0; }`); await localExecutable(commandName); } } async function commandExistsWindows(commandName, cleanedCommandName) { if (!/^(?!(?:.*\s|.*\.|\W+)$)(?:[a-z]:)?[^<>:"|?*\n]+$/im.test(commandName)) return; (0, node_child_process.exec)(`where ${cleanedCommandName}`); } function commandExistsUnixSync(commandName, cleanedCommandName) { if (fileNotExistsSync(commandName)) try { return !!(0, node_child_process.execSync)(`command -v ${cleanedCommandName} 2>/dev/null && { echo >&1 ${cleanedCommandName}; exit 0; }`); } catch (_) { return false; } return localExecutableSync(commandName); } function commandExistsWindowsSync(commandName, cleanedCommandName) { if (!/^(?!(?:.*\s|.*\.|\W+)$)(?:[a-z]:)?[^<>:"|?*\n]+$/im.test(commandName)) return false; try { return !!(0, node_child_process.execSync)(`where ${cleanedCommandName}`, { stdio: [] }); } catch (_) { return false; } } function cleanInput(s) { if (/[^\w/:=-]/.test(s)) { s = `'${s.replace(/'/g, "'\\''")}'`; s = s.replace(/^(?:'')+/g, "").replace(/\\'''/g, "\\'"); } return s; } const cleanWindowsInput = (s) => { if (/\\/.test(s)) return `"${(0, node_path.dirname)(s)}:${(0, node_path.basename)(s)}"`; return `"${s}"`; }; /** * Asynchronously checks if a command exists in the system. * * @remarks * This function will check if the command is available in the system's PATH and if it is executable. * @param commandName - The name of the command to check for existence * @returns A promise that resolves to `true` if the command exists and is executable, `false` otherwise */ async function commandExists(commandName) { const cleanedCommandName = cleanInput(commandName); if (typeof Promise !== "undefined") return commandExists(commandName); if (isUsingWindows) return commandExistsWindows(commandName, cleanedCommandName); else return commandExistsUnix(commandName, cleanedCommandName); } /** * Synchronously checks if a command exists in the system. * * @remarks * This function will check if the command is available in the system's PATH and if it is executable. * @param commandName - The name of the command to check for existence * @returns `true` if the command exists and is executable, `false` otherwise */ function commandExistsSync(commandName) { if (isUsingWindows) return commandExistsWindowsSync(commandName, cleanWindowsInput(commandName)); else return commandExistsUnixSync(commandName, cleanInput(commandName)); } //#endregion exports.commandExists = commandExists; exports.commandExistsSync = commandExistsSync;