apps.ts 4.4 KB

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