-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (69 loc) · 2.12 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
var cp = require('child_process');
var fs = require('fs');
var path = require('path');
function build() {
removeDistFolder();
minify();
copy3rdPartyPlugins();
changeHtmlImgExtensions();
openIndexHtml();
}
function removeDistFolder() {
var distPath = path.join(__dirname, 'dist');
if (fs.existsSync(distPath)) {
fs.rmSync(distPath, { recursive: true });
} else {
console.log('dist folder does not exist');
}
}
function minify() {
cp.execSync('npm run grunt', function (err, stdout, stderr) {
if (err) {
console.log(err);
return;
}
console.log('Minified');
});
}
function copy3rdPartyPlugins() {
cp.exec('npm run copy:3rdplugins', function (err, stdout, stderr) {
if (err) {
// Even though the operation is successful, it gives an error, so console.log was removed.
return;
}
console.log('Copied 3rd party plugins');
});
}
function changeHtmlImgExtensions() {
var distPath = path.join(__dirname, 'dist');
fs.readdirSync(distPath).forEach(file => {
if (path.extname(file) === '.html') {
var filePath = path.join(distPath, file);
var content = fs.readFileSync(filePath, 'utf8');
var updatedContent = content.replace(/<img\s+[^>]*src="([^"]+)\.(jpg|jpeg|png)"/g, (match, p1, p2) => {
return match.replace(p1 + '.' + p2, p1 + '.webp');
});
fs.writeFileSync(filePath, updatedContent, 'utf8');
}
});
}
async function openIndexHtml() {
var distPath = path.join(__dirname, 'dist');
var indexHtmlPath = path.join(distPath, 'index.html');
await sleep(1000);
if (fs.existsSync(indexHtmlPath)) {
cp.exec('start ' + indexHtmlPath, function (err, stdout, stderr) {
if (err) {
console.log(err);
return;
}
console.log('Opened index.html');
});
} else {
console.log('index.html does not exist');
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
build();