|
| 1 | +import fs from 'node:fs' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import process from 'node:process' |
| 5 | + |
| 6 | +import { XMLParser } from 'fast-xml-parser' |
| 7 | + |
| 8 | +import { uniqExistingDirs } from './shared' |
| 9 | + |
| 10 | +export interface JetbrainsRecentEntry { |
| 11 | + path: string |
| 12 | + ide: string // 例如 "PyCharm", "IntelliJIdea" |
| 13 | +} |
| 14 | + |
| 15 | +const ideNameMap = { |
| 16 | + 'IntelliJIdea': 'intellij-idea', |
| 17 | + 'PyCharm': 'pycharm', |
| 18 | + 'PhpStorm': 'phpstorm', |
| 19 | + 'GoLand': 'goLand', |
| 20 | + 'Rider': 'rider', |
| 21 | + 'CLion': 'clion', |
| 22 | + 'RustRover': 'rust-rover', |
| 23 | + 'WebStorm': 'webstorm', |
| 24 | + 'RubyMine': 'rubymine', |
| 25 | + 'Aqua': 'aqua', |
| 26 | + 'Fleet': 'fleet', |
| 27 | + 'AndroidStudio': 'android-studio', |
| 28 | +} as const |
| 29 | + |
| 30 | +/** |
| 31 | + * 获取 JetBrains 所有 IDE 的最近项目列表 |
| 32 | + * @param configRoot JetBrains 配置根目录(例如 C:\Users\xxx\AppData\Roaming\JetBrains) |
| 33 | + */ |
| 34 | +function getAllJetbrainsRecentProjects( |
| 35 | + configRoot: string, |
| 36 | +): JetbrainsRecentEntry[] { |
| 37 | + if (!fs.existsSync(configRoot)) { |
| 38 | + throw new Error(`JetBrains config root not found: ${configRoot}`) |
| 39 | + } |
| 40 | + |
| 41 | + const parser = new XMLParser({ |
| 42 | + ignoreAttributes: false, |
| 43 | + attributeNamePrefix: '', |
| 44 | + }) |
| 45 | + |
| 46 | + const userHome = os.homedir() |
| 47 | + const normalizePath = (p: string): string => |
| 48 | + p.replace(/\$USER_HOME\$/g, userHome).replace(/\\/g, '/') |
| 49 | + |
| 50 | + const entries: JetbrainsRecentEntry[] = [] |
| 51 | + |
| 52 | + // 遍历 JetBrains 目录下的所有子目录 |
| 53 | + for (const subdir of fs.readdirSync(configRoot)) { |
| 54 | + const fullDir = path.join(configRoot, subdir) |
| 55 | + if (!fs.statSync(fullDir).isDirectory()) |
| 56 | + continue |
| 57 | + |
| 58 | + const xmlPath = path.join(fullDir, 'options', 'recentProjects.xml') |
| 59 | + if (!fs.existsSync(xmlPath)) |
| 60 | + continue |
| 61 | + |
| 62 | + const xmlContent = fs.readFileSync(xmlPath, 'utf-8') |
| 63 | + const json = parser.parse(xmlContent) |
| 64 | + |
| 65 | + const ideNameRaw = subdir.replace(/\d.*$/, '') // 去掉版本号,比如 "PyCharm2025.1" -> "PyCharm" |
| 66 | + const ideName = ideNameMap[ideNameRaw] ?? null |
| 67 | + |
| 68 | + const options = json?.application?.component?.option |
| 69 | + if (!options) |
| 70 | + continue |
| 71 | + |
| 72 | + // 可能是数组,也可能是单个对象 |
| 73 | + const optionList = Array.isArray(options) ? options : [options] |
| 74 | + |
| 75 | + const additionalInfo = optionList.find(opt => opt.name === 'additionalInfo') |
| 76 | + if (!additionalInfo?.map?.entry) |
| 77 | + continue |
| 78 | + |
| 79 | + const projectEntries = additionalInfo.map.entry |
| 80 | + |
| 81 | + if (Array.isArray(projectEntries)) { |
| 82 | + for (const e of projectEntries) { |
| 83 | + if (e.key) |
| 84 | + entries.push({ path: normalizePath(e.key), ide: ideName }) |
| 85 | + } |
| 86 | + } |
| 87 | + else if (projectEntries?.key) { |
| 88 | + entries.push({ path: normalizePath(projectEntries.key), ide: ideName }) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // 去重:按 path 唯一 |
| 93 | + const seen = new Set<string>() |
| 94 | + const uniqueEntries: JetbrainsRecentEntry[] = [] |
| 95 | + for (const e of entries) { |
| 96 | + if (!seen.has(e.path)) { |
| 97 | + seen.add(e.path) |
| 98 | + uniqueEntries.push(e) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return uniqueEntries |
| 103 | +} |
| 104 | + |
| 105 | +export function collectFromJetbrains(configRoot: string | null): JetbrainsRecentEntry[] { |
| 106 | + if (!configRoot) { |
| 107 | + return [] |
| 108 | + } |
| 109 | + let entries: JetbrainsRecentEntry[] = [] |
| 110 | + try { |
| 111 | + entries = getAllJetbrainsRecentProjects(configRoot) |
| 112 | + } |
| 113 | + catch { |
| 114 | + return [] |
| 115 | + } |
| 116 | + const candidates: JetbrainsRecentEntry[] = [] |
| 117 | + for (const e of entries) { |
| 118 | + const p = e.path |
| 119 | + // Skip placeholders and JetBrains light-edit/scratches etc. |
| 120 | + if (!p || /\$[A-Z_]+\$/.test(p) || /light-edit|scratches/i.test(p)) |
| 121 | + continue |
| 122 | + const norm = process.platform === 'win32' ? p.replace(/\//g, '\\') : p |
| 123 | + candidates.push({ path: norm, ide: e.ide }) |
| 124 | + } |
| 125 | + return uniqExistingDirs(candidates) |
| 126 | +} |
0 commit comments