Skip to content

Commit 4ca7efb

Browse files
committed
Turn directory constants into getter functions
1 parent a2d4abc commit 4ca7efb

File tree

7 files changed

+88
-59
lines changed

7 files changed

+88
-59
lines changed

lib/constants.ts

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,69 +13,83 @@ import fs from "node:fs";
1313
export const BASE_DIR = new URL("..", import.meta.url);
1414

1515
/**
16-
* The directory path for the Browser Compatibility Data (BCD) repository.
17-
* If the environment variable BCD_DIR is set, it uses the resolved path of BCD_DIR.
18-
* Otherwise, it uses the resolved path of "../browser-compat-data" relative to BASE_DIR.
16+
* Tests a specified path to see if it's a local checkout of mdn/browser-compat-data
17+
* @param dir The directory to test
18+
* @returns {boolean} If the directory is a BCD checkout
1919
*/
20-
const _get_bcd_dir = {
21-
confirmed_path: "",
22-
/**
23-
* Tests a specified path to see if it's a local checkout of mdn/browser-compat-data
24-
* @param dir The directory to test
25-
* @returns {boolean} If the directory is a BCD checkout
26-
*/
27-
try_bcd_dir: (dir) => {
28-
try {
29-
const packageJsonFile = fs.readFileSync(`${dir}/package.json`);
30-
const packageJson = JSON.parse(packageJsonFile.toString("utf8"));
31-
if (packageJson.name == "@mdn/browser-compat-data") {
32-
return true;
33-
}
34-
return false;
35-
} catch (e) {
36-
// If anything fails, we know that we're not looking at BCD
37-
return false;
38-
}
39-
},
40-
/**
41-
* Returns a valid BCD directory path based upon environment variable or relative path, or throws an error if none is found
42-
* @returns {string} The BCD path detected
43-
* @throws An error if no valid BCD path detected
44-
*/
45-
get dir() {
46-
if (this.confirmed_path) {
47-
return this.confirmed_path;
20+
const try_bcd_dir = (dir) => {
21+
try {
22+
const packageJsonFile = fs.readFileSync(`${dir}/package.json`);
23+
const packageJson = JSON.parse(packageJsonFile.toString("utf8"));
24+
if (packageJson.name == "@mdn/browser-compat-data") {
25+
return true;
4826
}
27+
return false;
28+
} catch (e) {
29+
// If anything fails, we know that we're not looking at BCD
30+
return false;
31+
}
32+
};
4933

50-
// First run: determine the BCD path
51-
if (process.env.BCD_DIR) {
52-
const env_dir = path.resolve(process.env.BCD_DIR);
53-
if (this.try_bcd_dir(env_dir)) {
54-
this.confirmed_path = env_dir;
55-
return env_dir;
56-
}
57-
}
34+
/**
35+
* Tests a specified path to see if it's a local checkout of mdn-bcd-results
36+
* @param dir The directory to test
37+
* @returns {boolean} If the directory is a mdn-bcd-results checkout
38+
*/
39+
const try_results_dir = (dir) => {
40+
try {
41+
return fs.existsSync(`${dir}/README.md`);
42+
} catch (e) {
43+
// If anything fails, we know that we're not looking at mdn-bcd-results
44+
return false;
45+
}
46+
};
5847

59-
const relative_dir = fileURLToPath(
60-
new URL("../browser-compat-data", BASE_DIR),
61-
);
62-
if (this.try_bcd_dir(relative_dir)) {
63-
this.confirmed_path = relative_dir;
64-
return relative_dir;
48+
/**
49+
* Returns a valid directory path based upon environment variable or relative path and a checker function, or throws an error if none is found
50+
* @returns {string} The BCD path detected
51+
* @throws An error if no valid BCD path detected
52+
*/
53+
const get_dir = (env_variable, relative_path, github_url, try_func) => {
54+
if (process.env[env_variable]) {
55+
const env_dir = path.resolve(process.env[env_variable]);
56+
if (try_func(env_dir)) {
57+
return env_dir;
6558
}
59+
}
60+
61+
const relative_dir = fileURLToPath(new URL(relative_path, BASE_DIR));
62+
if (try_func(relative_dir)) {
63+
return relative_dir;
64+
}
6665

67-
throw new Error(
68-
"You must have https://github.com/mdn/browser-compat-data locally checked out, and its path defined using the BCD_DIR environment variable.",
69-
);
70-
},
66+
throw new Error(
67+
`You must have ${github_url} locally checked out, and its path defined using the ${env_variable} environment variable.`,
68+
);
7169
};
72-
export const BCD_DIR = _get_bcd_dir.dir;
70+
71+
/**
72+
* The directory path for the Browser Compatibility Data (BCD) repository.
73+
* If the environment variable BCD_DIR is set, it uses the resolved path of BCD_DIR.
74+
* Otherwise, it uses the resolved path of "../browser-compat-data" relative to BASE_DIR.
75+
*/
76+
export const getBCDDir = () =>
77+
get_dir(
78+
"BCD_DIR",
79+
"../browser-compat-data",
80+
"https://github.com/mdn/browser-compat-data",
81+
try_bcd_dir,
82+
);
7383

7484
/**
7585
* The directory path where the results are stored.
7686
* If the RESULTS_DIR environment variable is set, it will be used.
7787
* Otherwise, the default path is resolved relative to the BASE_DIR.
7888
*/
79-
export const RESULTS_DIR = process.env.RESULTS_DIR
80-
? path.resolve(process.env.RESULTS_DIR)
81-
: fileURLToPath(new URL("../mdn-bcd-results", BASE_DIR));
89+
export const getResultsDir = () =>
90+
get_dir(
91+
"RESULTS_DIR",
92+
"../mdn-bcd-results",
93+
"https://github.com/openwebdocs/mdn-bcd-results",
94+
try_results_dir,
95+
);

scripts/add-new-bcd.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import esMain from "es-main";
1515
import yargs from "yargs";
1616
import {hideBin} from "yargs/helpers";
1717

18-
import {BCD_DIR, RESULTS_DIR} from "../lib/constants.js";
18+
import {getBCDDir, getResultsDir} from "../lib/constants.js";
1919
import {namespaces as jsNamespaces} from "../test-builder/javascript.js";
2020

2121
import {getMissing} from "./feature-coverage.js";
2222
import {main as updateBcd} from "./update-bcd.js";
2323

24+
const BCD_DIR = getBCDDir();
25+
const RESULTS_DIR = getResultsDir();
26+
2427
const tests = await fs.readJson(new URL("../tests.json", import.meta.url));
2528
const overrides = await fs.readJson(
2629
new URL("../custom/overrides.json", import.meta.url),

scripts/feature-coverage.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import yargs from "yargs";
1919
import {hideBin} from "yargs/helpers";
2020

2121
import {Tests} from "../types/types.js";
22-
import {BCD_DIR} from "../lib/constants.js";
22+
import {getBCDDir} from "../lib/constants.js";
23+
24+
const BCD_DIR = getBCDDir();
2325

2426
const untestableFeatures = jsonc.parse(
2527
await fs.readFile(

scripts/find-missing-reports.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ import fs from "fs-extra";
2121
import yargs from "yargs";
2222
import {hideBin} from "yargs/helpers";
2323

24-
import {BCD_DIR, RESULTS_DIR} from "../lib/constants.js";
24+
import {getBCDDir, getResultsDir} from "../lib/constants.js";
2525
import filterVersions from "../lib/filter-versions.js";
2626
import {parseUA} from "../lib/ua-parser.js";
2727

2828
import {loadJsonFiles} from "./update-bcd.js";
2929

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

32+
const BCD_DIR = getBCDDir();
33+
const RESULTS_DIR = getResultsDir();
34+
3235
const {default: bcd}: {default: CompatData} = await import(
3336
`${BCD_DIR}/index.js`
3437
);

scripts/report-stats.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ import {hideBin} from "yargs/helpers";
1717
import {CompatData} from "@mdn/browser-compat-data/types";
1818

1919
import {Report, ReportStats} from "../types/types.js";
20-
import {BCD_DIR} from "../lib/constants.js";
20+
import {getBCDDir} from "../lib/constants.js";
2121
import {parseUA} from "../lib/ua-parser.js";
2222

2323
import {findMissing} from "./feature-coverage.js";
2424

25+
const BCD_DIR = getBCDDir();
26+
2527
const {default: bcd}: {default: CompatData} = await import(
2628
`${BCD_DIR}/index.js`
2729
);

scripts/selenium.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ import {Listr, ListrTask, ListrTaskWrapper} from "listr2";
3131
import yargs from "yargs";
3232
import {hideBin} from "yargs/helpers";
3333

34-
import {RESULTS_DIR} from "../lib/constants.js";
34+
import {getResultsDir} from "../lib/constants.js";
3535
import filterVersionsLib from "../lib/filter-versions.js";
3636
import getSecrets from "../lib/secrets.js";
3737

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

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

42+
const RESULTS_DIR = getResultsDir();
43+
4244
type Task = ListrTaskWrapper<any, any, any>;
4345

4446
const collectorVersion = (

scripts/update-bcd.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ import {Minimatch} from "minimatch";
4242
import yargs from "yargs";
4343
import {hideBin} from "yargs/helpers";
4444

45-
import {BCD_DIR, RESULTS_DIR} from "../lib/constants.js";
45+
import {getBCDDir, getResultsDir} from "../lib/constants.js";
4646
import logger from "../lib/logger.js";
4747
import {parseUA} from "../lib/ua-parser.js";
4848

49+
const BCD_DIR = getBCDDir();
50+
const RESULTS_DIR = getResultsDir();
51+
4952
const {default: mirror} = await import(`${BCD_DIR}/scripts/build/mirror.js`);
5053

5154
/**

0 commit comments

Comments
 (0)