80 lines
2.9 KiB
JavaScript
80 lines
2.9 KiB
JavaScript
const fs = require('fs')
|
||
const base = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/'
|
||
function patch(file, pairs) {
|
||
const p = base + file
|
||
let c = fs.readFileSync(p, 'utf8')
|
||
for (const [from, to] of pairs) {
|
||
if (!c.includes(from)) {
|
||
console.log('MISS in', file, '::', from.slice(0, 70).replace(/\n/g, '\\n'))
|
||
continue
|
||
}
|
||
c = c.replace(from, to)
|
||
}
|
||
fs.writeFileSync(p, c, 'utf8')
|
||
}
|
||
|
||
// ---- 1) 目录变更后自动刷新面板/详情
|
||
patch('renderer/src/panel/Panel.tsx', [
|
||
[
|
||
" const offSkins = a?.onSkins(setSkins)",
|
||
" const offSkins = a?.onSkins(setSkins)\n const offCatalog = a?.onCatalogChanged(() => window.location.reload())"
|
||
],
|
||
[
|
||
" offSkins?.()\n clearInterval(timer)",
|
||
" offSkins?.()\n offCatalog?.()\n clearInterval(timer)"
|
||
]
|
||
])
|
||
patch('renderer/src/detail/Detail.tsx', [
|
||
[
|
||
" return () => off?.()",
|
||
" const offCat = a?.onCatalogChanged(() => window.location.reload())\n return () => {\n off?.()\n offCat?.()\n }"
|
||
]
|
||
])
|
||
|
||
// ---- 2) 图标导入 IPC(复制到 data/icons)
|
||
patch('main/index.ts', [
|
||
[
|
||
` ipcMain.handle('pet:catalog:get', () => snapshotCatalog())`,
|
||
` ipcMain.handle('pet:icon:import', async () => {
|
||
const res = await dialog.showOpenDialog({
|
||
title: '选择图标图片',
|
||
properties: ['openFile'],
|
||
filters: [{ name: '图片', extensions: ['png', 'jpg', 'jpeg', 'gif', 'webp'] }]
|
||
})
|
||
if (res.canceled || !res.filePaths.length) return ''
|
||
try {
|
||
const src = res.filePaths[0]
|
||
const dir = path.join(HOME, 'data', 'icons')
|
||
fs.mkdirSync(dir, { recursive: true })
|
||
const ext = path.extname(src) || '.png'
|
||
const name = 'icon-' + Date.now() + ext
|
||
fs.copyFileSync(src, path.join(dir, name))
|
||
return 'img:icons/' + name
|
||
} catch (err) {
|
||
console.error('[icon] 导入失败', err)
|
||
return ''
|
||
}
|
||
})
|
||
|
||
ipcMain.handle('pet:catalog:get', () => snapshotCatalog())`
|
||
]
|
||
])
|
||
patch('preload/index.ts', [
|
||
[
|
||
" onCatalogChanged: (cb: () => void) => () => void",
|
||
" onCatalogChanged: (cb: () => void) => () => void\n importIcon: () => Promise<string>"
|
||
],
|
||
[
|
||
" onCatalogChanged: (cb: () => void) => {\n const l = (): void => cb()\n ipcRenderer.on('pet:catalog:changed', l)\n return () => ipcRenderer.removeListener('pet:catalog:changed', l)\n },",
|
||
" onCatalogChanged: (cb: () => void) => {\n const l = (): void => cb()\n ipcRenderer.on('pet:catalog:changed', l)\n return () => ipcRenderer.removeListener('pet:catalog:changed', l)\n },\n importIcon: () => ipcRenderer.invoke('pet:icon:import'),"
|
||
]
|
||
])
|
||
patch('renderer/src/api.ts', [
|
||
[
|
||
" onCatalogChanged: (cb: () => void) => () => void",
|
||
" onCatalogChanged: (cb: () => void) => () => void\n importIcon: () => Promise<string>"
|
||
]
|
||
])
|
||
|
||
console.log('catalog refresh + icon import patched')
|