@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 +1 -1
- package/dist/jlink.d.ts +1 -1
- package/dist/jlink.js +125 -98
- package/package.json +1 -1
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:
|
|
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: (
|
|
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
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
throw new Error('Unable to read JLink install path');
|
|
76
|
+
if (!jlinkDir) {
|
|
77
|
+
throw new Error('JLink not installed');
|
|
59
78
|
}
|
|
60
|
-
return (
|
|
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
|
-
|
|
75
|
-
|
|
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
|
-
|
|
87
|
-
var
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
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
|
|
106
|
-
var
|
|
107
|
-
var
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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 (
|
|
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)()
|