|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import fs from 'fs'; |
|
|
import path from 'path'; |
|
|
import { fileURLToPath } from 'url'; |
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url); |
|
|
const __dirname = path.dirname(__filename); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const isPkg = typeof process.pkg !== 'undefined'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getProjectRoot() { |
|
|
if (isPkg) { |
|
|
return path.dirname(process.execPath); |
|
|
} |
|
|
return path.join(__dirname, '../..'); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getDataDir() { |
|
|
if (isPkg) { |
|
|
|
|
|
const exeDir = path.dirname(process.execPath); |
|
|
const exeDataDir = path.join(exeDir, 'data'); |
|
|
|
|
|
try { |
|
|
if (!fs.existsSync(exeDataDir)) { |
|
|
fs.mkdirSync(exeDataDir, { recursive: true }); |
|
|
} |
|
|
return exeDataDir; |
|
|
} catch (e) { |
|
|
|
|
|
const cwdDataDir = path.join(process.cwd(), 'data'); |
|
|
try { |
|
|
if (!fs.existsSync(cwdDataDir)) { |
|
|
fs.mkdirSync(cwdDataDir, { recursive: true }); |
|
|
} |
|
|
return cwdDataDir; |
|
|
} catch (e2) { |
|
|
|
|
|
const homeDataDir = path.join(process.env.HOME || process.env.USERPROFILE || '.', '.antigravity', 'data'); |
|
|
if (!fs.existsSync(homeDataDir)) { |
|
|
fs.mkdirSync(homeDataDir, { recursive: true }); |
|
|
} |
|
|
return homeDataDir; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return path.join(__dirname, '..', '..', 'data'); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getPublicDir() { |
|
|
if (isPkg) { |
|
|
|
|
|
const exeDir = path.dirname(process.execPath); |
|
|
const exePublicDir = path.join(exeDir, 'public'); |
|
|
if (fs.existsSync(exePublicDir)) { |
|
|
return exePublicDir; |
|
|
} |
|
|
|
|
|
const cwdPublicDir = path.join(process.cwd(), 'public'); |
|
|
if (fs.existsSync(cwdPublicDir)) { |
|
|
return cwdPublicDir; |
|
|
} |
|
|
|
|
|
return path.join(__dirname, '../../public'); |
|
|
} |
|
|
|
|
|
return path.join(__dirname, '../../public'); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getImageDir() { |
|
|
if (isPkg) { |
|
|
|
|
|
const exeDir = path.dirname(process.execPath); |
|
|
const exeImageDir = path.join(exeDir, 'public', 'images'); |
|
|
try { |
|
|
if (!fs.existsSync(exeImageDir)) { |
|
|
fs.mkdirSync(exeImageDir, { recursive: true }); |
|
|
} |
|
|
return exeImageDir; |
|
|
} catch (e) { |
|
|
|
|
|
const cwdImageDir = path.join(process.cwd(), 'public', 'images'); |
|
|
try { |
|
|
if (!fs.existsSync(cwdImageDir)) { |
|
|
fs.mkdirSync(cwdImageDir, { recursive: true }); |
|
|
} |
|
|
return cwdImageDir; |
|
|
} catch (e2) { |
|
|
|
|
|
const homeImageDir = path.join(process.env.HOME || process.env.USERPROFILE || '.', '.antigravity', 'images'); |
|
|
if (!fs.existsSync(homeImageDir)) { |
|
|
fs.mkdirSync(homeImageDir, { recursive: true }); |
|
|
} |
|
|
return homeImageDir; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return path.join(__dirname, '../../public/images'); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getEnvPath() { |
|
|
if (isPkg) { |
|
|
|
|
|
const exeDir = path.dirname(process.execPath); |
|
|
const exeEnvPath = path.join(exeDir, '.env'); |
|
|
if (fs.existsSync(exeEnvPath)) { |
|
|
return exeEnvPath; |
|
|
} |
|
|
|
|
|
const cwdEnvPath = path.join(process.cwd(), '.env'); |
|
|
if (fs.existsSync(cwdEnvPath)) { |
|
|
return cwdEnvPath; |
|
|
} |
|
|
|
|
|
return exeEnvPath; |
|
|
} |
|
|
|
|
|
return path.join(__dirname, '../../.env'); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getConfigPaths() { |
|
|
if (isPkg) { |
|
|
|
|
|
const exeDir = path.dirname(process.execPath); |
|
|
const cwdDir = process.cwd(); |
|
|
|
|
|
|
|
|
let envPath = path.join(exeDir, '.env'); |
|
|
if (!fs.existsSync(envPath)) { |
|
|
const cwdEnvPath = path.join(cwdDir, '.env'); |
|
|
if (fs.existsSync(cwdEnvPath)) { |
|
|
envPath = cwdEnvPath; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
let configJsonPath = path.join(exeDir, 'config.json'); |
|
|
if (!fs.existsSync(configJsonPath)) { |
|
|
const cwdConfigPath = path.join(cwdDir, 'config.json'); |
|
|
if (fs.existsSync(cwdConfigPath)) { |
|
|
configJsonPath = cwdConfigPath; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
let examplePath = path.join(exeDir, '.env.example'); |
|
|
if (!fs.existsSync(examplePath)) { |
|
|
const cwdExamplePath = path.join(cwdDir, '.env.example'); |
|
|
if (fs.existsSync(cwdExamplePath)) { |
|
|
examplePath = cwdExamplePath; |
|
|
} |
|
|
} |
|
|
|
|
|
return { envPath, configJsonPath, examplePath }; |
|
|
} |
|
|
|
|
|
|
|
|
return { |
|
|
envPath: path.join(__dirname, '../../.env'), |
|
|
configJsonPath: path.join(__dirname, '../../config.json'), |
|
|
examplePath: path.join(__dirname, '../../.env.example') |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getRelativePath(absolutePath) { |
|
|
if (isPkg) { |
|
|
const exeDir = path.dirname(process.execPath); |
|
|
if (absolutePath.startsWith(exeDir)) { |
|
|
return '.' + absolutePath.slice(exeDir.length).replace(/\\/g, '/'); |
|
|
} |
|
|
const cwdDir = process.cwd(); |
|
|
if (absolutePath.startsWith(cwdDir)) { |
|
|
return '.' + absolutePath.slice(cwdDir.length).replace(/\\/g, '/'); |
|
|
} |
|
|
} |
|
|
return absolutePath; |
|
|
} |