@@ -39,52 +39,29 @@ function getSVGOConfig ({ id }) {
39
39
}
40
40
}
41
41
42
+ function readFile ( ...parts ) {
43
+ return fs . readFileSync ( path . resolve ( __dirname , ...parts ) , 'utf8' )
44
+ }
45
+
46
+ function writeFile ( content , ...parts ) {
47
+ fs . writeFileSync ( path . resolve ( __dirname , ...parts ) , content , 'utf8' )
48
+ }
49
+
42
50
const ENDPOINT = process . env . DLS_ICONS_API
43
51
const RAW_DIR = path . resolve ( __dirname , '../raw' )
44
52
const SVG_DIR = path . resolve ( __dirname , '../svg' )
45
- const ICON_PATTERN = / ^ ( .+ ) \. s v g $ /
46
-
47
- const DATA_TPL = fs . readFileSync (
48
- path . resolve ( __dirname , 'data/data.tpl' ) ,
49
- 'utf8'
50
- )
51
- const DATA_EXPORT_TPL = fs . readFileSync (
52
- path . resolve ( __dirname , 'data/export.tpl' ) ,
53
- 'utf8'
54
- )
55
- const ICON_TPL = fs . readFileSync (
56
- path . resolve ( __dirname , 'icon/icon.tpl' ) ,
57
- 'utf8'
58
- )
59
- const ICON_EXPORT_TPL = fs . readFileSync (
60
- path . resolve ( __dirname , 'icon/export.tpl' ) ,
61
- 'utf8'
62
- )
63
-
64
- const TYPINGS_DATA_INDEX_TPL = fs . readFileSync (
65
- path . resolve ( __dirname , 'typings/data.index.tpl' ) ,
66
- 'utf8'
67
- )
68
- const TYPINGS_DATA_TPL = fs . readFileSync (
69
- path . resolve ( __dirname , 'typings/data.tpl' ) ,
70
- 'utf8'
71
- )
72
- const TYPINGS_REACT_INDEX_TPL = fs . readFileSync (
73
- path . resolve ( __dirname , 'typings/react.index.tpl' ) ,
74
- 'utf8'
75
- )
76
- const TYPINGS_REACT_TPL = fs . readFileSync (
77
- path . resolve ( __dirname , 'typings/react.tpl' ) ,
78
- 'utf8'
79
- )
80
- const TYPINGS_VUE_INDEX_TPL = fs . readFileSync (
81
- path . resolve ( __dirname , 'typings/vue.index.tpl' ) ,
82
- 'utf8'
83
- )
84
- const TYPINGS_VUE_TPL = fs . readFileSync (
85
- path . resolve ( __dirname , 'typings/vue.tpl' ) ,
86
- 'utf8'
87
- )
53
+
54
+ const DATA_TPL = readFile ( 'data/data.tpl' )
55
+ const DATA_EXPORT_TPL = readFile ( 'data/export.tpl' )
56
+ const ICON_TPL = readFile ( 'icon/icon.tpl' )
57
+ const ICON_EXPORT_TPL = readFile ( 'icon/export.tpl' )
58
+
59
+ const TYPINGS_DATA_INDEX_TPL = readFile ( 'typings/data.index.tpl' )
60
+ const TYPINGS_DATA_TPL = readFile ( 'typings/data.tpl' )
61
+ const TYPINGS_REACT_INDEX_TPL = readFile ( 'typings/react.index.tpl' )
62
+ const TYPINGS_REACT_TPL = readFile ( 'typings/react.tpl' )
63
+ const TYPINGS_VUE_INDEX_TPL = readFile ( 'typings/vue.index.tpl' )
64
+ const TYPINGS_VUE_TPL = readFile ( 'typings/vue.tpl' )
88
65
89
66
const ICON_PACKS = [ 'dls-icons-react' , 'dls-icons-vue' , 'dls-icons-vue-3' ]
90
67
const DATA_PACK = 'dls-icons-data'
@@ -126,66 +103,117 @@ async function generate () {
126
103
} )
127
104
128
105
return Promise . all (
129
- ( await getSVGFiles ( ) ) . map ( async ( { slug, content } ) => {
130
- const file = `${ slug } .svg`
131
- const { el, content : svg , width, height } = await normalizeSVG ( content , file )
132
-
133
- fs . writeFileSync ( path . join ( SVG_DIR , file ) , svg , 'utf8' )
134
-
135
- const name = camelCase ( slug )
136
- const Name = upperFirst ( name )
137
-
138
- const { width : w , height : h , ...attributes } = el . attributes
139
-
140
- const iconCode = stringifyObject (
141
- {
142
- name : `Icon${ Name } ` ,
143
- content : el . children . map ( ( child ) => stringify ( child ) ) . join ( '' ) ,
144
- attributes,
145
- width : Number ( width ) ,
146
- height : Number ( height )
147
- } ,
148
- {
149
- indent : ' '
150
- }
151
- )
106
+ ( await getSVGFiles ( ) ) . map (
107
+ async ( { slug, content, category, desc, type, colorType } ) => {
108
+ const file = `${ slug } .svg`
109
+ const {
110
+ el,
111
+ content : svg ,
112
+ width,
113
+ height
114
+ } = await normalizeSVG ( content , file )
115
+
116
+ fs . writeFileSync ( path . join ( SVG_DIR , file ) , svg , 'utf8' )
117
+
118
+ const name = camelCase ( slug )
119
+ const Name = upperFirst ( name )
120
+
121
+ const { width : w , height : h , ...attributes } = el . attributes
122
+
123
+ const iconCode = stringifyObject (
124
+ {
125
+ name : `Icon${ Name } ` ,
126
+ content : el . children . map ( ( child ) => stringify ( child ) ) . join ( '' ) ,
127
+ attributes,
128
+ width : Number ( width ) ,
129
+ height : Number ( height )
130
+ } ,
131
+ {
132
+ indent : ' '
133
+ }
134
+ )
152
135
153
- const tplData = {
154
- name,
155
- Name,
156
- icon : iconCode
157
- }
136
+ const deprecated = category === '@deprecated'
137
+ const annotation = deprecated ? '/** @deprecated */\n' : ''
158
138
159
- const dataModuleCode = renderTpl ( DATA_TPL , tplData )
160
- const iconModuleCode = renderTpl ( ICON_TPL , tplData )
139
+ const tplData = {
140
+ name,
141
+ Name,
142
+ icon : iconCode ,
143
+ annotation
144
+ }
161
145
162
- fs . writeFileSync (
163
- path . join ( DATA_DIR , `icons/${ Name } .js` ) ,
164
- dataModuleCode ,
165
- 'utf8'
166
- )
146
+ const dataModuleCode = renderTpl ( DATA_TPL , tplData )
147
+ const iconModuleCode = renderTpl ( ICON_TPL , tplData )
167
148
168
- ICON_PACKS . forEach ( ( pack ) => {
169
- const iconsDir = path . join ( getPackDir ( pack ) , 'src/icons' )
170
149
fs . writeFileSync (
171
- path . join ( iconsDir , `${ Name } .js` ) ,
172
- iconModuleCode ,
150
+ path . join ( DATA_DIR , `icons/ ${ Name } .js` ) ,
151
+ dataModuleCode ,
173
152
'utf8'
174
153
)
175
- } )
176
154
177
- return { slug, name, Name, file }
178
- } )
155
+ ICON_PACKS . forEach ( ( pack ) => {
156
+ const iconsDir = path . join ( getPackDir ( pack ) , 'src/icons' )
157
+ fs . writeFileSync (
158
+ path . join ( iconsDir , `${ Name } .js` ) ,
159
+ iconModuleCode ,
160
+ 'utf8'
161
+ )
162
+ } )
163
+
164
+ return {
165
+ slug,
166
+ name,
167
+ Name,
168
+ file,
169
+ category,
170
+ deprecated,
171
+ desc,
172
+ type,
173
+ colorType,
174
+ annotation
175
+ }
176
+ }
177
+ )
179
178
) . then ( ( icons ) => {
180
179
const dataIndex = icons
181
180
. map ( ( data ) => renderTpl ( DATA_EXPORT_TPL , data ) )
182
181
. join ( '' )
183
182
184
183
fs . writeFileSync ( path . join ( DATA_DIR , 'index.js' ) , dataIndex , 'utf8' )
185
184
185
+ const TYPE_MAP = {
186
+ 0 : 'outline' ,
187
+ 1 : 'solid'
188
+ }
189
+ const COLOR_TYPE_MAP = {
190
+ 0 : 'monocolor' ,
191
+ 1 : 'multicolor'
192
+ }
193
+ fs . writeFileSync (
194
+ getPackDir ( DATA_PACK , 'meta.json' ) ,
195
+ JSON . stringify (
196
+ icons . reduce ( ( acc , { Name, slug, category, desc, deprecated, type, colorType } ) => {
197
+ acc [ `Icon${ Name } ` ] = {
198
+ slug,
199
+ category,
200
+ desc,
201
+ deprecated,
202
+ type : TYPE_MAP [ type ] ,
203
+ colorType : COLOR_TYPE_MAP [ colorType ]
204
+ }
205
+
206
+ return acc
207
+ } , { } ) ,
208
+ null ,
209
+ ' '
210
+ ) ,
211
+ 'utf8'
212
+ )
213
+
186
214
const iconIndex =
187
215
icons . map ( ( data ) => renderTpl ( ICON_EXPORT_TPL , data ) ) . join ( '' ) +
188
- ' export { createIcon, SharedResources } from \ './common\ '\n'
216
+ " export { createIcon, SharedResources } from './common'\n"
189
217
190
218
ICON_PACKS . concat ( DATA_PACK ) . forEach ( ( pack ) => {
191
219
const packDir = getPackDir ( pack )
@@ -210,9 +238,17 @@ async function generate () {
210
238
. map ( ( _ , j ) => icons [ i * cols + j ] )
211
239
. map (
212
240
( icon ) =>
213
- `<td align="center">${
241
+ `<td align="center"${
242
+ icon && icon . deprecated ? ' title="@deprecated"' : ''
243
+ } >${
214
244
icon
215
- ? `<img src="https://raw.githubusercontent.com/ecomfe/dls-icons/master/svg/${ icon . file } " height="24"/><br/><sub>${ prefix } ${ icon . Name } </sub>`
245
+ ? `<img src="https://raw.githubusercontent.com/ecomfe/dls-icons/master/svg/${
246
+ icon . file
247
+ } " height="24"/><br/><sub>${
248
+ icon . deprecated ? '<s>' : ''
249
+ } ${ prefix } ${ icon . Name } ${
250
+ icon . deprecated ? '</s>' : ''
251
+ } </sub>`
216
252
: ''
217
253
} </td>`
218
254
)
@@ -224,16 +260,15 @@ async function generate () {
224
260
225
261
const readmeFiles = [ 'README.md' , 'README.zh-Hans.md' ]
226
262
227
- readmeFiles . forEach ( readme => {
228
- const file = path . join ( packDir , readme )
229
- const content = fs . readFileSync ( file , 'utf8' )
263
+ readmeFiles . forEach ( ( readme ) => {
264
+ const content = readFile ( packDir , readme )
230
265
231
- fs . writeFileSync (
232
- file ,
266
+ writeFile (
233
267
commentMark ( content , {
234
268
icons : iconTable
235
269
} ) ,
236
- 'utf8'
270
+ packDir ,
271
+ readme
237
272
)
238
273
} )
239
274
} )
@@ -247,39 +282,41 @@ function sortFileData (f1, f2) {
247
282
}
248
283
249
284
async function getSVGFiles ( ) {
285
+ let iconData
250
286
if ( ENDPOINT ) {
251
- const { data } = JSON . parse ( await fetch ( ENDPOINT ) . then ( ( res ) => res . text ( ) ) )
287
+ iconData = await fetch ( ENDPOINT ) . then ( ( res ) => res . text ( ) )
288
+ writeFile ( iconData , 'icons.json' )
289
+ } else {
290
+ try {
291
+ iconData = readFile ( 'icons.json' )
292
+ } catch ( e ) {
293
+ console . error ( 'No local `icons.json` found. You must specify an `ENDPOINT`.' )
294
+ process . exit ( 1 )
295
+ }
296
+ }
252
297
253
- clearDir ( RAW_DIR )
298
+ const { data } = JSON . parse ( iconData )
254
299
255
- data . forEach ( ( { label, svg } ) => {
256
- fs . writeFileSync (
257
- path . join ( RAW_DIR , label . replace ( / _ / g, '-' ) + '.svg' ) ,
258
- svg ,
259
- 'utf8'
260
- )
261
- } )
300
+ clearDir ( RAW_DIR )
262
301
263
- return data
264
- . map ( ( { label, svg } ) => ( {
265
- slug : label . replace ( / _ / g, '-' ) ,
266
- content : svg
267
- } ) )
268
- . sort ( sortFileData )
269
- } else {
270
- return fs
271
- . readdirSync ( RAW_DIR )
272
- . filter ( ( file ) => ICON_PATTERN . test ( file ) )
273
- . map ( ( file ) => {
274
- const slug = file . replace ( ICON_PATTERN , ( _ , $1 ) => $1 )
275
- const content = fs . readFileSync ( path . resolve ( RAW_DIR , file ) , 'utf8' )
276
- return {
277
- slug,
278
- content
279
- }
280
- } )
281
- . sort ( sortFileData )
282
- }
302
+ data . forEach ( ( { label, svg } ) => {
303
+ fs . writeFileSync (
304
+ path . join ( RAW_DIR , label . replace ( / _ / g, '-' ) + '.svg' ) ,
305
+ svg ,
306
+ 'utf8'
307
+ )
308
+ } )
309
+
310
+ return data
311
+ . map ( ( { label, svg, name, category, type, colorType } ) => ( {
312
+ slug : label . replace ( / _ / g, '-' ) ,
313
+ content : svg ,
314
+ category,
315
+ desc : name , // Text description
316
+ type, // 0: outline, 1: solid
317
+ colorType // 0: monocolor, 1: multicolor
318
+ } ) )
319
+ . sort ( sortFileData )
283
320
}
284
321
285
322
async function normalizeSVG ( content , file ) {
@@ -299,7 +336,7 @@ async function normalizeSVG (content, file) {
299
336
let { width, height, viewBox } = attributes
300
337
301
338
if ( ! viewBox && ! ( width && height ) ) {
302
- console . error ( file , ' doesn\ 't contain a valid size declaration.' )
339
+ console . error ( file , " doesn't contain a valid size declaration." )
303
340
console . error ( width , height , viewBox )
304
341
} else if ( viewBox ) {
305
342
// has viewBox, override width/height
0 commit comments