-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathprocess-icons.js
94 lines (80 loc) · 2.8 KB
/
process-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
91
92
93
94
#!/usr/bin/env node
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const processIcon = (srcPath, fd, scaleWidth, scaleHeight) => {
// get icon name from filename
const iconName = path.basename(srcPath, path.extname(srcPath));
// regex will extract width, height and svg content into $1, $2 and $3 respectively
const regex = new RegExp(
/<svg.*viewBox="(.*)">[\s|\n]*(.*?)[\s|\n]*<\/svg>/i
);
const content = fs.readFileSync(srcPath, 'utf8');
const match = content.match(regex);
if (!match) {
// no matching result, bail
return;
}
const viewBox = match[1];
const svgContent = match[2];
// append the content to the target file handle
fs.writeSync(
fd,
`<symbol id="spectrum-icon-${iconName}" viewBox="${viewBox}">${svgContent}</symbol>`
);
};
// where is spectrum-css?
// TODO: use resolve package to find node_modules
const spectrumIconsPath = path.resolve(
path.join(
__dirname,
'..',
'node_modules',
'@spectrum-css',
'ui-icons',
'dist'
)
);
// define the target icon sizes for each scale
const scales = {
medium: { width: 18, height: 18 },
large: { width: 24, height: 24 },
};
// process the scales
Object.keys(scales).forEach((scaleKey) => {
console.log(`processing scale ${scaleKey}...`);
const scale = scales[scaleKey];
const srcPath = path.join(spectrumIconsPath, scaleKey);
const outputPath = path.join(
__dirname,
'..',
'packages',
'icons',
'src',
`icons-${scaleKey}.svg.ts`
);
let outputFd = fs.openSync(outputPath, 'w');
fs.writeSync(
outputFd,
'import { svg } from \'@spectrum-web-components/base\'; export default svg`<svg xmlns="http://www.w3.org/2000/svg">'
);
fs.readdirSync(srcPath).forEach((iconFile) => {
const srcIconPath = path.join(srcPath, iconFile);
console.log(`\ticon ${iconFile}`);
processIcon(srcIconPath, outputFd, scale.width, scale.height);
});
fs.writeSync(outputFd, '</svg>`;');
fs.closeSync(outputFd);
});
console.log('complete.');