1+ const fs = require ( 'fs' ) ;
2+ const path = require ( 'path' ) ;
3+
4+ async function updateSphinxThemeFiles ( ) {
5+ const siteConfigPath = path . join ( __dirname , './siteConfig.js' ) ;
6+ const staticDocsPath = path . join ( __dirname , 'static/docs' ) ;
7+
8+ let content = fs . readFileSync ( siteConfigPath , 'utf8' ) ;
9+ let lines = content . split ( '\n' ) ;
10+
11+ const pattern = / \W s t a t i c \/ s p h i n x _ i m m a t e r i a l _ t h e m e \. [ a - f 0 - 9 ] + \. m i n \. c s s \W / ;
12+ const insertLineIndex = lines . findIndex ( line => pattern . test ( line ) ) ;
13+
14+ if ( insertLineIndex === - 1 ) {
15+ console . error ( 'Could not find sphinx theme file references in siteConfig.js' ) ;
16+ process . exit ( 1 ) ;
17+ }
18+
19+ matchSet = new Set ( ) ;
20+ lines = lines . filter ( line => {
21+ if ( ! pattern . test ( line ) ) {
22+ return true ;
23+ } else {
24+ matchSet . add ( line . trim ( ) ) ;
25+ return false ;
26+ }
27+ } ) ;
28+
29+ const versionFolders = fs . readdirSync ( staticDocsPath )
30+ . filter ( folder => / ^ \d + \. \d + ( \. \d + ) ? $ / . test ( folder ) ) ;
31+
32+ if ( versionFolders . length === 0 ) {
33+ console . error ( 'No version folders found' ) ;
34+ process . exit ( 1 ) ;
35+ }
36+
37+ const allCssFiles = [ ] ;
38+ const fileSet = new Set ( ) ;
39+ for ( const version of versionFolders ) {
40+ const staticPath = path . join ( staticDocsPath , version , '_static' ) ;
41+ if ( fs . existsSync ( staticPath ) ) {
42+ const files = fs . readdirSync ( staticPath )
43+ . sort ( )
44+ . filter ( file => file . startsWith ( 'sphinx_immaterial_theme.' ) && file . endsWith ( '.min.css' ) )
45+ . filter ( file => {
46+ if ( fileSet . has ( file ) ) return false ;
47+ fileSet . add ( file ) ;
48+ return true ;
49+ } )
50+ . map ( file => ` "static/${ file } ",` ) ;
51+ allCssFiles . push ( ...files ) ;
52+ }
53+ }
54+
55+ if ( matchSet . size === allCssFiles . length &&
56+ allCssFiles . every ( file => matchSet . has ( file . trim ( ) ) ) ) {
57+ process . exit ( 1 ) ;
58+ }
59+
60+ console . info ( 'Found CSS files:' , allCssFiles ) ;
61+ lines . splice ( insertLineIndex , 0 , ...allCssFiles ) ;
62+
63+ fs . writeFileSync ( siteConfigPath , lines . join ( '\n' ) ) ;
64+ console . log ( 'Successfully updated sphinx theme file references' ) ;
65+ process . exit ( 0 ) ;
66+ }
67+
68+ updateSphinxThemeFiles ( ) . catch ( console . error ) ;
0 commit comments