From 04cec64fa1c6f7b6171e83cc2b71957eec16e7bb Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Thu, 24 Oct 2024 14:03:34 +0300 Subject: [PATCH] docs: show latest version in docs + update automatically (#9753) --- .github/workflows/docs-update-version.yml | 54 +++++++++++++ www/apps/api-reference/config/index.ts | 2 + www/apps/book/config/index.ts | 2 + www/apps/resources/config/index.ts | 2 + www/apps/ui/src/config/site.ts | 2 + www/apps/user-guide/config/index.ts | 2 + .../src/components/MainNav/Version/index.tsx | 65 +++++++++++++++ .../docs-ui/src/components/MainNav/index.tsx | 2 + www/packages/docs-ui/src/global-config.ts | 10 +++ www/packages/docs-ui/src/index.ts | 1 + .../src/providers/SiteConifg/index.tsx | 2 + www/packages/types/src/config.ts | 4 + .../packages/scripts/change-config-version.ts | 81 +++++++++++++++++++ www/utils/packages/scripts/package.json | 4 +- 14 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/docs-update-version.yml create mode 100644 www/packages/docs-ui/src/components/MainNav/Version/index.tsx create mode 100644 www/packages/docs-ui/src/global-config.ts create mode 100644 www/utils/packages/scripts/change-config-version.ts diff --git a/.github/workflows/docs-update-version.yml b/.github/workflows/docs-update-version.yml new file mode 100644 index 0000000000..ea300fb24c --- /dev/null +++ b/.github/workflows/docs-update-version.yml @@ -0,0 +1,54 @@ +name: Update Version in Docs +on: + release: + types: [published] + +jobs: + update-docs-version: + runs-on: ubuntu-latest + env: + GH_TOKEN: ${{ secrets.REFERENCE_PAT }} + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.11.0 + with: + access_token: ${{ github.token }} + + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Setup Node.js environment + uses: actions/setup-node@v3 + with: + node-version: 20 + cache: "yarn" + + - name: Install dependencies + uses: ./.github/actions/cache-deps + with: + extension: docs-update-version + + - name: Install Workspace dependencies + run: yarn install + working-directory: www/utils + + - name: Build Workspace packages + run: yarn build + working-directory: www/utils + + - name: Update Docs Version + run: yarn update:config-version + working-directory: www/utils/packages/scripts + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v4 + with: + commit-message: "chore(docs): Update version in documentation (automated)" + base: "develop" + title: "chore(docs): Update version in documentation (automated)" + labels: "type: chore" + add-paths: www/packages/docs-ui/src/global-config.ts + branch: "docs/update-config-version" + branch-suffix: "timestamp" \ No newline at end of file diff --git a/www/apps/api-reference/config/index.ts b/www/apps/api-reference/config/index.ts index b4a875caf6..917be855b6 100644 --- a/www/apps/api-reference/config/index.ts +++ b/www/apps/api-reference/config/index.ts @@ -1,8 +1,10 @@ +import { globalConfig } from "docs-ui" import { DocsConfig } from "types" const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000" export const config: DocsConfig = { + ...globalConfig, baseUrl, basePath: process.env.NEXT_PUBLIC_BASE_PATH, // sidebar is auto generated diff --git a/www/apps/book/config/index.ts b/www/apps/book/config/index.ts index 43c73a685a..1807b17682 100644 --- a/www/apps/book/config/index.ts +++ b/www/apps/book/config/index.ts @@ -1,9 +1,11 @@ import { DocsConfig } from "types" import { sidebarConfig } from "./sidebar" +import { globalConfig } from "docs-ui" const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000" export const config: DocsConfig = { + ...globalConfig, titleSuffix: "Medusa v2 Documentation", baseUrl, basePath: process.env.NEXT_PUBLIC_BASE_PATH, diff --git a/www/apps/resources/config/index.ts b/www/apps/resources/config/index.ts index caf5a1006f..0b160b1e27 100644 --- a/www/apps/resources/config/index.ts +++ b/www/apps/resources/config/index.ts @@ -1,9 +1,11 @@ import { DocsConfig, SidebarItem } from "types" import { generatedSidebar } from "../generated/sidebar.mjs" +import { globalConfig } from "docs-ui" const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000" export const config: DocsConfig = { + ...globalConfig, titleSuffix: "Medusa Development Resources", baseUrl, basePath: process.env.NEXT_PUBLIC_BASE_PATH, diff --git a/www/apps/ui/src/config/site.ts b/www/apps/ui/src/config/site.ts index 081ecc616b..763386324a 100644 --- a/www/apps/ui/src/config/site.ts +++ b/www/apps/ui/src/config/site.ts @@ -1,3 +1,4 @@ +import { globalConfig } from "docs-ui" import { DocsConfig } from "types" type SiteConfig = { @@ -9,6 +10,7 @@ type SiteConfig = { const baseUrl = process.env.NEXT_PUBLIC_DOCS_URL || "http://localhost:3000" export const siteConfig: SiteConfig = { + ...globalConfig, name: "Medusa UI", baseUrl, basePath: process.env.NEXT_PUBLIC_BASE_PATH, diff --git a/www/apps/user-guide/config/index.ts b/www/apps/user-guide/config/index.ts index 5113d4f24b..7d39f26c91 100644 --- a/www/apps/user-guide/config/index.ts +++ b/www/apps/user-guide/config/index.ts @@ -1,9 +1,11 @@ import { DocsConfig, SidebarItem } from "types" import { generatedSidebar as sidebar } from "@/generated/sidebar.mjs" +import { globalConfig } from "docs-ui" const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:3000" export const config: DocsConfig = { + ...globalConfig, titleSuffix: "Medusa Admin User Guide", baseUrl, basePath: process.env.NEXT_PUBLIC_BASE_PATH, diff --git a/www/packages/docs-ui/src/components/MainNav/Version/index.tsx b/www/packages/docs-ui/src/components/MainNav/Version/index.tsx new file mode 100644 index 0000000000..f9e4380aa1 --- /dev/null +++ b/www/packages/docs-ui/src/components/MainNav/Version/index.tsx @@ -0,0 +1,65 @@ +"use state" + +import React, { useEffect, useMemo, useState } from "react" +import { useIsBrowser, useSiteConfig } from "../../../providers" +import Link from "next/link" +import { Tooltip } from "../../Tooltip" +import clsx from "clsx" + +const LOCAL_STORAGE_SUFFIX = "-seen" + +export const MainNavVersion = () => { + const { + config: { version }, + } = useSiteConfig() + const [showNewBadge, setShowNewBadge] = useState(false) + const { isBrowser } = useIsBrowser() + const localStorageKey = useMemo( + () => `${version.number}${LOCAL_STORAGE_SUFFIX}`, + [version] + ) + + useEffect(() => { + if (!isBrowser) { + return + } + + if (!localStorage.getItem(localStorageKey)) { + setShowNewBadge(true) + } + }, [isBrowser, localStorageKey]) + + const afterHover = () => { + if (!showNewBadge) { + return + } + + setShowNewBadge(false) + localStorage.setItem(localStorageKey, "true") + } + + return ( + <> + + + + v{version.number} + {showNewBadge && ( + + )} + + + + · + + ) +} diff --git a/www/packages/docs-ui/src/components/MainNav/index.tsx b/www/packages/docs-ui/src/components/MainNav/index.tsx index b4f981c002..370238d61c 100644 --- a/www/packages/docs-ui/src/components/MainNav/index.tsx +++ b/www/packages/docs-ui/src/components/MainNav/index.tsx @@ -18,6 +18,7 @@ import { MainNavDesktopMenu } from "./DesktopMenu" import { SidebarLeftIcon } from "../Icons/SidebarLeft" import { MainNavMobileMenu } from "./MobileMenu" import Link from "next/link" +import { MainNavVersion } from "./Version" type MainNavProps = { className?: string @@ -60,6 +61,7 @@ export const MainNav = ({ className, itemsClassName }: MainNavProps) => {
+ {editDate && } = { + "version": { + "number": "2.0", + "releaseUrl": "https://github.com/medusajs/medusa/releases/tag/v2.0.0" + } +} diff --git a/www/packages/docs-ui/src/index.ts b/www/packages/docs-ui/src/index.ts index 2c5cc2232d..d9447631a4 100644 --- a/www/packages/docs-ui/src/index.ts +++ b/www/packages/docs-ui/src/index.ts @@ -1,4 +1,5 @@ export * from "./constants" +export * from "./global-config" export * from "./components" export * from "./hooks" diff --git a/www/packages/docs-ui/src/providers/SiteConifg/index.tsx b/www/packages/docs-ui/src/providers/SiteConifg/index.tsx index 6a85ad82e9..ef106e6f23 100644 --- a/www/packages/docs-ui/src/providers/SiteConifg/index.tsx +++ b/www/packages/docs-ui/src/providers/SiteConifg/index.tsx @@ -2,6 +2,7 @@ import React, { createContext, useContext, useState } from "react" import { DocsConfig } from "types" +import { globalConfig } from "../../global-config" export type SiteConfigContextType = { config: DocsConfig @@ -33,6 +34,7 @@ export const SiteConfigProvider = ({ breadcrumbOptions: { showCategories: true, }, + ...globalConfig, } ) diff --git a/www/packages/types/src/config.ts b/www/packages/types/src/config.ts index d298e6b7cc..89c39791f8 100644 --- a/www/packages/types/src/config.ts +++ b/www/packages/types/src/config.ts @@ -16,4 +16,8 @@ export declare type DocsConfig = { key: string } breadcrumbOptions?: BreadcrumbOptions + version: { + number: string + releaseUrl: string + } } diff --git a/www/utils/packages/scripts/change-config-version.ts b/www/utils/packages/scripts/change-config-version.ts new file mode 100644 index 0000000000..25083aba35 --- /dev/null +++ b/www/utils/packages/scripts/change-config-version.ts @@ -0,0 +1,81 @@ +#!/usr/bin/env node + +import { Octokit } from "@octokit/core" +import fs from "fs" +import path from "path" +import { fileURLToPath } from "url" + +const octokit = new Octokit({ + auth: process.env.GH_TOKEN, +}) + +type DocsConfig = { + version: { + number: string + releaseUrl: string + } +} + +function getConfigPath() { + const __filename = fileURLToPath(import.meta.url) + const __dirname = path.dirname(__filename) + return path.join( + __dirname, + "..", + "..", + "..", + "..", + "packages", + "docs-ui", + "src", + "global-config.ts" + ) +} + +const configFilePrefix = `/* eslint-disable comma-dangle */ +/* eslint-disable prettier/prettier */ +import { DocsConfig } from "types" + +export const globalConfig: Pick = ` + +async function main() { + //retrieve the latest release + const response = await octokit.request( + "GET /repos/{owner}/{repo}/releases/latest", + { + owner: "medusajs", + repo: "medusa", + sha: "develop", + } + ) + + const version = response.data.tag_name.replace(/\.0$/, "").replace(/^v/, "") + + // get the existing version + const configPath = getConfigPath() + const configContent = fs + .readFileSync(configPath, "utf-8") + .replace(configFilePrefix, "") + const oldConfig: DocsConfig = JSON.parse(configContent) + + if (oldConfig.version.number === version) { + return + } + + //add new announcement + const config: DocsConfig = { + version: { + number: version, + releaseUrl: response.data.html_url, + }, + } + + //write new config file + fs.writeFileSync( + configPath, + `${configFilePrefix}${JSON.stringify(config, undefined, 2)}` + ) + console.log(`Version in config has been updated.`) +} + +void main() diff --git a/www/utils/packages/scripts/package.json b/www/utils/packages/scripts/package.json index 67297026df..9f490e0278 100644 --- a/www/utils/packages/scripts/package.json +++ b/www/utils/packages/scripts/package.json @@ -7,11 +7,13 @@ }, "scripts": { "build": "tsc", + "watch": "tsc --watch", "check:freshness": "node ./dist/freshness-check.js", "generate:message": "node ./dist/get-generate-diff-message.js", "check:release-commit": "node ./dist/check-release-commit.js", "generate:changeset": "node ./dist/run-changeset.js", - "check:pr-files-count": "node ./dist/check-pr-files-count.js" + "check:pr-files-count": "node ./dist/check-pr-files-count.js", + "update:config-version": "node ./dist/change-config-version.js" }, "version": "0.0.0", "type": "module",