apps.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { execFileSync } from "node:child_process"
  2. import { existsSync, readFileSync, readdirSync } from "node:fs"
  3. import { dirname, extname, join } from "node:path"
  4. export function checkAppExists(appName: string): boolean {
  5. if (process.platform === "win32") return true
  6. if (process.platform === "linux") return true
  7. return checkMacosApp(appName)
  8. }
  9. export function resolveAppPath(appName: string): string | null {
  10. if (process.platform !== "win32") return appName
  11. return resolveWindowsAppPath(appName)
  12. }
  13. export function wslPath(path: string, mode: "windows" | "linux" | null): string {
  14. if (process.platform !== "win32") return path
  15. const flag = mode === "windows" ? "-w" : "-u"
  16. try {
  17. if (path.startsWith("~")) {
  18. const suffix = path.slice(1)
  19. const cmd = `wslpath ${flag} \"$HOME${suffix.replace(/\"/g, '\\"')}\"`
  20. const output = execFileSync("wsl", ["-e", "sh", "-lc", cmd])
  21. return output.toString().trim()
  22. }
  23. const output = execFileSync("wsl", ["-e", "wslpath", flag, path])
  24. return output.toString().trim()
  25. } catch (error) {
  26. throw new Error(`Failed to run wslpath: ${String(error)}`)
  27. }
  28. }
  29. function checkMacosApp(appName: string) {
  30. const locations = [`/Applications/${appName}.app`, `/System/Applications/${appName}.app`]
  31. const home = process.env.HOME
  32. if (home) locations.push(`${home}/Applications/${appName}.app`)
  33. if (locations.some((location) => existsSync(location))) return true
  34. try {
  35. execFileSync("which", [appName])
  36. return true
  37. } catch {
  38. return false
  39. }
  40. }
  41. function resolveWindowsAppPath(appName: string): string | null {
  42. let output: string
  43. try {
  44. output = execFileSync("where", [appName]).toString()
  45. } catch {
  46. return null
  47. }
  48. const paths = output
  49. .split(/\r?\n/)
  50. .map((line) => line.trim())
  51. .filter((line) => line.length > 0)
  52. const hasExt = (path: string, ext: string) => extname(path).toLowerCase() === `.${ext}`
  53. const exe = paths.find((path) => hasExt(path, "exe"))
  54. if (exe) return exe
  55. const resolveCmd = (path: string) => {
  56. const content = readFileSync(path, "utf8")
  57. for (const token of content.split('"').map((value: string) => value.trim())) {
  58. const lower = token.toLowerCase()
  59. if (!lower.includes(".exe")) continue
  60. const index = lower.indexOf("%~dp0")
  61. if (index >= 0) {
  62. const base = dirname(path)
  63. const suffix = token.slice(index + 5)
  64. const resolved = suffix
  65. .replace(/\//g, "\\")
  66. .split("\\")
  67. .filter((part: string) => part && part !== ".")
  68. .reduce((current: string, part: string) => {
  69. if (part === "..") return dirname(current)
  70. return join(current, part)
  71. }, base)
  72. if (existsSync(resolved)) return resolved
  73. }
  74. if (existsSync(token)) return token
  75. }
  76. return null
  77. }
  78. for (const path of paths) {
  79. if (hasExt(path, "cmd") || hasExt(path, "bat")) {
  80. const resolved = resolveCmd(path)
  81. if (resolved) return resolved
  82. }
  83. if (!extname(path)) {
  84. const cmd = `${path}.cmd`
  85. if (existsSync(cmd)) {
  86. const resolved = resolveCmd(cmd)
  87. if (resolved) return resolved
  88. }
  89. const bat = `${path}.bat`
  90. if (existsSync(bat)) {
  91. const resolved = resolveCmd(bat)
  92. if (resolved) return resolved
  93. }
  94. }
  95. }
  96. const key = appName
  97. .split("")
  98. .filter((value: string) => /[a-z0-9]/i.test(value))
  99. .map((value: string) => value.toLowerCase())
  100. .join("")
  101. if (key) {
  102. for (const path of paths) {
  103. const dirs = [dirname(path), dirname(dirname(path)), dirname(dirname(dirname(path)))]
  104. for (const dir of dirs) {
  105. try {
  106. for (const entry of readdirSync(dir)) {
  107. const candidate = join(dir, entry)
  108. if (!hasExt(candidate, "exe")) continue
  109. const stem = entry.replace(/\.exe$/i, "")
  110. const name = stem
  111. .split("")
  112. .filter((value: string) => /[a-z0-9]/i.test(value))
  113. .map((value: string) => value.toLowerCase())
  114. .join("")
  115. if (name.includes(key) || key.includes(name)) return candidate
  116. }
  117. } catch {
  118. continue
  119. }
  120. }
  121. }
  122. }
  123. return paths[0] ?? null
  124. }