Created
April 4, 2025 09:14
-
-
Save infomiho/946893e3d98fbda799b576785f16e13e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
/** | |
* Main function to sync redirects from the appropriate docs version to static directory | |
*/ | |
function syncRedirects() { | |
try { | |
const isDevMode = checkDevelopmentMode(); | |
const staticRedirectsPath = getStaticRedirectsPath(); | |
// Try to copy from current version if in development mode | |
if (isDevMode && tryCopyFromCurrentDocs(staticRedirectsPath)) { | |
return; | |
} | |
// Otherwise use the latest versioned docs | |
copyFromVersionedDocs(staticRedirectsPath); | |
} catch (error) { | |
console.error('Error syncing redirects:', error); | |
process.exit(1); | |
} | |
} | |
/** | |
* Check if we're in development mode with current version included | |
* @returns {boolean} True if in development mode | |
*/ | |
function checkDevelopmentMode() { | |
return process.env.DOCS_INCLUDE_CURRENT_VERSION === 'true'; | |
} | |
/** | |
* Get the destination path for redirects in static directory | |
* @returns {string} Path to the static _redirects file | |
*/ | |
function getStaticRedirectsPath() { | |
return path.join(__dirname, '../static/_redirects'); | |
} | |
/** | |
* Try to copy redirects from current docs (development version) | |
* @param {string} destPath Path to copy the redirects file to | |
* @returns {boolean} True if successful, false otherwise | |
*/ | |
function tryCopyFromCurrentDocs(destPath) { | |
const currentRedirectsPath = path.join(__dirname, '../docs/_redirects'); | |
if (fs.existsSync(currentRedirectsPath)) { | |
copyRedirectsFile(currentRedirectsPath, destPath); | |
appendSourceComment(destPath, 'current development version'); | |
console.log('Successfully copied _redirects from current version to static directory'); | |
return true; | |
} | |
console.warn('Current version _redirects file not found, falling back to latest versioned docs'); | |
return false; | |
} | |
/** | |
* Copy redirects from the latest versioned docs | |
* @param {string} destPath Path to copy the redirects file to | |
*/ | |
function copyFromVersionedDocs(destPath) { | |
const latestVersion = getLatestVersion(); | |
const redirectsSourcePath = path.join( | |
__dirname, | |
`../versioned_docs/version-${latestVersion}/_redirects` | |
); | |
if (!fs.existsSync(redirectsSourcePath)) { | |
console.error(`_redirects file not found in version ${latestVersion}`); | |
process.exit(1); | |
} | |
copyRedirectsFile(redirectsSourcePath, destPath); | |
appendSourceComment(destPath, `version-${latestVersion}`); | |
console.log(`Successfully copied _redirects from version ${latestVersion} to static directory`); | |
} | |
/** | |
* Get the latest version from versions.json | |
* @returns {string} Latest version number | |
*/ | |
function getLatestVersion() { | |
const versionsPath = path.join(__dirname, '../versions.json'); | |
if (!fs.existsSync(versionsPath)) { | |
console.error('versions.json not found. Are you sure you have versioned docs?'); | |
process.exit(1); | |
} | |
const versions = JSON.parse(fs.readFileSync(versionsPath, 'utf8')); | |
const latestVersion = versions[0]; // First version in the array is the latest | |
if (!latestVersion) { | |
console.error('No versions found in versions.json'); | |
process.exit(1); | |
} | |
console.log(`Latest version: ${latestVersion}`); | |
return latestVersion; | |
} | |
/** | |
* Copy a file from source to destination | |
* @param {string} sourcePath Source file path | |
* @param {string} destPath Destination file path | |
*/ | |
function copyRedirectsFile(sourcePath, destPath) { | |
fs.copyFileSync(sourcePath, destPath); | |
} | |
/** | |
* Append a source comment to the redirects file | |
* @param {string} filePath Path to the redirects file | |
* @param {string} source Source identifier | |
*/ | |
function appendSourceComment(filePath, source) { | |
fs.appendFileSync( | |
filePath, | |
`\n\n# This file was automatically copied from ${source}. DO NOT EDIT DIRECTLY.\n` | |
); | |
} | |
// Execute the function | |
syncRedirects(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment