@@ -13,69 +13,93 @@ import fs from "node:fs";
1313export 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+ * @param env_variable The name of the environment variable to check
51+ * @param relative_path The expected relative path
52+ * @param github_url The URL to the GitHub repository for the expected folder
53+ * @param try_func The function to run to test if the path is a valid checkout of the expected repository
54+ * @returns {string } The path detected
55+ * @throws An error if no valid path detected
56+ */
57+ const get_dir = ( env_variable , relative_path , github_url , try_func ) => {
58+ if ( process . env [ env_variable ] ) {
59+ const env_dir = path . resolve ( process . env [ env_variable ] ) ;
60+ if ( try_func ( env_dir ) ) {
61+ return env_dir ;
6562 }
63+ }
64+
65+ const relative_dir = fileURLToPath ( new URL ( relative_path , BASE_DIR ) ) ;
66+ if ( try_func ( relative_dir ) ) {
67+ return relative_dir ;
68+ }
6669
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- } ,
70+ throw new Error (
71+ `You must have ${ github_url } locally checked out, and its path defined using the ${ env_variable } environment variable.` ,
72+ ) ;
7173} ;
72- export const BCD_DIR = _get_bcd_dir . dir ;
74+
75+ /**
76+ * The directory path for the Browser Compatibility Data (BCD) repository.
77+ * If the environment variable BCD_DIR is set, it uses the resolved path of BCD_DIR.
78+ * Otherwise, it uses the resolved path of "../browser-compat-data" relative to BASE_DIR.
79+ * @returns The directory where BCD is located
80+ * @throws An error if no valid BCD path detected
81+ */
82+ export const getBCDDir = ( ) =>
83+ get_dir (
84+ "BCD_DIR" ,
85+ "../browser-compat-data" ,
86+ "https://github.com/mdn/browser-compat-data" ,
87+ try_bcd_dir ,
88+ ) ;
7389
7490/**
7591 * The directory path where the results are stored.
7692 * If the RESULTS_DIR environment variable is set, it will be used.
7793 * Otherwise, the default path is resolved relative to the BASE_DIR.
94+ * @returns The directory where mdn-bcd-results is located
95+ * @throws An error if no valid mdn-bcd-results path detected
7896 */
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 ) ) ;
97+ export const getResultsDir = ( ) =>
98+ process . env . NODE_ENV === "test"
99+ ? ""
100+ : get_dir (
101+ "RESULTS_DIR" ,
102+ "../mdn-bcd-results" ,
103+ "https://github.com/openwebdocs/mdn-bcd-results" ,
104+ try_results_dir ,
105+ ) ;
0 commit comments