forked from baotlake/office-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_html.js
More file actions
46 lines (38 loc) · 1.59 KB
/
update_html.js
File metadata and controls
46 lines (38 loc) · 1.59 KB
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
const fs = require('fs');
const path = require('path');
function getAllHtmlFiles(dir, fileList = []) {
const files = fs.readdirSync(dir);
files.forEach(file => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
getAllHtmlFiles(filePath, fileList);
} else if (filePath.endsWith('.html')) {
fileList.push(filePath);
}
});
return fileList;
}
const targetDir = path.join(process.cwd(), 'public/v9.3.1-1');
const htmlFiles = getAllHtmlFiles(targetDir);
htmlFiles.forEach(file => {
let content = fs.readFileSync(file, 'utf8');
// Replace v9.3.0.24-1 with v9.3.1-1
const oldVersion = 'v9.3.0.24-1';
const newVersion = 'v9.3.1-1';
content = content.split(oldVersion).join(newVersion);
// Calculate new base href
// File: /Users/dev/Documents/office/public/v9.3.1-1/web-apps/apps/api/documents/preload.html
// Desired: /v9.3.1-1/web-apps/apps/api/documents/
const relativePath = path.relative(path.join(process.cwd(), 'public'), path.dirname(file));
let baseHref = '/' + relativePath.replace(/\\/g, '/') + '/';
// Update or insert <base href="...">
const baseHrefRegex = /<base\s+href="[^"]*"/i;
if (baseHrefRegex.test(content)) {
content = content.replace(baseHrefRegex, `<base href="${baseHref}"`);
} else {
// Insert after <head>
content = content.replace(/<head>/i, `<head>\n <base href="${baseHref}">`);
}
fs.writeFileSync(file, content);
console.log(`Updated: ${file} -> base href="${baseHref}"`);
});