-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdate.node.js
executable file
·75 lines (65 loc) · 1.9 KB
/
update.node.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
#!/usr/bin/env node
// Script to download all Google fonts
// and clean-up the output.
const https = require('https');
const fs = require('fs');
const path = require('path');
const { mkdirp } = require('mkdirp');
const googWebFontDlFork = require('goog-webfont-dl-fork');
const init = async () => {
const archiveList = await getArchive()
const googList = await getFontsList();
// filter the archiveList out of the googList
const newFonts = googList.filter( font => archiveList.indexOf( font ) < 0 );
await getFonts(newFonts);
updateArchive([...archiveList, ...newFonts]);
};
const updateArchive = list => {
fs.writeFileSync('archive', list.join("\n"), {
encoding: "utf8"
});
}
const getFonts = async (newFonts) => {
return new Promise(resolve => {
Promise.all(newFonts.map(font => downloadFont(font))).then(() => {
resolve();
});
});
};
const downloadFont = font => {
return new Promise(async (resolve, reject) => {
const ltr = font.substr(0,1).toUpperCase();
const ltrPath = `${process.cwd()}/FONTS/${ltr}`;
const fulPath = path.resolve(`${ltrPath}/${font}`);
mkdirp.sync(fulPath);
await googWebFontDlFork({
font: font,
destination: fulPath
});
resolve(font);
});
}
const getArchive = async () => {
const archive = fs.readFileSync('archive');
// return the list split and filtering out empties
return archive.toString().split("\n").filter( x => x );
};
const getFontsList = async () => {
const URL = 'https://fonts.google.com/metadata/fonts';
let fonts = "";
return new Promise(resolve => {
const req = https.get(URL, resp => {
resp.on('data', chunk => {
if (chunk) {
fonts += chunk.toString();
}
});
resp.on('end', () => {
// Convert fonts to JSON
// get the family names from familyMetadataList
resolve(JSON.parse(fonts).familyMetadataList.map( font => font.family ));
});
});
});
};
init();