-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathscriptutils.js
161 lines (128 loc) · 4.97 KB
/
scriptutils.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* Portions Copyright (C) Philipp Kewisch */
import { Octokit } from "@octokit/core";
import fetch from 'node-fetch';
import fs from "fs/promises";
import fsc from "fs";
import path from "path";
import { pipeline } from "stream/promises";
const octokit = new Octokit();
async function get_latest_release(outFile) {
let response = await octokit.request('GET /repos/{owner}/{repo}/releases/latest', {
owner: 'kewisch',
repo: 'ical.js'
});
let release = response.data.name;
let icaljsAsset = response.data.assets.find(asset => asset.name == "ical.js");
if (!icaljsAsset) {
console.error("ical.js asset missing from " + release);
}
response = await fetch(icaljsAsset.browser_download_url);
let icaljs = await response.text();
await fs.writeFile(outFile, icaljs);
console.log("Latest release written to " + outFile);
}
async function get_latest_main(outFile) {
let response = await octokit.request("GET /repos/{owner}/{repo}/actions/runs", {
workflow_id: "ci.yml",
branch: "es6",
//branch: "main",
status: "success",
//exclude_pull_requests: true,
//event: "push",
owner: "kewisch",
repo: "ical.js"
});
let workflows = response.data.workflow_runs;
workflows.sort((a, b) => {
let datea = new Date(a);
let dateb = new Date(b);
return (datea < dateb) - (dateb < datea);
});
let archive_download_url = `https://nightly.link/kewisch/ical.js/actions/runs/${workflows[0].id}/distribution.zip`;
console.log(archive_download_url);
response = await fetch(archive_download_url);
if (!response.ok) {
throw new Error(response.status);
}
let buffer = Buffer.from(await response.arrayBuffer());
// Yauzl has been difficult due to the native bindings for crc32.
// Import ad-hoc to make GitHub Actions work.
let yauzl = await import("yauzl-promise");
let archive = await yauzl.fromBuffer(buffer);
let entry;
do {
entry = await archive.readEntry();
} while (entry && entry.fileName == "ical.js");
if (!entry) {
throw new Error("ical.js not found in distribution");
}
let stream = await entry.openReadStream();
let writeStream = fsc.createWriteStream(outFile);
await pipeline(stream, writeStream);
console.log("Latest main written to " + outFile);
}
async function performance_downloader() {
await Promise.allSettled([
get_latest_main("./tools/benchmark/ical_main.cjs"),
get_latest_release("./tools/benchmark/ical_release.js")
]);
}
async function generateZonesFile(tzdbDir) {
async function processZone(zoneFile) {
let contents = await fs.readFile(zoneFile, "utf-8");
let lines = contents.split("\r\n");
let vtimezone = lines.slice(lines.indexOf("BEGIN:VTIMEZONE") + 1, lines.indexOf("END:VTIMEZONE")).join("\r\n");
return ` register(${JSON.stringify(vtimezone)});`;
}
let tzdbVersion = (await fs.readFile(path.join(tzdbDir, "version"), "utf-8")).trim();
let lines = [
`(function() {`,
` function register(tzdata) { ICAL.TimezoneService.register(ICAL.Component.fromString("BEGIN:VTIMEZONE\\r\\n" + tzdata + "\\r\\nEND:VTIMEZONE")) };`,
` ICAL.TimezoneService.IANA_TZDB_VERSION = "${tzdbVersion}";`
];
let contents = await fs.readFile(path.join(tzdbDir, "zoneinfo", "zones.tab"), "utf-8");
for (let line of contents.split("\n")) {
let parts = line.split(" ");
if (parts.length == 3 && parts[2].length) {
lines.push(await processZone(path.join(tzdbDir, "zoneinfo", parts[2] + ".ics")));
} else if (parts.length == 1 && parts[0].length) {
lines.push(await processZone(path.join(tzdbDir, "zoneinfo", parts[0] + ".ics")));
}
}
lines.push("})();");
return lines.join("\n");
}
async function get_tzdb_version() {
let response = await fetch('https://www.iana.org/time-zones');
let text = await response.text();
let match = text.match(/version">([0-9a-z]*)<\/span>/);
if (!match) {
throw new Error('Could not detect latest timezone database version');
}
return match[1];
}
async function replace_unpkg(input, output) {
let content = await fs.readFile(input, { encoding: "utf-8" });
let pkg = JSON.parse(await fs.readFile(path.join(import.meta.dirname, "..", "package.json"), { encoding: "utf-8" }));
await fs.writeFile(output, content.replace(/unpkg.com\/ical.js/g, `unpkg.com/ical.js@${pkg.version}/dist/ical.js`));
console.log(`unpkg link from ${input} updated to ${pkg.version} and written to ${output}`);
}
async function main() {
switch (process.argv[2]) {
case "tzdb-version":
console.log(await get_tzdb_version());
break;
case "generate-zones":
console.log(await generateZonesFile(process.argv[3]));
break;
case "performance-downloader":
await performance_downloader();
break;
case "replace-unpkg":
await replace_unpkg(process.argv[3], process.argv[4]);
}
}
main();