Skip to content

Commit

Permalink
Extend the regen-apis command to regenerate dev versions (#788)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arnaucasau authored Feb 9, 2024
1 parent 4a9f837 commit 92c8599
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions scripts/commands/regenerateApiDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -82,24 +85,33 @@ async function processVersions(
pkgName: string,
historicalVersions: string[],
currentVersion: string,
maybeDevVersion: string | undefined,
): Promise<string[]> {
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<string> {
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}`;
}
Expand All @@ -116,7 +128,18 @@ async function regenerateVersion(
}
}

async function getPackageVersions(
async function getDevVersion(pkgName: string): Promise<string | undefined> {
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]> {
Expand Down

0 comments on commit 92c8599

Please sign in to comment.