1
1
import outlineStroke from 'svg-outline-stroke'
2
2
import { asyncForEach , getCompileOptions , getPackageDir , HOME_DIR , readSvgs } from '../../../.build/helpers.mjs'
3
3
import fs from 'fs'
4
- import { resolve } from 'path'
4
+ import { resolve , basename } from 'path'
5
+ import crypto from 'crypto'
6
+ import { glob } from 'glob'
7
+ import { execSync } from 'child_process'
5
8
6
9
const DIR = getPackageDir ( 'icons-webfont' )
7
10
8
11
const buildOutline = async ( ) => {
9
- let files = readSvgs ( )
10
- const compileOptions = getCompileOptions ( )
12
+ let files = readSvgs ( ) ,
13
+ filesList = [ ]
11
14
12
- const iconfontUnicode = JSON . parse ( fs . readFileSync ( resolve ( HOME_DIR , 'tags.json' ) , 'utf-8' ) )
15
+ const compileOptions = getCompileOptions ( ) ,
16
+ iconfontUnicode = JSON . parse ( fs . readFileSync ( resolve ( HOME_DIR , 'tags.json' ) , 'utf-8' ) )
13
17
14
- await asyncForEach ( files , async function ( { name, contents } ) {
18
+ await asyncForEach ( files , async function ( { name, contents } ) {
15
19
if ( compileOptions . includeIcons . length === 0 || compileOptions . includeIcons . indexOf ( name ) >= 0 ) {
16
20
17
21
if ( iconfontUnicode [ name ] ) {
18
22
const unicode = iconfontUnicode [ name ] . unicode
19
- await console . log ( 'Stroke for:' , name , unicode )
23
+ console . log ( 'Stroke for:' , name , unicode )
24
+
25
+ let filename = `${ name } .svg`
26
+ if ( unicode ) {
27
+ filename = `u${ unicode . toUpperCase ( ) } -${ name } .svg`
28
+ }
29
+
30
+ filesList . push ( filename )
20
31
21
32
contents = contents
22
- . replace ( 'width="24"' , 'width="1000"' )
23
- . replace ( 'height="24"' , 'height="1000"' )
33
+ . replace ( 'width="24"' , 'width="1000"' )
34
+ . replace ( 'height="24"' , 'height="1000"' )
24
35
25
36
if ( compileOptions . strokeWidth ) {
26
37
contents = contents
27
- . replace ( 'stroke-width="2"' , `stroke-width="${ compileOptions . strokeWidth } "` )
38
+ . replace ( 'stroke-width="2"' , `stroke-width="${ compileOptions . strokeWidth } "` )
39
+ }
40
+
41
+ const cachedFilename = `u${ unicode . toUpperCase ( ) } -${ name } .svg` ;
42
+
43
+ if ( unicode && fs . existsSync ( resolve ( DIR , `icons-outlined/${ cachedFilename } ` ) ) ) {
44
+ // Get content
45
+ let cachedContent = fs . readFileSync ( resolve ( DIR , `icons-outlined/${ cachedFilename } ` ) , 'utf-8' )
46
+
47
+ // Get hash
48
+ let cachedHash = '' ;
49
+ cachedContent = cachedContent . replace ( / < ! - - \! c a c h e : ( [ a - z 0 - 9 ] + ) - - > / , function ( m , hash ) {
50
+ cachedHash = hash ;
51
+ return '' ;
52
+ } )
53
+
54
+ // Check hash
55
+ if ( crypto . createHash ( 'sha1' ) . update ( cachedContent ) . digest ( "hex" ) === cachedHash ) {
56
+ console . log ( 'Cached stroke for:' , name , unicode )
57
+ return true ;
58
+ }
28
59
}
29
60
30
61
await outlineStroke ( contents , {
@@ -35,15 +66,44 @@ const buildOutline = async () => {
35
66
fixedWidth : false ,
36
67
color : 'black'
37
68
} ) . then ( outlined => {
38
- if ( unicode ) {
39
- fs . writeFileSync ( resolve ( DIR , `icons-outlined/u${ unicode . toUpperCase ( ) } -${ name } .svg` ) , outlined , 'utf-8' )
40
- } else {
41
- fs . writeFileSync ( resolve ( DIR , `icons-outlined/${ name } .svg` ) , outlined , 'utf-8' )
42
- }
43
- } ) . catch ( error => console . log ( error ) )
44
- }
69
+ filesList [ filename ]
70
+
71
+ // Save file
72
+ fs . writeFileSync ( resolve ( DIR , `icons-outlined/${ filename } ` ) , outlined , 'utf-8' )
73
+
74
+ // Fix outline
75
+ execSync ( `fontforge -lang=py -script .build/fix-outline.py icons-outlined/${ filename } ` )
76
+ execSync ( `svgo icons-outlined/${ filename } ` )
77
+
78
+ // Add hash
79
+ const fixedFileContent = fs
80
+ . readFileSync ( resolve ( DIR , `icons-outlined/${ filename } ` ) , 'utf-8' )
81
+ . replace ( / \n / g, ' ' )
82
+ . trim ( ) ,
83
+ hashString = `<!--!cache:${ crypto . createHash ( 'sha1' ) . update ( fixedFileContent ) . digest ( "hex" ) } -->`
84
+
85
+ // Save file
86
+ fs . writeFileSync (
87
+ resolve ( DIR , `icons-outlined/${ filename } ` ) ,
88
+ fixedFileContent + hashString ,
89
+ 'utf-8'
90
+ )
91
+ } ) . catch ( error => console . log ( error ) )
92
+ }
45
93
}
46
94
} )
95
+
96
+ // Remove old files
97
+ const existedFiles = ( await glob ( resolve ( DIR , `icons-outlined/*.svg` ) ) ) . map ( file => basename ( file ) )
98
+
99
+ existedFiles . forEach ( file => {
100
+ if ( filesList . indexOf ( file ) === - 1 ) {
101
+ console . log ( 'Remove:' , file )
102
+ fs . unlinkSync ( resolve ( DIR , `icons-outlined/${ file } ` ) )
103
+ }
104
+ } )
105
+
106
+ console . log ( 'Done' )
47
107
}
48
108
49
109
await buildOutline ( )
0 commit comments