-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathupdateSiteConfig.js
68 lines (56 loc) · 2.19 KB
/
updateSiteConfig.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
const fs = require('fs');
const path = require('path');
async function updateSphinxThemeFiles() {
const siteConfigPath = path.join(__dirname, './siteConfig.js');
const staticDocsPath = path.join(__dirname, 'static/docs');
let content = fs.readFileSync(siteConfigPath, 'utf8');
let lines = content.split('\n');
const pattern = /\Wstatic\/sphinx_immaterial_theme\.[a-f0-9]+\.min\.css\W/;
const insertLineIndex = lines.findIndex(line => pattern.test(line));
if (insertLineIndex === -1) {
console.error('Could not find sphinx theme file references in siteConfig.js');
process.exit(1);
}
matchSet = new Set();
lines = lines.filter(line => {
if ( !pattern.test(line) ){
return true;
} else {
matchSet.add(line.trim());
return false;
}
});
const versionFolders = fs.readdirSync(staticDocsPath)
.filter(folder => /^\d+\.\d+(\.\d+)?$/.test(folder));
if (versionFolders.length === 0) {
console.error('No version folders found');
process.exit(1);
}
const allCssFiles = [];
const fileSet = new Set();
for (const version of versionFolders) {
const staticPath = path.join(staticDocsPath, version, '_static');
if (fs.existsSync(staticPath)) {
const files = fs.readdirSync(staticPath)
.sort()
.filter(file => file.startsWith('sphinx_immaterial_theme.') && file.endsWith('.min.css'))
.filter(file => {
if ( fileSet.has(file) ) return false;
fileSet.add(file);
return true;
})
.map(file => ` "static/${file}",`);
allCssFiles.push(...files);
}
}
if (matchSet.size === allCssFiles.length &&
allCssFiles.every(file => matchSet.has(file.trim()))) {
process.exit(1);
}
console.info('Found CSS files:', allCssFiles);
lines.splice(insertLineIndex, 0, ...allCssFiles);
fs.writeFileSync(siteConfigPath, lines.join('\n'));
console.log('Successfully updated sphinx theme file references');
process.exit(0);
}
updateSphinxThemeFiles().catch(console.error);