@@ -13,69 +13,83 @@ import fs from "node:fs";
13
13
export const BASE_DIR = new URL ( ".." , import . meta. url ) ;
14
14
15
15
/**
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
19
19
*/
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 ;
48
26
}
27
+ return false ;
28
+ } catch ( e ) {
29
+ // If anything fails, we know that we're not looking at BCD
30
+ return false ;
31
+ }
32
+ } ;
49
33
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
+ } ;
58
47
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 ;
65
58
}
59
+ }
60
+
61
+ const relative_dir = fileURLToPath ( new URL ( relative_path , BASE_DIR ) ) ;
62
+ if ( try_func ( relative_dir ) ) {
63
+ return relative_dir ;
64
+ }
66
65
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
+ ) ;
71
69
} ;
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
+ ) ;
73
83
74
84
/**
75
85
* The directory path where the results are stored.
76
86
* If the RESULTS_DIR environment variable is set, it will be used.
77
87
* Otherwise, the default path is resolved relative to the BASE_DIR.
78
88
*/
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
+ ) ;
0 commit comments