From 85df38f5509d1102e7e581e36c15d36c7845fc96 Mon Sep 17 00:00:00 2001 From: Karl Cardenas <29551334+karl-cardenas-coding@users.noreply.github.com> Date: Fri, 18 Oct 2024 16:58:39 -0700 Subject: [PATCH] Backport prettier update (#4372) * chore: update noIndex logic * ci: update noindex job (cherry picked from commit 323fdec9d5d818410cf41c84b868eb7ad1da53f3) --- .github/workflows/versions_robot.yaml | 2 +- scripts/noindex_docusaurus_config.js | 20 +++++++------------- 2 files changed, 8 insertions(+), 14 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..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. @@ -18,35 +17,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 +50,12 @@ const addNoIndexProperty = () => { addNoIndexProperty(); -// This is where the new config object is converted back into code. +// Generate the updated code from the AST and overwrite the original config file const updatedCode = generate(ast).code; try { - // Lastly, this is where the new config object is written to the temp.docusaurus.config.js file. - fs.writeFileSync(`${tempDirectory}/temp.docusaurus.config.js`, updatedCode); + // 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("Could not write to temp.docusaurus.config.js:", err); + console.error("Could not write to docusaurus.config.js:", err); }