Skip to content

Commit

Permalink
created create-docs-website-pr-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
kyzooghost committed Dec 17, 2024
1 parent 346f4c8 commit 729db03
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
47 changes: 47 additions & 0 deletions contracts/docs/scripts/create-docs-website-pr-branch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# ASSUMPTIONS
# Must execute this script from within linea-monorepo

# CONSTANTS
DOCS_WEBSITE_REPO_NAME=doc.linea
DOCS_WEBSITE_REPO_GIT_LINK=https://github.com/Consensys/$DOCS_WEBSITE_REPO_NAME.git
DOCS_REPO_PR_BRANCH_NAME=update-linea-smart-contract-docs
DOCS_REPO_PR_COMMIT_MESSAGE="Update Linea smart contract docs"

UPDATE_SIDEBAR_SCRIPT_NAME=updateSidebar.js
UPDATE_SIDEBAR_SCRIPT_PATH=contracts/docs/scripts/$UPDATE_SIDEBAR_SCRIPT_NAME
MONOREPO_SMART_CONTRACT_DOCS_DIRECTORY=contracts/docs/api

DOCS_REPO_SMART_CONTRACT_DOC_DIRECTORY=docs/api/linea-smart-contracts

# Ensure that `contracts` is the working directory
MONOREPO_ROOT_PATH=$(git rev-parse --show-toplevel)
cd $MONOREPO_ROOT_PATH
cd contracts

# Docgen
npx hardhat docgen

# Git clone docs website and create new branch
cd $MONOREPO_ROOT_PATH
cd ..
git clone $DOCS_WEBSITE_REPO_GIT_LINK
cd $DOCS_WEBSITE_REPO_NAME
DOCS_WEBSITE_REPO_PATH=$(pwd)
git checkout -b $(DOCS_REPO_PR_BRANCH_NAME)

# Copy autogenerated smart contract docs + updateSidebar.js to docs website repo
cd $MONOREPO_ROOT_PATH
cp "$MONOREPO_ROOT_PATH/$MONOREPO_SMART_CONTRACT_DOCS_DIRECTORY" "$DOCS_WEBSITE_REPO_PATH/$DOCS_REPO_SMART_CONTRACT_DOC_DIRECTORY"
cp "$MONOREPO_ROOT_PATH/$UPDATE_SIDEBAR_SCRIPT_PATH" "$DOCS_WEBSITE_REPO_PATH/$UPDATE_SIDEBAR_SCRIPT_NAME"

# Execute updateSidebar script, then delete script (so it is not in the commit)
cd $DOCS_WEBSITE_REPO_PATH
node $UPDATE_SIDEBAR_SCRIPT_NAME
rm $UPDATE_SIDEBAR_SCRIPT_NAME

# Git commit and push
git commit -m $DOCS_REPO_PR_COMMIT_MESSAGE
# git push -u origin

111 changes: 111 additions & 0 deletions contracts/docs/scripts/updateSidebar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* eslint-disable */
// This script is meant to be executed in the root directory of https://github.com/Consensys/doc.linea, which has different linting rules
// The purpose of this script is to modify the sidebars.js file to correctly include the autogenerated smart contract documentation

const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");

const SIDEBAR_FILE_PATH = process.env.SIDEBAR_FILE_PATH ?? "sidebars.js";

// Get JSON object from sidebar
const sidebars = require(path.join(__dirname, SIDEBAR_FILE_PATH));

// TYPES
class FolderSidebar {
type = "category";
label = "";
link = null;
collapsible = true;
items = [];

constructor(label = "", collapsible = false) {
this.label = label;
this.collapsible = collapsible;
}
}

// MAIN

main();

function main() {
// Create and populate smart contract sidebar
const smartContractsPath = path.join(
__dirname,
"docs",
"api",
"linea-smart-contracts",
);

let smartContractSidebarNode = new FolderSidebar(
"Linea Smart Contracts",
true,
);

smartContractSidebarNode = getFileTree(
smartContractSidebarNode,
smartContractsPath,
".mdx",
);

// Push smart contract sidebar to main sidebar object
sidebars?.apiSidebar?.push(smartContractSidebarNode);

// Create new js file
const sidebarFileLine1 =
"/** @type {import('@docusaurus/plugin-content-docs').SidebarsConfig} */";
const sidebarFileLine2 = "const sidebars =";
const sidebarFileLineFinal = "module.exports = sidebars;";

const newSidebarFileContent = `${sidebarFileLine1}\n${sidebarFileLine2}\n${JSON.stringify(sidebars, null, 2)}\n\n${sidebarFileLineFinal}`;

// Save new js file
const newSidebarFilePath = path.join(__dirname, "sidebar.tmp.js");

fs.writeFileSync(newSidebarFilePath, newSidebarFileContent);

// Lint the file
lintJSFile(newSidebarFilePath);
}

// HELPER FUNCTIONS

// Recursive function to populate sidebar object for a given folder
function getFileTree(nodePointer, subdirectoryPath, fileExtension) {
const subdirectoryFileList = fs.readdirSync(subdirectoryPath);

for (const fileNode of subdirectoryFileList) {
const filePath = path.join(subdirectoryPath, fileNode);
const fileMetadata = fs.statSync(filePath);

// Directory => Create folder node, recurse then add folder node
if (fileMetadata.isDirectory()) {
let newFolderNode = new FolderSidebar(fileNode);
newFolderNode = getFileTree(newFolderNode, filePath, fileExtension);
nodePointer?.items.push(newFolderNode);

// Base case *.mdx file => Add relative path
} else if (fileMetadata.isFile() && fileNode.endsWith(fileExtension)) {
const relativePath = path.relative(
path.join(__dirname, "docs"),
filePath.split(fileExtension)[0],
);
nodePointer?.items.push(relativePath);
}
}

return nodePointer;
}

function lintJSFile(filePath) {
try {
const cmd = `npx eslint --fix ${filePath}`;
// Execute command synchronously and route output directly to the current stdout
execSync(cmd, { stdio: "inherit" });
} catch (error) {
console.error(`Error:`, error.message);
console.error(`Exiting...`);
process.exit(1);
}
}

0 comments on commit 729db03

Please sign in to comment.