@nordicsemiconductor/nrf-jlink-js 0.0.4 → 0.0.6

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.
@@ -1,2 +1,2 @@
1
- import { downloadAndInstallJLink, getVersionToInstall, downloadAndSaveJLink } from './jlink';
2
- export { downloadAndInstallJLink, getVersionToInstall, downloadAndSaveJLink };
1
+ import { downloadAndInstallJLink, getVersionToInstall, downloadAndSaveJLink, installJLink } from './jlink';
2
+ export { downloadAndInstallJLink, getVersionToInstall, downloadAndSaveJLink, installJLink };
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.downloadAndSaveJLink = exports.getVersionToInstall = exports.downloadAndInstallJLink = void 0;
3
+ exports.installJLink = exports.downloadAndSaveJLink = exports.getVersionToInstall = exports.downloadAndInstallJLink = void 0;
4
4
  var jlink_1 = require("./jlink");
5
5
  Object.defineProperty(exports, "downloadAndInstallJLink", { enumerable: true, get: function () { return jlink_1.downloadAndInstallJLink; } });
6
6
  Object.defineProperty(exports, "getVersionToInstall", { enumerable: true, get: function () { return jlink_1.getVersionToInstall; } });
7
7
  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; } });
@@ -2,6 +2,7 @@ interface Update {
2
2
  step: 'install' | 'download';
3
3
  percentage: number;
4
4
  }
5
+ export declare const installJLink: (installerPath: string, onUpdate?: (update: Update) => void) => Promise<void>;
5
6
  interface JLinkState {
6
7
  outdated: boolean;
7
8
  installed: boolean;
@@ -35,20 +35,11 @@ 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
- };
47
38
  var __importDefault = (this && this.__importDefault) || function (mod) {
48
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
49
40
  };
50
41
  Object.defineProperty(exports, "__esModule", { value: true });
51
- exports.downloadAndInstallJLink = exports.downloadAndSaveJLink = exports.getVersionToInstall = void 0;
42
+ exports.downloadAndInstallJLink = exports.downloadAndSaveJLink = exports.getVersionToInstall = exports.installJLink = void 0;
52
43
  var child_process_1 = require("child_process");
53
44
  var os_1 = __importDefault(require("os"));
54
45
  var path_1 = __importDefault(require("path"));
@@ -112,52 +103,45 @@ var getInstalledJLinkVersion = function () {
112
103
  });
113
104
  });
114
105
  };
115
- var downloadJLink = function (_a, onUpdate_1) {
116
- var args_1 = [];
117
- for (var _i = 2; _i < arguments.length; _i++) {
118
- args_1[_i - 2] = arguments[_i];
119
- }
120
- return __awaiter(void 0, __spreadArray([_a, onUpdate_1], args_1, true), void 0, function (_b, onUpdate, destinationPath) {
121
- var platform, arch, url, _c, status, stream;
122
- var _d;
123
- var jlinkUrls = _b.jlinkUrls;
124
- if (destinationPath === void 0) { destinationPath = os_1.default.tmpdir(); }
125
- return __generator(this, function (_e) {
126
- switch (_e.label) {
127
- case 0:
128
- platform = os_1.default.platform();
129
- arch = os_1.default.arch();
130
- // @ts-expect-error It is quite literally checked right before
131
- if (!(platform in jlinkUrls) || !(arch in jlinkUrls[platform])) {
132
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
133
- }
134
- url = (_d = jlinkUrls[platform]) === null || _d === void 0 ? void 0 : _d[arch];
135
- if (!url) {
136
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
137
- }
138
- return [4 /*yield*/, axios_1.default.get(url, {
139
- responseType: 'stream',
140
- onDownloadProgress: function (_a) {
141
- var loaded = _a.loaded, total = _a.total;
142
- return loaded &&
143
- total &&
144
- (onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({
145
- step: 'download',
146
- percentage: Number(((loaded / total) * 100).toFixed(2)),
147
- }));
148
- },
149
- })];
150
- case 1:
151
- _c = _e.sent(), status = _c.status, stream = _c.data;
152
- if (status !== 200) {
153
- throw new Error("Unable to download ".concat(url, ". Got status code ").concat(status, "."));
154
- }
155
- return [4 /*yield*/, (0, common_1.saveToFile)(stream, path_1.default.join(destinationPath, path_1.default.basename(url)))];
156
- case 2: return [2 /*return*/, _e.sent()];
157
- }
158
- });
106
+ var downloadJLink = function (_a, onUpdate_1, destinationFilePath_1) { return __awaiter(void 0, [_a, onUpdate_1, destinationFilePath_1], void 0, function (_b, onUpdate, destinationFilePath) {
107
+ var platform, arch, url, _c, status, stream;
108
+ var _d;
109
+ var jlinkUrls = _b.jlinkUrls;
110
+ return __generator(this, function (_e) {
111
+ switch (_e.label) {
112
+ case 0:
113
+ platform = os_1.default.platform();
114
+ arch = os_1.default.arch();
115
+ // @ts-expect-error It is quite literally checked right before
116
+ if (!(platform in jlinkUrls) || !(arch in jlinkUrls[platform])) {
117
+ throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
118
+ }
119
+ url = (_d = jlinkUrls[platform]) === null || _d === void 0 ? void 0 : _d[arch];
120
+ if (!url) {
121
+ throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
122
+ }
123
+ return [4 /*yield*/, axios_1.default.get(url, {
124
+ responseType: 'stream',
125
+ onDownloadProgress: function (_a) {
126
+ var loaded = _a.loaded, total = _a.total;
127
+ return loaded &&
128
+ total &&
129
+ (onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({
130
+ step: 'download',
131
+ percentage: Number(((loaded / total) * 100).toFixed(2)),
132
+ }));
133
+ },
134
+ })];
135
+ case 1:
136
+ _c = _e.sent(), status = _c.status, stream = _c.data;
137
+ if (status !== 200) {
138
+ throw new Error("Unable to download ".concat(url, ". Got status code ").concat(status, "."));
139
+ }
140
+ return [4 /*yield*/, (0, common_1.saveToFile)(stream, destinationFilePath || path_1.default.join(os_1.default.tmpdir(), path_1.default.basename(url)))];
141
+ case 2: return [2 /*return*/, _e.sent()];
142
+ }
159
143
  });
160
- };
144
+ }); };
161
145
  var installJLink = function (installerPath, onUpdate) {
162
146
  var command;
163
147
  var args;
@@ -190,6 +174,7 @@ var installJLink = function (installerPath, onUpdate) {
190
174
  });
191
175
  });
192
176
  };
177
+ exports.installJLink = installJLink;
193
178
  var convertToSemverVersion = function (version) {
194
179
  var _a;
195
180
  var _b = (_a = version.match(/V?(\d+\.\d+)(.)?/)) !== null && _a !== void 0 ? _a : [], majorMinor = _b[1], patchLetter = _b[2];
@@ -233,6 +218,6 @@ exports.downloadAndSaveJLink = downloadAndSaveJLink;
233
218
  var downloadAndInstallJLink = function (onUpdate) {
234
219
  return (0, common_1.fetchIndex)()
235
220
  .then(function (v) { return downloadJLink(v, onUpdate); })
236
- .then(function (v) { return installJLink(v, onUpdate); });
221
+ .then(function (v) { return (0, exports.installJLink)(v, onUpdate); });
237
222
  };
238
223
  exports.downloadAndInstallJLink = downloadAndInstallJLink;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nordicsemiconductor/nrf-jlink-js",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "main": "dist",
5
5
  "types": "dist",
6
6
  "files": [
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env ts-node
2
- export {};
@@ -1,295 +0,0 @@
1
- #!/usr/bin/env ts-node
2
- "use strict";
3
- var __assign = (this && this.__assign) || function () {
4
- __assign = Object.assign || function(t) {
5
- for (var s, i = 1, n = arguments.length; i < n; i++) {
6
- s = arguments[i];
7
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8
- t[p] = s[p];
9
- }
10
- return t;
11
- };
12
- return __assign.apply(this, arguments);
13
- };
14
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16
- return new (P || (P = Promise))(function (resolve, reject) {
17
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20
- step((generator = generator.apply(thisArg, _arguments || [])).next());
21
- });
22
- };
23
- var __generator = (this && this.__generator) || function (thisArg, body) {
24
- 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);
25
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
26
- function verb(n) { return function (v) { return step([n, v]); }; }
27
- function step(op) {
28
- if (f) throw new TypeError("Generator is already executing.");
29
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
30
- 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;
31
- if (y = 0, t) op = [op[0] & 2, t.value];
32
- switch (op[0]) {
33
- case 0: case 1: t = op; break;
34
- case 4: _.label++; return { value: op[1], done: false };
35
- case 5: _.label++; y = op[1]; op = [0]; continue;
36
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
37
- default:
38
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
39
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
40
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
41
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
42
- if (t[2]) _.ops.pop();
43
- _.trys.pop(); continue;
44
- }
45
- op = body.call(thisArg, _);
46
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
47
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
48
- }
49
- };
50
- var __importDefault = (this && this.__importDefault) || function (mod) {
51
- return (mod && mod.__esModule) ? mod : { "default": mod };
52
- };
53
- Object.defineProperty(exports, "__esModule", { value: true });
54
- var path_1 = __importDefault(require("path"));
55
- var axios_1 = __importDefault(require("axios"));
56
- var os_1 = __importDefault(require("os"));
57
- var fs_1 = __importDefault(require("fs"));
58
- var common_1 = require("../src/common");
59
- var SEGGER_DOWNLOAD_BASE_URL = 'https://www.segger.com/downloads/jlink';
60
- var ARTIFACTORY_UPLOAD_BASE_URL = "https://files.nordicsemi.com/artifactory/swtools/external/ncd/jlink";
61
- var platformToJlinkPlatform = function (variant) {
62
- switch (variant) {
63
- case 'win32':
64
- return 'Windows';
65
- case 'linux':
66
- return 'Linux';
67
- case 'darwin':
68
- return 'MacOSX';
69
- default:
70
- throw new Error("Unknown variant ".concat(variant));
71
- }
72
- };
73
- var doPerVariant = function (variants, action) { return __awaiter(void 0, void 0, void 0, function () {
74
- var ret, _loop_1, _i, platforms_1, platform;
75
- return __generator(this, function (_a) {
76
- switch (_a.label) {
77
- case 0:
78
- ret = {};
79
- _loop_1 = function (platform) {
80
- var _b, _c, _d, _e;
81
- return __generator(this, function (_f) {
82
- switch (_f.label) {
83
- case 0:
84
- _b = ret;
85
- _c = platform;
86
- _e = (_d = Object).fromEntries;
87
- return [4 /*yield*/, Promise.all(common_1.archs.map(function (arch) { return __awaiter(void 0, void 0, void 0, function () {
88
- var _a;
89
- return __generator(this, function (_b) {
90
- switch (_b.label) {
91
- case 0:
92
- _a = [arch];
93
- return [4 /*yield*/, action(variants[platform][arch])];
94
- case 1: return [2 /*return*/, _a.concat([
95
- (_b.sent()) ||
96
- variants[platform][arch]
97
- ])];
98
- }
99
- });
100
- }); }))];
101
- case 1:
102
- _b[_c] = _e.apply(_d, [_f.sent()]);
103
- return [2 /*return*/];
104
- }
105
- });
106
- };
107
- _i = 0, platforms_1 = common_1.platforms;
108
- _a.label = 1;
109
- case 1:
110
- if (!(_i < platforms_1.length)) return [3 /*break*/, 4];
111
- platform = platforms_1[_i];
112
- return [5 /*yield**/, _loop_1(platform)];
113
- case 2:
114
- _a.sent();
115
- _a.label = 3;
116
- case 3:
117
- _i++;
118
- return [3 /*break*/, 1];
119
- case 4: return [2 /*return*/, ret];
120
- }
121
- });
122
- }); };
123
- var downloadInstallers = function (fileNames) { return __awaiter(void 0, void 0, void 0, function () {
124
- var incorrectFiles, ret;
125
- return __generator(this, function (_a) {
126
- switch (_a.label) {
127
- case 0:
128
- console.log('Started downloading all JLink variants.');
129
- incorrectFiles = [];
130
- return [4 /*yield*/, doPerVariant(fileNames, function (fileName) { return __awaiter(void 0, void 0, void 0, function () {
131
- var url, _a, status, stream;
132
- return __generator(this, function (_b) {
133
- switch (_b.label) {
134
- case 0:
135
- url = "".concat(SEGGER_DOWNLOAD_BASE_URL, "/").concat(fileName);
136
- console.log('Started download:', url);
137
- return [4 /*yield*/, axios_1.default.postForm(url, { accept_license_agreement: 'accepted' }, {
138
- responseType: 'stream',
139
- headers: {
140
- 'Content-Type': 'application/x-www-form-urlencoded',
141
- },
142
- })];
143
- case 1:
144
- _a = _b.sent(), status = _a.status, stream = _a.data;
145
- if (status !== 200) {
146
- throw new Error("Unable to download ".concat(url, ". Got status code ").concat(status, "."));
147
- }
148
- if (!(stream.statusMessage === 'OK')) return [3 /*break*/, 3];
149
- console.log('Finished download:', url);
150
- return [4 /*yield*/, (0, common_1.saveToFile)(stream, path_1.default.join(os_1.default.tmpdir(), fileName))];
151
- case 2: return [2 /*return*/, _b.sent()];
152
- case 3:
153
- incorrectFiles.push(fileName);
154
- console.log('Failed to download (check if file exists):', url);
155
- return [2 /*return*/, 'Incorrect'];
156
- }
157
- });
158
- }); })];
159
- case 1:
160
- ret = _a.sent();
161
- if (incorrectFiles.length > 0) {
162
- throw new Error("Failed to download: ".concat(incorrectFiles.join(', ')));
163
- }
164
- console.log('Finished downloading all JLink variants.');
165
- return [2 /*return*/, ret];
166
- }
167
- });
168
- }); };
169
- var getStandardisedVersion = function (rawVersion) {
170
- var _a;
171
- var regex = /[vV]?(\d+)\.(\d\d)(.{0,1})/;
172
- var _b = (_a = rawVersion.match(regex)) !== null && _a !== void 0 ? _a : [], parsedVersion = _b[0], major = _b[1], minor = _b[2], patch = _b[3];
173
- if (!parsedVersion || !major || !minor) {
174
- throw new Error("Unable to parse version ".concat(rawVersion, ". Valid formats: v12.34, v1.23a, V1.23a, 12.34, 1.23a"));
175
- }
176
- return {
177
- major: major,
178
- minor: minor,
179
- patch: patch === null || patch === void 0 ? void 0 : patch.toLowerCase(),
180
- };
181
- };
182
- var getFileFormat = function (platform) {
183
- switch (platform) {
184
- case 'win32':
185
- return 'exe';
186
- case 'darwin':
187
- return 'pkg';
188
- case 'linux':
189
- return 'deb';
190
- default:
191
- throw new Error("Unknown platform ".concat(process.platform));
192
- }
193
- };
194
- var getFileNames = function (rawVersion) {
195
- var version = getStandardisedVersion(rawVersion);
196
- var fileNames = {};
197
- var _loop_2 = function (platform) {
198
- fileNames[platform] = Object.fromEntries(common_1.archs.map(function (arch) {
199
- var _a;
200
- return [
201
- arch,
202
- "JLink_".concat(platformToJlinkPlatform(platform), "_V").concat(version.major).concat(version.minor).concat((_a = version.patch) !== null && _a !== void 0 ? _a : '', "_").concat(arch == 'x64' ? 'x86_64' : arch, ".").concat(getFileFormat(platform)),
203
- ];
204
- }));
205
- };
206
- for (var _i = 0, platforms_2 = common_1.platforms; _i < platforms_2.length; _i++) {
207
- var platform = platforms_2[_i];
208
- _loop_2(platform);
209
- }
210
- return fileNames;
211
- };
212
- var getUpdatedSourceJson = function (version, jlinkUrls) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
213
- return [2 /*return*/, (0, common_1.fetchIndex)().then(function (index) { return (__assign(__assign({}, index), { version: version, jlinkUrls: jlinkUrls })); })];
214
- }); }); };
215
- var uploadFile = function (url, data) { return __awaiter(void 0, void 0, void 0, function () {
216
- var res, _a, _b, _c;
217
- return __generator(this, function (_d) {
218
- switch (_d.label) {
219
- case 0: return [4 /*yield*/, fetch(url, {
220
- method: 'PUT',
221
- body: data,
222
- headers: {
223
- Authorization: "Bearer ".concat(process.env.ARTIFACTORY_TOKEN),
224
- },
225
- })];
226
- case 1:
227
- res = _d.sent();
228
- if (!!res.ok) return [3 /*break*/, 3];
229
- _a = Error.bind;
230
- _c = (_b = "Unable to upload to ".concat(url, ". Status code: ").concat(res.status, ". Body: ")).concat;
231
- return [4 /*yield*/, res.text()];
232
- case 2: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_d.sent()])]))();
233
- case 3: return [2 /*return*/];
234
- }
235
- });
236
- }); };
237
- var upload = function (version, files) {
238
- if (!process.env.ARTIFACTORY_TOKEN) {
239
- throw new Error('ARTIFACTORY_TOKEN environment variable not set');
240
- }
241
- console.log('Started uploading all JLink variants.');
242
- return new Promise(function (resolve) { return __awaiter(void 0, void 0, void 0, function () {
243
- var jlinkUrls, targetUrl, versionObject, updatedIndexJSON;
244
- return __generator(this, function (_a) {
245
- switch (_a.label) {
246
- case 0: return [4 /*yield*/, doPerVariant(files, function (filePath) { return __awaiter(void 0, void 0, void 0, function () {
247
- var fileName, targetUrl;
248
- return __generator(this, function (_a) {
249
- switch (_a.label) {
250
- case 0:
251
- fileName = path_1.default.basename(filePath);
252
- console.log('Started upload:', fileName);
253
- targetUrl = "".concat(ARTIFACTORY_UPLOAD_BASE_URL, "/").concat(fileName);
254
- return [4 /*yield*/, uploadFile(targetUrl, fs_1.default.readFileSync(filePath))];
255
- case 1:
256
- _a.sent();
257
- fs_1.default.rmSync(filePath);
258
- console.log('Finished upload:', fileName);
259
- return [2 /*return*/, targetUrl];
260
- }
261
- });
262
- }); })];
263
- case 1:
264
- jlinkUrls = _a.sent();
265
- // Index
266
- console.log('Started uploading Index');
267
- targetUrl = "".concat(ARTIFACTORY_UPLOAD_BASE_URL, "/index.json");
268
- versionObject = getStandardisedVersion(version);
269
- return [4 /*yield*/, getUpdatedSourceJson("v".concat(versionObject.major, ".").concat(versionObject.minor).concat(versionObject.patch || ''), jlinkUrls)];
270
- case 2:
271
- updatedIndexJSON = _a.sent();
272
- return [4 /*yield*/, uploadFile(targetUrl, Buffer.from(JSON.stringify(updatedIndexJSON, null, 2)))];
273
- case 3:
274
- _a.sent();
275
- console.log('Finished uploading Index');
276
- return [2 /*return*/, resolve(targetUrl)];
277
- }
278
- });
279
- }); });
280
- };
281
- var main = function (version) {
282
- return downloadInstallers(getFileNames(version)).then(function (files) {
283
- return upload(version, files);
284
- });
285
- };
286
- var runAsScript = require.main === module;
287
- if (runAsScript) {
288
- var versionIndex = process.argv.findIndex(function (arg) { return arg === '--version' || arg === '-v'; }) + 1;
289
- var version = versionIndex > 0 ? process.argv[versionIndex] : undefined;
290
- if (!version) {
291
- console.error('No version passed with --version or -v');
292
- process.exit(1);
293
- }
294
- main(version);
295
- }
File without changes
File without changes