1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const SVGO = require('svgo');
|
15 | const svgstore = require('svgstore');
|
16 | const path = require('path');
|
17 | const fs = require('fs-extra');
|
18 | const utils = require('../utils');
|
19 | const chalk = require('chalk');
|
20 | const data = utils.getManifest();
|
21 | const createLogger = require('logging').default;
|
22 | const logger = createLogger('Icons');
|
23 |
|
24 | const iconPath = path.resolve(data.paths.source, data.paths.icons);
|
25 | const svgo = new SVGO({plugins:[{removeViewBox:false}]});
|
26 |
|
27 | let config = data.config.svg;
|
28 | if (!config.hasOwnProperty('sprite')) { config.sprite = 'icons.svg'; }
|
29 | if (!config.hasOwnProperty('scss')) { config.scss = data.paths.source + data.paths.styles; }
|
30 |
|
31 | logger.info('Starting Icons', config.scss);
|
32 |
|
33 | let files = fs.readdirSync(iconPath);
|
34 | let sprite = svgstore();
|
35 | let icons = [];
|
36 | let scss = '';
|
37 | let iconDest = path.resolve(data.paths.source + 'images/');
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function forEachPromise(items, fn) {
|
46 | return items.reduce(function (promise, item) {
|
47 | return promise.then(function () {
|
48 | return fn(item);
|
49 | });
|
50 | }, Promise.resolve());
|
51 | }
|
52 |
|
53 | function buildIcon(fileName) {
|
54 | if (fileName === '.keep') { return false; }
|
55 | if (fileName === '.DS_Store') { return false; }
|
56 | const title = path.basename(fileName, '.svg');
|
57 |
|
58 | let file = fs.readFileSync(path.resolve(iconPath, fileName));
|
59 | return svgo.optimize(file).then(function(result) {
|
60 | if (result.error) {
|
61 | logger.info('Icon error ' + fileName +'.svg : ', result.error);
|
62 | } else {
|
63 | icons.push(Object.assign({title}, result.info));
|
64 | sprite.add('icon--' + path.parse(fileName).name, result.data);
|
65 | }
|
66 | });
|
67 | }
|
68 |
|
69 | function storeSprite() {
|
70 | fs.ensureDirSync(iconDest);
|
71 | const destination = path.resolve(iconDest, config.sprite);
|
72 | fs.writeFileSync(destination, sprite.toString());
|
73 | logger.info('Icons compiled to: ', destination);
|
74 | }
|
75 |
|
76 | function makeScssFile() {
|
77 | fs.ensureDirSync(config.scss);
|
78 | scss = "$icon-sizes: ();\n";
|
79 | const destination = path.resolve(config.scss, "_icons.scss");
|
80 | icons.forEach(icon => {
|
81 | scss = scss.concat(
|
82 | `.icon--${icon.title}, .icon--${icon.title} svg { width: ${icon.width}px; height: ${icon.height}px }\n`
|
83 | );
|
84 | scss = scss.concat(
|
85 | `$icon-sizes: map-merge($icon-sizes, ( ${icon.title}: ( width: ${icon.width}px, height: ${icon.height}px ) ) );\n`
|
86 | );
|
87 | });
|
88 | fs.writeFileSync(destination, scss);
|
89 | logger.info("Icons SCSS file written at: ", destination);
|
90 | }
|
91 |
|
92 |
|
93 | forEachPromise(files, buildIcon).then(() => {
|
94 | storeSprite();
|
95 | makeScssFile();
|
96 | logger.info(chalk.green('Finished Icons'));
|
97 | });
|