This repository was archived by the owner on Aug 27, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
91 lines (80 loc) · 2.05 KB
/
Copy pathcli.js
File metadata and controls
91 lines (80 loc) · 2.05 KB
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
const { default: fetch } = require("node-fetch");
const fs = require("fs/promises");
const generator = require(".");
const path = require("path");
const googleAPIKey = process.env.GOOGLE_API_KEY;
main().catch((err) => console.error(err));
async function main() {
const [base64Data, json] = await Promise.all([
generateImageData(),
fetchListJson(),
]);
const dir = await prepareDestDir();
await Promise.all([
saveImage(dir, base64Data),
saveJson(dir, json),
]);
console.log("Done.");
}
/**
* Returns PNG image in Base64 format.
* @returns {Promise<string>}
*/
function generateImageData() {
return new Promise((resolve) => {
generator.getImage({
callback(base64Data) {
resolve(base64Data);
},
googleAPIKey,
options: {
lineHeight: "25px",
fontSize: "17px",
width: "400px"
},
port: 1224,
});
});
}
function fetchListJson() {
const url = `https://www.googleapis.com/webfonts/v1/webfonts?key=${googleAPIKey}`;
return fetch(url).then((res) => {
return res.text();
});
}
/**
* @return {Promise<string>} Created dir name
*/
async function prepareDestDir() {
const listVersion = generateListVersion(new Date);
const dir = path.resolve(`dest/${listVersion}`);
await fs.mkdir(dir, { recursive: true });
return dir;
}
/**
* @param {string} dir
* @param {string} base64Data
* @returns {Promise<void>}
*/
async function saveImage(dir, base64Data) {
const destPath = path.resolve(dir, "google-font-image.png");
await fs.writeFile(destPath, base64Data, "base64");
}
/**
* @param {string} dir
* @param {string} json
* @returns {Promise<void>}
*/
async function saveJson(dir, json) {
const destPath = path.resolve(dir, "google-font-data.json");
await fs.writeFile(destPath, json, "utf8");
}
/**
* @param {Date} date
*/
function generateListVersion(date) {
const sYear = `${date.getFullYear()}`;
const sMonth = `${date.getMonth() + 1}`.padStart(2, "0");
const sDate = `${date.getDate()}`.padStart(2, "0");
return `${sYear}${sMonth}${sDate}`;
}