@nordicsemiconductor/nrf-jlink-js 0.0.9 → 0.0.11

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/dist/common.d.ts CHANGED
@@ -7,4 +7,4 @@ export interface JLinkIndex {
7
7
  jlinkUrls: JLinkVariant;
8
8
  }
9
9
  export declare const fetchIndex: () => Promise<JLinkIndex>;
10
- export declare const saveToFile: (destinationFile: string, data: Buffer) => Promise<string>;
10
+ export declare const saveToFile: (destinationFile: string, data: string | NodeJS.ArrayBufferView) => Promise<string>;
package/dist/jlink.d.ts CHANGED
@@ -10,6 +10,6 @@ interface JLinkState {
10
10
  installedVersion?: string;
11
11
  }
12
12
  export declare const getVersionToInstall: (fallbackVersion?: string) => Promise<JLinkState>;
13
- export declare const downloadAndSaveJLink: (destination: string, onUpdate?: (update: Update) => void) => Promise<string>;
13
+ export declare const downloadAndSaveJLink: (destinationDir: string, destinationFileName?: string, onUpdate?: (update: Update) => void) => Promise<string>;
14
14
  export declare const downloadAndInstallJLink: (onUpdate?: (update: Update) => void) => Promise<void>;
15
15
  export {};
package/dist/jlink.js CHANGED
@@ -35,6 +35,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
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
+ };
38
47
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
48
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
49
  };
@@ -44,20 +53,30 @@ var child_process_1 = require("child_process");
44
53
  var os_1 = __importDefault(require("os"));
45
54
  var path_1 = __importDefault(require("path"));
46
55
  var semver_1 = __importDefault(require("semver"));
56
+ var fs_1 = require("fs");
47
57
  var common_1 = require("./common");
58
+ function winRegQuery(key) {
59
+ var _a;
60
+ if (process.platform !== 'win32') {
61
+ throw new Error('Unsupported platform');
62
+ }
63
+ var reg = path_1.default.resolve((_a = process.env.SystemRoot) !== null && _a !== void 0 ? _a : 'C:\\Windows', 'System32', 'reg.exe');
64
+ if (!(0, fs_1.existsSync)(reg)) {
65
+ reg = 'reg.exe';
66
+ }
67
+ return (0, child_process_1.execSync)("".concat(reg, " query ").concat(key)).toString().trim();
68
+ }
48
69
  var getJLinkExePath = function () {
49
70
  switch (os_1.default.platform()) {
50
71
  case 'win32':
51
- var path_2 = (0, child_process_1.execSync)('reg query HKEY_CURRENT_USER\\Software\\SEGGER\\J-Link /v InstallPath').toString();
52
- var pathAlternative = (0, child_process_1.execSync)('reg query HKEY_LOCAL_MACHINE\\Software\\SEGGER\\J-Link /v InstallPath').toString();
53
- if (!path_2 && !pathAlternative) {
54
- throw new Error('JLink not installed');
72
+ var jlinkDir = winRegQuery('HKEY_CURRENT_USER\\Software\\SEGGER\\J-Link /v InstallPath');
73
+ if (!jlinkDir) {
74
+ jlinkDir = winRegQuery('HKEY_LOCAL_MACHINE\\Software\\SEGGER\\J-Link /v InstallPath');
55
75
  }
56
- else if ((path_2 && typeof path_2 !== 'string') ||
57
- (pathAlternative && typeof pathAlternative !== 'string')) {
58
- throw new Error('Unable to read JLink install path');
76
+ if (!jlinkDir) {
77
+ throw new Error('JLink not installed');
59
78
  }
60
- return (path_2 || pathAlternative);
79
+ return path_1.default.resolve(jlinkDir, 'JLink.exe');
61
80
  case 'linux':
62
81
  case 'darwin':
63
82
  return 'JLinkExe';
@@ -65,107 +84,111 @@ var getJLinkExePath = function () {
65
84
  throw new Error('Invalid platform');
66
85
  }
67
86
  };
87
+ function killProcess(childProcess, signal) {
88
+ var pid = typeof childProcess === 'number' ? childProcess : childProcess === null || childProcess === void 0 ? void 0 : childProcess.pid;
89
+ if (!pid) {
90
+ return;
91
+ }
92
+ if (process.platform === 'win32') {
93
+ (0, child_process_1.spawn)('taskkill', ['/pid', "".concat(pid), '/f', '/t']);
94
+ }
95
+ else {
96
+ process.kill(pid, signal);
97
+ }
98
+ }
68
99
  var getInstalledJLinkVersion = function () {
69
100
  return new Promise(function (resolve, reject) {
70
- var jlinkExeCmd = (0, child_process_1.spawn)(getJLinkExePath(), ['-NoGUI', '1'], {
101
+ var jlinkExeCmd = (0, child_process_1.spawn)(getJLinkExePath(), ['-NoGUI', '1', '-USB', '0'], {
71
102
  shell: true,
72
103
  });
104
+ var output = '';
73
105
  var timeout = setTimeout(function () {
74
- var pid = jlinkExeCmd === null || jlinkExeCmd === void 0 ? void 0 : jlinkExeCmd.pid;
75
- if (pid) {
76
- if (process.platform === 'win32') {
77
- (0, child_process_1.spawn)('taskkill', ['/pid', "".concat(pid), '/f', '/t']);
78
- }
79
- else {
80
- process.kill(pid);
81
- }
82
- }
83
- reject('Failed to read Jlink Version');
106
+ killProcess(jlinkExeCmd.pid);
107
+ reject(new Error("Timeout while waiting for J-Link version. Output: ".concat(output)));
84
108
  }, 5000);
109
+ var versionRegExp = /^SEGGER J-Link Commander V([0-9a-z.]+) .*$/m;
85
110
  jlinkExeCmd.stdout.on('data', function (data) {
86
- var versionRegExp = /DLL version (V\d+\.\d+\w*),.*/;
87
- var versionMatch = data.toString().match(versionRegExp);
88
- if (versionMatch === null || versionMatch === void 0 ? void 0 : versionMatch[1]) {
89
- jlinkExeCmd.stdin.write(' exit\n');
90
- resolve(versionMatch[1].toLowerCase());
91
- }
92
- else if (data.toString().includes('Connecting to')) {
93
- jlinkExeCmd.stdin.write(' exit\n');
94
- reject('Failed to read Jlink Version');
111
+ output += data.toString();
112
+ var match = output.match(versionRegExp);
113
+ if (match === null || match === void 0 ? void 0 : match[1]) {
114
+ clearTimeout(timeout);
115
+ killProcess(jlinkExeCmd.pid);
116
+ resolve(match[1]);
95
117
  }
96
118
  });
97
- jlinkExeCmd.stderr.on('data', function () {
98
- reject('Failed to read Jlink Version');
99
- });
100
- jlinkExeCmd.on('close', function () {
101
- clearTimeout(timeout);
102
- });
103
119
  });
104
120
  };
105
- var downloadJLink = function (_a, onUpdate_1, destinationFilePath_1) { return __awaiter(void 0, [_a, onUpdate_1, destinationFilePath_1], void 0, function (_b, onUpdate, destinationFilePath) {
106
- var platform, arch, url, response, hasContentLength, contentLength, reader, chunks, receivedLength, _c, done, value, chunksAll, position;
107
- var _d, _e;
108
- var jlinkUrls = _b.jlinkUrls;
109
- return __generator(this, function (_f) {
110
- switch (_f.label) {
111
- case 0:
112
- platform = os_1.default.platform();
113
- arch = os_1.default.arch();
114
- // @ts-expect-error It is quite literally checked right before
115
- if (!(platform in jlinkUrls) || !(arch in jlinkUrls[platform])) {
116
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
117
- }
118
- url = (_d = jlinkUrls[platform]) === null || _d === void 0 ? void 0 : _d[arch];
119
- if (!url) {
120
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
121
- }
122
- return [4 /*yield*/, fetch(url, {
123
- headers: {
124
- Range: 'bytes=0-',
125
- },
126
- })];
127
- case 1:
128
- response = _f.sent();
129
- if (!response.ok) {
130
- throw new Error("Unable to download ".concat(url, ". Got status code ").concat(status, "."));
131
- }
132
- hasContentLength = response.headers.has('content-length');
133
- contentLength = hasContentLength
134
- ? Number(response.headers.get('content-length'))
135
- : 1;
136
- reader = (_e = response.body) === null || _e === void 0 ? void 0 : _e.getReader();
137
- chunks = [];
138
- receivedLength = 0;
139
- _f.label = 2;
140
- case 2:
141
- if (!reader) return [3 /*break*/, 4];
142
- return [4 /*yield*/, reader.read()];
143
- case 3:
144
- _c = _f.sent(), done = _c.done, value = _c.value;
145
- if (done) {
146
- return [3 /*break*/, 4];
147
- }
148
- chunks.push(value);
149
- receivedLength += value.length;
150
- onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({
151
- step: 'download',
152
- percentage: hasContentLength
153
- ? Number(((receivedLength / contentLength) * 100).toFixed(2))
154
- : 0,
155
- });
156
- return [3 /*break*/, 2];
157
- case 4:
158
- chunksAll = new Uint8Array(receivedLength);
159
- position = 0;
160
- chunks.forEach(function (chunk) {
161
- chunksAll.set(chunk, position);
162
- position += chunk.length;
163
- });
164
- return [4 /*yield*/, (0, common_1.saveToFile)(destinationFilePath || path_1.default.join(os_1.default.tmpdir(), path_1.default.basename(url)), Buffer.from(chunksAll))];
165
- case 5: return [2 /*return*/, _f.sent()];
166
- }
121
+ var downloadJLink = function (_a, onUpdate_1) {
122
+ var args_1 = [];
123
+ for (var _i = 2; _i < arguments.length; _i++) {
124
+ args_1[_i - 2] = arguments[_i];
125
+ }
126
+ return __awaiter(void 0, __spreadArray([_a, onUpdate_1], args_1, true), void 0, function (_b, onUpdate, destinationDir, destinationFileName) {
127
+ var platform, arch, url, response, hasContentLength, contentLength, reader, chunks, receivedLength, _c, done, value, chunksAll, position;
128
+ var _d, _e;
129
+ var jlinkUrls = _b.jlinkUrls;
130
+ if (destinationDir === void 0) { destinationDir = os_1.default.tmpdir(); }
131
+ return __generator(this, function (_f) {
132
+ switch (_f.label) {
133
+ case 0:
134
+ platform = os_1.default.platform();
135
+ arch = os_1.default.arch();
136
+ // @ts-expect-error It is quite literally checked right before
137
+ if (!(platform in jlinkUrls) || !(arch in jlinkUrls[platform])) {
138
+ throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
139
+ }
140
+ url = (_d = jlinkUrls[platform]) === null || _d === void 0 ? void 0 : _d[arch];
141
+ if (!url) {
142
+ throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
143
+ }
144
+ return [4 /*yield*/, fetch(url, {
145
+ headers: {
146
+ Range: 'bytes=0-',
147
+ },
148
+ })];
149
+ case 1:
150
+ response = _f.sent();
151
+ if (!response.ok) {
152
+ throw new Error("Unable to download ".concat(url, ". Got status code ").concat(status, "."));
153
+ }
154
+ hasContentLength = response.headers.has('content-length');
155
+ contentLength = hasContentLength
156
+ ? Number(response.headers.get('content-length'))
157
+ : 1;
158
+ reader = (_e = response.body) === null || _e === void 0 ? void 0 : _e.getReader();
159
+ chunks = [];
160
+ receivedLength = 0;
161
+ _f.label = 2;
162
+ case 2:
163
+ if (!reader) return [3 /*break*/, 4];
164
+ return [4 /*yield*/, reader.read()];
165
+ case 3:
166
+ _c = _f.sent(), done = _c.done, value = _c.value;
167
+ if (done) {
168
+ return [3 /*break*/, 4];
169
+ }
170
+ chunks.push(value);
171
+ receivedLength += value.length;
172
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({
173
+ step: 'download',
174
+ percentage: hasContentLength
175
+ ? Number(((receivedLength / contentLength) * 100).toFixed(2))
176
+ : 0,
177
+ });
178
+ return [3 /*break*/, 2];
179
+ case 4:
180
+ chunksAll = new Uint8Array(receivedLength);
181
+ position = 0;
182
+ chunks.forEach(function (chunk) {
183
+ chunksAll.set(chunk, position);
184
+ position += chunk.length;
185
+ });
186
+ return [4 /*yield*/, (0, common_1.saveToFile)(path_1.default.join(destinationDir, destinationFileName || path_1.default.basename(url)), Buffer.from(chunksAll))];
187
+ case 5: return [2 /*return*/, _f.sent()];
188
+ }
189
+ });
167
190
  });
168
- }); };
191
+ };
169
192
  var installJLink = function (installerPath, onUpdate) {
170
193
  var command;
171
194
  var args;
@@ -235,7 +258,11 @@ var getVersionToInstall = function (fallbackVersion) { return __awaiter(void 0,
235
258
  });
236
259
  }); };
237
260
  exports.getVersionToInstall = getVersionToInstall;
238
- var downloadAndSaveJLink = function (destination, onUpdate) { return (0, common_1.fetchIndex)().then(function (v) { return downloadJLink(v, onUpdate, destination); }); };
261
+ var downloadAndSaveJLink = function (destinationDir, destinationFileName, onUpdate) {
262
+ return (0, common_1.fetchIndex)().then(function (v) {
263
+ return downloadJLink(v, onUpdate, destinationDir, destinationFileName);
264
+ });
265
+ };
239
266
  exports.downloadAndSaveJLink = downloadAndSaveJLink;
240
267
  var downloadAndInstallJLink = function (onUpdate) {
241
268
  return (0, common_1.fetchIndex)()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nordicsemiconductor/nrf-jlink-js",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "main": "dist",
5
5
  "types": "dist",
6
6
  "files": [