From 92c85994a31e6fb2612f8d617721f11b8fefd5cb Mon Sep 17 00:00:00 2001 From: Arnau Casau <47946624+arnaucasau@users.noreply.github.com> Date: Fri, 9 Feb 2024 10:52:15 +0100 Subject: [PATCH] Extend the `regen-apis` command to regenerate dev versions (#788) Part of #316 This PR extends the API automatic regeneration script to regenerate dev versions. When using the `npm run regen-apis` command, the script will read the `_package.json` from the `dev` folder if it exists and regenerate that version as the last one of the package. --- scripts/commands/regenerateApiDocs.ts | 39 +++++++++++++++++++++------ 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/scripts/commands/regenerateApiDocs.ts b/scripts/commands/regenerateApiDocs.ts index 09ef3255e61..8d4a443216c 100644 --- a/scripts/commands/regenerateApiDocs.ts +++ b/scripts/commands/regenerateApiDocs.ts @@ -15,10 +15,10 @@ import { readFile, readdir } from "fs/promises"; import yargs from "yargs/yargs"; import { hideBin } from "yargs/helpers"; import { $ } from "zx"; -import fs from "fs"; import { Pkg } from "../lib/api/Pkg"; import { zxMain } from "../lib/zx"; +import { pathExists } from "../lib/fs"; interface Arguments { [x: string]: unknown; @@ -54,14 +54,17 @@ zxMain(async () => { continue; } - const [historicalVersions, currentVersion] = await getPackageVersions( + const [historicalVersions, currentVersion] = await getReleasedVersions( pkgName, args.currentApisOnly, ); + const maybeDevVersion = await getDevVersion(pkgName); + const result = await processVersions( pkgName, historicalVersions, currentVersion, + maybeDevVersion, ); results.set(pkgName, result); } @@ -82,24 +85,33 @@ async function processVersions( pkgName: string, historicalVersions: string[], currentVersion: string, + maybeDevVersion: string | undefined, ): Promise { const results: string[] = []; for (const historicalVersion of historicalVersions) { - results.push(await regenerateVersion(pkgName, historicalVersion)); + results.push( + await regenerateVersion(pkgName, historicalVersion, "historical"), + ); + } + + results.push(await regenerateVersion(pkgName, currentVersion)); + + if (maybeDevVersion) { + results.push(await regenerateVersion(pkgName, maybeDevVersion, "dev")); } - results.push(await regenerateVersion(pkgName, currentVersion, false)); + return results; } async function regenerateVersion( pkgName: string, version: string, - historical: boolean = true, + typeArgument?: "historical" | "dev", ): Promise { try { - if (historical) { - await $`npm run gen-api -- -p ${pkgName} -v ${version} --historical`; + if (typeArgument) { + await $`npm run gen-api -- -p ${pkgName} -v ${version} --${typeArgument}`; } else { await $`npm run gen-api -- -p ${pkgName} -v ${version}`; } @@ -116,7 +128,18 @@ async function regenerateVersion( } } -async function getPackageVersions( +async function getDevVersion(pkgName: string): Promise { + const devPath = `docs/api/${pkgName}/dev`; + + if (await pathExists(devPath)) { + return JSON.parse(await readFile(`${devPath}/_package.json`, "utf-8")) + .version; + } + + return undefined; +} + +async function getReleasedVersions( pkgName: string, currentApisOnly: boolean, ): Promise<[string[], string]> {