From 2b188ea447d20ff48b74e7d4bd6f05763982acec Mon Sep 17 00:00:00 2001 From: Luiz Ferraz Date: Sun, 19 May 2024 11:47:16 -0300 Subject: [PATCH] chore(docs): Use local version of packages instead of NPM release --- docs/package.json | 1 + docs/src/components/Version.astro | 29 +++++++++++++++++++++++++++++ pnpm-lock.yaml | 3 +++ 3 files changed, 33 insertions(+) diff --git a/docs/package.json b/docs/package.json index 3cfad897..4c7705d2 100644 --- a/docs/package.json +++ b/docs/package.json @@ -12,6 +12,7 @@ "validate": "astro check" }, "dependencies": { + "@inox-tools/utils": "workspace:^", "@astrojs/starlight": "^0.23.1", "@vercel/analytics": "^1.2.2", "astro": "^4.8.6", diff --git a/docs/src/components/Version.astro b/docs/src/components/Version.astro index 65ffb7bb..0563c402 100644 --- a/docs/src/components/Version.astro +++ b/docs/src/components/Version.astro @@ -5,6 +5,33 @@ */ import { simpleFetch } from '@/utils/fetch'; +import { Lazy } from '@inox-tools/utils/lazy'; +import { join } from 'node:path'; +import { existsSync, readFileSync, readdirSync } from 'node:fs'; + +export const localPackages = Lazy.of(() => { + const packagesDir = new URL('../../../packages/', import.meta.url); + + const entries = readdirSync(packagesDir, { + recursive: false, + encoding: 'utf-8', + withFileTypes: true, + }); + + const packages = new Map(); + + for (const entry of entries) { + if (!entry.isDirectory()) continue; + + const packageJsonPath = join(entry.parentPath, entry.name, 'package.json'); + if (!existsSync(packageJsonPath)) continue; + const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); + + packages.set(packageJson.name, packageJson.version); + } + + return packages; +}); export interface Props { pkgName: string; @@ -13,6 +40,8 @@ export interface Props { const { pkgName } = Astro.props as Props; async function getVersion() { + if (localPackages.get().has(pkgName)) return localPackages.get().get(pkgName); + const url = `https://registry.npmjs.org/${pkgName}/latest`; const response = await simpleFetch(url); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index aab6108c..d3e6d33c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,6 +41,9 @@ importers: '@astrojs/starlight': specifier: ^0.23.1 version: 0.23.1(astro@4.8.6(@types/node@20.12.12)(typescript@5.4.5)) + '@inox-tools/utils': + specifier: workspace:^ + version: link:../packages/utils '@vercel/analytics': specifier: ^1.2.2 version: 1.2.2