Skip to content

Commit

Permalink
[CP-3185] Integrate the flashing mechanism with the application on Wi…
Browse files Browse the repository at this point in the history
…ndows
  • Loading branch information
dkarski committed Oct 14, 2024
1 parent aea62e5 commit e61a4ff
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
import IDeviceFlash from "./device-flash.interface"
import LinuxDeviceFlashService from "./linux/linux-device-flash.service"
import MacDeviceFlashService from "./macos/macos-device-flash-service"
import WindowsDeviceFlashService from "./windows/windows-device-flash.service"

class DeviceFlashFactory {
static createDeviceFlashService(temporaryDirectoryPath: string): IDeviceFlash {
static createDeviceFlashService(
temporaryDirectoryPath: string
): IDeviceFlash {
const platform = process.platform

if (platform === "linux") {
return new LinuxDeviceFlashService()
} else if (platform === "darwin") {
return new MacDeviceFlashService(temporaryDirectoryPath)
} else if (platform === "win32") {
return new WindowsDeviceFlashService()
} else {
throw new Error(`Unsupported platform: ${platform}`)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import IDeviceFlash from "../device-flash.interface"
import { execCommandWithSudo, execPromise } from "shared/utils"

interface DiskInformation {
DiskNumber: number
FriendlyName: string
Size: string
OperationalStatus: string
}

class WindowsDeviceFlashService implements IDeviceFlash {
async findDeviceByDeviceName(deviceName: string): Promise<string> {
console.log(
`Searching for the device with the friendly name: ${deviceName}`
)

const getDiskResult = await execPromise(
`powershell.exe -Command "Get-Disk | Where-Object { $_.FriendlyName -eq '${deviceName}' }| ConvertTo-Json"`
)

if (!getDiskResult) {
throw new Error(`Disk not found for friendly name: ${deviceName}`)
}

const diskInformation: DiskInformation = JSON.parse(getDiskResult)

console.log(
`Disk information retrieved successfully: Disk Number - ${diskInformation.DiskNumber}, Size - ${diskInformation.Size} bytes, Status - ${diskInformation.OperationalStatus}`
)

return String(diskInformation.DiskNumber)
}

async execute(device: string, imagePath: string, scriptPath: string): Promise<void> {
await this.flashDevice(device, imagePath, scriptPath)
await this.ejectDevice(device)
console.log("Flash process completed successfully")
}

private async flashDevice(device: string, imagePath: string, scriptPath: string): Promise<void> {
const command = `powershell.exe -ExecutionPolicy Bypass -File ${scriptPath} -file ${imagePath} -diskid ${device} -force`

try {
await execCommandWithSudo(command, { name: "Mudita Auto Flash" })
} catch (error) {
throw new Error(`An error occurred during flashing: ${error}`)

Check warning on line 51 in libs/msc-flash/msc-flash-harmony/src/lib/services/device-flash/windows/windows-device-flash.service.ts

View workflow job for this annotation

GitHub Actions / validate (18.16.1)

Invalid type "unknown" of template literal expression
}
}

private async ejectDevice(device: string): Promise<void> {
// TODO: Implement eject logic using a proper method for safely removing the device based on the disk number.
}
}

export default WindowsDeviceFlashService
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ const startFlashingProcess = async (

const device = await deviceFlash.findDeviceByDeviceName("HARMONY")

console.log("flashingFiles.scripts")
console.log(flashingFiles?.scripts)

const flashingScriptName = flashingFiles
? flashingFiles.scripts[0].name
: ""
Expand Down

0 comments on commit e61a4ff

Please sign in to comment.