Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"pack": "zx script/pack.mjs",
"start": "electron ./dist/main/index.js --inspect=9229",
"lint": "eslint \"src/**/*.{ts,tsx}\"",
"test": "node --experimental-strip-types --disable-warning=ExperimentalWarning --disable-warning=MODULE_TYPELESS_PACKAGE_JSON --test src/main/lib/hdc/targets.test.ts",
"init": "npm run resources",
"resources": "zx script/resources.mjs",
"format": "lsla prettier \"src/**/*.{ts,tsx,scss,css,json}\" \"*.{js,ts,json}\" \"script/*.mjs\" --write",
Expand Down
22 changes: 2 additions & 20 deletions src/main/lib/hdc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import { handleEvent, resolveResources } from 'share/main/lib/util'
import Hdc, { Client } from 'hdckit'
import log from 'share/common/log'
import map from 'licia/map'
import startWith from 'licia/startWith'
import trim from 'licia/trim'
import os from 'node:os'
Expand All @@ -28,6 +27,7 @@ import { app } from 'electron'
import * as window from 'share/main/lib/window'
import isMac from 'licia/isMac'
import childProcess from 'node:child_process'
import { getTargets as fetchTargets } from './hdc/targets'

const logger = log('hdc')

Expand All @@ -36,25 +36,7 @@ const settingsStore = getSettingsStore()
let client: Client

const getTargets: IpcGetTargets = async function () {
const targets = await client.listTargets()

return Promise.all(
map(targets, async (connectKey: string) => {
const parameters = await client.getTarget(connectKey).getParameters()
let ohosVersion =
parameters['const.product.software.version'].split(/\s/)[1]
ohosVersion = ohosVersion.slice(0, ohosVersion.indexOf('('))

const sdkVersion = parameters['const.ohos.apiversion']

return {
name: parameters['const.product.name'],
key: connectKey,
ohosVersion,
sdkVersion,
}
})
).catch(() => [])
return fetchTargets(client)
}

const getOverview: IpcGetOverview = async function (connectKey) {
Expand Down
87 changes: 87 additions & 0 deletions src/main/lib/hdc/targets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { getTargets, type HdcTargetClient } from './targets.ts'

function mockClient(
entries: Record<string, Record<string, any> | Error>
): HdcTargetClient {
return {
listTargets: async () => Object.keys(entries),
getTarget(connectKey: string) {
return {
async getParameters() {
const value = entries[connectKey]
if (value instanceof Error) {
throw value
}
if (!value) {
throw new Error(`unknown target: ${connectKey}`)
}
return value
},
}
},
}
}

describe('getTargets', () => {
it('returns target info on the happy path', async () => {
const client = mockClient({
'2HU0223G15000621': {
'const.product.name': 'ALN-AL00',
'const.product.software.version':
'ALN-AL00 5.0.0.22(SP35DEVC00E22R4P1log)',
'const.ohos.apiversion': '12',
},
})

assert.deepEqual(await getTargets(client), [
{
name: 'ALN-AL00',
key: '2HU0223G15000621',
ohosVersion: '5.0.0.22',
sdkVersion: '12',
},
])
})

it('keeps other devices when one target throws', async () => {
const client = mockClient({
'bad-device': new Error('getParameters failed'),
'good-device': {
'const.product.name': 'OH emulator',
'const.product.software.version':
'OpenHarmony 4.1.0(API Version 11 Release)',
'const.ohos.apiversion': '11',
},
})

assert.deepEqual(await getTargets(client), [
{
name: 'OH emulator',
key: 'good-device',
ohosVersion: '4.1.0',
sdkVersion: '11',
},
])
})

it('parses a version string without space or parenthesis', async () => {
const client = mockClient({
emulator: {
'const.product.name': 'rk3568',
'const.product.software.version': '5.0.0.22',
'const.ohos.apiversion': '12',
},
})

assert.deepEqual(await getTargets(client), [
{
name: 'rk3568',
key: 'emulator',
ohosVersion: '5.0.0.22',
sdkVersion: '12',
},
])
})
})
63 changes: 63 additions & 0 deletions src/main/lib/hdc/targets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export interface HdcTargetInfo {
name: string
key: string
ohosVersion: string
sdkVersion: string
}

export interface HdcTargetClient {
listTargets(): Promise<string[]>
getTarget(connectKey: string): {
getParameters(): Promise<Record<string, any>>
}
}

export function parseOhosVersion(softwareVersion: unknown): string {
if (typeof softwareVersion !== 'string') {
return ''
}

const trimmed = softwareVersion.trim()
if (!trimmed) {
return ''
}

const tokens = trimmed.split(/\s+/)
const versionToken = tokens.length > 1 ? tokens[1] : tokens[0]
const parenIdx = versionToken.indexOf('(')
const version =
parenIdx === -1 ? versionToken : versionToken.slice(0, parenIdx)

return version.trim()
}

export async function getTargets(
client: HdcTargetClient
): Promise<HdcTargetInfo[]> {
let connectKeys: string[]
try {
connectKeys = await client.listTargets()
} catch {
return []
}

const results = await Promise.all(
connectKeys.map(async (connectKey) => {
try {
const parameters = await client.getTarget(connectKey).getParameters()
return {
name: parameters['const.product.name'],
key: connectKey,
ohosVersion: parseOhosVersion(
parameters['const.product.software.version']
),
sdkVersion: parameters['const.ohos.apiversion'],
}
} catch {
return null
}
})
)

return results.filter((target): target is HdcTargetInfo => target !== null)
}