From 6f55f3c414e1ee152a8ad9418f8ad9d3fe12cdbe Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Wed, 13 Sep 2023 08:01:38 +0200 Subject: [PATCH] Keep only the latest 5 version of documentation (#83) --- .github/workflows/rebuild-and-deploy-docs.yml | 6 ++ README.md | 7 +++ scripts/purge-old-versions.js | 56 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 scripts/purge-old-versions.js diff --git a/.github/workflows/rebuild-and-deploy-docs.yml b/.github/workflows/rebuild-and-deploy-docs.yml index 0d59d20fec..40c0dca07e 100644 --- a/.github/workflows/rebuild-and-deploy-docs.yml +++ b/.github/workflows/rebuild-and-deploy-docs.yml @@ -81,6 +81,10 @@ jobs: npx docusaurus docs:version ${{ env.REMOTE_VERSION }} env: REMOTE_VERSION: ${{ steps.check_main_repo_version.outputs.remote-version }} + - name: Purge old versions + if: (!inputs.force && steps.evaluate_versions.outputs.versions-compare == 'true') + run: | + node ./scripts/purge-old-versions.js - name: Build Docusaurus website if: inputs.original_event == 'push_on_main' || inputs.force || steps.evaluate_versions.outputs.versions-compare == 'true' run: | @@ -92,6 +96,8 @@ jobs: uses: stefanzweifel/git-auto-commit-action@v4 with: commit_message: '[automated commit] Bump docs to version ${{ env.REMOTE_VERSION }}' + tagging_message: 'v${{ env.REMOTE_VERSION }}' + env: REMOTE_VERSION: ${{ steps.check_main_repo_version.outputs.remote-version }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/README.md b/README.md index 63c215dc3f..90066c5311 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,13 @@ On a unix system: Deploy happens in Github Actions. Take a look at the workflow in `.github` folder. +## Generate docs for an old Platformatic version + +We keep online only the last 5 versions of Platformatic backwards. +Every time a new version is realeased, this repository is tagged with `vX.Y.Z` + +To generate the documentation for a specific version, checkout the related tag and run the development server. + ## Known issues ### Deleting directories diff --git a/scripts/purge-old-versions.js b/scripts/purge-old-versions.js new file mode 100644 index 0000000000..8b4e39efb7 --- /dev/null +++ b/scripts/purge-old-versions.js @@ -0,0 +1,56 @@ +'use strict' +const { join } = require('node:path') +const { readFile, writeFile, readdir, unlink, rmdir, rm } = require('node:fs/promises') +const { parseArgs } = require('node:util') + +const parseArgsOptions = { + 'dry-run': { + type: 'boolean', + default: false + }, + 'keep': { + type: 'string', + default: '5' + } +} + +async function main() { + const { + values + } = parseArgs({ options: parseArgsOptions }) + const versionsToKeep = await purgeVersionsJsonFile(values.keep, values['dry-run']) + await purgeDirectories(versionsToKeep, values['dry-run']) +} + +async function purgeVersionsJsonFile(numberOfOldVersionToKeep, dryRun) { + const versionsFile = join(__dirname, '..', 'versions.json') + const versionsArray = JSON.parse(await readFile(versionsFile, 'utf8')) + const newVersions = versionsArray.slice(0, numberOfOldVersionToKeep) + if (!dryRun) { + await writeFile(versionsFile, JSON.stringify(newVersions, null, 2)) + } else { + console.log(`Would keep only the following versions in versions.json file: ${newVersions.join(', ')}`) + } + return newVersions +} + +async function purgeDirectories(versionsToKeep = [], dryRun) { + const versionsFolder = join(__dirname, '..', 'versioned_docs') + const sidebarsFolder = join(__dirname, '..', 'versioned_sidebars') + const entries = await readdir(versionsFolder) + + for (const dir of entries) { + const flat = versionsToKeep.join('|').replace(/\./g, '\\.') + const re = new RegExp(`(${flat})`) + if (!dir.match(re)) { + if (dryRun) { + console.log(`Would delete ${dir} directory`) + } else { + await rm(join(versionsFolder, dir), { recursive: true }) + await rm(join(sidebarsFolder, `${dir}-sidebars.json`)) + } + } + } +} + +main() \ No newline at end of file