forked from aws/aws-toolkit-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateIcons.ts
203 lines (171 loc) · 6.83 KB
/
generateIcons.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import webfont from 'webfont'
import * as path from 'path'
import * as nodefs from 'fs' // eslint-disable-line no-restricted-imports
const fontId = 'aws-toolkit-icons'
const projectDir = process.cwd() // root/packages/toolkit
const rootDir = path.join(projectDir, '../..') // root/
const iconsDir = path.join(projectDir, 'resources', 'icons')
const fontsDir = path.join(projectDir, 'resources', 'fonts')
const stylesheetsDir = path.join(projectDir, 'resources', 'css')
const packageJson = JSON.parse(nodefs.readFileSync(path.join(projectDir, 'package.json'), { encoding: 'utf-8' }))
const iconSources = [
// Paths relative to packages/toolkit
`resources/icons/**/*.svg`,
`../../node_modules/@vscode/codicons/src/icons/**/*.svg`,
'!**/{dark,light}/**',
]
interface PackageIcon {
readonly description: string
readonly default: {
readonly fontPath: string
readonly fontCharacter: string
}
}
interface IconsContribution {
[id: string]: PackageIcon
}
function createPackageIcon(fontPath: string, unicode: string, description?: string): PackageIcon {
const codePoint = unicode.codePointAt(0)?.toString(16)
if (!codePoint) {
throw new Error(`Invalid unicode character: ${unicode}`)
}
return {
description: description ?? 'AWS Contributed Icon',
default: {
fontPath,
fontCharacter: `\\${codePoint}`,
},
}
}
async function updatePackage(fontPath: string, icons: [id: string, icon: PackageIcon][]): Promise<void> {
const contributes = packageJson.contributes as { icons?: IconsContribution }
const iconsContribution = (contributes.icons ??= {})
if (typeof iconsContribution !== 'object') {
throw new TypeError('Expected `icons` contribution to be an object')
}
for (const [id, icon] of Object.entries(iconsContribution)) {
if (icon.default?.fontPath === fontPath) {
delete iconsContribution[id]
}
}
for (const [id, icon] of icons) {
iconsContribution[id] = icon
}
// prettier adds a newline to JSON files
const newPackage = `${JSON.stringify(packageJson, undefined, 4)}\n`
nodefs.writeFileSync(path.join(projectDir, 'package.json'), newPackage)
console.log('Updated package.json')
}
async function generate(mappings: Record<string, number | undefined> = {}) {
const dest = path.join(fontsDir, `${fontId}.woff`)
const relativeDest = path.relative(projectDir, dest)
const icons: { name: string; path: string; data?: PackageIcon }[] = []
const generated = new GeneratedFilesManifest()
const result = await webfont({
files: iconSources,
fontName: fontId,
formats: ['woff'],
startUnicode: 0xf000,
verbose: true,
normalize: true,
sort: true,
fontHeight: 1000,
template: 'css',
templateClassName: 'icon',
descent: 200, // Icons were negatively offset on the y-axes, this fixes it
templateFontPath: path.relative(stylesheetsDir, fontsDir).replace(/\\/g, '/'),
glyphTransformFn: (obj) => {
const filePath = (obj as { path?: string }).path
if (!filePath) {
throw new Error(`Expected glyph "${obj.name}" to have a file path`)
}
if (!obj.unicode) {
throw new Error(`Expected glyph "${obj.name}" to have the unicode property.`)
}
// If icon came from `node_modules` -> it's a codicon
if (filePath.includes('node_modules')) {
obj.name = `vscode-${path.basename(filePath).replace('.svg', '')}`
const mapping = mappings[obj.name]
if (mapping === undefined) {
throw new Error(`No unicode mapping found for icon "${obj.name}"`)
}
obj.unicode = [String.fromCodePoint(mapping)]
} else {
const parts = path.relative(iconsDir, filePath).split(path.sep)
obj.name = parts.join('-').replace('.svg', '')
}
if (!obj.name.startsWith('vscode')) {
// Normalize the font path regardless of platform
// See https://github.com/aws/aws-toolkit-vscode/pull/3066#discussion_r1063662657
const normalizedPath = relativeDest.split(path.sep).join(path.posix.sep)
icons.push({
name: obj.name,
path: filePath,
data: createPackageIcon(`./${normalizedPath}`, obj.unicode[0]),
})
} else {
icons.push({ name: obj.name, path: filePath })
}
return obj
},
})
const template = `
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*
* This style sheet was generated using "${path.relative(rootDir, __filename)}".
*/
${result.template}
`.trim()
const stylesheetPath = path.join(stylesheetsDir, 'icons.css')
const isValidIcon = (i: (typeof icons)[number]): i is Required<typeof i> => i.data !== undefined
nodefs.mkdirSync(fontsDir, { recursive: true })
if (result.woff) {
nodefs.writeFileSync(dest, result.woff)
}
nodefs.writeFileSync(stylesheetPath, template)
await updatePackage(
`./${relativeDest}`,
icons.filter(isValidIcon).map((i) => [i.name, i.data])
)
generated.addEntry(dest)
generated.addEntry(stylesheetPath)
generated.emit(path.join(projectDir, 'dist'))
}
class GeneratedFilesManifest {
private readonly files: string[] = []
public addEntry(file: string): void {
this.files.push(file)
}
public emit(dir: string): void {
const dest = path.join(dir, 'generated.buildinfo')
const data = JSON.stringify(this.files, undefined, 4)
nodefs.mkdirSync(dir, { recursive: true })
nodefs.writeFileSync(dest, data)
}
}
async function loadCodiconMappings(): Promise<Record<string, number | undefined>> {
const codicons = path.join(rootDir, 'node_modules', '@vscode', 'codicons', 'src')
const data = JSON.parse(nodefs.readFileSync(path.join(codicons, 'template', 'mapping.json'), 'utf-8'))
const mappings: Record<string, number | undefined> = {}
for (const [k, v] of Object.entries(data)) {
if (typeof k === 'string' && typeof v === 'number') {
if (v < 0xe000 || v >= 0xf000) {
// Will warn us if the codepoint moves outside the expected range
throw new Error(`Codicon "${k}" has unexpected codepoint: ${v}`)
}
mappings[`vscode-${k}`] = v
}
}
return mappings
}
async function main() {
const mappings = await loadCodiconMappings()
await generate(mappings)
}
void main()