-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
update-aegis-icons.cjs
78 lines (68 loc) · 2.86 KB
/
update-aegis-icons.cjs
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
const AdmZip = require('adm-zip');
const argv = require('yargs/yargs')(process.argv.slice(2)).argv;
const fs = require('node:fs/promises');
const IconsDirectory = './static/totp-icons';
const IconsCatalogPath = './src/lib/totp-icons/catalog.json';
function mapAegisCatalog(data, tag) {
return {
uuid: data.uuid,
name: data.name,
version: data.version,
tag: tag,
icons: data.icons
.filter(i => i.filename.includes('1_Primary'))
.map(m => ({ filename: m.filename.replace('icons/1_Primary/', ''), issuer: m.issuer.map(i => i.toLowerCase()) })),
};
}
async function main() {
const release = argv.release || 'latest';
const ref = argv.ref;
let actualTag;
let downloadUrl;
if (ref) {
const refState = await fetch(`https://api.github.com/repos/aegis-icons/aegis-icons/git/${ref}`).then(r => r.json());
actualTag = refState.object.sha;
downloadUrl = `https://api.github.com/repos/aegis-icons/aegis-icons/zipball/${ref}`;
} else {
const latestReleaseResponse = await fetch(
`https://api.github.com/repos/aegis-icons/aegis-icons/releases/${release}`,
).then(r => r.json());
actualTag = latestReleaseResponse.tag_name;
downloadUrl = latestReleaseResponse.assets[0].browser_download_url;
}
let existingCatalogTag = 'N/A';
try {
const existingCatalog = require(IconsCatalogPath);
existingCatalogTag = existingCatalog.tag;
} catch {
/* empty */
}
if (existingCatalogTag != actualTag) {
console.info(`New version of AEGIS icons are available! Existing: ${existingCatalogTag}, New: ${actualTag}`);
console.info('Downloading ZIP');
const catalogZipResponse = await fetch(downloadUrl);
const catalogZipBuffer = Buffer.from(await catalogZipResponse.arrayBuffer());
const catalogZip = new AdmZip(catalogZipBuffer, { readEntries: true });
await fs.rm(IconsDirectory, { recursive: true, force: true });
console.info('Extracting icons');
await fs.mkdir(IconsDirectory, { recursive: true });
const includePathRegex = /^(aegis-icons-aegis-icons-\w+\/)?icons\/(1_Primary|3_Generic\/Key.svg$)/gi;
const packEntryRegex = /^(aegis-icons-aegis-icons-\w+\/)?pack\.json$/gi;
let packEntry;
for (let entry of catalogZip.getEntries()) {
if (includePathRegex.test(entry.entryName)) {
catalogZip.extractEntryTo(entry, IconsDirectory, false, true, false);
} else if (packEntryRegex.test(entry.entryName)) {
packEntry = entry;
}
}
console.info('Building catalog file');
const rawCatalog = JSON.parse(packEntry.getData().toString('utf-8'));
const filteredCatalog = mapAegisCatalog(rawCatalog, actualTag);
await fs.writeFile(IconsCatalogPath, JSON.stringify(filteredCatalog));
console.log('Done!');
} else {
console.info('No new version available');
}
}
main();