diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 6f892f03..44e1abfd 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' diff --git a/lib/constants.ts b/lib/constants.ts index d98dd9d8..7773fd59 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -13,69 +13,93 @@ 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 + * @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]) { + 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. + * @returns The directory where BCD is located + * @throws An error if no valid BCD path detected + */ +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. + * @returns The directory where mdn-bcd-results is located + * @throws An error if no valid mdn-bcd-results path detected */ -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 = () => + process.env.NODE_ENV === "test" + ? "" + : 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 9103e8fe..bcf1145a 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 a82d5f83..1f6e8ec3 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 746c9a57..c53c85bf 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 17ac220e..a63cd172 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 0326edb7..aceb93ff 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 e51fbe9b..6c59c20f 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`); /**