Skip to content

Turn directory constants into getter functions #2487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ permissions:

env:
FORCE_COLOR: 3
BCD_DIR: ./browser-compat-data
RESULTS_DIR: ./mdn-bcd-results

jobs:
Expand Down Expand Up @@ -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'
Expand Down
130 changes: 77 additions & 53 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
5 changes: 4 additions & 1 deletion scripts/add-new-bcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 3 additions & 1 deletion scripts/feature-coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion scripts/find-missing-reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ 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";

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`
);
Expand Down
4 changes: 3 additions & 1 deletion scripts/report-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
Expand Down
4 changes: 3 additions & 1 deletion scripts/selenium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ 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";

import type {BrowserName} from "@mdn/browser-compat-data";

import "../lib/selenium-keepalive.js";

const RESULTS_DIR = getResultsDir();

type Task = ListrTaskWrapper<any, any, any>;

const collectorVersion = (
Expand Down
5 changes: 4 additions & 1 deletion scripts/update-bcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);

/**
Expand Down