open-file-manager
Version:
Cross-platform utility to open a file or directory in the system's default file manager (Finder, Explorer, Nautilus, etc.)
52 lines (51 loc) • 1.7 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// index.ts
import path from "path";
import util from "util";
import cb from "child_process";
var managers = ["xdg-open", "nautilus", "dolphin", "thunar", "pcmanfm", "nemo"];
var openFileManager = /* @__PURE__ */ __name(async (pathname = ".") => {
const exec = util.promisify(cb.exec);
const filepath = path.resolve(process.cwd(), pathname);
let lastError;
try {
switch (process.platform) {
case "win32":
return exec(`explorer /select "${filepath}"`);
case "darwin":
return exec(`open -R "${filepath}"`);
case "linux":
case "freebsd":
case "openbsd":
case "sunos":
for (const manager of managers) {
try {
await exec(`which ${manager}`);
if (manager === "xdg-open") {
return exec(`${manager} "${path.dirname(filepath)}"`);
}
return exec(`${manager} "${filepath}"`);
} catch (err) {
lastError = err;
continue;
}
}
throw new Error(`No file manager found. Tried: ${managers.join(", ")}. Last error: ${lastError?.message}`);
default:
throw new Error(`Unsupported platform: ${process.platform}`);
}
} catch (error) {
if (process.platform === "win32" && error.code === 1) {
const parentDir = path.dirname(filepath);
return exec(`explorer "${parentDir}"`);
}
throw error;
}
}, "openFileManager");
var index_default = openFileManager;
export {
index_default as default,
openFileManager
};
//# sourceMappingURL=index.mjs.map