@nordicsemiconductor/nrf-jlink-js 0.11.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,7 +1,10 @@
1
+ LicenseID: LicenseRef-Nordic-4-Clause
2
+
3
+ ExtractedText: <text>
4
+
1
5
  Copyright (c) 2025 Nordic Semiconductor ASA
2
6
  All rights reserved.
3
7
 
4
- SPDX-License-Identifier: Nordic-4-Clause
5
8
 
6
9
  Use in source and binary forms, redistribution in binary form only, with
7
10
  or without modification, are permitted provided that the following conditions
@@ -33,3 +36,4 @@ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
36
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34
37
  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35
38
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
+ </text>
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # nrf-jlink-js
2
2
 
3
- `nrf-jlink-js` is a Node.js module to check and install the recommended JLink version used by some products of Nordic Semiconductor ASA.
3
+ `nrf-jlink-js` is a Node.js module to check and install the recommended JLink
4
+ version used by some products of Nordic Semiconductor ASA.
4
5
 
5
6
  ## Installation
6
7
 
@@ -11,9 +12,9 @@ npm install @nordicsemiconductor/nrf-jlink-js
11
12
  ## Usage
12
13
 
13
14
  ```js
14
- const { outdated, versionToBeInstalled } = await getVersionToInstall()
15
+ const { status } = await getJLinkState();
15
16
 
16
- if (outdated) {
17
- downloadAndInstallJlink(console.log)
17
+ if (status === 'should be updated') {
18
+ downloadAndInstallJlink(console.log);
18
19
  }
19
20
  ```
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
- import { downloadAndInstallJLink, getVersionToInstall, downloadAndSaveJLink, installJLink, Update as JLinkUpdate } from './jlink';
2
- export { downloadAndInstallJLink, getVersionToInstall, downloadAndSaveJLink, installJLink, JLinkUpdate, };
1
+ export type { Update as JLinkUpdate } from './shared/update';
2
+ export { downloadAndInstallJLink, downloadAndSaveJLink } from './jlink';
3
+ export { getJLinkState, type JLinkState } from './operations/getJLinkState';
4
+ export { installJLink } from './operations/installJLink';
package/dist/index.js CHANGED
@@ -1,8 +1,15 @@
1
1
  "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
2
7
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.installJLink = exports.downloadAndSaveJLink = exports.getVersionToInstall = exports.downloadAndInstallJLink = void 0;
8
+ exports.installJLink = exports.getJLinkState = exports.downloadAndSaveJLink = exports.downloadAndInstallJLink = void 0;
4
9
  var jlink_1 = require("./jlink");
5
10
  Object.defineProperty(exports, "downloadAndInstallJLink", { enumerable: true, get: function () { return jlink_1.downloadAndInstallJLink; } });
6
- Object.defineProperty(exports, "getVersionToInstall", { enumerable: true, get: function () { return jlink_1.getVersionToInstall; } });
7
11
  Object.defineProperty(exports, "downloadAndSaveJLink", { enumerable: true, get: function () { return jlink_1.downloadAndSaveJLink; } });
8
- Object.defineProperty(exports, "installJLink", { enumerable: true, get: function () { return jlink_1.installJLink; } });
12
+ var getJLinkState_1 = require("./operations/getJLinkState");
13
+ Object.defineProperty(exports, "getJLinkState", { enumerable: true, get: function () { return getJLinkState_1.getJLinkState; } });
14
+ var installJLink_1 = require("./operations/installJLink");
15
+ Object.defineProperty(exports, "installJLink", { enumerable: true, get: function () { return installJLink_1.installJLink; } });
package/dist/jlink.d.ts CHANGED
@@ -1,18 +1,6 @@
1
- export interface Update {
2
- step: 'install' | 'download';
3
- percentage: number;
4
- }
5
- export declare const installJLink: (installerPath: string, onUpdate?: (update: Update) => void) => Promise<void>;
6
- interface JLinkState {
7
- outdated: boolean;
8
- installed: boolean;
9
- versionToBeInstalled?: string;
10
- installedVersion?: string;
11
- }
12
- export declare const getVersionToInstall: ({ fallbackVersion, checkOnline, }?: {
13
- fallbackVersion?: string;
14
- checkOnline?: boolean;
15
- }) => Promise<JLinkState>;
16
- export declare const downloadAndSaveJLink: (destinationDir: string, destinationFileName?: string, onUpdate?: (update: Update) => void) => Promise<string>;
17
- export declare const downloadAndInstallJLink: (onUpdate?: (update: Update) => void) => Promise<void>;
18
- export {};
1
+ import type { OnUpdate } from './shared/update';
2
+ export declare const downloadAndSaveJLink: (destinationDir: string, destinationFileName?: string, onUpdate?: OnUpdate) => Promise<{
3
+ version: string;
4
+ fileName: string;
5
+ }>;
6
+ export declare const downloadAndInstallJLink: (onUpdate?: OnUpdate) => Promise<void>;
package/dist/jlink.js CHANGED
@@ -1,4 +1,9 @@
1
1
  "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
2
7
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
8
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
9
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -35,241 +40,29 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
40
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
41
  }
37
42
  };
38
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
- if (ar || !(i in from)) {
41
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
- ar[i] = from[i];
43
- }
44
- }
45
- return to.concat(ar || Array.prototype.slice.call(from));
46
- };
47
- var __importDefault = (this && this.__importDefault) || function (mod) {
48
- return (mod && mod.__esModule) ? mod : { "default": mod };
49
- };
50
43
  Object.defineProperty(exports, "__esModule", { value: true });
51
- exports.downloadAndInstallJLink = exports.downloadAndSaveJLink = exports.getVersionToInstall = exports.installJLink = void 0;
52
- var child_process_1 = require("child_process");
53
- var os_1 = __importDefault(require("os"));
54
- var path_1 = __importDefault(require("path"));
55
- var semver_1 = __importDefault(require("semver"));
56
- var fs_1 = require("fs");
57
- var common_1 = require("./common");
58
- var util_1 = require("util");
59
- function winRegQuery(key) {
60
- var _a;
61
- if (process.platform !== 'win32') {
62
- throw new Error('Unsupported platform');
63
- }
64
- var reg = path_1.default.resolve((_a = process.env.SystemRoot) !== null && _a !== void 0 ? _a : 'C:\\Windows', 'System32', 'reg.exe');
65
- if (!(0, fs_1.existsSync)(reg)) {
66
- reg = 'reg.exe';
67
- }
68
- return (0, child_process_1.execSync)("".concat(reg, " query ").concat(key)).toString().trim();
69
- }
70
- var getJLinkExePath = function () {
71
- var _a;
72
- switch (os_1.default.platform()) {
73
- case 'win32':
74
- var cwd = winRegQuery('HKEY_CURRENT_USER\\Software\\SEGGER\\J-Link /v InstallPath');
75
- if (!cwd) {
76
- cwd = winRegQuery('HKEY_LOCAL_MACHINE\\Software\\SEGGER\\J-Link /v InstallPath');
77
- }
78
- cwd = ((_a = /InstallPath\s+\w+\s+(.*)/.exec(cwd)) !== null && _a !== void 0 ? _a : [])[1];
79
- if (!cwd) {
80
- throw new Error('JLink not installed');
81
- }
82
- return "\"".concat(path_1.default.join(cwd, 'JLink.exe'), "\"");
83
- case 'linux':
84
- case 'darwin':
85
- return 'JLinkExe';
86
- default:
87
- throw new Error('Invalid platform');
88
- }
89
- };
90
- var getInstalledJLinkVersion = function () { return __awaiter(void 0, void 0, void 0, function () {
91
- var output, versionRegExp, match;
92
- var _a;
93
- return __generator(this, function (_b) {
94
- switch (_b.label) {
95
- case 0: return [4 /*yield*/, (0, util_1.promisify)(child_process_1.exec)("".concat(getJLinkExePath(), " -CommandFile foo")).catch(function (e) { return e; })];
44
+ exports.downloadAndInstallJLink = exports.downloadAndSaveJLink = void 0;
45
+ var fetchIndex_1 = require("./operations/fetchIndex");
46
+ var downloadJLink_1 = require("./operations/downloadJLink");
47
+ var installJLink_1 = require("./operations/installJLink");
48
+ var downloadAndSaveJLink = function (destinationDir, destinationFileName, onUpdate) { return __awaiter(void 0, void 0, void 0, function () {
49
+ var index, fileName;
50
+ return __generator(this, function (_a) {
51
+ switch (_a.label) {
52
+ case 0: return [4 /*yield*/, (0, fetchIndex_1.fetchIndex)()];
96
53
  case 1:
97
- output = _b.sent();
98
- versionRegExp = /^SEGGER J-Link Commander V([0-9a-z.]+) .*$/m;
99
- match = (_a = output.stdout.match(versionRegExp)) === null || _a === void 0 ? void 0 : _a[1];
100
- if (!match) {
101
- throw new Error("Couldn't get jlink version.");
102
- }
103
- return [2 /*return*/, "v".concat(match)];
54
+ index = _a.sent();
55
+ return [4 /*yield*/, (0, downloadJLink_1.downloadJLink)(index, onUpdate, destinationDir, destinationFileName)];
56
+ case 2:
57
+ fileName = _a.sent();
58
+ return [2 /*return*/, { version: index.version, fileName: fileName }];
104
59
  }
105
60
  });
106
61
  }); };
107
- var downloadJLink = function (_a, onUpdate_1) {
108
- var args_1 = [];
109
- for (var _i = 2; _i < arguments.length; _i++) {
110
- args_1[_i - 2] = arguments[_i];
111
- }
112
- return __awaiter(void 0, __spreadArray([_a, onUpdate_1], args_1, true), void 0, function (_b, onUpdate, destinationDir, destinationFileName) {
113
- var platform, arch, url, response, hasContentLength, contentLength, reader, chunks, receivedLength, _c, done, value, chunksAll, position;
114
- var _d, _e;
115
- var jlinkUrls = _b.jlinkUrls;
116
- if (destinationDir === void 0) { destinationDir = os_1.default.tmpdir(); }
117
- return __generator(this, function (_f) {
118
- switch (_f.label) {
119
- case 0:
120
- platform = os_1.default.platform();
121
- arch = os_1.default.arch();
122
- // @ts-expect-error It is quite literally checked right before
123
- if (!(platform in jlinkUrls) || !(arch in jlinkUrls[platform])) {
124
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
125
- }
126
- url = (_d = jlinkUrls[platform]) === null || _d === void 0 ? void 0 : _d[arch];
127
- if (!url) {
128
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
129
- }
130
- return [4 /*yield*/, fetch(url, {
131
- headers: {
132
- Range: 'bytes=0-',
133
- },
134
- })];
135
- case 1:
136
- response = _f.sent();
137
- if (!response.ok) {
138
- throw new Error("Unable to download ".concat(url, ". Got status code ").concat(status, "."));
139
- }
140
- hasContentLength = response.headers.has('content-length');
141
- contentLength = hasContentLength
142
- ? Number(response.headers.get('content-length'))
143
- : 1;
144
- reader = (_e = response.body) === null || _e === void 0 ? void 0 : _e.getReader();
145
- chunks = [];
146
- receivedLength = 0;
147
- _f.label = 2;
148
- case 2:
149
- if (!reader) return [3 /*break*/, 4];
150
- return [4 /*yield*/, reader.read()];
151
- case 3:
152
- _c = _f.sent(), done = _c.done, value = _c.value;
153
- if (done) {
154
- return [3 /*break*/, 4];
155
- }
156
- chunks.push(value);
157
- receivedLength += value.length;
158
- onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({
159
- step: 'download',
160
- percentage: hasContentLength
161
- ? Number(((receivedLength / contentLength) * 100).toFixed(2))
162
- : 0,
163
- });
164
- return [3 /*break*/, 2];
165
- case 4:
166
- chunksAll = new Uint8Array(receivedLength);
167
- position = 0;
168
- chunks.forEach(function (chunk) {
169
- chunksAll.set(chunk, position);
170
- position += chunk.length;
171
- });
172
- return [4 /*yield*/, (0, common_1.saveToFile)(path_1.default.join(destinationDir, destinationFileName || path_1.default.basename(url)), Buffer.from(chunksAll))];
173
- case 5: return [2 /*return*/, _f.sent()];
174
- }
175
- });
176
- });
177
- };
178
- var installJLink = function (installerPath, onUpdate) {
179
- var command;
180
- var args;
181
- switch (os_1.default.platform()) {
182
- case 'darwin':
183
- command = 'open';
184
- args = ['-W', installerPath];
185
- break;
186
- case 'linux':
187
- command = 'pkexec';
188
- args = ['sh', '-c', "dpkg -i \"".concat(installerPath, "\"")];
189
- break;
190
- case 'win32':
191
- command = installerPath;
192
- args = ['-InstUSBDriver=1'];
193
- break;
194
- default:
195
- throw new Error('Invalid platform');
196
- }
197
- onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ step: 'install', percentage: 0 });
198
- return new Promise(function (resolve, reject) {
199
- (0, child_process_1.execFile)(command, args, function (error, _, stderr) {
200
- if (error) {
201
- return reject(error);
202
- }
203
- if (stderr) {
204
- return reject(stderr);
205
- }
206
- onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ step: 'install', percentage: 100 });
207
- return resolve();
208
- });
209
- });
210
- };
211
- exports.installJLink = installJLink;
212
- var convertToSemverVersion = function (version) {
213
- var _a;
214
- var _b = (_a = version.match(/V?(\d+\.\d+)(.)?/)) !== null && _a !== void 0 ? _a : [], majorMinor = _b[1], patchLetter = _b[2];
215
- var patch = patchLetter
216
- ? patchLetter.charCodeAt(0) - 'a'.charCodeAt(0) + 1
217
- : 0;
218
- return "".concat(majorMinor, ".").concat(patch);
219
- };
220
- var isValidVersion = function (installedVersion, expectedVersion) {
221
- return semver_1.default.gte(convertToSemverVersion(installedVersion), convertToSemverVersion(expectedVersion));
222
- };
223
- var getVersionToInstall = function () {
224
- var args_1 = [];
225
- for (var _i = 0; _i < arguments.length; _i++) {
226
- args_1[_i] = arguments[_i];
227
- }
228
- return __awaiter(void 0, __spreadArray([], args_1, true), void 0, function (_a) {
229
- var onlineRecommendedVersion, _b, versionToBeInstalled, installedVersion, installed, outdated;
230
- var _c;
231
- var _d = _a === void 0 ? {} : _a, fallbackVersion = _d.fallbackVersion, _e = _d.checkOnline, checkOnline = _e === void 0 ? true : _e;
232
- return __generator(this, function (_f) {
233
- switch (_f.label) {
234
- case 0:
235
- if (!checkOnline) return [3 /*break*/, 2];
236
- return [4 /*yield*/, (0, common_1.fetchIndex)().catch(function () { return undefined; })];
237
- case 1:
238
- _b = (_c = (_f.sent())) === null || _c === void 0 ? void 0 : _c.version;
239
- return [3 /*break*/, 3];
240
- case 2:
241
- _b = undefined;
242
- _f.label = 3;
243
- case 3:
244
- onlineRecommendedVersion = _b;
245
- versionToBeInstalled = onlineRecommendedVersion !== null && onlineRecommendedVersion !== void 0 ? onlineRecommendedVersion : fallbackVersion;
246
- return [4 /*yield*/, getInstalledJLinkVersion().catch(function () { return undefined; })];
247
- case 4:
248
- installedVersion = _f.sent();
249
- installed = !!installedVersion;
250
- outdated = !installed ||
251
- !versionToBeInstalled ||
252
- !isValidVersion(installedVersion, versionToBeInstalled);
253
- return [2 /*return*/, {
254
- outdated: outdated,
255
- installedVersion: installedVersion,
256
- installed: installed,
257
- versionToBeInstalled: versionToBeInstalled,
258
- }];
259
- }
260
- });
261
- });
262
- };
263
- exports.getVersionToInstall = getVersionToInstall;
264
- var downloadAndSaveJLink = function (destinationDir, destinationFileName, onUpdate) {
265
- return (0, common_1.fetchIndex)().then(function (v) {
266
- return downloadJLink(v, onUpdate, destinationDir, destinationFileName);
267
- });
268
- };
269
62
  exports.downloadAndSaveJLink = downloadAndSaveJLink;
270
63
  var downloadAndInstallJLink = function (onUpdate) {
271
- return (0, common_1.fetchIndex)()
272
- .then(function (v) { return downloadJLink(v, onUpdate); })
273
- .then(function (v) { return (0, exports.installJLink)(v, onUpdate); });
64
+ return (0, fetchIndex_1.fetchIndex)()
65
+ .then(function (index) { return (0, downloadJLink_1.downloadJLink)(index, onUpdate); })
66
+ .then(function (fileName) { return (0, installJLink_1.installJLink)(fileName, onUpdate); });
274
67
  };
275
68
  exports.downloadAndInstallJLink = downloadAndInstallJLink;
@@ -0,0 +1,3 @@
1
+ import { JLinkIndex } from './fetchIndex';
2
+ import { OnUpdate } from '../shared/update';
3
+ export declare const downloadJLink: ({ jlinkUrls }: JLinkIndex, onUpdate?: OnUpdate, destinationDir?: string, destinationFileName?: string) => Promise<string>;
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
7
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
9
+ return new (P || (P = Promise))(function (resolve, reject) {
10
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
12
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
13
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
14
+ });
15
+ };
16
+ var __generator = (this && this.__generator) || function (thisArg, body) {
17
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
18
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
19
+ function verb(n) { return function (v) { return step([n, v]); }; }
20
+ function step(op) {
21
+ if (f) throw new TypeError("Generator is already executing.");
22
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
23
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
24
+ if (y = 0, t) op = [op[0] & 2, t.value];
25
+ switch (op[0]) {
26
+ case 0: case 1: t = op; break;
27
+ case 4: _.label++; return { value: op[1], done: false };
28
+ case 5: _.label++; y = op[1]; op = [0]; continue;
29
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
30
+ default:
31
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
32
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
33
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
34
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
35
+ if (t[2]) _.ops.pop();
36
+ _.trys.pop(); continue;
37
+ }
38
+ op = body.call(thisArg, _);
39
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
40
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
41
+ }
42
+ };
43
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
44
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
45
+ if (ar || !(i in from)) {
46
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
47
+ ar[i] = from[i];
48
+ }
49
+ }
50
+ return to.concat(ar || Array.prototype.slice.call(from));
51
+ };
52
+ var __importDefault = (this && this.__importDefault) || function (mod) {
53
+ return (mod && mod.__esModule) ? mod : { "default": mod };
54
+ };
55
+ Object.defineProperty(exports, "__esModule", { value: true });
56
+ exports.downloadJLink = void 0;
57
+ var os_1 = __importDefault(require("os"));
58
+ var path_1 = __importDefault(require("path"));
59
+ var fs_1 = require("../shared/fs");
60
+ var net_1 = require("../shared/net");
61
+ var getDownloadJLinkUrl = function (jlinkUrls) {
62
+ var _a;
63
+ var platform = os_1.default.platform();
64
+ var arch = os_1.default.arch();
65
+ // @ts-expect-error It is quite literally checked right before
66
+ if (!(platform in jlinkUrls) || !(arch in jlinkUrls[platform])) {
67
+ throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
68
+ }
69
+ // @ts-expect-error We know it exists but it is also handled if undefined
70
+ var url = (_a = jlinkUrls[platform]) === null || _a === void 0 ? void 0 : _a[arch];
71
+ if (!url) {
72
+ throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
73
+ }
74
+ return url;
75
+ };
76
+ var downloadJLink = function (_a, onUpdate_1) {
77
+ var args_1 = [];
78
+ for (var _i = 2; _i < arguments.length; _i++) {
79
+ args_1[_i - 2] = arguments[_i];
80
+ }
81
+ return __awaiter(void 0, __spreadArray([_a, onUpdate_1], args_1, true), void 0, function (_b, onUpdate, destinationDir, destinationFileName) {
82
+ var url, jlink;
83
+ var jlinkUrls = _b.jlinkUrls;
84
+ if (destinationDir === void 0) { destinationDir = os_1.default.tmpdir(); }
85
+ return __generator(this, function (_c) {
86
+ switch (_c.label) {
87
+ case 0:
88
+ url = getDownloadJLinkUrl(jlinkUrls);
89
+ return [4 /*yield*/, (0, net_1.download)(url, onUpdate)];
90
+ case 1:
91
+ jlink = _c.sent();
92
+ return [2 /*return*/, (0, fs_1.saveToFile)(path_1.default.join(destinationDir, destinationFileName || path_1.default.basename(url)), jlink)];
93
+ }
94
+ });
95
+ });
96
+ };
97
+ exports.downloadJLink = downloadJLink;
@@ -6,5 +6,5 @@ export interface JLinkIndex {
6
6
  version: string;
7
7
  jlinkUrls: JLinkVariant;
8
8
  }
9
+ export declare const indexUrl = "https://files.nordicsemi.com/ui/api/v1/download?isNativeBrowsing=true&repoKey=swtools&path=external/ncd/jlink/index.json";
9
10
  export declare const fetchIndex: () => Promise<JLinkIndex>;
10
- export declare const saveToFile: (destinationFile: string, data: string | NodeJS.ArrayBufferView) => Promise<string>;
@@ -1,4 +1,9 @@
1
1
  "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
2
7
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
8
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
9
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -35,39 +40,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
40
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
41
  }
37
42
  };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
43
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.saveToFile = exports.fetchIndex = exports.archs = exports.platforms = void 0;
43
- var fs_1 = require("fs");
44
- var path_1 = __importDefault(require("path"));
44
+ exports.fetchIndex = exports.indexUrl = exports.archs = exports.platforms = void 0;
45
+ var net_1 = require("../shared/net");
45
46
  exports.platforms = ['darwin', 'linux', 'win32'];
46
47
  exports.archs = ['arm64', 'x64'];
47
- var fetchJSON = function (url) { return __awaiter(void 0, void 0, void 0, function () {
48
- var response;
49
- return __generator(this, function (_a) {
50
- switch (_a.label) {
51
- case 0: return [4 /*yield*/, fetch(url, {
52
- headers: {
53
- Range: 'bytes=0-',
54
- },
55
- })];
56
- case 1:
57
- response = _a.sent();
58
- if (!response.ok) {
59
- throw new Error("Unable to fetch file from ".concat(indexUrl, ". Got status code ").concat(status, "."));
60
- }
61
- return [2 /*return*/, response.json()];
62
- }
63
- });
64
- }); };
65
- var indexUrl = 'https://files.nordicsemi.com/ui/api/v1/download?isNativeBrowsing=true&repoKey=swtools&path=external/ncd/jlink/index.json';
48
+ exports.indexUrl = 'https://files.nordicsemi.com/ui/api/v1/download?isNativeBrowsing=true&repoKey=swtools&path=external/ncd/jlink/index.json';
66
49
  var fetchIndex = function () { return __awaiter(void 0, void 0, void 0, function () {
67
50
  var res;
68
51
  return __generator(this, function (_a) {
69
52
  switch (_a.label) {
70
- case 0: return [4 /*yield*/, fetchJSON(indexUrl)];
53
+ case 0: return [4 /*yield*/, (0, net_1.fetchJSON)(exports.indexUrl)];
71
54
  case 1:
72
55
  res = _a.sent();
73
56
  if (res == null ||
@@ -81,16 +64,3 @@ var fetchIndex = function () { return __awaiter(void 0, void 0, void 0, function
81
64
  });
82
65
  }); };
83
66
  exports.fetchIndex = fetchIndex;
84
- var saveToFile = function (destinationFile, data) { return __awaiter(void 0, void 0, void 0, function () {
85
- return __generator(this, function (_a) {
86
- (0, fs_1.mkdirSync)(path_1.default.dirname(destinationFile), { recursive: true });
87
- try {
88
- (0, fs_1.writeFileSync)(destinationFile, data);
89
- }
90
- catch (e) {
91
- throw new Error("Unable to write file to ".concat(destinationFile, ". Error: ").concat(e));
92
- }
93
- return [2 /*return*/, destinationFile];
94
- });
95
- }); };
96
- exports.saveToFile = saveToFile;
@@ -0,0 +1,23 @@
1
+ type JLinkNotInstalled = {
2
+ status: 'not installed';
3
+ versionToBeInstalled: string;
4
+ };
5
+ type JLinkNotInstalledNonAvailable = {
6
+ status: 'not installed but none available';
7
+ };
8
+ type JLinkUpToDate = {
9
+ status: 'up to date';
10
+ installedVersion: string;
11
+ versionToBeInstalled?: string;
12
+ };
13
+ type JLinkShouldBeUpdated = {
14
+ status: 'should be updated';
15
+ installedVersion?: string;
16
+ versionToBeInstalled: string;
17
+ };
18
+ export type JLinkState = JLinkNotInstalled | JLinkNotInstalledNonAvailable | JLinkUpToDate | JLinkShouldBeUpdated;
19
+ export declare const getJLinkState: ({ fallbackVersion, checkOnline, }?: {
20
+ fallbackVersion?: string;
21
+ checkOnline?: boolean;
22
+ }) => Promise<JLinkState>;
23
+ export {};
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
7
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
9
+ return new (P || (P = Promise))(function (resolve, reject) {
10
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
12
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
13
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
14
+ });
15
+ };
16
+ var __generator = (this && this.__generator) || function (thisArg, body) {
17
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
18
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
19
+ function verb(n) { return function (v) { return step([n, v]); }; }
20
+ function step(op) {
21
+ if (f) throw new TypeError("Generator is already executing.");
22
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
23
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
24
+ if (y = 0, t) op = [op[0] & 2, t.value];
25
+ switch (op[0]) {
26
+ case 0: case 1: t = op; break;
27
+ case 4: _.label++; return { value: op[1], done: false };
28
+ case 5: _.label++; y = op[1]; op = [0]; continue;
29
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
30
+ default:
31
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
32
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
33
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
34
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
35
+ if (t[2]) _.ops.pop();
36
+ _.trys.pop(); continue;
37
+ }
38
+ op = body.call(thisArg, _);
39
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
40
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
41
+ }
42
+ };
43
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
44
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
45
+ if (ar || !(i in from)) {
46
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
47
+ ar[i] = from[i];
48
+ }
49
+ }
50
+ return to.concat(ar || Array.prototype.slice.call(from));
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.getJLinkState = void 0;
54
+ var fetchIndex_1 = require("./fetchIndex");
55
+ var jLinkVersion_1 = require("../shared/jLinkVersion");
56
+ var getJLinkState = function () {
57
+ var args_1 = [];
58
+ for (var _i = 0; _i < arguments.length; _i++) {
59
+ args_1[_i] = arguments[_i];
60
+ }
61
+ return __awaiter(void 0, __spreadArray([], args_1, true), void 0, function (_a) {
62
+ var onlineRecommendedVersion, _b, versionToBeInstalled, installedVersion;
63
+ var _c;
64
+ var _d = _a === void 0 ? {} : _a, fallbackVersion = _d.fallbackVersion, _e = _d.checkOnline, checkOnline = _e === void 0 ? true : _e;
65
+ return __generator(this, function (_f) {
66
+ switch (_f.label) {
67
+ case 0:
68
+ if (!checkOnline) return [3 /*break*/, 2];
69
+ return [4 /*yield*/, (0, fetchIndex_1.fetchIndex)().catch(function () { return undefined; })];
70
+ case 1:
71
+ _b = (_c = (_f.sent())) === null || _c === void 0 ? void 0 : _c.version;
72
+ return [3 /*break*/, 3];
73
+ case 2:
74
+ _b = undefined;
75
+ _f.label = 3;
76
+ case 3:
77
+ onlineRecommendedVersion = _b;
78
+ versionToBeInstalled = onlineRecommendedVersion !== null && onlineRecommendedVersion !== void 0 ? onlineRecommendedVersion : fallbackVersion;
79
+ return [4 /*yield*/, (0, jLinkVersion_1.getInstalledJLinkVersion)().catch(function () { return undefined; })];
80
+ case 4:
81
+ installedVersion = _f.sent();
82
+ if (installedVersion == null) {
83
+ if (versionToBeInstalled == null) {
84
+ return [2 /*return*/, { status: 'not installed but none available' }];
85
+ }
86
+ return [2 /*return*/, { status: 'not installed', versionToBeInstalled: versionToBeInstalled }];
87
+ }
88
+ /* If `versionToBeInstalled` is undefined we do not really know but at
89
+ least there is some J-Link installed and we do not know that it is
90
+ outdated. So, we return 'up to date', but this may be changed to a
91
+ fifth state "installed, unknown if up-to-date" if we see the need for
92
+ this in the future. */
93
+ if (versionToBeInstalled == null ||
94
+ (0, jLinkVersion_1.isValidVersion)(installedVersion, versionToBeInstalled)) {
95
+ return [2 /*return*/, { status: 'up to date', installedVersion: installedVersion, versionToBeInstalled: versionToBeInstalled }];
96
+ }
97
+ return [2 /*return*/, {
98
+ status: 'should be updated',
99
+ installedVersion: installedVersion,
100
+ versionToBeInstalled: versionToBeInstalled,
101
+ }];
102
+ }
103
+ });
104
+ });
105
+ };
106
+ exports.getJLinkState = getJLinkState;
@@ -0,0 +1,2 @@
1
+ import type { OnUpdate } from '../shared/update';
2
+ export declare const installJLink: (installerPath: string, onUpdate?: OnUpdate) => Promise<void>;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
7
+ var __importDefault = (this && this.__importDefault) || function (mod) {
8
+ return (mod && mod.__esModule) ? mod : { "default": mod };
9
+ };
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.installJLink = void 0;
12
+ var child_process_1 = require("child_process");
13
+ var os_1 = __importDefault(require("os"));
14
+ var installJLink = function (installerPath, onUpdate) {
15
+ var command;
16
+ var args;
17
+ switch (os_1.default.platform()) {
18
+ case 'darwin':
19
+ command = 'open';
20
+ args = ['-W', installerPath];
21
+ break;
22
+ case 'linux':
23
+ command = 'pkexec';
24
+ args = ['sh', '-c', "dpkg -i \"".concat(installerPath, "\"")];
25
+ break;
26
+ case 'win32':
27
+ command = installerPath;
28
+ args = ['-InstUSBDriver=1'];
29
+ break;
30
+ default:
31
+ throw new Error('Invalid platform');
32
+ }
33
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ step: 'install', percentage: 0 });
34
+ return new Promise(function (resolve, reject) {
35
+ (0, child_process_1.execFile)(command, args, function (error, _, stderr) {
36
+ if (error) {
37
+ return reject(error);
38
+ }
39
+ if (stderr) {
40
+ return reject(stderr);
41
+ }
42
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ step: 'install', percentage: 100 });
43
+ return resolve();
44
+ });
45
+ });
46
+ };
47
+ exports.installJLink = installJLink;
@@ -0,0 +1,5 @@
1
+ export declare const saveToFile: (destinationFile: string, data: string | NodeJS.ArrayBufferView) => string;
2
+ export declare const createTemporaryScriptFile: (content: string) => {
3
+ filePath: string;
4
+ [Symbol.dispose]: () => void;
5
+ };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
7
+ var __importDefault = (this && this.__importDefault) || function (mod) {
8
+ return (mod && mod.__esModule) ? mod : { "default": mod };
9
+ };
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.createTemporaryScriptFile = exports.saveToFile = void 0;
12
+ var fs_1 = require("fs");
13
+ var os_1 = require("os");
14
+ var path_1 = __importDefault(require("path"));
15
+ var saveToFile = function (destinationFile, data) {
16
+ try {
17
+ (0, fs_1.mkdirSync)(path_1.default.dirname(destinationFile), { recursive: true });
18
+ (0, fs_1.writeFileSync)(destinationFile, data);
19
+ }
20
+ catch (e) {
21
+ throw new Error("Unable to write file to ".concat(destinationFile, ". Error: ").concat(e));
22
+ }
23
+ return destinationFile;
24
+ };
25
+ exports.saveToFile = saveToFile;
26
+ var createTemporaryScriptFile = function (content) {
27
+ var _a;
28
+ var tmpDir = (0, fs_1.mkdtempSync)(path_1.default.join((0, os_1.tmpdir)(), 'jlink-'));
29
+ var filePath = path_1.default.join(tmpDir, 'script');
30
+ (0, fs_1.writeFileSync)(filePath, content);
31
+ return _a = {
32
+ filePath: filePath
33
+ },
34
+ _a[Symbol.dispose] = function () {
35
+ (0, fs_1.rmSync)(tmpDir, { recursive: true, force: true });
36
+ },
37
+ _a;
38
+ };
39
+ exports.createTemporaryScriptFile = createTemporaryScriptFile;
@@ -0,0 +1,2 @@
1
+ export declare const isValidVersion: (installedVersion: string, expectedVersion: string) => boolean;
2
+ export declare const getInstalledJLinkVersion: () => Promise<string>;
@@ -0,0 +1,182 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
7
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
9
+ return new (P || (P = Promise))(function (resolve, reject) {
10
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
12
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
13
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
14
+ });
15
+ };
16
+ var __generator = (this && this.__generator) || function (thisArg, body) {
17
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
18
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
19
+ function verb(n) { return function (v) { return step([n, v]); }; }
20
+ function step(op) {
21
+ if (f) throw new TypeError("Generator is already executing.");
22
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
23
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
24
+ if (y = 0, t) op = [op[0] & 2, t.value];
25
+ switch (op[0]) {
26
+ case 0: case 1: t = op; break;
27
+ case 4: _.label++; return { value: op[1], done: false };
28
+ case 5: _.label++; y = op[1]; op = [0]; continue;
29
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
30
+ default:
31
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
32
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
33
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
34
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
35
+ if (t[2]) _.ops.pop();
36
+ _.trys.pop(); continue;
37
+ }
38
+ op = body.call(thisArg, _);
39
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
40
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
41
+ }
42
+ };
43
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
44
+ if (value !== null && value !== void 0) {
45
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
46
+ var dispose, inner;
47
+ if (async) {
48
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
49
+ dispose = value[Symbol.asyncDispose];
50
+ }
51
+ if (dispose === void 0) {
52
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
53
+ dispose = value[Symbol.dispose];
54
+ if (async) inner = dispose;
55
+ }
56
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
57
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
58
+ env.stack.push({ value: value, dispose: dispose, async: async });
59
+ }
60
+ else if (async) {
61
+ env.stack.push({ async: true });
62
+ }
63
+ return value;
64
+ };
65
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
66
+ return function (env) {
67
+ function fail(e) {
68
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
69
+ env.hasError = true;
70
+ }
71
+ var r, s = 0;
72
+ function next() {
73
+ while (r = env.stack.pop()) {
74
+ try {
75
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
76
+ if (r.dispose) {
77
+ var result = r.dispose.call(r.value);
78
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
79
+ }
80
+ else s |= 1;
81
+ }
82
+ catch (e) {
83
+ fail(e);
84
+ }
85
+ }
86
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
87
+ if (env.hasError) throw env.error;
88
+ }
89
+ return next();
90
+ };
91
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
92
+ var e = new Error(message);
93
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
94
+ });
95
+ var __importDefault = (this && this.__importDefault) || function (mod) {
96
+ return (mod && mod.__esModule) ? mod : { "default": mod };
97
+ };
98
+ Object.defineProperty(exports, "__esModule", { value: true });
99
+ exports.getInstalledJLinkVersion = exports.isValidVersion = void 0;
100
+ var child_process_1 = require("child_process");
101
+ var fs_1 = require("fs");
102
+ var os_1 = __importDefault(require("os"));
103
+ var path_1 = __importDefault(require("path"));
104
+ var semver_1 = __importDefault(require("semver"));
105
+ var util_1 = require("util");
106
+ var fs_2 = require("./fs");
107
+ var reg = function () {
108
+ var _a;
109
+ var defaultRegLocation = path_1.default.resolve((_a = process.env.SystemRoot) !== null && _a !== void 0 ? _a : 'C:\\Windows', 'System32', 'reg.exe');
110
+ return (0, fs_1.existsSync)(defaultRegLocation) ? defaultRegLocation : 'reg.exe';
111
+ };
112
+ var winRegQuery = function (key) {
113
+ if (process.platform !== 'win32') {
114
+ throw new Error('Unsupported platform');
115
+ }
116
+ return (0, child_process_1.execSync)("".concat(reg(), " query ").concat(key)).toString().trim();
117
+ };
118
+ var getJLinkExePath = function () {
119
+ var _a;
120
+ switch (os_1.default.platform()) {
121
+ case 'win32':
122
+ var cwd = winRegQuery('HKEY_CURRENT_USER\\Software\\SEGGER\\J-Link /v InstallPath');
123
+ if (!cwd) {
124
+ cwd = winRegQuery('HKEY_LOCAL_MACHINE\\Software\\SEGGER\\J-Link /v InstallPath');
125
+ }
126
+ cwd = ((_a = /InstallPath\s+\w+\s+(.*)/.exec(cwd)) !== null && _a !== void 0 ? _a : [])[1];
127
+ if (!cwd) {
128
+ throw new Error('JLink not installed');
129
+ }
130
+ return "\"".concat(path_1.default.join(cwd, 'JLink.exe'), "\"");
131
+ case 'linux':
132
+ case 'darwin':
133
+ return 'JLinkExe';
134
+ default:
135
+ throw new Error('Invalid platform');
136
+ }
137
+ };
138
+ var convertToSemverVersion = function (version) {
139
+ var _a;
140
+ var _b = (_a = version.match(/V?(\d+\.\d+)(.)?/)) !== null && _a !== void 0 ? _a : [], majorMinor = _b[1], patchLetter = _b[2];
141
+ var patch = patchLetter
142
+ ? patchLetter.charCodeAt(0) - 'a'.charCodeAt(0) + 1
143
+ : 0;
144
+ return "".concat(majorMinor, ".").concat(patch);
145
+ };
146
+ var isValidVersion = function (installedVersion, expectedVersion) {
147
+ return semver_1.default.gte(convertToSemverVersion(installedVersion), convertToSemverVersion(expectedVersion));
148
+ };
149
+ exports.isValidVersion = isValidVersion;
150
+ var getInstalledJLinkVersion = function () { return __awaiter(void 0, void 0, void 0, function () {
151
+ var env_1, scriptFile, output, versionRegExp, match, e_1;
152
+ var _a;
153
+ return __generator(this, function (_b) {
154
+ switch (_b.label) {
155
+ case 0:
156
+ env_1 = { stack: [], error: void 0, hasError: false };
157
+ _b.label = 1;
158
+ case 1:
159
+ _b.trys.push([1, 3, 4, 5]);
160
+ scriptFile = __addDisposableResource(env_1, (0, fs_2.createTemporaryScriptFile)('Exit'), false);
161
+ return [4 /*yield*/, (0, util_1.promisify)(child_process_1.exec)("".concat(getJLinkExePath(), " -CommandFile ").concat(scriptFile.filePath)).catch(function (e) { return e; })];
162
+ case 2:
163
+ output = _b.sent();
164
+ versionRegExp = /^SEGGER J-Link Commander V([0-9a-z.]+) .*$/m;
165
+ match = (_a = output.stdout.match(versionRegExp)) === null || _a === void 0 ? void 0 : _a[1];
166
+ if (!match) {
167
+ throw new Error("Couldn't get jlink version.");
168
+ }
169
+ return [2 /*return*/, "v".concat(match)];
170
+ case 3:
171
+ e_1 = _b.sent();
172
+ env_1.error = e_1;
173
+ env_1.hasError = true;
174
+ return [3 /*break*/, 5];
175
+ case 4:
176
+ __disposeResources(env_1);
177
+ return [7 /*endfinally*/];
178
+ case 5: return [2 /*return*/];
179
+ }
180
+ });
181
+ }); };
182
+ exports.getInstalledJLinkVersion = getInstalledJLinkVersion;
@@ -0,0 +1,3 @@
1
+ import type { OnUpdate } from './update';
2
+ export declare const download: (url: string, onUpdate?: OnUpdate) => Promise<Buffer<ArrayBuffer>>;
3
+ export declare const fetchJSON: <T>(url: string) => Promise<T>;
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
7
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
9
+ return new (P || (P = Promise))(function (resolve, reject) {
10
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
12
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
13
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
14
+ });
15
+ };
16
+ var __generator = (this && this.__generator) || function (thisArg, body) {
17
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
18
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
19
+ function verb(n) { return function (v) { return step([n, v]); }; }
20
+ function step(op) {
21
+ if (f) throw new TypeError("Generator is already executing.");
22
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
23
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
24
+ if (y = 0, t) op = [op[0] & 2, t.value];
25
+ switch (op[0]) {
26
+ case 0: case 1: t = op; break;
27
+ case 4: _.label++; return { value: op[1], done: false };
28
+ case 5: _.label++; y = op[1]; op = [0]; continue;
29
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
30
+ default:
31
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
32
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
33
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
34
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
35
+ if (t[2]) _.ops.pop();
36
+ _.trys.pop(); continue;
37
+ }
38
+ op = body.call(thisArg, _);
39
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
40
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
41
+ }
42
+ };
43
+ Object.defineProperty(exports, "__esModule", { value: true });
44
+ exports.fetchJSON = exports.download = void 0;
45
+ var handleFailedRequest = function (response, url) {
46
+ if (!response.ok) {
47
+ throw new Error("Unable to fetch file from ".concat(url, ". Got status code ").concat(response.status, "."));
48
+ }
49
+ };
50
+ var download = function (url, onUpdate) { return __awaiter(void 0, void 0, void 0, function () {
51
+ var response, hasContentLength, contentLength, reader, chunks, receivedLength, _a, done, value;
52
+ var _b;
53
+ return __generator(this, function (_c) {
54
+ switch (_c.label) {
55
+ case 0: return [4 /*yield*/, fetch(url, {
56
+ headers: {
57
+ Range: 'bytes=0-',
58
+ },
59
+ })];
60
+ case 1:
61
+ response = _c.sent();
62
+ handleFailedRequest(response, url);
63
+ hasContentLength = response.headers.has('content-length');
64
+ contentLength = hasContentLength
65
+ ? Number(response.headers.get('content-length'))
66
+ : 1;
67
+ reader = (_b = response.body) === null || _b === void 0 ? void 0 : _b.getReader();
68
+ chunks = [];
69
+ receivedLength = 0;
70
+ _c.label = 2;
71
+ case 2:
72
+ if (!reader) return [3 /*break*/, 4];
73
+ return [4 /*yield*/, reader.read()];
74
+ case 3:
75
+ _a = _c.sent(), done = _a.done, value = _a.value;
76
+ if (done) {
77
+ return [3 /*break*/, 4];
78
+ }
79
+ chunks.push(value);
80
+ receivedLength += value.length;
81
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({
82
+ step: 'download',
83
+ percentage: hasContentLength
84
+ ? Number(((receivedLength / contentLength) * 100).toFixed(2))
85
+ : 0,
86
+ });
87
+ return [3 /*break*/, 2];
88
+ case 4: return [2 /*return*/, Buffer.concat(chunks)];
89
+ }
90
+ });
91
+ }); };
92
+ exports.download = download;
93
+ var fetchJSON = function (url) { return __awaiter(void 0, void 0, void 0, function () {
94
+ var response;
95
+ return __generator(this, function (_a) {
96
+ switch (_a.label) {
97
+ case 0: return [4 /*yield*/, fetch(url, {
98
+ headers: {
99
+ Range: 'bytes=0-',
100
+ },
101
+ })];
102
+ case 1:
103
+ response = _a.sent();
104
+ handleFailedRequest(response, url);
105
+ return [2 /*return*/, response.json()];
106
+ }
107
+ });
108
+ }); };
109
+ exports.fetchJSON = fetchJSON;
@@ -0,0 +1,5 @@
1
+ export interface Update {
2
+ step: 'install' | 'download';
3
+ percentage: number;
4
+ }
5
+ export type OnUpdate = (update: Update) => void;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2025 Nordic Semiconductor ASA
4
+ *
5
+ * SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nordicsemiconductor/nrf-jlink-js",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "main": "dist",
5
5
  "types": "dist",
6
6
  "files": [
@@ -23,6 +23,6 @@
23
23
  "url": "git+https://github.com/NordicSemiconductor/nrf-jlink-js.git"
24
24
  },
25
25
  "author": "Nordic Semiconductor ASA",
26
- "license": "Proprietary",
26
+ "license": "SEE LICENSE IN LICENSE",
27
27
  "description": "Shared library to handle J-Link versions"
28
28
  }