126 lines
3.3 KiB
TypeScript
126 lines
3.3 KiB
TypeScript
import { ipcMain, app, shell } from 'electron'
|
|
import { join } from 'path'
|
|
import { spawn } from 'child_process'
|
|
import { MaRelease, MaProduct } from 'solar-js-sdk'
|
|
import * as fs from 'fs'
|
|
|
|
export interface AppLibrary {
|
|
apps: LocalAppRecord[]
|
|
}
|
|
|
|
export let library: AppLibrary
|
|
|
|
function readLocalLibrary(): AppLibrary {
|
|
const basePath = app.getPath('userData')
|
|
const libraryPath = join(basePath, 'apps_library.json')
|
|
console.log(`[Library] Loading library from ${libraryPath}`)
|
|
if (fs.existsSync(libraryPath)) {
|
|
const data = fs.readFileSync(libraryPath, 'utf-8')
|
|
library = JSON.parse(data)
|
|
} else {
|
|
library = { apps: [] }
|
|
saveLocalLibrary()
|
|
}
|
|
return library
|
|
}
|
|
|
|
function saveLocalLibrary() {
|
|
const basePath = app.getPath('userData')
|
|
const libraryPath = join(basePath, 'apps_library.json')
|
|
fs.writeFileSync(libraryPath, JSON.stringify(library), 'utf-8')
|
|
}
|
|
|
|
export interface LocalAppRecord {
|
|
id: string
|
|
product: MaProduct
|
|
release: MaRelease
|
|
basePath: string
|
|
}
|
|
|
|
export function initLibrary(): void {
|
|
readLocalLibrary()
|
|
ipcMain.handle('get-app-library', () => JSON.stringify(getAppLibrary()))
|
|
ipcMain.handle('get-app-library-one', (_, id: string) =>
|
|
JSON.stringify(getAppLibrary().filter((ele) => ele.id === id)[0]),
|
|
)
|
|
|
|
ipcMain.handle('sync-record-app', async (_, id: string, product: string, release: string) => {
|
|
const app = getAppLibrary().filter((ele) => ele.id === id)[0]
|
|
if (!app) return
|
|
app.product = JSON.parse(product)
|
|
app.release = JSON.parse(release)
|
|
setAppRecord(app)
|
|
})
|
|
|
|
ipcMain.handle('uninstall-app', (_, id: string) => uninstallApp(id))
|
|
ipcMain.handle('launch-app', (_, id: string) => launchApp(id))
|
|
ipcMain.handle('open-path-app', (_, id: string) => openPathApp(id))
|
|
}
|
|
|
|
export function getAppLibrary(): LocalAppRecord[] {
|
|
return library.apps
|
|
}
|
|
|
|
export function setAppRecord(record: LocalAppRecord) {
|
|
let current = getAppLibrary()
|
|
let hit = false
|
|
|
|
current = current.map((rec) => {
|
|
if (rec.id === record.id) hit = true
|
|
return rec.id === record.id ? record : rec
|
|
})
|
|
|
|
if (!hit) {
|
|
current.push(record)
|
|
}
|
|
|
|
library.apps = current
|
|
saveLocalLibrary()
|
|
}
|
|
|
|
export function removeAppRecord(id: string) {
|
|
let current = getAppLibrary()
|
|
current = current.filter((rec) => rec.id !== id)
|
|
|
|
library.apps = current
|
|
saveLocalLibrary()
|
|
}
|
|
|
|
export function uninstallApp(id: string) {
|
|
const app = getAppLibrary().filter((ele) => ele.id === id)[0]
|
|
if (!app) return
|
|
|
|
const basePath = app.basePath
|
|
fs.rmSync(basePath, { recursive: true, force: true })
|
|
|
|
removeAppRecord(id)
|
|
}
|
|
|
|
export function launchApp(id: string): void {
|
|
const app = getAppLibrary().filter((ele) => ele.id === id)[0]
|
|
if (!app) return
|
|
|
|
const platform = process.platform
|
|
const runner = app.release.runners[platform]
|
|
|
|
const segments = runner.script.split(' ').map(decodeURIComponent)
|
|
|
|
try {
|
|
const child = spawn(segments[0], segments.length > 1 ? segments.slice(1) : [], {
|
|
detached: true,
|
|
cwd: runner.workdir ? join(app.basePath, runner.workdir) : app.basePath,
|
|
})
|
|
child.stdout.pipe(process.stdout)
|
|
child.stderr.pipe(process.stderr)
|
|
} catch (err: any) {
|
|
console.error(err)
|
|
}
|
|
}
|
|
|
|
export function openPathApp(id: string): void {
|
|
const app = getAppLibrary().filter((ele) => ele.id === id)[0]
|
|
if (!app) return
|
|
|
|
shell.openPath(app.basePath)
|
|
}
|