Skip to content

Commit

Permalink
Keep only the latest 5 version of documentation (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
leorossi authored Sep 13, 2023
1 parent f3fe3eb commit 6f55f3c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/rebuild-and-deploy-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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 }}
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions scripts/purge-old-versions.js
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 6f55f3c

Please sign in to comment.