@nordicsemiconductor/nrf-jlink-js 0.0.2 → 0.0.4

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.
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ts-node
2
+ export {};
@@ -0,0 +1,295 @@
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
+ }
@@ -0,0 +1,10 @@
1
+ export declare const platforms: readonly ["darwin", "linux", "win32"];
2
+ export declare const archs: readonly ["arm64", "x64"];
3
+ export type ArchUrl = Record<(typeof archs)[number], string>;
4
+ export type JLinkVariant = Record<(typeof platforms)[number], ArchUrl>;
5
+ export interface JLinkIndex {
6
+ version: string;
7
+ jlinkUrls: JLinkVariant;
8
+ }
9
+ export declare const fetchIndex: () => Promise<JLinkIndex>;
10
+ export declare const saveToFile: (stream: NodeJS.ReadableStream, destinationFile: string) => Promise<string>;
@@ -39,9 +39,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.saveToFile = exports.fetchIndex = void 0;
42
+ exports.saveToFile = exports.fetchIndex = exports.archs = exports.platforms = void 0;
43
43
  var axios_1 = __importDefault(require("axios"));
44
- var fs_1 = __importDefault(require("fs"));
44
+ var fs_1 = require("fs");
45
+ var path_1 = __importDefault(require("path"));
46
+ exports.platforms = ['darwin', 'linux', 'win32'];
47
+ exports.archs = ['arm64', 'x64'];
45
48
  var fetchJSON = function (url) { return __awaiter(void 0, void 0, void 0, function () {
46
49
  var _a, status, data;
47
50
  return __generator(this, function (_b) {
@@ -56,7 +59,7 @@ var fetchJSON = function (url) { return __awaiter(void 0, void 0, void 0, functi
56
59
  }
57
60
  });
58
61
  }); };
59
- var indexUrl = "https://files.nordicsemi.com/artifactory/swtools/external/ncd/jlink/index.json";
62
+ var indexUrl = 'https://files.nordicsemi.com/artifactory/swtools/external/ncd/jlink/index.json';
60
63
  var fetchIndex = function () { return __awaiter(void 0, void 0, void 0, function () {
61
64
  var res;
62
65
  return __generator(this, function (_a) {
@@ -64,7 +67,10 @@ var fetchIndex = function () { return __awaiter(void 0, void 0, void 0, function
64
67
  case 0: return [4 /*yield*/, fetchJSON(indexUrl)];
65
68
  case 1:
66
69
  res = _a.sent();
67
- if (res == null || typeof res !== 'object' || res.version === undefined || res.jlinkUrls === undefined) {
70
+ if (res == null ||
71
+ typeof res !== 'object' ||
72
+ res.version === undefined ||
73
+ res.jlinkUrls === undefined) {
68
74
  throw new Error('`index.json` does not have the expected content.');
69
75
  }
70
76
  return [2 /*return*/, res];
@@ -73,11 +79,12 @@ var fetchIndex = function () { return __awaiter(void 0, void 0, void 0, function
73
79
  }); };
74
80
  exports.fetchIndex = fetchIndex;
75
81
  var saveToFile = function (stream, destinationFile) {
82
+ (0, fs_1.mkdirSync)(path_1.default.dirname(destinationFile), { recursive: true });
76
83
  return new Promise(function (resolve, reject) {
77
- var file = fs_1.default.createWriteStream(destinationFile);
84
+ var file = (0, fs_1.createWriteStream)(destinationFile);
78
85
  stream.pipe(file);
79
- stream.on("error", reject);
80
- stream.on("end", function () {
86
+ stream.on('error', reject);
87
+ stream.on('end', function () {
81
88
  file.end(function () {
82
89
  return resolve(destinationFile);
83
90
  });
@@ -0,0 +1,2 @@
1
+ import { downloadAndInstallJLink, getVersionToInstall, downloadAndSaveJLink } from './jlink';
2
+ export { downloadAndInstallJLink, getVersionToInstall, downloadAndSaveJLink };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getVersionToInstall = exports.downloadAndInstallJLink = void 0;
3
+ 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
+ Object.defineProperty(exports, "downloadAndSaveJLink", { enumerable: true, get: function () { return jlink_1.downloadAndSaveJLink; } });
@@ -1,5 +1,5 @@
1
1
  interface Update {
2
- step: "install" | "download";
2
+ step: 'install' | 'download';
3
3
  percentage: number;
4
4
  }
5
5
  interface JLinkState {
@@ -9,5 +9,6 @@ interface JLinkState {
9
9
  installedVersion?: string;
10
10
  }
11
11
  export declare const getVersionToInstall: (fallbackVersion?: string) => Promise<JLinkState>;
12
+ export declare const downloadAndSaveJLink: (destination: string, onUpdate?: (percentage: number) => void) => Promise<string>;
12
13
  export declare const downloadAndInstallJLink: (onUpdate?: (update: Update) => void) => Promise<void>;
13
14
  export {};
@@ -35,13 +35,21 @@ 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
  };
41
50
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.downloadAndInstallJLink = exports.getVersionToInstall = void 0;
51
+ exports.downloadAndInstallJLink = exports.downloadAndSaveJLink = exports.getVersionToInstall = void 0;
43
52
  var child_process_1 = require("child_process");
44
- var promises_1 = require("fs/promises");
45
53
  var os_1 = __importDefault(require("os"));
46
54
  var path_1 = __importDefault(require("path"));
47
55
  var semver_1 = __importDefault(require("semver"));
@@ -49,26 +57,29 @@ var axios_1 = __importDefault(require("axios"));
49
57
  var common_1 = require("./common");
50
58
  var getJLinkExePath = function () {
51
59
  switch (os_1.default.platform()) {
52
- case "win32":
53
- var path_2 = (0, child_process_1.execSync)("reg query HKEY_CURRENT_USER\\Software\\SEGGER\\J-Link /v InstallPath").toString();
54
- var pathAlternative = (0, child_process_1.execSync)("reg query HKEY_LOCAL_MACHINE\\Software\\SEGGER\\J-Link /v InstallPath").toString();
60
+ case 'win32':
61
+ var path_2 = (0, child_process_1.execSync)('reg query HKEY_CURRENT_USER\\Software\\SEGGER\\J-Link /v InstallPath').toString();
62
+ var pathAlternative = (0, child_process_1.execSync)('reg query HKEY_LOCAL_MACHINE\\Software\\SEGGER\\J-Link /v InstallPath').toString();
55
63
  if (!path_2 && !pathAlternative) {
56
64
  throw new Error('JLink not installed');
57
65
  }
58
- else if ((path_2 && typeof path_2 !== 'string') || (pathAlternative && typeof pathAlternative !== 'string')) {
66
+ else if ((path_2 && typeof path_2 !== 'string') ||
67
+ (pathAlternative && typeof pathAlternative !== 'string')) {
59
68
  throw new Error('Unable to read JLink install path');
60
69
  }
61
70
  return (path_2 || pathAlternative);
62
- case "linux":
63
- case "darwin":
71
+ case 'linux':
72
+ case 'darwin':
64
73
  return 'JLinkExe';
65
74
  default:
66
- throw new Error("Invalid platform");
75
+ throw new Error('Invalid platform');
67
76
  }
68
77
  };
69
78
  var getInstalledJLinkVersion = function () {
70
79
  return new Promise(function (resolve, reject) {
71
- var jlinkExeCmd = (0, child_process_1.spawn)(getJLinkExePath(), ["-NoGUI", "1"], { shell: true });
80
+ var jlinkExeCmd = (0, child_process_1.spawn)(getJLinkExePath(), ['-NoGUI', '1'], {
81
+ shell: true,
82
+ });
72
83
  var timeout = setTimeout(function () {
73
84
  var pid = jlinkExeCmd === null || jlinkExeCmd === void 0 ? void 0 : jlinkExeCmd.pid;
74
85
  if (pid) {
@@ -81,19 +92,19 @@ var getInstalledJLinkVersion = function () {
81
92
  }
82
93
  reject('Failed to read Jlink Version');
83
94
  }, 5000);
84
- jlinkExeCmd.stdout.on("data", function (data) {
95
+ jlinkExeCmd.stdout.on('data', function (data) {
85
96
  var versionRegExp = /DLL version (V\d+\.\d+\w*),.*/;
86
97
  var versionMatch = data.toString().match(versionRegExp);
87
98
  if (versionMatch === null || versionMatch === void 0 ? void 0 : versionMatch[1]) {
88
99
  jlinkExeCmd.stdin.write(' exit\n');
89
100
  resolve(versionMatch[1].toLowerCase());
90
101
  }
91
- else if (data.toString().includes("Connecting to")) {
102
+ else if (data.toString().includes('Connecting to')) {
92
103
  jlinkExeCmd.stdin.write(' exit\n');
93
104
  reject('Failed to read Jlink Version');
94
105
  }
95
106
  });
96
- jlinkExeCmd.stderr.on("data", function () {
107
+ jlinkExeCmd.stderr.on('data', function () {
97
108
  reject('Failed to read Jlink Version');
98
109
  });
99
110
  jlinkExeCmd.on('close', function () {
@@ -101,47 +112,53 @@ var getInstalledJLinkVersion = function () {
101
112
  });
102
113
  });
103
114
  };
104
- var downloadJLink = function (_a, onUpdate_1) { return __awaiter(void 0, [_a, onUpdate_1], void 0, function (_b, onUpdate) {
105
- var platform, arch, url, _c, status, stream, destinationFile;
106
- var _d;
107
- var jlinkUrls = _b.jlinkUrls;
108
- return __generator(this, function (_e) {
109
- switch (_e.label) {
110
- case 0:
111
- platform = os_1.default.platform();
112
- arch = os_1.default.arch();
113
- // @ts-expect-error It is quite literally checked right before
114
- if (!(platform in jlinkUrls) || !(arch in jlinkUrls[platform])) {
115
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
116
- }
117
- url = (_d = jlinkUrls[platform]) === null || _d === void 0 ? void 0 : _d[arch];
118
- if (!url) {
119
- throw new Error("JLink not available for ".concat(platform, "/").concat(arch));
120
- }
121
- return [4 /*yield*/, axios_1.default.get(url, {
122
- responseType: "stream",
123
- onDownloadProgress: function (_a) {
124
- var loaded = _a.loaded, total = _a.total;
125
- return loaded && total &&
126
- (onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ step: "download", percentage: Number(((loaded / total) * 100).toFixed(2)) }));
127
- },
128
- })];
129
- case 1:
130
- _c = _e.sent(), status = _c.status, stream = _c.data;
131
- if (status !== 200) {
132
- throw new Error("Unable to download ".concat(jlinkUrls, ". Got status code ").concat(status, "."));
133
- }
134
- destinationFile = path_1.default.join(os_1.default.tmpdir(), path_1.default.basename(url));
135
- return [4 /*yield*/, (0, promises_1.mkdir)(path_1.default.dirname(destinationFile), { recursive: true })];
136
- case 2:
137
- _e.sent();
138
- return [4 /*yield*/, (0, common_1.saveToFile)(stream, destinationFile)];
139
- case 3: return [2 /*return*/, _e.sent()];
140
- }
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
+ });
141
159
  });
142
- }); };
160
+ };
143
161
  var installJLink = function (installerPath, onUpdate) {
144
- ;
145
162
  var command;
146
163
  var args;
147
164
  switch (os_1.default.platform()) {
@@ -157,21 +174,19 @@ var installJLink = function (installerPath, onUpdate) {
157
174
  command = "\"".concat(installerPath, "\"");
158
175
  break;
159
176
  default:
160
- throw new Error("Invalid platform");
177
+ throw new Error('Invalid platform');
161
178
  }
162
- onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ step: "install", percentage: 0 });
179
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ step: 'install', percentage: 0 });
163
180
  return new Promise(function (resolve, reject) {
164
- (0, child_process_1.execFile)(command, args, function (error, stdout, stderr) {
181
+ (0, child_process_1.execFile)(command, args, function (error, _, stderr) {
165
182
  if (error) {
166
- reject(error);
183
+ return reject(error);
167
184
  }
168
185
  if (stderr) {
169
- reject(stderr);
170
- }
171
- if (stdout.includes("successful")) {
172
- onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ step: "install", percentage: 100 });
173
- return resolve();
186
+ return reject(stderr);
174
187
  }
188
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate({ step: 'install', percentage: 100 });
189
+ return resolve();
175
190
  });
176
191
  });
177
192
  };
@@ -198,7 +213,9 @@ var getVersionToInstall = function (fallbackVersion) { return __awaiter(void 0,
198
213
  case 2:
199
214
  installedVersion = _c.sent();
200
215
  installed = !!installedVersion;
201
- outdated = !installed || !versionToBeInstalled || !isValidVersion(installedVersion, versionToBeInstalled);
216
+ outdated = !installed ||
217
+ !versionToBeInstalled ||
218
+ !isValidVersion(installedVersion, versionToBeInstalled);
202
219
  return [2 /*return*/, {
203
220
  outdated: outdated,
204
221
  installedVersion: installedVersion,
@@ -209,6 +226,10 @@ var getVersionToInstall = function (fallbackVersion) { return __awaiter(void 0,
209
226
  });
210
227
  }); };
211
228
  exports.getVersionToInstall = getVersionToInstall;
229
+ var downloadAndSaveJLink = function (destination, onUpdate) {
230
+ return (0, common_1.fetchIndex)().then(function (v) { return downloadJLink(v, function (update) { return onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(update.percentage); }, destination); });
231
+ };
232
+ exports.downloadAndSaveJLink = downloadAndSaveJLink;
212
233
  var downloadAndInstallJLink = function (onUpdate) {
213
234
  return (0, common_1.fetchIndex)()
214
235
  .then(function (v) { return downloadJLink(v, onUpdate); })
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
1
  {
2
- "name": "@nordicsemiconductor/nrf-jlink-js",
3
- "version": "0.0.2",
4
- "main": "dist",
5
- "types": "dist",
6
- "files": [
7
- "dist"
8
- ],
9
- "dependencies": {
10
- "axios": "^1.7.9",
11
- "semver": "^7.7.2"
12
- },
13
- "scripts": {
14
- "build": "tsc",
15
- "release-jlink": "ts-node scripts/release-jlink.ts"
16
- },
17
- "devDependencies": {
18
- "@types/semver": "^7.7.0",
19
- "ts-node": "^10.9.2",
20
- "typescript": "^5.7.2"
21
- },
22
- "repository": {
23
- "type": "git",
24
- "url": "git+https://github.com/NordicSemiconductor/nrf-jlink-js.git"
25
- },
26
- "author": "Nordic Semiconductor ASA",
27
- "license": "Proprietary",
28
- "description": "Shared library to handle J-Link versions"
2
+ "name": "@nordicsemiconductor/nrf-jlink-js",
3
+ "version": "0.0.4",
4
+ "main": "dist",
5
+ "types": "dist",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "dependencies": {
10
+ "axios": "^1.7.9",
11
+ "semver": "^7.7.2"
12
+ },
13
+ "scripts": {
14
+ "build": "tsc",
15
+ "upload-jlink": "ts-node scripts/upload-jlink.ts"
16
+ },
17
+ "devDependencies": {
18
+ "@types/semver": "^7.7.0",
19
+ "ts-node": "^10.9.2",
20
+ "typescript": "^5.7.2"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/NordicSemiconductor/nrf-jlink-js.git"
25
+ },
26
+ "author": "Nordic Semiconductor ASA",
27
+ "license": "Proprietary",
28
+ "description": "Shared library to handle J-Link versions"
29
29
  }
package/dist/common.d.ts DELETED
@@ -1,15 +0,0 @@
1
- export interface ArchUrl {
2
- arm64: string;
3
- x64: string;
4
- }
5
- export interface JLinkVariant {
6
- linux: ArchUrl;
7
- darwin: ArchUrl;
8
- win32: ArchUrl;
9
- }
10
- export interface JLinkIndex {
11
- version: string;
12
- jlinkUrls: JLinkVariant;
13
- }
14
- export declare const fetchIndex: () => Promise<JLinkIndex>;
15
- export declare const saveToFile: (stream: NodeJS.ReadableStream, destinationFile: string) => Promise<string>;
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { downloadAndInstallJLink, getVersionToInstall } from "./jlink";
2
- export { downloadAndInstallJLink, getVersionToInstall };