574cb353e8
- RestJobEx 对齐 task_id,path_file 提交目录路径 - 运行页进度仅跟接口轮询,失败态停留 page3 UI - 完成态卡位续做,USB/任务轮询生命周期优化 - 拆分 DLL 加载、任务 staging 与提交前校验 - 移除 mock 与冗余样式,补充 native 依赖 Co-authored-by: Cursor <cursoragent@cursor.com>
98 lines
2.8 KiB
TypeScript
98 lines
2.8 KiB
TypeScript
import { app } from 'electron'
|
|
import path from 'path'
|
|
import fs from 'fs'
|
|
import log from 'electron-log'
|
|
|
|
const RUNTIME_CONFIG_FILES = ['CapSettings.json'] as const
|
|
const DEFAULT_PRINT_TASKS = path.join('C:', 'PrintTasks')
|
|
|
|
const DEFAULT_CAP_SETTINGS = `{
|
|
"printerList": [],
|
|
"Img": [],
|
|
"Text": []
|
|
}
|
|
`
|
|
|
|
export function getNativeDir(): string {
|
|
if (app.isPackaged) {
|
|
return path.join(process.resourcesPath, 'native')
|
|
}
|
|
return path.join(app.getAppPath(), 'resources', 'native')
|
|
}
|
|
|
|
export function getProcessExecDir(): string {
|
|
return path.dirname(process.execPath)
|
|
}
|
|
|
|
function ensureBundledConfig(nativeDir: string): void {
|
|
fs.mkdirSync(nativeDir, { recursive: true })
|
|
for (const name of RUNTIME_CONFIG_FILES) {
|
|
const filePath = path.join(nativeDir, name)
|
|
if (!fs.existsSync(filePath)) {
|
|
fs.writeFileSync(filePath, DEFAULT_CAP_SETTINGS, 'utf8')
|
|
log.info(`Created default native config: ${filePath}`)
|
|
}
|
|
}
|
|
}
|
|
|
|
function deployConfigFile(src: string, destDir: string): void {
|
|
const dest = path.join(destDir, path.basename(src))
|
|
try {
|
|
fs.mkdirSync(destDir, { recursive: true })
|
|
if (!fs.existsSync(dest)) {
|
|
fs.copyFileSync(src, dest)
|
|
log.debug(`Deployed ${path.basename(src)} -> ${dest}`)
|
|
return
|
|
}
|
|
const srcStat = fs.statSync(src)
|
|
const destStat = fs.statSync(dest)
|
|
if (srcStat.mtimeMs > destStat.mtimeMs) {
|
|
fs.copyFileSync(src, dest)
|
|
log.debug(`Updated ${path.basename(src)} -> ${dest}`)
|
|
}
|
|
} catch (e) {
|
|
log.warn(`Deploy ${path.basename(src)} to ${destDir} failed`, e)
|
|
}
|
|
}
|
|
|
|
function deployRuntimeConfigs(nativeDir: string): void {
|
|
const deployDirs = new Set<string>([
|
|
nativeDir,
|
|
getProcessExecDir(),
|
|
DEFAULT_PRINT_TASKS,
|
|
path.join(app.getPath('userData'), 'Cardsoon', 'runtime')
|
|
])
|
|
|
|
for (const name of RUNTIME_CONFIG_FILES) {
|
|
const src = path.join(nativeDir, name)
|
|
if (!fs.existsSync(src)) continue
|
|
deployDirs.forEach((dir) => deployConfigFile(src, dir))
|
|
}
|
|
|
|
try {
|
|
fs.mkdirSync(DEFAULT_PRINT_TASKS, { recursive: true })
|
|
} catch (e) {
|
|
log.warn(`Cannot create ${DEFAULT_PRINT_TASKS}`, e)
|
|
}
|
|
}
|
|
|
|
export function setupNativeWorkingDir(): void {
|
|
const nativeDir = getNativeDir()
|
|
if (!fs.existsSync(path.join(nativeDir, 'workDll.dll'))) {
|
|
throw new Error(`Native DLL directory not found or incomplete: ${nativeDir}`)
|
|
}
|
|
ensureBundledConfig(nativeDir)
|
|
deployRuntimeConfigs(nativeDir)
|
|
const execDir = getProcessExecDir()
|
|
const pathHead = [nativeDir, execDir].join(path.delimiter)
|
|
if (!process.env.PATH?.toLowerCase().includes(nativeDir.toLowerCase())) {
|
|
process.env.PATH = `${pathHead}${path.delimiter}${process.env.PATH || ''}`
|
|
}
|
|
try {
|
|
process.chdir(execDir)
|
|
} catch (e) {
|
|
log.warn(`chdir to ${execDir} failed`, e)
|
|
}
|
|
log.debug(`Native DLL search path: ${nativeDir}; cwd=${process.cwd()}`)
|
|
}
|