Makelore 2.0 initial clean snapshot
This commit is contained in:
95
electron/utils/paths.ts
Normal file
95
electron/utils/paths.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Path Utilities
|
||||
* Cross-platform path resolution helpers
|
||||
*/
|
||||
import { createRequire } from 'node:module';
|
||||
import { join } from 'path';
|
||||
import { homedir } from 'os';
|
||||
import { existsSync, mkdirSync } from 'fs';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
type ElectronAppLike = Pick<typeof import('electron').app, 'isPackaged' | 'getPath' | 'getAppPath'>;
|
||||
|
||||
export {
|
||||
quoteForCmd,
|
||||
needsWinShell,
|
||||
prepareWinSpawn,
|
||||
normalizeNodeRequirePathForNodeOptions,
|
||||
appendNodeRequireToNodeOptions,
|
||||
} from './win-shell';
|
||||
|
||||
function getElectronApp() {
|
||||
if (process.versions?.electron) {
|
||||
return (require('electron') as typeof import('electron')).app;
|
||||
}
|
||||
|
||||
const fallbackUserData = process.env.NIANCODE_USER_DATA_DIR?.trim() || join(homedir(), '.niancode');
|
||||
const fallbackAppPath = process.cwd();
|
||||
const fallbackApp: ElectronAppLike = {
|
||||
isPackaged: false,
|
||||
getPath: (name) => {
|
||||
if (name === 'userData') return fallbackUserData;
|
||||
return fallbackUserData;
|
||||
},
|
||||
getAppPath: () => fallbackAppPath,
|
||||
};
|
||||
return fallbackApp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand ~ to home directory
|
||||
*/
|
||||
export function expandPath(path: string): string {
|
||||
if (path.startsWith('~')) {
|
||||
return path.replace('~', homedir());
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Makelore config directory
|
||||
*/
|
||||
export function getMakeloreConfigDir(): string {
|
||||
return join(homedir(), '.niancode');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Makelore logs directory
|
||||
*/
|
||||
export function getLogsDir(): string {
|
||||
return join(getElectronApp().getPath('userData'), 'logs');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Makelore data directory
|
||||
*/
|
||||
export function getDataDir(): string {
|
||||
return getElectronApp().getPath('userData');
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure directory exists
|
||||
*/
|
||||
export function ensureDir(dir: string): void {
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get resources directory (for bundled assets)
|
||||
*/
|
||||
export function getResourcesDir(): string {
|
||||
if (getElectronApp().isPackaged) {
|
||||
return join(process.resourcesPath, 'resources');
|
||||
}
|
||||
return join(__dirname, '../../resources');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get preload script path
|
||||
*/
|
||||
export function getPreloadPath(): string {
|
||||
return join(__dirname, '../preload/index.js');
|
||||
}
|
||||
Reference in New Issue
Block a user