diff --git a/electron.vite.config.js b/electron.vite.config.js index ba08b4c..8420ab7 100644 --- a/electron.vite.config.js +++ b/electron.vite.config.js @@ -10,6 +10,11 @@ export default defineConfig({ plugins: [externalizeDepsPlugin()], }, preload: { + resolve: { + alias: { + '@resources': resolve('resources'), + }, + }, plugins: [externalizeDepsPlugin()], }, renderer: { diff --git a/jsconfig.json b/jsconfig.json index 9ffae20..c40130f 100644 --- a/jsconfig.json +++ b/jsconfig.json @@ -4,7 +4,8 @@ "compilerOptions": { "baseUrl": ".", "paths": { - "@renderer/*": ["src/renderer/src/*"] + "@renderer/*": ["src/renderer/src/*"], + "@resources/*": ["resources/*"] } } } diff --git a/src/main/index.js b/src/main/index.js index 235e827..c05823b 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -16,7 +16,6 @@ function createWindow() { webPreferences: { preload: join(__dirname, '../preload/index.js'), sandbox: false, - devTools: true, }, }) diff --git a/src/preload/helpers/index.js b/src/preload/helpers/index.js index fa18309..2f39b00 100644 --- a/src/preload/helpers/index.js +++ b/src/preload/helpers/index.js @@ -1,6 +1,6 @@ import { contextBridge } from 'electron' -export function addContext(key, value) { +export function exposeContext(key, value) { if (process.contextIsolated) { try { contextBridge.exposeInMainWorld(key, value) diff --git a/src/preload/index.js b/src/preload/index.js index 14f28f2..618e617 100644 --- a/src/preload/index.js +++ b/src/preload/index.js @@ -1,74 +1,4 @@ -import { electronAPI } from '@electron-toolkit/preload' -import { Adb } from '@devicefarmer/adbkit' +import plugins from './plugins/index.js' +import { exposeContext } from './helpers/index.js' -import scrcpyPath from '../../resources/core/scrcpy.exe?asset&asarUnpack' -import adbPath from '../../resources/core/adb.exe?asset&asarUnpack' -import { addContext } from './helpers/index.js' - -const util = require('node:util') -const exec = util.promisify(require('node:child_process').exec) - -// Custom APIs for renderer -const api = {} - -// Use `contextBridge` APIs to expose Electron APIs to -// renderer only if context isolation is enabled, otherwise -// just add to the DOM global. -addContext('electron', electronAPI) -addContext('api', api) - -addContext('adbkit', () => { - const client = Adb.createClient({ bin: adbPath }) - console.log('client', client) - - const getDevices = async () => await client.listDevicesWithPaths() - const shell = async (id, command) => await client.getDevice(id).shell(command) - const kill = async (...params) => await client.kill(...params) - const connect = async (...params) => await client.connect(...params) - const disconnect = async (...params) => await client.disconnect(...params) - - const watch = async (callback) => { - const tracker = await client.trackDevices() - tracker.on('add', (device) => { - callback(device) - }) - - tracker.on('remove', (device) => { - callback(device) - }) - - tracker.on('end', (ret) => { - callback(ret) - }) - - tracker.on('error', (err) => { - callback(err) - }) - - const close = () => tracker.end() - - return close - } - - window.addEventListener('beforeunload', () => { - kill() - }) - - return { - getDevices, - shell, - kill, - connect, - disconnect, - watch, - } -}) - -addContext('scrcpy', () => { - const shell = command => - exec(`${scrcpyPath} ${command}`, { env: { ...process.env, ADB: adbPath } }) - - return { - shell, - } -}) +plugins.install(exposeContext) diff --git a/src/preload/plugins/adbkit/index.js b/src/preload/plugins/adbkit/index.js new file mode 100644 index 0000000..59a9b83 --- /dev/null +++ b/src/preload/plugins/adbkit/index.js @@ -0,0 +1,53 @@ +import { Adb } from '@devicefarmer/adbkit' +import adbPath from '@resources/core/adb.exe?asset&asarUnpack' + +let client = null + +window.addEventListener('beforeunload', () => { + if (client) { + client.kill() + } +}) + +const getDevices = async () => await client.listDevicesWithPaths() +const shell = async (id, command) => await client.getDevice(id).shell(command) +const kill = async (...params) => await client.kill(...params) +const connect = async (...params) => await client.connect(...params) +const disconnect = async (...params) => await client.disconnect(...params) + +const watch = async (callback) => { + const tracker = await client.trackDevices() + tracker.on('add', (device) => { + callback(device) + }) + + tracker.on('remove', (device) => { + callback(device) + }) + + tracker.on('end', (ret) => { + callback(ret) + }) + + tracker.on('error', (err) => { + callback(err) + }) + + const close = () => tracker.end() + + return close +} + +export default () => { + client = Adb.createClient({ bin: adbPath }) + console.log('client', client) + + return { + getDevices, + shell, + kill, + connect, + disconnect, + watch, + } +} diff --git a/src/preload/plugins/electron/index.js b/src/preload/plugins/electron/index.js new file mode 100644 index 0000000..408962e --- /dev/null +++ b/src/preload/plugins/electron/index.js @@ -0,0 +1,3 @@ +import { electronAPI } from '@electron-toolkit/preload' + +export default () => electronAPI diff --git a/src/preload/plugins/index.js b/src/preload/plugins/index.js new file mode 100644 index 0000000..873497e --- /dev/null +++ b/src/preload/plugins/index.js @@ -0,0 +1,11 @@ +import electron from './electron/index.js' +import adbkit from './adbkit/index.js' +import scrcpy from './scrcpy/index.js' + +export default { + install(expose) { + expose('electron', electron()) + expose('adbkit', adbkit()) + expose('scrcpy', scrcpy()) + }, +} diff --git a/src/preload/plugins/scrcpy/index.js b/src/preload/plugins/scrcpy/index.js new file mode 100644 index 0000000..afbf3e4 --- /dev/null +++ b/src/preload/plugins/scrcpy/index.js @@ -0,0 +1,12 @@ +import adbPath from '@resources/core/adb.exe?asset&asarUnpack' +import scrcpyPath from '@resources/core/scrcpy.exe?asset&asarUnpack' + +const util = require('node:util') +const exec = util.promisify(require('node:child_process').exec) + +const shell = command => + exec(`${scrcpyPath} ${command}`, { env: { ...process.env, ADB: adbPath } }) + +export default () => ({ + shell, +}) diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue index a451a76..71ce6a9 100644 --- a/src/renderer/src/App.vue +++ b/src/renderer/src/App.vue @@ -15,33 +15,27 @@ - - diff --git a/src/renderer/src/main.js b/src/renderer/src/main.js index d1d9a12..adaab46 100644 --- a/src/renderer/src/main.js +++ b/src/renderer/src/main.js @@ -11,7 +11,7 @@ const app = createApp(App) app.use(plugins) app.config.globalProperties.$electron = window.electron -app.config.globalProperties.$adb = window.adbkit() -app.config.globalProperties.$scrcpy = window.scrcpy() +app.config.globalProperties.$adb = window.adbkit +app.config.globalProperties.$scrcpy = window.scrcpy app.mount('#app')