Skip to content

Commit fa1aae2

Browse files
committed
Create local cache for IB
1 parent d67f483 commit fa1aae2

File tree

7 files changed

+166
-110
lines changed

7 files changed

+166
-110
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ npm-debug.log*
1919
yarn-debug.log*
2020
yarn-error.log*
2121
.env
22+
.integrationBuilderCache

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77

88
# Misc
99
/files
10+
.integrationBuilderCache

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,8 @@
4343
},
4444
"[javascript]": {
4545
"editor.defaultFormatter": "vscode.typescript-language-features"
46+
},
47+
"[typescriptreact]": {
48+
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
4649
}
4750
}

docusaurus.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ const config: Config = {
188188
],
189189
plugins: [
190190
path.resolve(__dirname, "plugins", "docusaurus-plugin-content-hub"),
191-
[path.resolve(__dirname, "plugins", "docusaurus-plugin-virtual-files"), { rootDir: "files" }],
191+
[path.resolve(__dirname, "plugins", "docusaurus-plugin-virtual-files"), { rootDir: ".integrationBuilderCache" }],
192192
path.resolve(__dirname, "plugins", "node-polyfills"),
193193
[
194194
path.resolve(__dirname, "plugins", "plugin-dynamic-route"),

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
"eslint-plugin-simple-import-sort": "^10.0.0",
7575
"eslint-plugin-tsdoc": "^0.2.16",
7676
"husky": "^8.0.3",
77+
"fs": "^0.0.1-security",
78+
"glob": "^8.0.3",
7779
"lint-staged": "^14.0.1",
7880
"path-browserify": "^1.0.1",
7981
"prettier": "^3.1.0",
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,66 @@
11
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+
225
const hostedFileLinks = require("../../src/common/hostedFileLinks.json");
326
module.exports = (context, options) => ({
427
name: "docusaurus-plugin-virtual-files",
528
async loadContent() {
29+
const dir = path.resolve(context.siteDir, options.rootDir);
630
const filenames = Object.values(hostedFileLinks);
731
const fileContents = {};
832

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;
1757
}
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);
2061
}
21-
fileContents[filename] = data;
2262
}
63+
2364
return fileContents;
2465
},
2566
async contentLoaded({ content, actions }) {
@@ -34,3 +75,11 @@ module.exports = (context, options) => ({
3475
});
3576
},
3677
});
78+
79+
module.exports.validateOptions = ({ options, validate }) =>
80+
validate(
81+
joi.object({
82+
rootDir: joi.string().required(),
83+
}),
84+
options
85+
);

0 commit comments

Comments
 (0)