open-file-manager
Version:
Cross-platform utility to open a file or directory in the system's default file manager (Finder, Explorer, Nautilus, etc.)
1 lines • 2.84 kB
Source Map (JSON)
{"version":3,"sources":["../index.ts"],"sourcesContent":["import path from 'node:path';\nimport util from 'node:util';\nimport cb from 'node:child_process';\n\nconst managers = ['xdg-open', 'nautilus', 'dolphin', 'thunar', 'pcmanfm', 'nemo'];\n\nexport const openFileManager = async (pathname = '.') => {\n const exec = util.promisify(cb.exec);\n const filepath = path.resolve(process.cwd(), pathname);\n let lastError;\n\n try {\n switch (process.platform) {\n case 'win32':\n return exec(`explorer /select \"${filepath}\"`);\n case 'darwin':\n return exec(`open -R \"${filepath}\"`);\n case 'linux':\n case 'freebsd':\n case 'openbsd':\n case 'sunos':\n // Try multiple file managers in order of preference\n for (const manager of managers) {\n try {\n // Check if the manager exists\n await exec(`which ${manager}`);\n\n // Try to open with the manager\n if (manager === 'xdg-open') {\n return exec(`${manager} \"${path.dirname(filepath)}\"`);\n }\n\n return exec(`${manager} \"${filepath}\"`);\n } catch (err) {\n lastError = err;\n continue;\n }\n }\n\n throw new Error(`No file manager found. Tried: ${managers.join(', ')}. Last error: ${lastError?.message}`);\n default:\n throw new Error(`Unsupported platform: ${process.platform}`);\n }\n } catch (error) {\n // Windows explorer returns exit code 1 when the file doesn't exist\n if (process.platform === 'win32' && error.code === 1) {\n // Try opening the parent directory\n const parentDir = path.dirname(filepath);\n return exec(`explorer \"${parentDir}\"`);\n }\n\n throw error;\n }\n};\n\nexport default openFileManager;\n"],"mappings":";;;;AAAA,OAAO,UAAU;AACjB,OAAO,UAAU;AACjB,OAAO,QAAQ;AAEf,IAAM,WAAW,CAAC,YAAY,YAAY,WAAW,UAAU,WAAW,MAAM;AAEzE,IAAM,kBAAkB,8BAAO,WAAW,QAAQ;AACvD,QAAM,OAAO,KAAK,UAAU,GAAG,IAAI;AACnC,QAAM,WAAW,KAAK,QAAQ,QAAQ,IAAI,GAAG,QAAQ;AACrD,MAAI;AAEJ,MAAI;AAC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