refactor: move sub-store path to WorkDir

This commit is contained in:
miwu04 2025-04-26 12:05:01 +08:00
parent 08f15c7e01
commit 944475d791
No known key found for this signature in database
GPG Key ID: D22D460B028C00CE
2 changed files with 18 additions and 43 deletions

View File

@ -1,12 +1,6 @@
import { getAppConfig, getControledMihomoConfig } from '../config' import { getAppConfig, getControledMihomoConfig } from '../config'
import { Worker } from 'worker_threads' import { Worker } from 'worker_threads'
import { import { dataDir, mihomoWorkDir, subStoreDir, substoreLogPath } from '../utils/dirs'
dataDir,
mihomoWorkDir,
resourcesFilesDir,
subStoreDir,
substoreLogPath
} from '../utils/dirs'
import subStoreIcon from '../../../resources/subStoreIcon.png?asset' import subStoreIcon from '../../../resources/subStoreIcon.png?asset'
import { createWriteStream, existsSync, mkdirSync } from 'fs' import { createWriteStream, existsSync, mkdirSync } from 'fs'
import { writeFile, rm, cp } from 'fs/promises' import { writeFile, rm, cp } from 'fs/promises'
@ -17,10 +11,6 @@ import { nativeImage } from 'electron'
import express from 'express' import express from 'express'
import axios from 'axios' import axios from 'axios'
import AdmZip from 'adm-zip' import AdmZip from 'adm-zip'
import { promisify } from 'util'
import { execFile } from 'child_process'
import { platform } from 'os'
import { is } from '@electron-toolkit/utils'
export let pacPort: number export let pacPort: number
export let subStorePort: number export let subStorePort: number
@ -87,7 +77,7 @@ export async function startSubStoreFrontendServer(): Promise<void> {
await stopSubStoreFrontendServer() await stopSubStoreFrontendServer()
subStoreFrontendPort = await findAvailablePort(14122) subStoreFrontendPort = await findAvailablePort(14122)
const app = express() const app = express()
app.use(express.static(path.join(resourcesFilesDir(), 'sub-store-frontend'))) app.use(express.static(path.join(mihomoWorkDir(), 'sub-store-frontend')))
subStoreFrontendServer = app.listen(subStoreFrontendPort, subStoreHost) subStoreFrontendServer = app.listen(subStoreFrontendPort, subStoreHost)
} }
@ -128,7 +118,7 @@ export async function startSubStoreBackendServer(): Promise<void> {
SUB_STORE_MMDB_COUNTRY_PATH: path.join(mihomoWorkDir(), 'country.mmdb'), SUB_STORE_MMDB_COUNTRY_PATH: path.join(mihomoWorkDir(), 'country.mmdb'),
SUB_STORE_MMDB_ASN_PATH: path.join(mihomoWorkDir(), 'ASN.mmdb') SUB_STORE_MMDB_ASN_PATH: path.join(mihomoWorkDir(), 'ASN.mmdb')
} }
subStoreBackendWorker = new Worker(path.join(resourcesFilesDir(), 'sub-store.bundle.js'), { subStoreBackendWorker = new Worker(path.join(mihomoWorkDir(), 'sub-store.bundle.js'), {
env: useProxyInSubStore env: useProxyInSubStore
? { ? {
...env, ...env,
@ -151,10 +141,9 @@ export async function stopSubStoreBackendServer(): Promise<void> {
export async function downloadSubStore(): Promise<void> { export async function downloadSubStore(): Promise<void> {
const { 'mixed-port': mixedPort = 7890 } = await getControledMihomoConfig() const { 'mixed-port': mixedPort = 7890 } = await getControledMihomoConfig()
const frontendDir = path.join(resourcesFilesDir(), 'sub-store-frontend') const frontendDir = path.join(mihomoWorkDir(), 'sub-store-frontend')
const backendPath = path.join(resourcesFilesDir(), 'sub-store.bundle.js') const backendPath = path.join(mihomoWorkDir(), 'sub-store.bundle.js')
const tempDir = path.join(dataDir(), 'temp') const tempDir = path.join(dataDir(), 'temp')
const execFilePromise = promisify(execFile)
try { try {
// 创建临时目录 // 创建临时目录
@ -195,28 +184,12 @@ export async function downloadSubStore(): Promise<void> {
// 先解压到临时目录 // 先解压到临时目录
const zip = new AdmZip(Buffer.from(frontendRes.data)) const zip = new AdmZip(Buffer.from(frontendRes.data))
zip.extractAllTo(tempDir, true) zip.extractAllTo(tempDir, true)
await cp(tempBackendPath, backendPath)
if (platform() === 'linux' && !is.dev) { if (existsSync(frontendDir)) {
try { await rm(frontendDir, { recursive: true })
const bashCmd = [
`cp "${tempBackendPath}" "${backendPath}"`,
`rm -rf "${frontendDir}"`,
`mkdir -p "${frontendDir}"`,
`cp -r "${tempFrontendDir}"/* "${frontendDir}/"`
].join(' && ')
await execFilePromise('pkexec', ['bash', '-c', bashCmd])
} catch (error) {
console.error('substore.downloadFailed:', error)
throw error
}
} else {
await cp(tempBackendPath, backendPath)
if (existsSync(frontendDir)) {
await rm(frontendDir, { recursive: true })
}
mkdirSync(frontendDir, { recursive: true })
await cp(path.join(tempDir, 'dist'), frontendDir, { recursive: true })
} }
mkdirSync(frontendDir, { recursive: true })
await cp(path.join(tempDir, 'dist'), frontendDir, { recursive: true })
await rm(tempDir, { recursive: true }) await rm(tempDir, { recursive: true })
} catch (error) { } catch (error) {
console.error('substore.downloadFailed:', error) console.error('substore.downloadFailed:', error)

View File

@ -22,7 +22,7 @@ import {
defaultProfileConfig defaultProfileConfig
} from './template' } from './template'
import yaml from 'yaml' import yaml from 'yaml'
import { mkdir, writeFile, copyFile, rm, readdir } from 'fs/promises' import { mkdir, writeFile,rm, readdir, cp } from 'fs/promises'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import path from 'path' import path from 'path'
import { import {
@ -88,13 +88,13 @@ async function initConfig(): Promise<void> {
async function initFiles(): Promise<void> { async function initFiles(): Promise<void> {
const copy = async (file: string): Promise<void> => { const copy = async (file: string): Promise<void> => {
const targetPath = path.join(mihomoWorkDir(), file) const targetPath = path.join(mihomoWorkDir(), file)
const testTargrtPath = path.join(mihomoTestDir(), file) const testTargetPath = path.join(mihomoTestDir(), file)
const sourcePath = path.join(resourcesFilesDir(), file) const sourcePath = path.join(resourcesFilesDir(), file)
if (!existsSync(targetPath) && existsSync(sourcePath)) { if (!existsSync(targetPath) && existsSync(sourcePath)) {
await copyFile(sourcePath, targetPath) await cp(sourcePath, targetPath, { recursive: true })
} }
if (!existsSync(testTargrtPath) && existsSync(sourcePath)) { if (!existsSync(testTargetPath) && existsSync(sourcePath)) {
await copyFile(sourcePath, testTargrtPath) await cp(sourcePath, testTargetPath, { recursive: true })
} }
} }
await Promise.all([ await Promise.all([
@ -102,7 +102,9 @@ async function initFiles(): Promise<void> {
copy('geoip.metadb'), copy('geoip.metadb'),
copy('geoip.dat'), copy('geoip.dat'),
copy('geosite.dat'), copy('geosite.dat'),
copy('ASN.mmdb') copy('ASN.mmdb'),
copy('sub-store.bundle.js'),
copy('sub-store-frontend')
]) ])
} }