1
1
const axios = require ( "axios" ) ;
2
+ const util = require ( "util" ) ;
3
+ const fs = require ( "fs" ) ;
4
+ const path = require ( "path" ) ;
5
+ const joi = require ( "joi" ) ;
6
+ const readFileAsync = util . promisify ( fs . readFile ) ;
7
+ const writeFileAsync = util . promisify ( fs . writeFile ) ;
8
+
9
+ const environment = process . env . NODE_ENV || "development" ;
10
+
11
+ async function fetchHostedFile ( filename ) {
12
+ try {
13
+ response = await axios . get ( "https:/raw.githubusercontent.com/" + filename ) ;
14
+ var fileContent = response . data ;
15
+ if ( typeof fileContent !== "string" ) {
16
+ fileContent = JSON . stringify ( fileContent , null , 2 ) ;
17
+ }
18
+ return fileContent ;
19
+ } catch ( e ) {
20
+ console . log ( `Error fetching ${ filename } : ${ e } ` ) ;
21
+ return "" ;
22
+ }
23
+ }
24
+
2
25
const hostedFileLinks = require ( "../../src/common/hostedFileLinks.json" ) ;
3
26
module . exports = ( context , options ) => ( {
4
27
name : "docusaurus-plugin-virtual-files" ,
5
28
async loadContent ( ) {
29
+ const dir = path . resolve ( context . siteDir , options . rootDir ) ;
6
30
const filenames = Object . values ( hostedFileLinks ) ;
7
31
const fileContents = { } ;
8
32
9
- for ( const filename of filenames ) {
10
- var data ;
11
- try {
12
- response = await axios . get ( filename ) ;
13
- data = response . data ;
14
- } catch ( e ) {
15
- data = "" ;
16
- console . log ( `Error fetching ${ filename } : ${ e } ` ) ;
33
+ if ( environment === "development" ) {
34
+ var data = "" ;
35
+ for ( const filename of filenames ) {
36
+ const filePath = path . join ( dir , filename . replaceAll ( "/" , "-" ) ) ;
37
+ const directoryPath = path . dirname ( filePath ) ;
38
+
39
+ try {
40
+ data = await readFileAsync ( filePath ) ;
41
+ } catch ( e ) {
42
+ console . log ( `Fetching ${ filename } since local cache not available` ) ;
43
+
44
+ data = await fetchHostedFile ( filename ) ;
45
+
46
+ try {
47
+ if ( ! fs . existsSync ( directoryPath ) ) {
48
+ fs . mkdirSync ( directoryPath , { recursive : true } ) ;
49
+ }
50
+ await writeFileAsync ( filePath , data ) ;
51
+ console . log ( `Saved ${ filename } to cache` ) ;
52
+ } catch ( error ) {
53
+ console . log ( `Error saving ${ filename } to cache` ) ;
54
+ }
55
+ }
56
+ fileContents [ filename ] = data ;
17
57
}
18
- if ( typeof data !== "string" ) {
19
- data = JSON . stringify ( data , null , 2 ) ;
58
+ } else {
59
+ for ( const filename of filenames ) {
60
+ fileContents [ filename ] = await fetchHostedFile ( filename ) ;
20
61
}
21
- fileContents [ filename ] = data ;
22
62
}
63
+
23
64
return fileContents ;
24
65
} ,
25
66
async contentLoaded ( { content, actions } ) {
@@ -34,3 +75,11 @@ module.exports = (context, options) => ({
34
75
} ) ;
35
76
} ,
36
77
} ) ;
78
+
79
+ module . exports . validateOptions = ( { options, validate } ) =>
80
+ validate (
81
+ joi . object ( {
82
+ rootDir : joi . string ( ) . required ( ) ,
83
+ } ) ,
84
+ options
85
+ ) ;
0 commit comments