Skip to content
Open
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
32 changes: 27 additions & 5 deletions src/main/jlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}
Expand Down