-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep only the latest 5 version of documentation (#83)
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
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() |