-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsvgs.js
More file actions
executable file
·261 lines (210 loc) · 5.66 KB
/
svgs.js
File metadata and controls
executable file
·261 lines (210 loc) · 5.66 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env node
'use strict'
/**
* Dependencies
*/
const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
const { optimize } = require('svgo/lib/svgo');
const svgstore = require('svgstore');
const args = require(`${__dirname}/util/args`).args;
const cnsl = require(`${__dirname}/util/console`);
const resolve = require(`${__dirname}/util/resolve`);
const alerts = resolve('config/alerts');
/**
* Get modules and configuration options
*
* @return {Object} Object containing initial modules and settings
*/
const options = () => {
let config = resolve('config/svgs', true, false);
return {
modules: config.map(m => {
m.ext = '.svg';
return m;
}),
globs: [
resolve('config/svgs', false)
]
}
};
/**
* Write the svg file to the distribution folder
*
* @param {Object} mod The svg module to write
*/
const write = async mod => {
try {
let dist = path.join(mod.dist, mod.file);
if (!fs.existsSync(path.dirname(dist))) {
fs.mkdirSync(path.dirname(dist), {recursive: true});
}
if (mod.hasOwnProperty('store')) {
await fs.writeFileSync(dist, mod.store.data);
cnsl.describe(`${alerts.package} Svgs sprite written to ${alerts.str.path(dist)}`);
} else {
await fs.writeFileSync(dist, mod.optimized.data);
cnsl.describe(`${alerts.compression} Svgs in ${alerts.str.path(mod.svg)} out ${alerts.str.path(dist)}`);
}
return mod;
} catch (err) {
cnsl.error(`Svgs (write): ${err.stack}`);
}
}
/**
* Create a global store
*
* @type {Object}
*/
let SPRITE = new svgstore();
/**
* Add a file and it's data to the store
*
* @param {Object} mod The svg module to write
*
* @return {Object} The svg sprite
*/
const store = async mod => {
try {
SPRITE.add(mod.name, mod.optimized.data);
return SPRITE;
} catch (err) {
cnsl.error(`Svgs failed (store): ${err.stack}`);
}
};
/**
* Optimize an svg by it's filename
*
* @param {String} file The filename of the svg
*
* @return {String} The optimized svg file contents
*/
const svgo = async mod => {
try {
let data = fs.readFileSync(mod.svg, 'utf-8');
let optimized = await optimize(data, {
path: mod.svg,
...mod.svgo
});
return optimized;
} catch (err) {
cnsl.error(`Svgs failed (svgo): ${err.stack}`);
}
};
/**
* The main function for the svg script
*
* @param {Object} mod The svg module to write
*
* @return {Object} The svg module to write
*/
const main = async mod => {
try {
// If the module has a restricted list, check to see if the file is permitted.
if (mod.hasOwnProperty('restrict') && !mod.restrict.find(n => path.basename(mod.svg).includes(n))) {
return mod;
}
mod.file = `${mod.prefix}${path.basename(mod.svg)}`;
mod.name = path.basename(mod.file, path.extname(mod.file));
mod.optimized = await svgo(mod);
await store(mod);
// Do not write the individual optimized svg if mod.write.source is false
if (mod.hasOwnProperty('write') && mod.write.source === false)
return mod;
await write(mod);
return mod;
} catch (err) {
cnsl.error(`Svgs failed (main): ${err.stack}`);
}
};
/**
* Read a specific file or if it's a directory, read all of the files in it
*
* @param {Object} mod The svg module to write
*/
const walk = async mod => {
if (mod.hasOwnProperty('svg')) {
await main(mod);
} else {
try {
let files = fs.readdirSync(mod.source, 'utf-8');
for (let i = files.length - 1; i >= 0; i--) {
let copy = Object.assign({}, mod);
let source = path.join(mod.source, files[i]);
if (path.basename(source).includes(mod.ext)) {
copy.svg = source;
} else if (fs.lstatSync(source).isDirectory()) {
copy.source = source;
} else {
continue;
}
await walk(copy);
}
} catch (err) {
cnsl.error(`Svgs failed (walk): ${err.stack}`);
}
}
};
/**
* Runner for the script
*
* @param {String} dir The base path to run the script on
*/
const run = async () => {
let opts = options();
if (args.watch) {
try {
for (let index = 0; index < opts.modules.length; index++) {
const mod = opts.modules[index];
if (!fs.existsSync(mod.source)) continue;
const watcher = chokidar.watch([
path.join(mod.source, `/**/*${mod.ext}`),
...opts.globs
], {
usePolling: false,
awaitWriteFinish: {
stabilityThreshold: 750
}
});
watcher.on('change', async changed => {
cnsl.watching(`Detected change on ${alerts.str.path(changed)}`);
SPRITE = (mod.hasOwnProperty('svgstore')) ?
new svgstore(mod.svgstore) : new svgstore();
await walk(mod);
mod.store = {
data: SPRITE.toString()
};
await write(mod);
cnsl.success(`Svgs finished`);
});
cnsl.watching(`Svgs watching ${alerts.str.ext(opts.globs.join(', '))}`);
}
} catch (err) {
console.error(`${alerts.error} Svgs (run): ${err}`);
}
} else {
for (let index = 0; index < opts.modules.length; index++) {
const mod = opts.modules[index];
if (!fs.existsSync(mod.source)) process.exit();
SPRITE = (mod.hasOwnProperty('svgstore')) ?
new svgstore(mod.svgstore) : new svgstore();
await walk(mod);
mod.store = {
data: SPRITE.toString()
};
await write(mod);
}
cnsl.success(`Svgs finished`);
process.exit();
}
};
/**
* Export our methods
*
* @type {Object}
*/
module.exports = {
main: main,
run: run
};