-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_icons.js
79 lines (72 loc) · 2.16 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
const fs = require('fs')
const {Transform} = require('stream')
// remove all the inline style information from all elements in the icon files
const inputPathName = './public/icons_raw/'
const outputPathName = './public/icons_proc/'
class StyleRemove extends Transform {
constructor() {
super()
this.buffer = []
this.index = 0
}
checkBuffer() {
const chars=['s', 't', 'y', 'l', 'e']
let equal = true
for (let c = 0; c < chars.length && equal; c++) {
equal = this.buffer[this.buffer.length - chars.length + c] === chars[c].charCodeAt()
}
return equal
}
_transform(chunk, enc, callback) {
if (chunk) {
for (let a = 0; a < chunk.length; a++) {
const charCode = chunk[a]
let char = String.fromCharCode(charCode)
if (this.checkBuffer()) {
this.passedStyle = true
// remove these characters from the buffer
this.buffer.splice(-5, 5)
}
if (!this.passedStyle) {
this.buffer.push(charCode)
} else if (!this.quoteChar) {
// check opening quote and store it for a
// match later
if (char === "'" || char === '"') {
this.quoteChar = char
}
} else if (this.quoteChar) {
// if quoteChar has been assigned then
// only reset after a match has been found
if (char === this.quoteChar) {
this.quoteChar = undefined
this.passedStyle = false
}
}
this.index ++
}
}
if (!this.passedStyle) {
// write out the buffer and clear the internal one
callback(null, Buffer.from(this.buffer))
this.buffer = []
}
}
}
async function processFile(fileName) {
const readStream = fs.createReadStream(inputPathName + '/' + fileName)
const styleRemove = new StyleRemove()
const outputStream = fs.createWriteStream(outputPathName + '/' + fileName)
readStream.pipe(styleRemove).pipe(outputStream)
}
async function processDir() {
try {
const files = await fs.promises.readdir(inputPathName)
for (const file of files) {
processFile(file)
}
} catch (err) {
console.log(err)
}
}
processDir()