-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregister-firmware-bundles.mjs
91 lines (77 loc) · 2.28 KB
/
register-firmware-bundles.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import fs, { createWriteStream } from "node:fs";
import { readFile } from "node:fs/promises";
import { mkdtemp } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import yazl from "yazl";
import { nameRegEx } from "./lib/nameRegEx.mjs";
const version = (process.argv[process.argv.length - 1] ?? "").trim();
const filenameRegEx = nameRegEx(version);
console.log(`Publishing release version`, version);
const assets = fs
.readdirSync(process.cwd())
.filter((name) => filenameRegEx.test(name));
if (assets.length === 0) {
console.error(`No assets found for release ${version}!`);
process.exit(1);
}
for (const asset of assets) {
const {
groups: { configuration },
} = filenameRegEx.exec(asset);
const fwversion = `${version}${
configuration !== undefined ? `+${configuration}` : ""
}`;
const fwName = `asset-tracker-template ${fwversion}`;
console.log(fwName);
const manifest = {
name: fwName,
description: `Firmware update for the Thingy:91 X (${
configuration ?? "default"
})`,
fwversion,
"format-version": 1,
files: [
{
file: asset,
type: "application",
size: fs.statSync(asset).size,
},
],
};
console.log(JSON.stringify(manifest, null, 2));
const zipfile = new yazl.ZipFile();
zipfile.addBuffer(
Buffer.from(JSON.stringify(manifest, null, 2), "utf-8"),
"manifest.json"
);
const tempDir = await mkdtemp(path.join(os.tmpdir(), asset));
zipfile.addFile(path.join(process.cwd(), asset), asset);
const zipFileName = path.join(tempDir, `${asset}.zip`);
await new Promise((resolve) => {
zipfile.outputStream
.pipe(createWriteStream(zipFileName))
.on("close", () => {
resolve();
});
zipfile.end();
});
const createFirmwareRes = await fetch(
"https://api.nrfcloud.com/v1/firmwares",
{
method: "POST",
headers: {
Authorization: `Bearer ${process.env.NRF_CLOUD_API_KEY}`,
"Content-Type": "application/zip",
},
body: await readFile(zipFileName),
}
);
if (createFirmwareRes.status !== 200) {
console.error(
`Failed to upload firmware bundle: ${await createFirmwareRes.text()}`
);
} else {
console.log(JSON.stringify(await createFirmwareRes.json(), null, 2));
}
}