|
1 | | -const xml2js = require('xml2js') |
2 | | -const parser = new xml2js.Parser() |
| 1 | +const xml2js = require("xml2js"); |
| 2 | +const parser = new xml2js.Parser(); |
3 | 3 |
|
4 | | -const fs = require('fs') |
5 | | -const path = require('path') |
6 | | -const ejs = require('ejs'); |
| 4 | +const fs = require("fs"); |
| 5 | +const path = require("path"); |
| 6 | +const ejs = require("ejs"); |
7 | 7 |
|
8 | | -const featherDir = path.resolve(__dirname, '../node_modules/feather-icons/dist/icons/') |
9 | | -const queue = [] |
10 | | -const featherIcons = {} |
| 8 | +const featherDir = path.resolve( |
| 9 | + __dirname, |
| 10 | + "../node_modules/feather-icons/dist/icons/" |
| 11 | +); |
| 12 | +const queue = []; |
| 13 | +const featherIcons = {}; |
11 | 14 | fs.readdir(featherDir, (err, files) => { |
12 | | - |
13 | | - files.forEach(fileName => { |
14 | | - |
15 | | - queue.push(new Promise(function (resolve, reject) { |
16 | | - fs.readFile(path.resolve(featherDir, fileName), function (err, data) { |
17 | | - if (err) { |
18 | | - reject(err); |
19 | | - } else { |
20 | | - var iconName = path.parse(fileName).name |
21 | | - parser.parseString(data, function (err, result) { |
22 | | - const children = [] |
23 | | - for (let item in result.svg) { |
24 | | - switch (item) { |
25 | | - |
26 | | - case 'path': |
27 | | - result.svg.path.forEach(function (pathItem) { |
28 | | - children.push('p', pathItem.$.d) |
29 | | - }) |
30 | | - break |
31 | | - case 'rect': |
32 | | - result.svg.rect.forEach(function (pathItem) { |
33 | | - let attr = pathItem.$ |
34 | | - children.push('r', `${attr.x} ${attr.y} ${attr.width} ${attr.height}` + (attr.rx ? ` ${attr.rx} ${attr.ry}` : '')) |
35 | | - }) |
36 | | - break |
37 | | - case 'circle': |
38 | | - result.svg.circle.forEach(function (pathItem) { |
39 | | - let attr = pathItem.$ |
40 | | - children.push('c', `${attr.cx} ${attr.cy} ${attr.r}`) |
41 | | - }) |
42 | | - break |
43 | | - case 'ellipse': |
44 | | - result.svg.ellipse.forEach(function (pathItem) { |
45 | | - let attr = pathItem.$ |
46 | | - children.push('e', `${attr.cx} ${attr.cy} ${attr.rx} ${attr.ry}`) |
47 | | - }) |
48 | | - break |
49 | | - case 'line': |
50 | | - result.svg.line.forEach(function (pathItem) { |
51 | | - let attr = pathItem.$ |
52 | | - children.push('l', `${attr.x1} ${attr.x2} ${attr.y1} ${attr.y2}`) |
53 | | - }) |
54 | | - break |
55 | | - case 'polygon': |
56 | | - result.svg[item].forEach(function (pathItem) { |
57 | | - children.push('g', pathItem.$.points) |
58 | | - }) |
59 | | - break |
60 | | - case 'polyline': |
61 | | - result.svg[item].forEach(function (pathItem) { |
62 | | - children.push('b', pathItem.$.points) |
63 | | - }) |
64 | | - break |
| 15 | + files.sort().forEach(fileName => { |
| 16 | + queue.push( |
| 17 | + new Promise(function(resolve, reject) { |
| 18 | + fs.readFile(path.resolve(featherDir, fileName), function( |
| 19 | + err, |
| 20 | + data |
| 21 | + ) { |
| 22 | + if (err) { |
| 23 | + reject(err); |
| 24 | + } else { |
| 25 | + var iconName = path.parse(fileName).name; |
| 26 | + parser.parseString(data, function(err, result) { |
| 27 | + const children = []; |
| 28 | + for (let item in result.svg) { |
| 29 | + switch (item) { |
| 30 | + case "path": |
| 31 | + result.svg.path.forEach(function( |
| 32 | + pathItem |
| 33 | + ) { |
| 34 | + children.push("p", pathItem.$.d); |
| 35 | + }); |
| 36 | + break; |
| 37 | + case "rect": |
| 38 | + result.svg.rect.forEach(function( |
| 39 | + pathItem |
| 40 | + ) { |
| 41 | + let attr = pathItem.$; |
| 42 | + children.push( |
| 43 | + "r", |
| 44 | + `${attr.x} ${attr.y} ${ |
| 45 | + attr.width |
| 46 | + } ${attr.height}` + |
| 47 | + (attr.rx |
| 48 | + ? ` ${attr.rx} ${ |
| 49 | + attr.ry |
| 50 | + }` |
| 51 | + : "") |
| 52 | + ); |
| 53 | + }); |
| 54 | + break; |
| 55 | + case "circle": |
| 56 | + result.svg.circle.forEach(function( |
| 57 | + pathItem |
| 58 | + ) { |
| 59 | + let attr = pathItem.$; |
| 60 | + children.push( |
| 61 | + "c", |
| 62 | + `${attr.cx} ${attr.cy} ${ |
| 63 | + attr.r |
| 64 | + }` |
| 65 | + ); |
| 66 | + }); |
| 67 | + break; |
| 68 | + case "ellipse": |
| 69 | + result.svg.ellipse.forEach(function( |
| 70 | + pathItem |
| 71 | + ) { |
| 72 | + let attr = pathItem.$; |
| 73 | + children.push( |
| 74 | + "e", |
| 75 | + `${attr.cx} ${attr.cy} ${ |
| 76 | + attr.rx |
| 77 | + } ${attr.ry}` |
| 78 | + ); |
| 79 | + }); |
| 80 | + break; |
| 81 | + case "line": |
| 82 | + result.svg.line.forEach(function( |
| 83 | + pathItem |
| 84 | + ) { |
| 85 | + let attr = pathItem.$; |
| 86 | + children.push( |
| 87 | + "l", |
| 88 | + `${attr.x1} ${attr.x2} ${ |
| 89 | + attr.y1 |
| 90 | + } ${attr.y2}` |
| 91 | + ); |
| 92 | + }); |
| 93 | + break; |
| 94 | + case "polygon": |
| 95 | + result.svg[item].forEach(function( |
| 96 | + pathItem |
| 97 | + ) { |
| 98 | + children.push( |
| 99 | + "g", |
| 100 | + pathItem.$.points |
| 101 | + ); |
| 102 | + }); |
| 103 | + break; |
| 104 | + case "polyline": |
| 105 | + result.svg[item].forEach(function( |
| 106 | + pathItem |
| 107 | + ) { |
| 108 | + children.push( |
| 109 | + "b", |
| 110 | + pathItem.$.points |
| 111 | + ); |
| 112 | + }); |
| 113 | + break; |
| 114 | + } |
65 | 115 | } |
66 | | - } |
67 | | - featherIcons[iconName] = children |
68 | | - resolve() |
69 | | - }); |
70 | | - } |
71 | | - }); |
72 | | - |
73 | | - })); |
| 116 | + featherIcons[iconName] = children; |
| 117 | + resolve(); |
| 118 | + }); |
| 119 | + } |
| 120 | + }); |
| 121 | + }) |
| 122 | + ); |
74 | 123 | }); |
75 | 124 |
|
76 | | - Promise.all(queue).then(function () { |
77 | | - fs.writeFile(path.resolve(__dirname, '../src/feather-icons.js'), 'export default ' + JSON.stringify(featherIcons), function (err, data) { |
78 | | - if (err) { |
79 | | - console.error(err) |
| 125 | + Promise.all(queue).then(function() { |
| 126 | + fs.writeFile( |
| 127 | + path.resolve(__dirname, "../src/feather-icons.js"), |
| 128 | + "export default " + JSON.stringify(featherIcons), |
| 129 | + function(err, data) { |
| 130 | + if (err) { |
| 131 | + console.error(err); |
| 132 | + } |
80 | 133 | } |
81 | | - }); |
| 134 | + ); |
82 | 135 |
|
83 | | - ejs.renderFile(path.resolve(__dirname, '../index.ejs'), {icons: Object.keys(featherIcons)}, function (err, str) { |
84 | | - fs.writeFile(path.resolve(__dirname, '../index.html'), str, function (err) { |
85 | | - console.log('index.html finished') |
86 | | - }); |
87 | | - }) |
| 136 | + ejs.renderFile( |
| 137 | + path.resolve(__dirname, "../index.ejs"), |
| 138 | + { icons: Object.keys(featherIcons).sort() }, |
| 139 | + function(err, str) { |
| 140 | + fs.writeFile( |
| 141 | + path.resolve(__dirname, "../index.html"), |
| 142 | + str, |
| 143 | + function(err) { |
| 144 | + console.log("index.html finished"); |
| 145 | + } |
| 146 | + ); |
| 147 | + } |
| 148 | + ); |
88 | 149 | }); |
89 | 150 | }); |
90 | | - |
91 | | - |
|
0 commit comments