/
packager-v2.js
131 lines (119 loc) · 3.85 KB
/
packager-v2.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
const _ = require(`lodash`)
const async = require(`async`)
const flatten = require(`flat`)
const fs = require(`fs-extra`)
const isAbsoluteUrl = require(`is-absolute-url`)
const { APIv2 } = require(`google-font-metadata`)
const download = require(`./download-file`)
const { fontFaceUnicode } = require(`./templates`)
module.exports = function (id) {
const font = APIv2[id]
const fontDir = `packages/${font.id}`
// Generate filenames
const makeFontDownloadPath = (subset, weight, style, extension) => {
return `./${fontDir}/files/${font.id}-${subset}-${weight}-${style}.${extension}`
}
const makeFontFilePath = (subset, weight, style, extension) => {
return `./files/${font.id}-${subset}-${weight}-${style}.${extension}`
}
// Parse API and split into variant + link array pairs. [['weight.style.subset.url|local.extension','link to font or local name'],...]
const downloadURLPairs = _.toPairs(flatten(font.variants))
// Filter out local font links and only leave URLs for each pair
const links = downloadURLPairs
.filter(pair => isAbsoluteUrl(pair[1].toString()))
.filter(file => file[0].split(".")[4] === "woff2")
.map(file => {
const types = file[0].split(".")
const dest = makeFontDownloadPath(
types[2].replace("[", "").replace("]", ""),
types[0],
types[1],
types[4]
)
const url = file[1]
return {
url,
dest,
}
})
// Get woff and ttf/otf all subset variant
let oldLinks = downloadURLPairs
.filter(pair => isAbsoluteUrl(pair[1].toString()))
.filter(file => {
const extension = file[0].split(".")[4]
if (extension === "woff") {
return true
}
return false
})
.map(file => {
const types = file[0].split(".")
const dest = makeFontDownloadPath("all", types[0], types[1], types[4])
const url = file[1]
return {
url,
dest,
}
})
// Remove all duplicate values for each "subset" to reduce unneccesary downloads
oldLinks = _.uniqBy(oldLinks, item => {
return item.url && item.dest
})
_.forEach(oldLinks, item => {
links.push(item)
})
// Download all font files
async.map(links, (d, downloadDone) => {
const { url, dest } = d
download(url, dest, err => {
if (err) {
console.log("Error downloading", font.id, url, err)
}
downloadDone()
})
})
// Generate CSS
const unicodeKeys = Object.keys(font.unicodeRange)
font.weights.forEach(weight => {
font.styles.forEach(style => {
const cssStyle = []
unicodeKeys.forEach(subset => {
// Some fonts may have variants 400, 400i, 700 but not 700i.
if (style in font.variants[weight]) {
const css = fontFaceUnicode({
fontId: font.id,
fontName: font.family,
locals: font.variants[weight][style][subset].local,
style,
subset,
weight,
woff2Path: makeFontFilePath(
subset.replace("[", "").replace("]", ""),
weight,
style,
"woff2"
),
woffPath: makeFontFilePath("all", weight, style, "woff"),
unicodeRange: font.unicodeRange[subset],
})
cssStyle.push(css)
}
})
// Write down CSS
if (style in font.variants[weight]) {
if (style === "normal") {
const cssPath = `${fontDir}/${weight}.css`
fs.writeFileSync(cssPath, cssStyle.join(""))
// Generate index CSS
if (weight === "400") {
fs.writeFileSync(`${fontDir}/index.css`, cssStyle.join(""))
}
} else {
// If italic or else, define specific style CSS file
const cssStylePath = `${fontDir}/${weight}-${style}.css`
fs.writeFileSync(cssStylePath, cssStyle.join(""))
}
}
})
})
}