-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
229 lines (189 loc) · 6.09 KB
/
build.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
225
226
227
228
229
const fs = require( 'fs' ).promises;
const prettier = require( 'prettier' );
const manifestDirectory = 'src/manifest/';
const svgDirectory = 'src/svg/';
const path = __dirname + '/src/';
const warningHeader = `// --
// -- WARNING!
// -- This is an auto-generated file. Do not edit.
// --`;
/**
* Scan for all the files inside /svg
*/
const init = async () => {
console.log( 'Processing files ...' );
const manifests = await fs.readdir( manifestDirectory );
manifests.forEach( createJSX );
createIndex( manifests );
createIconsMD( manifests );
};
/**
* Create a JSX file out of a SVG file
*
* @param {string} file - The name of the file to process
*/
const createJSX = async ( file ) => {
console.log( `Creating JSX file for : ${ file }` );
const content = await fs.readFile( manifestDirectory + file, 'utf-8' );
const iconData = JSON.parse( content );
const fileName = file.replace( '.json', '' );
renderStyles( iconData.styles, fileName ).then( async ( { svgs, primitivesUsed } ) => {
let newFileContent = `${ warningHeader }
import { ${ Array.from( primitivesUsed ).join( ', ' ) } } from '@wordpress/primitives';
const icon = {
styles: { ${ Object.keys( svgs ).map( ( key ) => `
${ key }: ( ${ svgs[ key ]} )`) }
},
meta: {
label: "${ iconData.label }",
keywords: [ ${ iconData.keywords.map( ( keyword ) => `"${ keyword }"` ) } ]
}
};
const defaultIcon = icon.styles.default;
const styles = icon.styles;
const meta = icon.meta;
export {
defaultIcon as default,
styles,
meta
}`;
newFileContent = prettier.format( newFileContent, {
singleQuote: true,
useTabs: true,
tabWidth: 4,
parser: 'babel',
}) ;
await writeFile( `${ path }library/`, `${ fileName }.js`, newFileContent );
});
};
async function renderStyles( styles, filename ) {
const svgs = {};
const primitivesUsed = new Set();
for ( let style of styles ) {
const name = style === 'default' ? `${ filename }.svg` : `${ filename }-${ style }.svg`;
let content = await fs.readFile( svgDirectory + name, 'utf-8' );
content = replacePrimitives( content );
findPrimitives( content ).forEach( ( primitive ) => primitivesUsed.add( primitive ) );
content = content.replace( /class\=\"/g, 'className="' );
svgs[ style ] = content;
}
return {
svgs,
primitivesUsed
};
}
/**
* Create the index file that contains all the icons
*
* @param {array} files - An array of all the files
*/
createIndex = async ( files ) => {
console.log( `Creating index file` );
let content = warningHeader + "\r\n\r\n";
files.forEach( ( file ) => {
const filename = file.replace( '.json', '' );
content =
content +
`export {
default as ${ toPascalCase( filename ) }Icon,
styles as ${ toPascalCase( filename ) }Styles,
meta as ${ toPascalCase( filename ) }Meta
} from './library/${ filename }';\r\n`;
} );
content = content + `
export const IconsList = [
${ files.map( file => `"${ file.replace( '.json', '' ) }"` ) }
]`;
await fs.mkdir( path, { recursive: true } );
await fs.writeFile( `${ path }index.js`, content );
};
/**
* Create the Icons markdown file
*
* @param {array} files - An array of all the files
*/
createIconsMD = async ( files ) => {
console.log( `Creating icons markdown file` );
let content = `# CoBlocks Icons
| Name (slug) | Icon | Style | Component name | Keywords |
| ------------- | ------ | ------- | ---------------- | ---------- |\r\n`;
for( const file of files ) {
let data = await fs.readFile( manifestDirectory + file, 'utf-8' );
data = JSON.parse( data );
const filename = file.replace( '.json', '' );
data.styles.forEach( ( style ) => {
content = style === 'default'
? content +
`| ${ data.label } (${ filename }) | <img src="./src/svg/${ filename }.svg" width="24" height="24"> | ${ style } | ${ toPascalCase( filename ) }Icon | ${ data.keywords.map( ( keyword ) => ` ${ keyword }` ) } |\r\n`
: content +
`| | <img src="./src/svg/${ filename }-${ style }.svg" width="24" height="24"> | ${ style } | ${ toPascalCase( filename ) }Styles.${ style } | |\r\n`
} );
};
await fs.writeFile( `${ __dirname }/icons.md`, content );
};
/**
* Replace primitives in SVG to match React imports
*
* @param {string} str - The content
* @return {string} The new content with items replaced
*/
const replacePrimitives = ( str ) => {
const primitivesToReplace = {
'<svg': '<SVG',
'svg>': 'SVG>',
'<g': '<G',
'g>': 'G>',
'<path': '<Path',
'<rect': '<Rect',
'<circle': '<Circle',
'<polygon': '<Polygon',
'<defs': '<Defs',
'stroke-width': 'strokeWidth',
'fill-rule': 'fillRule',
'clip-rule': 'clipRule',
'stroke-linejoin': 'strokeLinejoin',
'stroke-linecap': 'strokeLinecap',
'tabindex': 'tabIndex',
'datetime': 'dateTime',
'stroke-width': 'strokeWidth',
};
const regx = new RegExp( Object.keys( primitivesToReplace ).join( '|' ), 'gi' );
return str.replace( regx, ( matched ) => primitivesToReplace[ matched ] );
};
/**
* Find primitives inside content
*
* @param {string} str - The content
* @return {string} Primitives used ready to used in an import statement
*/
findPrimitives = ( content ) => {
const primitives = [ 'SVG', 'Path', 'G', 'Rect', 'Circle', 'Polygon', 'Defs' ];
return primitives
.filter( ( primitive ) => content.indexOf( `<${primitive}` ) != -1 );
}
/**
* Write the file and write the required directories if needed
*
* @param {string} path - The path where the new file should live
* @param {string} filename - The name of the new file
* @param {string} content - The content of the file
*/
const writeFile = async ( path, filename, content ) => {
await fs.mkdir( path, { recursive: true } );
await fs.writeFile( path + filename, content );
};
/**
* Pascal Case a string
*
* @param {string} text - The string to pascal case
* @return {string} The pascal cased string
*/
const toPascalCase = ( text ) => text.replace( /(^\w|-\w)/g, clearAndUpper );
/**
* Remove dashes and uppercase next letter
*
* @param {string} text - The string to uppercase
* @return {string} The upper cased string
*/
const clearAndUpper = ( text ) => text.replace( /-/, '' ).toUpperCase();
init();