@@ -20,12 +20,78 @@ const path = require('path');
20
20
const readline = require ( 'readline' ) ;
21
21
22
22
async function main ( ) {
23
+ await applyExtras ( ) ;
24
+ await fixHomePage ( ) ;
25
+ await fixTitles ( ) ;
26
+ }
27
+
28
+ /**
29
+ * Adds extra content to the generated markdown files. Content in each file in the `extras/`
30
+ * directory is added/merged to the top of the corresponding markdown file in the `markdown/`
31
+ * directory.
32
+ */
33
+ async function applyExtras ( ) {
23
34
const extras = await getExtraFiles ( ) ;
24
35
for ( const source of extras ) {
25
36
await applyExtraContentFrom ( source ) ;
26
37
}
27
38
}
28
39
40
+ /**
41
+ * Replace dotted module names in the home page with the correct slash-separated names. For
42
+ * example, `firebase-admin.foo` becomes `firebase-admin/foo`. Also replaces the term "Package"
43
+ * with "Module" for accuracy.
44
+ */
45
+ async function fixHomePage ( ) {
46
+ const homePage = path . join ( __dirname , 'markdown' , 'index.md' ) ;
47
+ const content = await fs . readFile ( homePage ) ;
48
+ const updatedText = content . toString ( )
49
+ . replace ( / \[ f i r e b a s e - a d m i n \. / g, '[firebase-admin/' )
50
+ . replace ( / _ p a c k a g e / g, '_module' )
51
+ . replace ( / P a c k a g e / g, 'Module' ) ;
52
+ console . log ( `Updating module listings in ${ homePage } ` ) ;
53
+ await fs . writeFile ( homePage , updatedText ) ;
54
+ }
55
+
56
+ /**
57
+ * Replaces dotted module names and the term "package" in page titles. For example, the title text
58
+ * `firebase-admin.foo package` becomes `firebase-admin/foo module`.
59
+ */
60
+ async function fixTitles ( ) {
61
+ const markdownDir = path . join ( __dirname , 'markdown' ) ;
62
+ const files = await fs . readdir ( markdownDir ) ;
63
+ for ( const file of files ) {
64
+ await fixTitleOf ( path . join ( markdownDir , file ) ) ;
65
+ }
66
+ }
67
+
68
+ async function fixTitleOf ( file ) {
69
+ const reader = readline . createInterface ( {
70
+ input : fs . createReadStream ( file ) ,
71
+ } ) ;
72
+
73
+ const buffer = [ ] ;
74
+ let updated = false ;
75
+ for await ( let line of reader ) {
76
+ if ( line . startsWith ( '{% block title %}' ) ) {
77
+ if ( line . match ( / f i r e b a s e - a d m i n \. / ) ) {
78
+ line = line . replace ( / f i r e b a s e - a d m i n \. / , 'firebase-admin/' ) . replace ( 'package' , 'module' ) ;
79
+ updated = true ;
80
+ } else {
81
+ break ;
82
+ }
83
+ }
84
+
85
+ buffer . push ( line ) ;
86
+ }
87
+
88
+ if ( updated ) {
89
+ console . log ( `Updating title in ${ file } ` ) ;
90
+ const content = Buffer . from ( buffer . join ( '\r\n' ) ) ;
91
+ await fs . writeFile ( file , content ) ;
92
+ }
93
+ }
94
+
29
95
async function getExtraFiles ( ) {
30
96
const extrasPath = path . join ( __dirname , 'extras' ) ;
31
97
const files = await fs . readdir ( extrasPath ) ;
@@ -45,6 +111,18 @@ async function applyExtraContentFrom(source) {
45
111
await writeExtraContentTo ( target , extra ) ;
46
112
}
47
113
114
+ async function readExtraContentFrom ( source ) {
115
+ const reader = readline . createInterface ( {
116
+ input : fs . createReadStream ( source ) ,
117
+ } ) ;
118
+ const content = [ '' ] ;
119
+ for await ( const line of reader ) {
120
+ content . push ( line ) ;
121
+ }
122
+
123
+ return content ;
124
+ }
125
+
48
126
async function writeExtraContentTo ( target , extra ) {
49
127
const output = [ ] ;
50
128
const reader = readline . createInterface ( {
@@ -62,18 +140,6 @@ async function writeExtraContentTo(target, extra) {
62
140
await fs . writeFile ( target , outputBuffer ) ;
63
141
}
64
142
65
- async function readExtraContentFrom ( source ) {
66
- const reader = readline . createInterface ( {
67
- input : fs . createReadStream ( source ) ,
68
- } ) ;
69
- const content = [ '' ] ;
70
- for await ( const line of reader ) {
71
- content . push ( line ) ;
72
- }
73
-
74
- return content ;
75
- }
76
-
77
143
( async ( ) => {
78
144
try {
79
145
await main ( ) ;
0 commit comments