-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathconstants.ts
More file actions
110 lines (101 loc) · 3.51 KB
/
constants.ts
File metadata and controls
110 lines (101 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// mdn-bcd-collector: lib/constants.ts
// Common variables used throughout all of the collector
//
// © Gooborg Studios
// See the LICENSE file for copyright details
//
import {fileURLToPath} from "node:url";
import path from "node:path";
import fs from "node:fs";
export const BASE_DIR = new URL("..", import.meta.url);
export const RUNTIME_IDS_WITH_PATCH_VERSIONING = new Set([
"bun",
"servo",
"servo_android",
]); // List of browsers/runtimes that might add features in patches
/**
* 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 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;
}
};
/**
* 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;
}
};
/**
* 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 ${github_url} locally checked out, and its path defined using the ${env_variable} environment variable.`,
);
};
/**
* 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 getResultsDir = () =>
process.env.NODE_ENV === "test"
? ""
: get_dir(
"RESULTS_DIR",
"../mdn-bcd-results",
"https://github.com/openwebdocs/mdn-bcd-results",
try_results_dir,
);