-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-icons.js
90 lines (78 loc) · 2.22 KB
/
generate-icons.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
'strict mode';
const fs = require('fs');
const path = require('path');
const ICONS_ROOT = path.resolve('node_modules/material-design-icons');
const ICONS_PATH = 'svg/production';
const BASE_SIZE = 24;
var mods = [];
var icons = [];
var sources = [];
var scopes = [
'action',
'alert',
'av',
'communication',
'content',
'device',
'editor',
'file',
'hardware',
'image',
'maps',
'navigation',
'notification',
'places',
'social',
'toggle'
];
sources.push(scopes.map(processScope));
function processScope(scopeName){
var scopeDir = path.resolve(ICONS_ROOT, scopeName, ICONS_PATH);
var scopeIcons = fs.readdirSync(scopeDir);
var filteredIcons = scopeIcons.filter(filterIcon);
var bemEntites = filteredIcons.map(bemify);
bemEntites.map(function(entity){
var filePath = 'common.blocks/icon/_'+entity.modName,
fileModVal = entity.modVal !== true? '_' + entity.modVal : '',
templateName = 'icon_' + entity.modName + fileModVal + '.bemhtml.js';
try{fs.accessSync(filePath)}
catch (e) {
if(e.code == 'ENOENT')
fs.mkdirSync(filePath);
}
return fs.writeFileSync(path.resolve(filePath, templateName), getTemplateContent(
entity.modName,
entity.modVal,
fs.readFileSync(path.resolve(scopeDir, entity.file), 'utf8')
));
});
return bemEntites;
}
function filterIcon(filename){
var fileData = filename.split('_');
return fileData.pop() === BASE_SIZE+'px.svg';
}
function bemify(filename){
var fileData = filename.split('_'),
modName,
modVal;
fileData.splice(0,1); //delete unused ic_ prefix
fileData.pop(); //delete unused file extension
modName = fileData.splice(0, 1)[0];
modVal = fileData.join('-') || true;
var entity = {
file : filename,
modName : modName,
modVal : modVal
};
return entity;
}
function getTemplateContent(mod, val, svg){
var modVal = val === true || `'${val}'`;
return [
`block('icon').mod('material', true).mod('${mod}', ${modVal})(`,
` content()({ html : '${svg}' })`,
');'
].join('\n');
}
module.exports = icons;