UNPKG

open-file-manager

Version:

Cross-platform utility to open a file or directory in the system's default file manager (Finder, Explorer, Nautilus, etc.)

85 lines (84 loc) 3.47 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // index.ts var index_exports = {}; __export(index_exports, { default: () => index_default, openFileManager: () => openFileManager }); module.exports = __toCommonJS(index_exports); var import_node_path = __toESM(require("path")); var import_node_util = __toESM(require("util")); var import_node_child_process = __toESM(require("child_process")); var managers = ["xdg-open", "nautilus", "dolphin", "thunar", "pcmanfm", "nemo"]; var openFileManager = /* @__PURE__ */ __name(async (pathname = ".") => { const exec = import_node_util.default.promisify(import_node_child_process.default.exec); const filepath = import_node_path.default.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} "${import_node_path.default.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 = import_node_path.default.dirname(filepath); return exec(`explorer "${parentDir}"`); } throw error; } }, "openFileManager"); var index_default = openFileManager; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { openFileManager }); //# sourceMappingURL=index.js.map