-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
88 lines (67 loc) · 2.78 KB
/
index.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
const url = require('url');
const path = require('path');
const fs = require("fs");
const got = require('got');
const Spinnies = require('spinnies');
const AdmZip = require('adm-zip');
const async = require('async');
const spinnies = new Spinnies();
const GOT_OPTIONS = {
headers: {
Origin: 'https://fontawesome.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
};
const FA_RELEASES_PAGE = 'https://github.com/FortAwesome/Font-Awesome/releases';
const FA_PRO_ASSET_BASE = 'https://kit-pro.fontawesome.com';
const fontUrlRegex = /url\((.*?)\)/gm;
const githubSpansRegex = /<div class="f1 flex-auto min-width-0 text-normal">(.+?)<\/div>/gms;
const githubReleasesRegex =/>(.*?)</;
main();
async function main() {
spinnies.add('loading-versions', { text: 'Loading Font Awesome 5 versions' });
const versions = await getFA5Versions();
if (!versions || !versions[0]) {
spinnies.fail('loading-versions', { text: 'Failed to load Font Awesome 5 versions' });
return;
}
spinnies.succeed('loading-versions', { text: 'Loaded Font Awesome 5 versions' });
const latestVersion = versions[0];
spinnies.add('ripping-start', { text: `Ripping FA5 v${latestVersion}` });
const zip = new AdmZip();
const css = await got.get(`${FA_PRO_ASSET_BASE}/releases/v${latestVersion}/css/pro.min.css`, GOT_OPTIONS);
GOT_OPTIONS.headers.Referer = `${FA_PRO_ASSET_BASE}/releases/v${latestVersion}/css/pro.min.css`;
GOT_OPTIONS.encoding = null;
const fontUrls = css.body
.match(fontUrlRegex).map(url => url.replace('url(', '').replace(')', '').replace('../../..', FA_PRO_ASSET_BASE));;
const cssFile = css.body
.replace(/https:\/\/kit-free.fontawesome.com\/algo\/1/g, '..')
.replace(/..\/..\/..\/algo\/1/g, '..')
.replace(/webfonts/g, 'fonts');
zip.addFile('css/all.css', Buffer.from(cssFile));
async.each(fontUrls, (fontUrl, callback) => {
const fileName = path.basename(url.parse(fontUrl).pathname);
got(fontUrl, GOT_OPTIONS)
.then(response => {
zip.addFile(`fonts/${fileName}`, response.body);
callback();
})
.catch(() => {
callback();
});
}, () => {
fs.writeFileSync(`${__dirname}/fa5-v${latestVersion}.zip`, zip.toBuffer());
spinnies.succeed('ripping-start', { text: `Ripped FA5 v${latestVersion}. Saved to ${__dirname}/fa5-v${latestVersion}.zip` });
});
}
async function getFA5Versions() {
// I decided to use RegEx on HTML rather than use the API to try and get around ratelimits
// This will break if GH changes the html layout
const response = await got(FA_RELEASES_PAGE);
const html = response.body;
const spans = html.match(githubSpansRegex);
const versions = spans.map(span => {
return span.match(githubReleasesRegex)[1].replace('Release', '').trim();
});
return versions;
}