diff --git a/src/main/jlink.ts b/src/main/jlink.ts index d7c9104f8..8e4a896d2 100644 --- a/src/main/jlink.ts +++ b/src/main/jlink.ts @@ -9,7 +9,8 @@ import { getJLinkState as getState, installJLink as install, } from '@nordicsemiconductor/nrf-jlink-js'; -import fs from 'fs'; +import fs, { copyFileSync, mkdirSync } from 'fs'; +import os from 'os'; import path from 'path'; import { inRenderer } from '../ipc/jlinkProgress'; @@ -27,16 +28,37 @@ const getSingleFileInFolder = (folder: string) => { return path.join(folder, files[0]); }; +const moveFileToTmp = (filePath: string) => { + const baseDir = os.tmpdir(); + const fileName = path.basename(filePath); + const destinationFile = path.join(baseDir, fileName); + try { + mkdirSync(path.dirname(destinationFile), { recursive: true }); + copyFileSync(filePath, destinationFile); + return destinationFile; + } catch (e) { + throw new Error( + `Unable to write file to ${destinationFile}. Error: ${e}`, + ); + } +}; + export const installJLink = async (offlineInstall = false) => { if (offlineInstall) { const bundledJLinkDir = getUnpackedBundledResourcePath( 'prefetched', 'jlink', ); - await install( - getSingleFileInFolder(bundledJLinkDir), - inRenderer.updateJLinkProgress, - ); + let bundledJLink: string; + if (process.platform === 'linux') { + // The bundled file resides in a user-created folder which the root user will be from blocked. + bundledJLink = moveFileToTmp( + getSingleFileInFolder(bundledJLinkDir), + ); + } else { + bundledJLink = getSingleFileInFolder(bundledJLinkDir); + } + await install(bundledJLink, inRenderer.updateJLinkProgress); } else { await downloadAndInstallJLink(inRenderer.updateJLinkProgress); }