-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·128 lines (100 loc) · 4.31 KB
/
index.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
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
#!/usr/bin/env node
const xml = require("node-xml-lite");
const fs = require("fs");
const path = require("path");
const util = require("util");
const svg2png = require("svg2png");
const dirExcludes = ['node_modules', '.git'];
function renderPrettyDate(uglyDate) {
return uglyDate.toISOString();
}
function parseDimension(dimensionValue, dimensionName, directoryName, imageName) {
const dimensionAsString = dimensionValue;
if (!dimensionAsString.endsWith("px")) {
throw new Error(dimensionName + " must end with px, but it rather is: " + dimensionAsString);
}
const dimensionAsInt = parseInt(dimensionAsString)
if (dimensionAsInt == NaN) {
throw new Error(dimensionName + " must be integer, but it rather is: " + dimensionAsString);
}
return dimensionAsInt
}
function getLastModifiedDateTimeForFile(fileName) {
const stats = fs.statSync(fileName)
return new Date(util.inspect(stats.mtime))
}
function convertSingleSVG2PNGWithAmplification(svgData, directoryName, imageName, amplification) {
try {
if (!(svgData.buffer)) {
// Determine the SVG file name
const svgFileName = path.resolve(directoryName, imageName + '.svg')
// Read file
const svgBuffer = fs.readFileSync(svgFileName)
// Read as XML and get width and height
const svgXml = xml.parseBuffer(svgBuffer)
const width = parseDimension(svgXml.attrib.width, "Width", directoryName, imageName)
const height = parseDimension(svgXml.attrib.height, "Height", directoryName, imageName)
// Get last modified date/time
const lastModified = getLastModifiedDateTimeForFile(svgFileName)
Object.assign(svgData, {
buffer: svgBuffer,
width,
height,
lastModified
})
console.log("Processing SVG: " + svgFileName + ", last modified: " + renderPrettyDate(lastModified))
}
// Determine PNG file name
const amplificationString = amplification > 1 ? '@' + amplification + 'x' : ''
const pngFileName = path.resolve(directoryName, imageName + amplificationString + '.png')
// Determine last modified and compare to SVG. Use 1/1/1970 if not found so that SVG appears older
let pngLastModified = null
try {
pngLastModified = getLastModifiedDateTimeForFile(pngFileName)
} catch (err) {
if (err.code === 'ENOENT') {
pngLastModified = new Date(0);
} else {
throw err
}
}
if (pngLastModified <= svgData.lastModified) {
const width = amplification * svgData.width
const height = amplification * svgData.height
console.log("Rendering PNG: " + pngFileName + ", width: " + width + ", height: " + height)
const pngBuffer = svg2png.sync(svgData.buffer, {
width,
height
})
fs.writeFileSync(pngFileName, pngBuffer)
} else {
console.log("Skipping PNG: " + pngFileName + ", last modified: " + renderPrettyDate(pngLastModified))
}
} catch (err) {
throw new Error("Could not convert " + directoryName + "." + imageName + ".svg because: " + err.message)
}
}
function convertSingleSVG2PNG(directoryName, svgName) {
const imageName = svgName.substring(0, svgName.length - 4)
const svgData = {}
convertSingleSVG2PNGWithAmplification(svgData, directoryName, imageName, 1)
convertSingleSVG2PNGWithAmplification(svgData, directoryName, imageName, 2)
convertSingleSVG2PNGWithAmplification(svgData, directoryName, imageName, 3)
}
function searchAndConvertSVG2PNG(directoryName) {
const dirItems = fs.readdirSync(directoryName);
//console.log(dirItems);
dirItems.forEach(fileName => {
if (dirExcludes.indexOf(fileName) !== -1) {
return false;
} else if (fs.statSync(directoryName + fileName).isDirectory()) {
const subDir = path.resolve(directoryName, fileName) + '/';
searchAndConvertSVG2PNG(subDir);
} else if (fileName.endsWith('.svg')) {
convertSingleSVG2PNG(directoryName, fileName)
} else {
return true;
}
});
}
searchAndConvertSVG2PNG('./');