UNPKG

@stryke/fs

Version:

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

71 lines (70 loc) 2.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.commandExists = commandExists; exports.commandExistsSync = commandExistsSync; var _nodeChild_process = require("node:child_process"); var _nodeFs = require("node:fs"); var _promises = require("node:fs/promises"); var _nodePath = require("node:path"); const u = process.platform == "win32"; async function g(t) { try { return await (0, _promises.access)(t, _promises.constants.F_OK), !1; } catch { return !0; } } function x(t) { try { return (0, _nodeFs.accessSync)(t, _promises.constants.F_OK), !1; } catch { return !0; } } async function y(t) { return (0, _promises.access)(t, _promises.constants.F_OK | _promises.constants.X_OK); } function p(t) { try { return (0, _nodeFs.accessSync)(t, _promises.constants.F_OK | _promises.constants.X_OK), !0; } catch { return !1; } } async function $(t, n) { (await g(t)) || ((0, _nodeChild_process.exec)(`command -v ${n} 2>/dev/null && { echo >&1 ${n}; exit 0; }`), await y(t)); } async function d(t, n) { /^(?!(?:.*\s|.*\.|\W+)$)(?:[a-z]:)?[^<>:"|?*\n]+$/im.test(t) && (0, _nodeChild_process.exec)(`where ${n}`); } function h(t, n) { if (x(t)) try { return !!(0, _nodeChild_process.execSync)(`command -v ${n} 2>/dev/null && { echo >&1 ${n}; exit 0; }`); } catch { return !1; } return p(t); } function w(t, n) { if (!/^(?!(?:.*\s|.*\.|\W+)$)(?:[a-z]:)?[^<>:"|?*\n]+$/im.test(t)) return !1; try { return !!(0, _nodeChild_process.execSync)(`where ${n}`, { stdio: [] }); } catch { return !1; } } function a(t) { return /[^\w/:=-]/.test(t) && (t = `'${t.replace(/'/g, "'\\''")}'`, t = t.replace(/^(?:'')+/g, "").replace(/\\'''/g, "\\'")), t; } const _ = t => /\\/.test(t) ? `"${(0, _nodePath.dirname)(t)}:${(0, _nodePath.basename)(t)}"` : `"${t}"`; async function commandExists(t) { const n = a(t); return typeof Promise < "u" ? commandExists(t) : u ? d(t, n) : $(t, n); } function commandExistsSync(t) { return u ? w(t, _(t)) : h(t, a(t)); }