This repository has been archived by the owner on May 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmaintenance.js
executable file
·92 lines (73 loc) · 2.41 KB
/
maintenance.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
/*
* WARN: Run this before checking in new svgs to the repository!
* This scripts adds bounds and contains an svg within a square
*/
const fs = require('fs');
const xml2js = require('xml2js');
const file = 'package_signs/' + process.argv[2].replace(/\u001b\[.*?m/g, '')
const applyChanges = (result, out, filename) => {
result.svg.$.width = out.width;
result.svg.$.height = out.height;
result.svg.$.viewBox = out.viewBox;
result.svg.rect = {
$: {
width: '100%',
height: '100%',
}
}
if (result.svg.g && result.svg.g[0]) {
console.log(' [INFO]: g', filename)
result.svg.g[0].$.transform = out.transform;
} else if (result.svg.path[0].$) {
console.log(' [INFO]: paths', filename)
for (let path of result.svg.path) {
path['$']['transform'] = out.transform;
}
} else {
console.log(' [INFO]: else', filename)
}
return result;
}
const effs = function (filename) {
const file = fs.readFileSync(filename, 'utf8');
xml2js.parseString(file, function (err, result) {
let w, h, g;
try {
w = Number(result.svg.$.width);
h = Number(result.svg.$.height);
viewBox = result.svg.$.viewBox.split(' ');
if (w == h) {
process.exit(0)
}
let width, height, translate;
if (w > h) {
width = w;
height = w;
translate = [0, (w - h) / 2].join(' ')
} else {
width = h;
height = h;
translate = [(h - w) / 2, 0].join(' ')
}
viewBox[2] = width;
viewBox[3] = height;
// Final values
const out = {
w, h, width, height,
viewBox: viewBox.join(' '),
transform: `translate(${translate})`
}
const changes = applyChanges(result, out, filename);
const builder = new xml2js.Builder();
const xml = builder.buildObject(changes);
const outName = filename.split('/')
fs.writeFileSync('./package_signs/' + outName[outName.length - 1], xml, 'utf8', function (err, data) {
})
} catch (e) {
console.log(' [ERR] ', e);
process.exit(0)
}
});
process.exit(0)
}
effs(file)