diff --git a/docs/package.json b/docs/package.json index 8bf5596..559972e 100644 --- a/docs/package.json +++ b/docs/package.json @@ -4,15 +4,17 @@ "private": true, "scripts": { "docusaurus": "docusaurus", - "start": "docusaurus start", - "build": "docusaurus build", + "start": "node scripts/version.js && docusaurus start", + "generate-version": "node scripts/version.js", + "build": "npm run generate-version && docusaurus build", "swizzle": "docusaurus swizzle", "deploy": "docusaurus deploy", "clear": "docusaurus clear", "serve": "docusaurus serve", "write-translations": "docusaurus write-translations", "write-heading-ids": "docusaurus write-heading-ids", - "typecheck": "tsc" + "typecheck": "tsc", + "version-json": "node scripts/version.js" }, "dependencies": { "@docusaurus/core": "3.7.0", diff --git a/docs/scripts/README.md b/docs/scripts/README.md new file mode 100644 index 0000000..b6da157 --- /dev/null +++ b/docs/scripts/README.md @@ -0,0 +1,31 @@ +# Docusaurus Scripts + +This directory contains utility scripts for the Docusaurus documentation site. + +## version.js + +This script reads the version from `../../ra_aid/__version__.py` and creates a `version.json` file in the Docusaurus `static/` directory, which will be included in the built site. + +### Usage + +The script is automatically run as part of the build and start processes via npm scripts defined in `package.json`, but can also be run manually: + +```bash +# From docs directory +npm run version-json + +# Or directly +node scripts/version.js +``` + +### Output + +The script creates a `static/version.json` file with the following format: + +```json +{ + "version": "x.y.z" +} +``` + +This file will be available at `/version.json` in the built site, allowing client-side version checks. \ No newline at end of file diff --git a/docs/scripts/version.js b/docs/scripts/version.js new file mode 100755 index 0000000..6dd14a4 --- /dev/null +++ b/docs/scripts/version.js @@ -0,0 +1,62 @@ +#!/usr/bin/env node + +/** + * Script to read version from ra_aid/__version__.py and create version.json + * in the Docusaurus static directory. + */ + +const fs = require('fs'); +const path = require('path'); + +// Paths +const versionFilePath = path.resolve(__dirname, '../../ra_aid/__version__.py'); +const outputPath = path.resolve(__dirname, '../static/version.json'); + +/** + * Extract version string from the __version__.py file + * @param {string} content - The file content + * @returns {string|null} - Extracted version or null if not found + */ +function extractVersion(content) { + const regex = /__version__\s*=\s*["']([^"']+)["']/; + const match = content.match(regex); + return match ? match[1] : null; +} + +// Main function to create version.json +function createVersionJson() { + try { + // Read version file + console.log(`Reading version from ${versionFilePath}...`); + const versionFileContent = fs.readFileSync(versionFilePath, 'utf8'); + + // Extract version + const version = extractVersion(versionFileContent); + if (!version) { + console.error('Failed to extract version from file'); + process.exit(1); + } + + console.log(`Extracted version: ${version}`); + + // Create version JSON + const versionJson = JSON.stringify({ version }, null, 2); + + // Ensure static directory exists + const staticDir = path.dirname(outputPath); + if (!fs.existsSync(staticDir)) { + console.log(`Creating directory: ${staticDir}`); + fs.mkdirSync(staticDir, { recursive: true }); + } + + // Write JSON file + fs.writeFileSync(outputPath, versionJson); + console.log(`Version JSON written to ${outputPath}`); + } catch (error) { + console.error(`Error creating version.json: ${error.message}`); + process.exit(1); + } +} + +// Execute the main function +createVersionJson(); \ No newline at end of file diff --git a/docs/static/.gitignore b/docs/static/.gitignore new file mode 100644 index 0000000..a193ad0 --- /dev/null +++ b/docs/static/.gitignore @@ -0,0 +1 @@ +version.json