22 lines
800 B
JavaScript
22 lines
800 B
JavaScript
// main/utils/paths.js
|
|
const path = require('path')
|
|
const { fileURLToPath } = require('node:url')
|
|
const { app } = require('electron')
|
|
|
|
function normalizePath(targetPath, baseDir) {
|
|
if (typeof targetPath !== 'string' || !targetPath.trim()) throw new Error('无效的路径参数')
|
|
if (targetPath.startsWith('file://')) return fileURLToPath(new URL(targetPath))
|
|
if (!path.isAbsolute(targetPath)) {
|
|
const base = baseDir && typeof baseDir === 'string' ? baseDir : process.cwd()
|
|
return path.resolve(base, targetPath)
|
|
}
|
|
return path.resolve(targetPath)
|
|
}
|
|
|
|
function resolveResource(relPath) {
|
|
const base = app.isPackaged ? process.resourcesPath : path.resolve(__dirname, '..')
|
|
return path.join(base, relPath)
|
|
}
|
|
|
|
module.exports = { normalizePath, resolveResource }
|