generated from eea/volto-frontend-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap
213 lines (187 loc) · 6.59 KB
/
bootstrap
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
const fs = require('fs');
const path = require('path');
const ejs = require('ejs');
const { createReadStream, createWriteStream } = require('fs');
const { pipeline } = require('stream');
const sharp = require('sharp');
const pngToIco = require('png-to-ico');
const xml2js = require('xml2js');
const currentDir = path.basename(process.cwd());
const inputImagePath = 'public/icon.png';
function isKnownBinaryExtension(extension) {
const binaryExtensions = ['.png', '.jpg', '.gif', '.svg', '.ico', '.pdf']; // add more if needed
return binaryExtensions.includes(extension);
}
function bootstrap(ofile) {
fs.readFile(ofile, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
const output = ofile.replace('.tpl', '');
const isBinary = isKnownBinaryExtension(path.extname(output));
const result = isBinary
? data
: ejs.render(data, {
name: currentDir,
});
fs.writeFile(output, result, 'utf8', function (err) {
if (err) {
return console.log(err);
}
});
fs.unlink(ofile, (err) => {
if (err) {
return console.error(err);
}
});
});
}
function parse_tpl(folder) {
fs.readdir(folder, { withFileTypes: true }, (err, dirents) => {
dirents.forEach(function (file) {
const ext = path.extname(file.name);
if (file.isFile() && file.name != 'bootstrap' && ext == '.tpl') {
bootstrap(`${folder}/${file.name}`);
}
if (file.isDirectory() && !['.git', 'node_modules'].includes(file.name)) {
parse_tpl(`${folder}/${file.name}`);
}
});
});
}
// Modifing the width and height of the SVG for better quality images
function resizeSVG() {
fs.readFile('public/icon.svg.tpl', 'utf8', function (err, data) {
if (err) {
console.log('Could not open file: %s', err);
return;
}
xml2js.parseString(data, function (err, result) {
if (err) {
console.log('Could not parse SVG: %s', err);
return;
}
// Assume that the root SVG element is result.svg and it has width and height attributes
if (result.svg.$) {
// Set new width and height
result.svg.$.width = '512';
result.svg.$.height = '512';
// Build an XML string (SVG)
var builder = new xml2js.Builder();
let newSvg = builder.buildObject(result);
fs.writeFile('public/icon.svg.tpl', newSvg, function (err) {
if (err) {
console.log('Could not write file: %s', err);
} else {
console.log('SVG file was saved!');
convertSVGtoPNG('public/icon.svg.tpl', 'public/icon.png');
}
});
} else {
console.log('SVG root element or its attributes not found.');
}
});
});
}
// Convert SVG to PNG
function convertSVGtoPNG(svgPath, pngPath) {
const promise = sharp(svgPath).png().toFile(pngPath);
// If the convertSVGtoPNG function is called with parameters from cli, don't call the resizePNG function
if (svgPath !== process.argv[3] && pngPath !== process.argv[4]) {
promise.then(function () {
resizePNG();
});
}
return promise;
}
function resizePNG() {
// Create a readable stream from the input image file
const readStream = createReadStream(inputImagePath);
// Generate ICO file
const outputIcoPath = 'public/favicon.ico';
pngToIco(inputImagePath, outputIcoPath)
.then((buf) => {
fs.writeFileSync(outputIcoPath, buf);
console.log('PNG image converted to ICO:', outputIcoPath);
})
.catch((err) => {
console.error('Error converting PNG to ICO:', err);
});
// Dimensions for resizing favicons
const dimensionsFavicons = [
{ width: 16, height: 16, title: 'favicon', opacity: '0' },
{ width: 32, height: 32, title: 'favicon', opacity: '0' },
{ width: 192, height: 192, title: 'android-chrome', opacity: '100' },
{ width: 512, height: 512, title: 'android-chrome', opacity: '100' },
{ width: 180, height: 180, title: 'apple-touch-icon', opacity: '100' },
// Add more dimensions as needed
];
// Resize and create new files
dimensionsFavicons.forEach((dimension, index) => {
const { width, height, title, opacity } = dimension;
const outputImagePath = title !== 'apple-touch-icon' ? `public/${title}-${width}x${height}.png` : `public/${title}.png`;
// Create a writable stream to the output image file
const writeStream = createWriteStream(outputImagePath);
// Use sharp to resize the image
const transformer = sharp()
.resize(width, height)
.flatten({
background: opacity === '100' ? { r: 255, g: 255, b: 255 } : 'transparent',
});
// Pipeline to stream data from the input file to the output file with resizing
pipeline(readStream, transformer, writeStream, (err) => {
if (err) {
console.error('Error processing image:', err);
} else {
console.log(`Resized image ${index + 1}/${dimensionsFavicons.length} created: ${outputImagePath}`);
}
});
});
}
function resizePNGtoCustomDimension(inputPath, dimensionsArray, outputPath) {
const readStream = createReadStream(inputPath);
const writeStream = createWriteStream(outputPath);
const width = dimensionsArray.split(',')[0];
const height = dimensionsArray.split(',')[1];
const transformer = sharp().resize(parseInt(width), parseInt(height));
pipeline(readStream, transformer, writeStream, (err) => {
if (err) {
console.error('Error processing image:', err);
} else {
console.log(`Resized png. Created: ${outputPath}`);
}
});
}
function changeBackgroundColor(inputPath, rgbArray, outputPath) {
const readStream = createReadStream(inputPath);
const r = rgbArray.split(',')[0];
const g = rgbArray.split(',')[1];
const b = rgbArray.split(',')[2];
const writeStream = createWriteStream(outputPath);
const transformer = sharp().flatten({
background: { r: parseInt(r), g: parseInt(g), b: parseInt(b) },
});
pipeline(readStream, transformer, writeStream, (err) => {
if (err) {
console.error('Error processing image:', err);
} else {
console.log(`Backgound color changed. Created: ${outputPath}`);
}
});
}
const command = process.argv.length >= 2 ? process.argv[2] || 'default' : 'default';
switch (command) {
case 'default':
resizeSVG();
parse_tpl('.');
break;
case 'svgtopng':
convertSVGtoPNG(process.argv[3], process.argv[4]);
break;
case 'resizepng':
resizePNGtoCustomDimension(process.argv[3], process.argv[4], process.argv[5]);
break;
case 'chagebackgroundcolor':
changeBackgroundColor(process.argv[3], process.argv[4], process.argv[5]);
break;
}