Skip to content

Commit

Permalink
fix: 🐛 修复安装路径包含空格会导致无法启动服务的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Oct 25, 2023
1 parent 217d82d commit 29ae786
Show file tree
Hide file tree
Showing 27 changed files with 790 additions and 312 deletions.
15 changes: 11 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"i18n-ally.localesPaths": [
"src/locales",
"dist-release/win-arm64-unpacked/locales",
"dist-release/win-unpacked/locales"
"src/locales/index.js",
"src/locales/languages"
],
"i18n-ally.sourceLanguage": "zh"
"i18n-ally.sourceLanguage": "zh",
"i18n-ally.keystyle": "nested",
"i18n-ally.extract.ignored": [
"${item.id}(${item.$name}${\r\n item.$remark ? `,${item.$remark}` : ''\r\n })",
",${item.$remark}",
"${row.$remark ? `${row.$remark}-` : ''}${\r\n row.$name\r\n }-${this.$replaceIP(row.id)}-recording-${dayjs().format(\r\n 'YYYY-MM-DD-HH-mm-ss',\r\n )}.${recordFormat}",
"--serial=${row.id} --window-title=${\r\n row.$remark ? `${row.$remark}-` : ''\r\n }${row.$name}-${\r\n row.id\r\n }-🎥录制中... --record=${savePath} ${this.scrcpyArgs(row.id)}",
"--serial=${row.id} --window-title=${\r\n row.$remark ? `${row.$remark}-` : ''\r\n }${row.$name}-${row.id} ${this.scrcpyArgs(row.id)}"
]
}
2 changes: 2 additions & 0 deletions electron/configs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ export const scrcpyPath
? extraResolve('core/scrcpy.exe')
: which.sync('scrcpy', { nothrow: true })

export const logPath = process.env.LOG_PATH

// console.log('adbPath', adbPath)
// console.log('scrcpyPath', scrcpyPath)
29 changes: 28 additions & 1 deletion electron/exposes/adbkit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dayjs from 'dayjs'
import { Adb } from '@devicefarmer/adbkit'
import appStore from '@electron/helpers/store.js'
import { adbPath } from '@electron/configs/index.js'
import { uniq } from 'lodash-es'

const exec = util.promisify(child_process.exec)

Expand Down Expand Up @@ -38,7 +39,10 @@ appStore.onDidChange('scrcpy.global.adbPath', async (value, oldValue) => {

const shell = async command => exec(`${adbPath} ${command}`)
const getDevices = async () => client.listDevicesWithPaths()
const deviceShell = async (id, command) => client.getDevice(id).shell(command)
const deviceShell = async (id, command) => {
const res = await client.getDevice(id).shell(command).then(Adb.util.readAll)
return res.toString()
}
const kill = async (...params) => client.kill(...params)
const connect = async (...params) => client.connect(...params)
const disconnect = async (...params) => client.disconnect(...params)
Expand Down Expand Up @@ -95,6 +99,28 @@ const install = async (id, path) => client.getDevice(id).install(path)

const version = async () => client.version()

const display = async (deviceId) => {
let value = []
try {
const res = await deviceShell(deviceId, 'dumpsys display')

const regex = /Display Id=(\d+)/g

const match = res.match(regex) || []

const mapValue = match.map(item => item.split('=')[1])

value = uniq(mapValue)
}
catch (error) {
console.error(error?.message || error)
}

console.log('display.deviceId.value', value)

return value
}

const watch = async (callback) => {
const tracker = await client.trackDevices()
tracker.on('add', async (ret) => {
Expand Down Expand Up @@ -140,6 +166,7 @@ export default () => {
screencap,
install,
version,
display,
watch,
}
}
10 changes: 8 additions & 2 deletions electron/exposes/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import path from 'node:path'

import log from '@electron/helpers/log.js'
import '@electron/helpers/console.js'

import store from '@electron/helpers/store.js'
import * as configs from '@electron/configs/index.js'

import electron from './electron/index.js'
import adbkit from './adbkit/index.js'
import scrcpy from './scrcpy/index.js'
Expand All @@ -12,12 +16,14 @@ export default {

expose('appStore', store)

expose('appLog', log)

expose('electron', {
...electron(),
configs,
})

expose('adbkit', adbkit())
expose('scrcpy', scrcpy())
expose('adbkit', adbkit({ log }))
expose('scrcpy', scrcpy({ log }))
},
}
12 changes: 12 additions & 0 deletions electron/exposes/log/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import log from 'electron-log/main'
import { createProxy } from '@electron/helpers/index'

const levels = Object.keys(log.functions)

const functions = () => ({ ...createProxy(log, levels) })

export default {
levels,
functions: functions(),
...functions(),
}
5 changes: 3 additions & 2 deletions electron/exposes/scrcpy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const shell = async (command, { stdout, stderr } = {}) => {
const ADB = appStore.get('scrcpy.global.adbPath') || adbPath
const args = command.split(' ')

const scrcpyProcess = spawn(spawnPath, args, {
env: { ...process.env, ADB },
const scrcpyProcess = spawn(`"${spawnPath}"`, args, {
env: { ...process.env, ADB: `"${ADB}"` },
shell: true,
encoding: 'utf8',
})

scrcpyProcess.stdout.on('data', (data) => {
Expand Down
8 changes: 8 additions & 0 deletions electron/helpers/console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import log from '@electron/helpers/log.js'

import { createProxy } from './index.js'

Object.assign(console, {
...createProxy(log.functions, log.levels),
raw: console.log,
})
4 changes: 3 additions & 1 deletion electron/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'node:path'
import { contextBridge } from 'electron'
import { cloneDeep } from 'lodash-es'

export const isPackaged = process.env.IS_PACKAGED === 'true'

Expand Down Expand Up @@ -35,7 +36,8 @@ export function exposeContext(key, value) {
*/
export function createProxy(targetObject, methodNames) {
return methodNames.reduce((proxyObj, methodName) => {
proxyObj[methodName] = (...args) => targetObject[methodName](...args)
proxyObj[methodName] = (...args) =>
targetObject[methodName](...cloneDeep(args))

return proxyObj
}, {})
Expand Down
19 changes: 19 additions & 0 deletions electron/helpers/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { shell } from 'electron'
import log from 'electron-log/main'
import { createProxy } from '@electron/helpers/index'

log.transports.console.level = false

const levels = Object.keys(log.functions)

const getFilePath = () => log.transports.file.getFile()?.path

console.log('logPath', getFilePath())

export default {
...createProxy(log, ['initialize', ...levels]),
levels,
functions: createProxy(log, levels),
getFilePath,
openInEditor: () => shell.openPath(getFilePath()),
}
12 changes: 9 additions & 3 deletions electron/helpers/store.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import Store from 'electron-store'
import { isEqual } from 'lodash-es'
import { createProxy } from './index.js'

const appStore = new Store()

appStore.onDidAnyChange((value) => {
console.log('appStore.onDidAnyChange.value', value)
})
// appStore.onDidAnyChange((value) => {
// console.log('appStore.onDidAnyChange.value', value)
// })

// 如果没有数据则手动设置值,以保证配置文件生成成功
if (isEqual(appStore.store, {})) {
appStore.store = {}
}

export default {
...createProxy(appStore, [
Expand Down
7 changes: 7 additions & 0 deletions electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ import { electronApp, optimizer } from '@electron-toolkit/utils'
import './helpers/process.js'
import './helpers/store.js'

import log from './helpers/log.js'
import './helpers/console.js'

import { icnsLogoPath, icoLogoPath, logoPath } from './configs/index.js'

import events from './events/index.js'

log.initialize({ preload: true })

console.log('Successfully initialized the Escrcpy logging system.')

// The built directory structure
//
// ├─┬─┬ dist
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dayjs": "^1.11.10",
"electron": "^27.0.2",
"electron-builder": "^24.6.4",
"electron-log": "^5.0.0",
"electron-store": "^8.1.0",
"electron-updater": "^6.1.4",
"element-plus": "^2.4.0",
Expand Down
Loading

0 comments on commit 29ae786

Please sign in to comment.