109 lines
3.1 KiB
JavaScript
109 lines
3.1 KiB
JavaScript
import fs from 'fs';
|
|
import os from 'os';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const currentFilePath = fileURLToPath(import.meta.url);
|
|
const projectRoot = path.resolve(path.dirname(currentFilePath), '..');
|
|
|
|
export function isElectronRuntime() {
|
|
return Boolean(process.versions?.electron);
|
|
}
|
|
|
|
export function isPackagedElectron() {
|
|
return isElectronRuntime() && !process.defaultApp;
|
|
}
|
|
|
|
export function getProjectRoot() {
|
|
return projectRoot;
|
|
}
|
|
|
|
export function getResourcePath(...segments) {
|
|
const candidatePaths = [];
|
|
|
|
if (isPackagedElectron()) {
|
|
candidatePaths.push(path.join(process.resourcesPath, ...segments));
|
|
candidatePaths.push(path.join(path.dirname(process.execPath), 'resources', ...segments));
|
|
candidatePaths.push(path.join(path.dirname(process.execPath), ...segments));
|
|
candidatePaths.push(path.join(projectRoot, ...segments));
|
|
} else {
|
|
candidatePaths.push(path.join(projectRoot, ...segments));
|
|
candidatePaths.push(path.join(process.cwd(), ...segments));
|
|
}
|
|
|
|
for (const candidate of candidatePaths) {
|
|
if (fs.existsSync(candidate)) {
|
|
return candidate;
|
|
}
|
|
}
|
|
|
|
return candidatePaths[0];
|
|
}
|
|
|
|
export function ensureDirectory(dirPath) {
|
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
return dirPath;
|
|
}
|
|
|
|
function getUserRoamingDir() {
|
|
return process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming');
|
|
}
|
|
|
|
function getSharedDataRoot() {
|
|
return process.env.ProgramData || path.join(process.env.SystemDrive || 'C:', 'ProgramData');
|
|
}
|
|
|
|
function getLegacyElectronConfigPath() {
|
|
return path.join(getUserRoamingDir(), 'printServer', 'config.json');
|
|
}
|
|
|
|
export function getAppDataDir() {
|
|
if (!isElectronRuntime()) {
|
|
return projectRoot;
|
|
}
|
|
|
|
if (isPackagedElectron()) {
|
|
return ensureDirectory(path.join(getSharedDataRoot(), 'printServer'));
|
|
}
|
|
|
|
return ensureDirectory(path.join(getUserRoamingDir(), 'printServer'));
|
|
}
|
|
|
|
export function getBundledConfigPath() {
|
|
return getResourcePath('config.json');
|
|
}
|
|
|
|
export function getWritableConfigPath() {
|
|
if (!isElectronRuntime()) {
|
|
return path.join(projectRoot, 'config.json');
|
|
}
|
|
|
|
return path.join(getAppDataDir(), 'config.json');
|
|
}
|
|
|
|
export function ensureWritableConfigFile() {
|
|
const writableConfigPath = getWritableConfigPath();
|
|
if (fs.existsSync(writableConfigPath)) {
|
|
return writableConfigPath;
|
|
}
|
|
|
|
// For packaged builds, do not migrate legacy roaming config by default.
|
|
// This prevents test tokens/settings from being copied into production runtime.
|
|
const allowLegacyMigration = process.env.PRINTSERVER_ALLOW_LEGACY_CONFIG_MIGRATION === '1';
|
|
if (isPackagedElectron() && allowLegacyMigration) {
|
|
const legacyConfigPath = getLegacyElectronConfigPath();
|
|
if (fs.existsSync(legacyConfigPath)) {
|
|
fs.copyFileSync(legacyConfigPath, writableConfigPath);
|
|
return writableConfigPath;
|
|
}
|
|
}
|
|
|
|
const bundledConfigPath = getBundledConfigPath();
|
|
if (fs.existsSync(bundledConfigPath)) {
|
|
fs.copyFileSync(bundledConfigPath, writableConfigPath);
|
|
} else {
|
|
fs.writeFileSync(writableConfigPath, '{}\n', 'utf8');
|
|
}
|
|
|
|
return writableConfigPath;
|
|
} |