-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generateSvgStrings.js
224 lines (198 loc) 路 7.21 KB
/
generateSvgStrings.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import * as fs from 'fs'
import rimraf from 'rimraf'
import copyfiles from 'copyfiles'
import Renamer from 'renamer'
import debounce from 'debounce'
import replace from 'replace-in-file'
import Sort from 'fast-sort'
import { pascalCase } from 'case-anything'
import { filePathToIconName, filePathToIconSynonyms, filePathToIconCategory } from './utils.js'
import { listFiles } from './_listFiles.js'
const { sort } = Sort
const renamer = new Renamer()
const PATH_PEPICONS = './packages/pepicons'
const nextTick = () => new Promise((resolve) => setTimeout(resolve, 100))
const deleteIconsFolder = () =>
new Promise((resolve, reject) => {
rimraf(PATH_PEPICONS + '/src/icons', resolve)
})
const copyPopSvgs = () =>
new Promise((resolve, reject) => {
const from = PATH_PEPICONS + '/svg/pop/*.svg'
const to = PATH_PEPICONS + '/src/icons/'
copyfiles([from, to], { up: 3 }, resolve)
})
const copyPrintSvgs = () =>
new Promise((resolve, reject) => {
const from = PATH_PEPICONS + '/svg/print/*.svg'
const to = PATH_PEPICONS + '/src/icons/'
copyfiles([from, to], { up: 3 }, resolve)
})
const renameSvgsToTs = () =>
new Promise((resolve, reject) => {
renamer.on('replace-result', (replaceResult) => {
debounce(resolve, 200)()
})
const path = PATH_PEPICONS + '/src/icons/**/*.svg'
renamer.rename({
files: [path],
find: '.svg',
replace: '.ts',
})
})
const formatToExportSvgString = () =>
new Promise((resolve, reject) => {
const path = PATH_PEPICONS + '/src/icons/**/*.ts'
replace({
files: path,
from: /([\S\s]+)\n*/g,
to: (...args) => `export default \`${args[1]}\`\n`,
})
.then(resolve)
.catch(reject)
})
const autoGeneratedWarning = `/** Do not edit! This file is auto-generated. */`
/**
* @param {'pop' | 'print' | 'synonyms' | 'categories' | 'types'} kind
*/
const filesArrayToExportFileContents = (kind = 'pop', iconNameFilePathEntries = []) => {
const importTypePepicon =
kind === 'print'
? `import { PepiconPrint } from '../types'`
: kind === 'pop' || kind === 'synonyms'
? `import { Pepicon } from '../types'`
: `import { Pepicon } from './types'`
if (kind === 'pop' || kind === 'print') {
// imports
const importStatements = iconNameFilePathEntries
.map(([iconName, filePath]) => {
return `import ${pascalCase(iconName)} from './${kind}/${filePathToIconName(filePath)}'\n`
})
.join('')
// exports
const pepiconSvgExports = iconNameFilePathEntries
.map(([iconName]) => {
return `export const ${kind}${pascalCase(iconName)} = ${pascalCase(iconName)}\n`
})
.join('')
const pepiconSvgDic = iconNameFilePathEntries
.map(([iconName]) => ` '${iconName}': ${pascalCase(iconName)},\n`)
.join('')
const exportSvgDic =
kind === 'print'
? `export const ${kind}: { [name in PepiconPrint]: string } = {\n${pepiconSvgDic}}`
: `export const ${kind}: { [name in Pepicon]: string } = {\n${pepiconSvgDic}}`
// all together
return `${autoGeneratedWarning}
// ======= //
/* IMPORTS */
// ======= //
${importTypePepicon}
${importStatements}
// ======= //
/* EXPORTS */
// ======= //
${pepiconSvgExports}
${exportSvgDic}
`
}
if (kind === 'types') {
const pepiconArray = `[${iconNameFilePathEntries
.map(([iconName]) => `'${iconName}'`)
.join(', ')}]`
const pepiconType = iconNameFilePathEntries.map(([iconName]) => `'${iconName}'`).join(' | ')
const pepiconPrintType = `Exclude<Pepicon, ${iconNameFilePathEntries
.flatMap(([iconName]) => (iconName.endsWith('-filled') ? `'${iconName}'` : []))
.join(' | ')}>`
return `${autoGeneratedWarning}
export type Pepicon = ${pepiconType}
export type PepiconName = Pepicon
export const pepiconArray: Pepicon[] = ${pepiconArray}
export type PepiconPrint = ${pepiconPrintType}
`
}
if (kind === 'categories') {
const allCategories = new Set()
const categoryProps = iconNameFilePathEntries
.map(([iconName, filePath]) => {
allCategories.add(filePathToIconCategory(filePath))
return ` '${iconName}': '${filePathToIconCategory(filePath)}',\n`
})
.join('')
const cats = sort([...allCategories]).desc()
const categoryArray = `[${cats.map((c) => `'${c}'`).join(', ')}]`
const pepiconCategoryDic = `export const pepiconCategoryDic: { [name in Pepicon]: string } = {\n${categoryProps}}`
return `${autoGeneratedWarning}
${importTypePepicon}
export const categories: string[] = ${categoryArray}
${pepiconCategoryDic}
`
}
if (kind === 'synonyms') {
const iconSynonymArray = filePath => `[${filePathToIconSynonyms(filePath).map(s => `'${s}'`).join(', ')}]` // prettier-ignore
const synonymProps = iconNameFilePathEntries
.map(([iconName, filePath]) => ` '${iconName}': ${iconSynonymArray(filePath)},\n`)
.join('')
return `${autoGeneratedWarning}\n\n${importTypePepicon}\n\nexport const synonyms: { [name in Pepicon]: string[] } = {\n${synonymProps}}\n`
}
}
/**
* @param {'pop' | 'print' | 'synonyms' | 'categories' | 'types'} kind
*/
const generateExportsFile = async (kind = 'pop', iconNameFilePathEntries = []) => {
const content = filesArrayToExportFileContents(kind, iconNameFilePathEntries)
const path =
kind === 'pop' || kind === 'print'
? PATH_PEPICONS + `/src/icons/${kind}.ts`
: kind === 'synonyms'
? PATH_PEPICONS + `/src/synonyms/en.ts`
: PATH_PEPICONS + `/src/${kind}.ts`
fs.writeFileSync(path, content)
}
/**
* @param {'pop' | 'print'} kind
*/
const getIconNameFilePathEntries = async (kind = 'pop') => {
const regex = new RegExp(`.+${kind}\/.+\.svg`, 'gi')
const files = await listFiles(PATH_PEPICONS + `/export/`, regex)
const iconNameFilePathEntries = files.map((filePath) => [
filePathToIconName(filePath),
filePath.split('/').slice(-2).join('/'), // prettier-ignore
])
return sort(iconNameFilePathEntries).asc((entry) => entry[0])
}
const generateIndexFiles = async () => {
const iconNameFilePathEntries__pop = await getIconNameFilePathEntries('pop')
const iconNameFilePathEntries__print = await getIconNameFilePathEntries('print')
// write several required files
await Promise.all([
generateExportsFile('pop', iconNameFilePathEntries__pop),
generateExportsFile('print', iconNameFilePathEntries__print),
generateExportsFile('types', iconNameFilePathEntries__pop),
generateExportsFile('categories', iconNameFilePathEntries__pop),
generateExportsFile('synonyms', iconNameFilePathEntries__pop),
])
// write one more index file
const path = PATH_PEPICONS + '/src/index.ts'
const content = `${autoGeneratedWarning}
export type { Pepicon, PepiconName, PepiconPrint } from './types'
export { pepiconArray } from './types'
export { categories, pepiconCategoryDic } from './categories'
export { synonyms } from './synonyms/en'
export { synonyms as synonymsJa } from './synonyms/ja'
export * from './icons/pop'
export * from './icons/print'
`
fs.writeFileSync(path, content)
}
/**
* @returns {Promise<void>}
*/
export async function generateSvgStrings() {
await deleteIconsFolder()
await copyPopSvgs()
await copyPrintSvgs()
await renameSvgsToTs()
await formatToExportSvgString()
await generateIndexFiles()
}