Skip to content

Commit

Permalink
Backport prettier update (#4372)
Browse files Browse the repository at this point in the history
* chore: update noIndex logic

* ci: update noindex job

(cherry picked from commit 323fdec)
  • Loading branch information
karl-cardenas-coding committed Oct 18, 2024
1 parent f509981 commit 85df38f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/versions_robot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 7 additions & 13 deletions scripts/noindex_docusaurus_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.');
Expand All @@ -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);
}

0 comments on commit 85df38f

Please sign in to comment.