187 lines
5.2 KiB
JavaScript
187 lines
5.2 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;
|
||
}
|
||
|
||
/**
|
||
* 判断当前是否为"录制专用"模式
|
||
* - Linux/非 Windows 平台:强制录制模式(无打印能力)
|
||
* - config.mode === 'recording':显式指定录制模式
|
||
* - config.mode === 'minimal':最简模式(仅 HTTP+WS)
|
||
*/
|
||
export function isRecordingMode(config) {
|
||
// 非 Windows 平台强制录制模式
|
||
if (process.platform !== 'win32') {
|
||
return true;
|
||
}
|
||
// Windows 平台按配置决定
|
||
const mode = (config && config.mode) || 'full';
|
||
return mode === 'recording' || mode === 'minimal';
|
||
}
|
||
|
||
/**
|
||
* 获取当前运行模式标签
|
||
*/
|
||
export function getRunMode(config) {
|
||
if (process.platform !== 'win32') return 'recording';
|
||
return (config && config.mode) || 'full';
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
/**
|
||
* 运行时状态文件路径(记录后端实际选中的端口等动态信息)
|
||
*/
|
||
export function getRuntimeStatePath() {
|
||
if (!isElectronRuntime()) {
|
||
return path.join(projectRoot, 'runtime.json');
|
||
}
|
||
return path.join(getAppDataDir(), 'runtime.json');
|
||
}
|
||
|
||
export function readRuntimeState() {
|
||
try {
|
||
const p = getRuntimeStatePath();
|
||
if (fs.existsSync(p)) {
|
||
return JSON.parse(fs.readFileSync(p, 'utf-8'));
|
||
}
|
||
} catch (_) { /* ignore */ }
|
||
return {};
|
||
}
|
||
|
||
export function writeRuntimeState(state) {
|
||
try {
|
||
const p = getRuntimeStatePath();
|
||
fs.writeFileSync(p, JSON.stringify(state, null, 2), 'utf-8');
|
||
} catch (_) { /* ignore */ }
|
||
}
|
||
|
||
/**
|
||
* 解析端口配置,支持单端口(Number)或端口数组([Number])
|
||
* @param {number|number[]} raw
|
||
* @returns {number[]}
|
||
*/
|
||
export function resolvePortCandidates(raw) {
|
||
if (Array.isArray(raw)) {
|
||
return raw.filter(p => Number.isFinite(p) && p > 0 && p < 65536);
|
||
}
|
||
if (Number.isFinite(raw) && raw > 0 && raw < 65536) {
|
||
return [raw];
|
||
}
|
||
return [];
|
||
}
|
||
|
||
/**
|
||
* 从服务构造函数参数中提取安全端口候选(兼容旧字段名与新字段名)
|
||
*/
|
||
export function extractPortCandidates(config, key, fallbackKey, defaultPorts) {
|
||
const raw = config[key] || config[fallbackKey];
|
||
const candidates = resolvePortCandidates(raw);
|
||
if (candidates.length > 0) {
|
||
return candidates;
|
||
}
|
||
return resolvePortCandidates(defaultPorts);
|
||
} |