Skip to content

Commit

Permalink
ci: convert api doc generation script from args to env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinWilkinson committed Sep 5, 2024
1 parent 5ea5555 commit 5377b0a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
38 changes: 21 additions & 17 deletions .github/cicd/scripts/generate-new-api-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@ import { Utils } from "../deps.ts";

console.clear();

if (Deno.args.length < 3) {
let errorMsg = `The required number of arguments is 3 but only '${Deno.args.length}' were given.`;
errorMsg += "\nThe required arguments are:";
errorMsg += "\n\t1. The path to the directory to generate the API documentation.";
errorMsg += "\n\t2. The tag or branch name to generate the API documentation from.";
errorMsg += "\n\t3. The GitHub token to use for accessing the GitHub API.";

console.error(`::error::${errorMsg}`);
type GenerateSrcType = "api version" | "branch";

const generateOutputDirPath: string = (Deno.env.get("OUTPUT_DIR_PATH") ?? "").trim();

if (generateOutputDirPath === "") {
Utils.printAsGitHubError("The environment variable 'OUTPUT_DIR_PATH' does not exist.");
Deno.exit(1);
}

type GenerateSrcType = "api version" | "branch";
let tagOrBranch = (Deno.env.get("TAG_OR_BRANCH") ?? "").trim().toLowerCase();

const generateOutputDirPath: string = Deno.args[0];
let tagOrBranch = Deno.args[1].trim().toLowerCase();
if (tagOrBranch === "") {
Utils.printAsGitHubError("The environment variable 'TAG_OR_BRANCH' does not exist.");
Deno.exit(1);
}

const token = Deno.args[2].trim();
const token = (Deno.env.get("GITHUB_TOKEN") ?? "").trim();

if (token === "") {
Utils.printAsGitHubError("The environment variable 'GITHUB_TOKEN' does not exist.");
}

// Optional argument
const isInteractive = Deno.args.length >= 4 && Deno.args[3].trim().toLowerCase() === "true";
// Optional env variable
const isInteractive = (Deno.env.get("IS_INTERACTIVE") ?? "").trim().toLowerCase() === "true";

console.log(`Generate Output Dir Path: ${generateOutputDirPath}`);
Utils.printAsGitHubNotice(`Generate Output Dir Path: ${generateOutputDirPath}`);

/*NOTE:
This script is executed by the 'api-release.yml' workflow. The workflow
Expand Down Expand Up @@ -54,7 +58,7 @@ if (isInteractive) {

if (Utils.isNothing(token)) {
const errorMsg = "The environment variable 'CICD_TOKEN' does not exist, is empty, or null.";
console.log(errorMsg);
Utils.printAsGitHubError(errorMsg);
Deno.exit(1);
}

Expand All @@ -72,7 +76,7 @@ if (isInteractive) {
Deno.exit();
}
} else {
console.log(`Version To Generate: ${tagOrBranch}`);
Utils.printAsGitHubNotice(`Version To Generate: ${tagOrBranch}`);
tagOrBranch = tagOrBranch.startsWith("v") ? tagOrBranch : `v${tagOrBranch}`;
}

Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/api-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,14 @@ jobs:
"${{ github.workspace }}/.github/cicd/scripts/delete-oldest-api-docs.ts";
- name: Generate API Docs (${{ inputs.api-version }})
env:
OUTPUT_DIR_PATH: "${{ github.workspace }}/docs/api"
TAG_OR_BRANCH: "${{ inputs.api-version }}"
GITHUB_TOKEN: "${{ secrets.CICD_TOKEN }}"
run: |
deno run `
--allow-read --allow-write --allow-run --allow-net `
"${{ github.workspace }}/.github/cicd/scripts/generate-new-api-docs.ts" `
"${{ github.workspace }}/docs/api" `
"${{ inputs.api-version }}" `
"${{ secrets.CICD_TOKEN }}";
"${{ github.workspace }}/.github/cicd/scripts/generate-new-api-docs.ts";
- name: Install Dependencies (pnpm)
run: pnpm install;
Expand Down
15 changes: 6 additions & 9 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@
"type": "node",
"program": "${workspaceFolder}/.github/cicd/scripts/generate-new-api-docs.ts",
"cwd": "${workspaceFolder}",
"env": {
"OUTPUT_DIR_PATH": "${workspaceFolder}/docs/api",
"TAG_OR_BRANCH": "v1.0.0-preview.32",
"GITHUB_TOKEN": "${env:CICD_TOKEN}",
"IS_INTERACTIVE": "true"
},
"runtimeExecutable": "${userHome}/.deno/bin/deno.EXE",
"runtimeArgs": [
"run",
Expand All @@ -109,15 +115,6 @@
"--allow-env",
"--inspect-wait",
],
"args": [
"${workspaceFolder}/docs/api",
"v1.0.0-preview.32",
"${env:CICD_TOKEN}",
"true" // Is Interactive
],
"env": {
"CICD_TOKEN": "${env:CICD_TOKEN}"
},
"console": "integratedTerminal",
"attachSimplePort": 9229
},
Expand Down
20 changes: 12 additions & 8 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"options": {
"cwd": "${workspaceFolder}/.github/cicd"
},
"dependsOn": ["clear-terminal"],
"dependsOn": [
"clear-terminal"
],
"command": "deno",
"args": [
"fmt",
Expand All @@ -34,7 +36,13 @@
"detail": "Generates the API docs for the website.",
"type": "shell",
"options": {
"cwd": "${workspaceFolder}"
"cwd": "${workspaceFolder}",
"env": {
"OUTPUT_DIR_PATH": "${workspaceFolder}/docs/api",
"TAG_OR_BRANCH": "no-value",
"GITHUB_TOKEN": "${env:CICD_TOKEN}",
"IS_INTERACTIVE": "true"
},
},
"command": "deno",
"args": [
Expand All @@ -46,10 +54,6 @@
"--allow-run",
"--allow-env",
"${workspaceFolder}/.github/cicd/scripts/generate-new-api-docs.ts",
"${workspaceFolder}/docs/api",
"no-value",
"${env:CICD_TOKEN}",
"true", // Is Interactive
]
},
{ // RESET DOCS
Expand All @@ -73,7 +77,7 @@
"type": "shell",
"options": {
"cwd": "${workspaceFolder}/.github/cicd"
},
},
"command": "deno",
"args": [
"run",
Expand Down Expand Up @@ -158,4 +162,4 @@
],
}
]
}
}

0 comments on commit 5377b0a

Please sign in to comment.