-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgenerate.js
175 lines (146 loc) · 4.99 KB
/
generate.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
const fs = require('fs');
const { sync: mkdirp } = require('mkdirp');
const path = require('path');
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const meta = require('@mdi/svg/meta.json');
const svgPathRegex = /\sd="(.*)"/;
const startsWithNumberRegex = /^\d/;
function getRollupInputConfig(target) {
return {
external: [target],
plugins: [
babel({
presets: [
['es2015', { modules: false }],
target
],
plugins: [
'transform-object-rest-spread',
'external-helpers'
]
})
]
};
}
function normalizeName(name) {
return name.split(/[ -]/g).map(part => {
return part.charAt(0).toUpperCase() + part.slice(1);
}).join('') + 'Icon';
}
function collectComponents(svgFilesPath) {
const svgFiles = fs.readdirSync(svgFilesPath);
const icons = [];
for (const svgFile of svgFiles) {
const origName = svgFile.slice(0, -4);
const name = normalizeName(origName);
const content = fs.readFileSync(path.join(svgFilesPath, svgFile));
const svgPathMatches = svgPathRegex.exec(content);
const svgPath = svgPathMatches && svgPathMatches[1];
// skip on empty svgPath
if (!svgPath) throw new Error('Empty SVG path');
const icon = {
name: name,
aliases: [],
fileName: name + '.js',
defFileName: name + '.d.ts',
svgPath
};
const iconMeta = meta.find(entry => entry.name === origName);
if (iconMeta) {
icon.aliases = iconMeta.aliases;
}
icons.push(icon);
}
const aliases = [];
const removeAliases = [];
for (const icon of icons) {
for (const alias of icon.aliases) {
const normalizedAlias = normalizeName(alias);
// if the alias starts with a number, ignore it since JavaScript
// doesn't support variable names starting with a number
if (startsWithNumberRegex.test(normalizedAlias)) {
continue;
}
// check if alias duplicates top-level icon name and ignore
if (icons.find(icon => icon.name.toLowerCase() === normalizedAlias.toLowerCase())) {
continue;
}
// check if alias itself is duplicated
const duplicateAlias = aliases.find(alias2 => alias2.name.toLowerCase() === normalizedAlias.toLowerCase());
if (duplicateAlias) {
// check if duplicate alias is on same icon
// if not note for removal from final list
if (duplicateAlias.aliasFor !== icon.name) {
console.warn(`Duplicate alias ${normalizedAlias} (${icon.name}, ${duplicateAlias.aliasFor})`);
removeAliases.push(duplicateAlias.name);
continue;
}
continue;
}
aliases.push({
name: normalizedAlias,
aliasFor: icon.name,
fileName: normalizedAlias + '.js',
defFileName: normalizedAlias + '.d.ts',
svgPath: icon.svgPath
});
}
// removed no longer required aliases array
delete icon.aliases;
}
// clean up remaining alias duplicates
for (const aliasName of removeAliases) {
const index = aliases.find(alias => aliasName === alias);
aliases.splice(index, 1);
}
return [...icons, ...aliases];
}
async function generate(target, jsCb, tsCb, tsAllCb) {
const basePath = path.resolve(__dirname, '..');
const svgFilesPath = path.resolve(basePath, 'node_modules/@mdi/svg/svg');
const buildPath = path.resolve(basePath, 'build');
mkdirp(buildPath);
const publishPath = path.resolve(basePath, 'publish-' + target);
mkdirp(publishPath);
const distPath = path.resolve(publishPath, 'dist');
mkdirp(distPath);
console.log('Collecting components...');
const components = collectComponents(svgFilesPath);
console.log('Generating components...');
const pathsToUnlink = [];
for (const [index, component] of components.entries()) {
if (!component.aliasFor) {
console.log(`Generating ${component.name}... (${index + 1}/${components.length})`);
} else {
console.log(`Generating alias ${component.name}... (${index + 1}/${components.length})`);
}
const fileContent = jsCb(component);
const inputPath = path.resolve(buildPath, component.fileName);
const outputPath = path.resolve(publishPath, component.fileName);
fs.writeFileSync(inputPath, fileContent);
const bundle = await rollup.rollup({
input: inputPath,
...getRollupInputConfig(target)
});
await bundle.write({
file: outputPath,
format: 'cjs'
});
// remember paths to unlink later
if (!pathsToUnlink.includes(inputPath)) {
pathsToUnlink.push(inputPath);
}
const definitionContent = tsCb(component);
fs.writeFileSync(path.join(publishPath, component.defFileName), definitionContent);
}
console.log('Generating typings...');
// create the global typings.d.ts
const typingsContent = tsAllCb();
fs.writeFileSync(path.resolve(distPath, 'typings.d.ts'), typingsContent);
// clean up
for (const pathToUnlink of pathsToUnlink) {
fs.unlinkSync(pathToUnlink);
}
}
module.exports = generate;