From 4ca7efb2f7b81d2b8655cb7e85bc0d049043547d Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Fri, 20 Jun 2025 04:12:05 -0700 Subject: [PATCH 1/6] Turn directory constants into getter functions --- lib/constants.ts | 120 ++++++++++++++++++-------------- scripts/add-new-bcd.ts | 5 +- scripts/feature-coverage.ts | 4 +- scripts/find-missing-reports.ts | 5 +- scripts/report-stats.ts | 4 +- scripts/selenium.ts | 4 +- scripts/update-bcd.ts | 5 +- 7 files changed, 88 insertions(+), 59 deletions(-) diff --git a/lib/constants.ts b/lib/constants.ts index d98dd9d84..0400b76f9 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -13,69 +13,83 @@ import fs from "node:fs"; export const BASE_DIR = new URL("..", import.meta.url); /** - * The directory path for the Browser Compatibility Data (BCD) repository. - * If the environment variable BCD_DIR is set, it uses the resolved path of BCD_DIR. - * Otherwise, it uses the resolved path of "../browser-compat-data" relative to BASE_DIR. + * Tests a specified path to see if it's a local checkout of mdn/browser-compat-data + * @param dir The directory to test + * @returns {boolean} If the directory is a BCD checkout */ -const _get_bcd_dir = { - confirmed_path: "", - /** - * Tests a specified path to see if it's a local checkout of mdn/browser-compat-data - * @param dir The directory to test - * @returns {boolean} If the directory is a BCD checkout - */ - try_bcd_dir: (dir) => { - try { - const packageJsonFile = fs.readFileSync(`${dir}/package.json`); - const packageJson = JSON.parse(packageJsonFile.toString("utf8")); - if (packageJson.name == "@mdn/browser-compat-data") { - return true; - } - return false; - } catch (e) { - // If anything fails, we know that we're not looking at BCD - return false; - } - }, - /** - * Returns a valid BCD directory path based upon environment variable or relative path, or throws an error if none is found - * @returns {string} The BCD path detected - * @throws An error if no valid BCD path detected - */ - get dir() { - if (this.confirmed_path) { - return this.confirmed_path; +const try_bcd_dir = (dir) => { + try { + const packageJsonFile = fs.readFileSync(`${dir}/package.json`); + const packageJson = JSON.parse(packageJsonFile.toString("utf8")); + if (packageJson.name == "@mdn/browser-compat-data") { + return true; } + return false; + } catch (e) { + // If anything fails, we know that we're not looking at BCD + return false; + } +}; - // First run: determine the BCD path - if (process.env.BCD_DIR) { - const env_dir = path.resolve(process.env.BCD_DIR); - if (this.try_bcd_dir(env_dir)) { - this.confirmed_path = env_dir; - return env_dir; - } - } +/** + * Tests a specified path to see if it's a local checkout of mdn-bcd-results + * @param dir The directory to test + * @returns {boolean} If the directory is a mdn-bcd-results checkout + */ +const try_results_dir = (dir) => { + try { + return fs.existsSync(`${dir}/README.md`); + } catch (e) { + // If anything fails, we know that we're not looking at mdn-bcd-results + return false; + } +}; - const relative_dir = fileURLToPath( - new URL("../browser-compat-data", BASE_DIR), - ); - if (this.try_bcd_dir(relative_dir)) { - this.confirmed_path = relative_dir; - return relative_dir; +/** + * Returns a valid directory path based upon environment variable or relative path and a checker function, or throws an error if none is found + * @returns {string} The BCD path detected + * @throws An error if no valid BCD path detected + */ +const get_dir = (env_variable, relative_path, github_url, try_func) => { + if (process.env[env_variable]) { + const env_dir = path.resolve(process.env[env_variable]); + if (try_func(env_dir)) { + return env_dir; } + } + + const relative_dir = fileURLToPath(new URL(relative_path, BASE_DIR)); + if (try_func(relative_dir)) { + return relative_dir; + } - throw new Error( - "You must have https://github.com/mdn/browser-compat-data locally checked out, and its path defined using the BCD_DIR environment variable.", - ); - }, + throw new Error( + `You must have ${github_url} locally checked out, and its path defined using the ${env_variable} environment variable.`, + ); }; -export const BCD_DIR = _get_bcd_dir.dir; + +/** + * The directory path for the Browser Compatibility Data (BCD) repository. + * If the environment variable BCD_DIR is set, it uses the resolved path of BCD_DIR. + * Otherwise, it uses the resolved path of "../browser-compat-data" relative to BASE_DIR. + */ +export const getBCDDir = () => + get_dir( + "BCD_DIR", + "../browser-compat-data", + "https://github.com/mdn/browser-compat-data", + try_bcd_dir, + ); /** * The directory path where the results are stored. * If the RESULTS_DIR environment variable is set, it will be used. * Otherwise, the default path is resolved relative to the BASE_DIR. */ -export const RESULTS_DIR = process.env.RESULTS_DIR - ? path.resolve(process.env.RESULTS_DIR) - : fileURLToPath(new URL("../mdn-bcd-results", BASE_DIR)); +export const getResultsDir = () => + get_dir( + "RESULTS_DIR", + "../mdn-bcd-results", + "https://github.com/openwebdocs/mdn-bcd-results", + try_results_dir, + ); diff --git a/scripts/add-new-bcd.ts b/scripts/add-new-bcd.ts index 9103e8fe2..bcf1145a9 100644 --- a/scripts/add-new-bcd.ts +++ b/scripts/add-new-bcd.ts @@ -15,12 +15,15 @@ import esMain from "es-main"; import yargs from "yargs"; import {hideBin} from "yargs/helpers"; -import {BCD_DIR, RESULTS_DIR} from "../lib/constants.js"; +import {getBCDDir, getResultsDir} from "../lib/constants.js"; import {namespaces as jsNamespaces} from "../test-builder/javascript.js"; import {getMissing} from "./feature-coverage.js"; import {main as updateBcd} from "./update-bcd.js"; +const BCD_DIR = getBCDDir(); +const RESULTS_DIR = getResultsDir(); + const tests = await fs.readJson(new URL("../tests.json", import.meta.url)); const overrides = await fs.readJson( new URL("../custom/overrides.json", import.meta.url), diff --git a/scripts/feature-coverage.ts b/scripts/feature-coverage.ts index a82d5f830..1f6e8ec3a 100644 --- a/scripts/feature-coverage.ts +++ b/scripts/feature-coverage.ts @@ -19,7 +19,9 @@ import yargs from "yargs"; import {hideBin} from "yargs/helpers"; import {Tests} from "../types/types.js"; -import {BCD_DIR} from "../lib/constants.js"; +import {getBCDDir} from "../lib/constants.js"; + +const BCD_DIR = getBCDDir(); const untestableFeatures = jsonc.parse( await fs.readFile( diff --git a/scripts/find-missing-reports.ts b/scripts/find-missing-reports.ts index 746c9a574..c53c85bf3 100644 --- a/scripts/find-missing-reports.ts +++ b/scripts/find-missing-reports.ts @@ -21,7 +21,7 @@ import fs from "fs-extra"; import yargs from "yargs"; import {hideBin} from "yargs/helpers"; -import {BCD_DIR, RESULTS_DIR} from "../lib/constants.js"; +import {getBCDDir, getResultsDir} from "../lib/constants.js"; import filterVersions from "../lib/filter-versions.js"; import {parseUA} from "../lib/ua-parser.js"; @@ -29,6 +29,9 @@ import {loadJsonFiles} from "./update-bcd.js"; import type {BrowserName} from "@mdn/browser-compat-data"; +const BCD_DIR = getBCDDir(); +const RESULTS_DIR = getResultsDir(); + const {default: bcd}: {default: CompatData} = await import( `${BCD_DIR}/index.js` ); diff --git a/scripts/report-stats.ts b/scripts/report-stats.ts index 17ac220eb..a63cd1725 100644 --- a/scripts/report-stats.ts +++ b/scripts/report-stats.ts @@ -17,11 +17,13 @@ import {hideBin} from "yargs/helpers"; import {CompatData} from "@mdn/browser-compat-data/types"; import {Report, ReportStats} from "../types/types.js"; -import {BCD_DIR} from "../lib/constants.js"; +import {getBCDDir} from "../lib/constants.js"; import {parseUA} from "../lib/ua-parser.js"; import {findMissing} from "./feature-coverage.js"; +const BCD_DIR = getBCDDir(); + const {default: bcd}: {default: CompatData} = await import( `${BCD_DIR}/index.js` ); diff --git a/scripts/selenium.ts b/scripts/selenium.ts index 0326edb79..aceb93ffc 100644 --- a/scripts/selenium.ts +++ b/scripts/selenium.ts @@ -31,7 +31,7 @@ import {Listr, ListrTask, ListrTaskWrapper} from "listr2"; import yargs from "yargs"; import {hideBin} from "yargs/helpers"; -import {RESULTS_DIR} from "../lib/constants.js"; +import {getResultsDir} from "../lib/constants.js"; import filterVersionsLib from "../lib/filter-versions.js"; import getSecrets from "../lib/secrets.js"; @@ -39,6 +39,8 @@ import type {BrowserName} from "@mdn/browser-compat-data"; import "../lib/selenium-keepalive.js"; +const RESULTS_DIR = getResultsDir(); + type Task = ListrTaskWrapper; const collectorVersion = ( diff --git a/scripts/update-bcd.ts b/scripts/update-bcd.ts index e51fbe9b2..6c59c20f7 100644 --- a/scripts/update-bcd.ts +++ b/scripts/update-bcd.ts @@ -42,10 +42,13 @@ import {Minimatch} from "minimatch"; import yargs from "yargs"; import {hideBin} from "yargs/helpers"; -import {BCD_DIR, RESULTS_DIR} from "../lib/constants.js"; +import {getBCDDir, getResultsDir} from "../lib/constants.js"; import logger from "../lib/logger.js"; import {parseUA} from "../lib/ua-parser.js"; +const BCD_DIR = getBCDDir(); +const RESULTS_DIR = getResultsDir(); + const {default: mirror} = await import(`${BCD_DIR}/scripts/build/mirror.js`); /** From 311a4aac5aa419cd9af3a8c08ef71e5dc10f97f1 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Fri, 20 Jun 2025 04:15:51 -0700 Subject: [PATCH 2/6] Remove now-redundant need to checkout BCD in deployment --- .github/workflows/deploy.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 6f892f039..44e1abfd5 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -11,7 +11,6 @@ permissions: env: FORCE_COLOR: 3 - BCD_DIR: ./browser-compat-data RESULTS_DIR: ./mdn-bcd-results jobs: @@ -46,12 +45,6 @@ jobs: heroku_api_key: ${{secrets.HEROKU_API_KEY}} heroku_app_name: "mdn-bcd-collector" heroku_email: ${{secrets.HEROKU_EMAIL}} - - name: Clone browser-compat-data - uses: actions/checkout@v4 - if: steps.check.outputs.changed == 'true' - with: - repository: mdn/browser-compat-data - path: browser-compat-data - name: Checkout mdn-bcd-results uses: actions/checkout@v4 if: steps.check.outputs.changed == 'true' From 2164ea6611cb4263ff6370e22d11acd382bab72d Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 24 Jun 2025 04:03:08 -0700 Subject: [PATCH 3/6] Update JSDoc --- lib/constants.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/constants.ts b/lib/constants.ts index 0400b76f9..146e5d31e 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -47,8 +47,12 @@ const try_results_dir = (dir) => { /** * Returns a valid directory path based upon environment variable or relative path and a checker function, or throws an error if none is found - * @returns {string} The BCD path detected - * @throws An error if no valid BCD path detected + * @param env_variable The name of the environment variable to check + * @param relative_path The expected relative path + * @param github_url The URL to the GitHub repository for the expected folder + * @param try_func The function to run to test if the path is a valid checkout of the expected repository + * @returns {string} The path detected + * @throws An error if no valid path detected */ const get_dir = (env_variable, relative_path, github_url, try_func) => { if (process.env[env_variable]) { @@ -72,6 +76,8 @@ const get_dir = (env_variable, relative_path, github_url, try_func) => { * The directory path for the Browser Compatibility Data (BCD) repository. * If the environment variable BCD_DIR is set, it uses the resolved path of BCD_DIR. * Otherwise, it uses the resolved path of "../browser-compat-data" relative to BASE_DIR. + * @returns The directory where BCD is located + * @throws An error if no valid BCD path detected */ export const getBCDDir = () => get_dir( @@ -85,6 +91,8 @@ export const getBCDDir = () => * The directory path where the results are stored. * If the RESULTS_DIR environment variable is set, it will be used. * Otherwise, the default path is resolved relative to the BASE_DIR. + * @returns The directory where mdn-bcd-results is located + * @throws An error if no valid mdn-bcd-results path detected */ export const getResultsDir = () => get_dir( From fc342a055c13e834a39a0ba5348387505063aa14 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 24 Jun 2025 04:18:32 -0700 Subject: [PATCH 4/6] Checkout mdn-bcd-results during testing --- .github/workflows/test.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 50258f5f1..c10e08835 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,6 +11,7 @@ permissions: env: FORCE_COLOR: 3 BCD_DIR: ./browser-compat-data + RESULTS_DIR: ./mdn-bcd-results jobs: test: @@ -37,6 +38,12 @@ jobs: uses: actions/setup-node@v4 with: node-version-file: ".nvmrc" + - name: Checkout mdn-bcd-results + uses: actions/checkout@v4 + if: steps.check.outputs.changed == 'true' + with: + repository: openwebdocs/mdn-bcd-results + path: mdn-bcd-results - name: Prepare browser-compat-data run: npm ci working-directory: browser-compat-data From ed4fcb622120de25641038013731b93db86c010f Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 24 Jun 2025 04:21:23 -0700 Subject: [PATCH 5/6] Revert "Checkout mdn-bcd-results during testing" This reverts commit fc342a055c13e834a39a0ba5348387505063aa14. --- .github/workflows/test.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c10e08835..50258f5f1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,7 +11,6 @@ permissions: env: FORCE_COLOR: 3 BCD_DIR: ./browser-compat-data - RESULTS_DIR: ./mdn-bcd-results jobs: test: @@ -38,12 +37,6 @@ jobs: uses: actions/setup-node@v4 with: node-version-file: ".nvmrc" - - name: Checkout mdn-bcd-results - uses: actions/checkout@v4 - if: steps.check.outputs.changed == 'true' - with: - repository: openwebdocs/mdn-bcd-results - path: mdn-bcd-results - name: Prepare browser-compat-data run: npm ci working-directory: browser-compat-data From 974b7d8ad6ab2ba208310842ae912d1e8bd57123 Mon Sep 17 00:00:00 2001 From: "Queen Vinyl Da.i'gyu-Kazotetsu" Date: Tue, 24 Jun 2025 04:24:26 -0700 Subject: [PATCH 6/6] Results directory is not needed during unittesting --- lib/constants.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/constants.ts b/lib/constants.ts index 146e5d31e..7773fd596 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -95,9 +95,11 @@ export const getBCDDir = () => * @throws An error if no valid mdn-bcd-results path detected */ export const getResultsDir = () => - get_dir( - "RESULTS_DIR", - "../mdn-bcd-results", - "https://github.com/openwebdocs/mdn-bcd-results", - try_results_dir, - ); + process.env.NODE_ENV === "test" + ? "" + : get_dir( + "RESULTS_DIR", + "../mdn-bcd-results", + "https://github.com/openwebdocs/mdn-bcd-results", + try_results_dir, + );