From 0dcd4b9a841d6a452e4e975b10e6e5d1e7b54942 Mon Sep 17 00:00:00 2001 From: Karl Cardenas Date: Fri, 18 Oct 2024 16:27:25 -0700 Subject: [PATCH 1/2] chore: update noIndex logic --- .github/workflows/versions_robot.yaml | 2 +- scripts/noindex_docusaurus_config.js | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/.github/workflows/versions_robot.yaml b/.github/workflows/versions_robot.yaml index e38aec6660..83dc1c1d18 100644 --- a/.github/workflows/versions_robot.yaml +++ b/.github/workflows/versions_robot.yaml @@ -83,7 +83,7 @@ jobs: - name: Ensure noIndex is set id: check_noindex - run: node scripts/noindex_docusaurus_config.js $RUNNER_TEMP $PWD && mv $RUNNER_TEMP/temp.docusaurus.config.js $PWD/docusaurus.config.js + run: node scripts/noindex_docusaurus_config.js $RUNNER_TEMP $PWD - name: Auto Format (Prettier) run: make format diff --git a/scripts/noindex_docusaurus_config.js b/scripts/noindex_docusaurus_config.js index 7a24d5112c..fbf80c7a70 100644 --- a/scripts/noindex_docusaurus_config.js +++ b/scripts/noindex_docusaurus_config.js @@ -18,35 +18,29 @@ const ast = parser.parse(configCode, { // Function to add noIndex: true after trailingSlash if not already present const addNoIndexProperty = () => { - // Find the main "config" declaration const configDeclaration = ast.program.body.find( (node) => node.type === "VariableDeclaration" && node.declarations[0].id.name === "config" ); - // If the "config" declaration is not found, log an error and return if (!configDeclaration) { console.error('Could not locate the main "config" declaration.'); return; } - // Check if the "noIndex" property already exists const noIndexExists = configDeclaration.declarations[0].init.properties.some((prop) => prop.key.name === "noIndex"); if (noIndexExists) { console.log('"noIndex" property already exists in the config.'); - return; // Do nothing if noIndex already exists + return; } - // Find the "trailingSlash" property in the "config" declaration const trailingSlashProperty = configDeclaration.declarations[0].init.properties.find( (prop) => prop.key.name === "trailingSlash" ); - // If "trailingSlash" is found, insert "noIndex: true" after it if (trailingSlashProperty) { const noIndexProperty = t.objectProperty(t.identifier("noIndex"), t.booleanLiteral(true)); - // Find the index of the trailingSlash property and insert noIndex after it const index = configDeclaration.declarations[0].init.properties.indexOf(trailingSlashProperty); configDeclaration.declarations[0].init.properties.splice(index + 1, 0, noIndexProperty); console.log('"noIndex" property added to the config.'); @@ -57,11 +51,16 @@ const addNoIndexProperty = () => { addNoIndexProperty(); -// This is where the new config object is converted back into code. const updatedCode = generate(ast).code; try { - // Lastly, this is where the new config object is written to the temp.docusaurus.config.js file. + // Write the new config object to the temp.docusaurus.config.js file fs.writeFileSync(`${tempDirectory}/temp.docusaurus.config.js`, updatedCode); + console.log("Temp file created."); + + // Now replace the original docusaurus.config.js with the temp file's content + const tempConfigCode = fs.readFileSync(`${tempDirectory}/temp.docusaurus.config.js`, "utf8"); + fs.writeFileSync(`${baseDirectory}/${docusaurusConfigFile}`, tempConfigCode); + console.log("Original docusaurus.config.js replaced with the updated content."); } catch (err) { - console.error("Could not write to temp.docusaurus.config.js:", err); + console.error("An error occurre when writing the updated config object to the real docusaurus.config.js file."); } From 64b3effba47d04a13fdc1e076ff486ec12a677bb Mon Sep 17 00:00:00 2001 From: Karl Cardenas Date: Fri, 18 Oct 2024 16:28:32 -0700 Subject: [PATCH 2/2] ci: update noindex job --- scripts/noindex_docusaurus_config.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/scripts/noindex_docusaurus_config.js b/scripts/noindex_docusaurus_config.js index fbf80c7a70..de8a31c87b 100644 --- a/scripts/noindex_docusaurus_config.js +++ b/scripts/noindex_docusaurus_config.js @@ -4,8 +4,7 @@ const generate = require("@babel/generator").default; const t = require("@babel/types"); const docusaurusConfigFile = "docusaurus.config.js"; -const tempDirectory = process.argv[2]; -const baseDirectory = process.argv[3]; +const baseDirectory = process.argv[2]; // Remove the tempDirectory argument // This reads the docusaurus.config.js file and parses it into an AST. // We need to parse it into an AST so that we can add the new versions to the config object. @@ -51,16 +50,12 @@ const addNoIndexProperty = () => { addNoIndexProperty(); +// Generate the updated code from the AST and overwrite the original config file const updatedCode = generate(ast).code; try { - // Write the new config object to the temp.docusaurus.config.js file - fs.writeFileSync(`${tempDirectory}/temp.docusaurus.config.js`, updatedCode); - console.log("Temp file created."); - - // Now replace the original docusaurus.config.js with the temp file's content - const tempConfigCode = fs.readFileSync(`${tempDirectory}/temp.docusaurus.config.js`, "utf8"); - fs.writeFileSync(`${baseDirectory}/${docusaurusConfigFile}`, tempConfigCode); - console.log("Original docusaurus.config.js replaced with the updated content."); + // Write directly to the original docusaurus.config.js file + fs.writeFileSync(`${baseDirectory}/${docusaurusConfigFile}`, updatedCode); + console.log("Original docusaurus.config.js updated with the new content."); } catch (err) { - console.error("An error occurre when writing the updated config object to the real docusaurus.config.js file."); + console.error("Could not write to docusaurus.config.js:", err); }