Skip to content

Commit

Permalink
feat: add os agnostic pathfinder (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cafe137 authored Apr 7, 2022
1 parent 3c661e5 commit b02143e
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/path.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import { app } from 'electron'
import { platform } from 'os'
import { join, parse, resolve } from 'path'
import { existsSync } from 'fs'
import { join, sep } from 'path'

export function resolvePath(path: string) {
if (platform() === 'win32') {
if (process.execPath.includes('node_modules')) {
return join(process.execPath.split('node_modules')[0], path)
}
const parsedPath = parse(process.execPath)
return join(parsedPath.dir, path)
}
if (process.execPath.includes('node_modules/electron/dist/Electron.app')) {
return resolve(path)
}
const appName = `${app.getName()}.app`
let execPath = process.execPath
const origin = process.execPath
const parts = origin.split(sep)
while (parts.length) {
const currentPath = join(sep, ...parts, path)

if (execPath.includes(appName)) {
execPath = execPath.split(appName)[0]
if (existsSync(currentPath)) {
return currentPath
}
parts.pop()
}

return join(execPath, path)
throw Error(`Path ${path} is not found`)
}

0 comments on commit b02143e

Please sign in to comment.