@@ -7,15 +7,52 @@ import path from 'path';
7
7
import JSON5 from 'json5' ;
8
8
9
9
const RESULTS_DIR = 'pixel_processing_results' ;
10
+ export const GLOBAL_PIXEL_DIR = 'global_pixel_definitions' ;
11
+
12
+ /**
13
+ * Attempt to read and parse a file using JSON5. Tries .json
14
+ * first but will try to json5 if missing.
15
+ *
16
+ * @param {string } filePath - Absolute path to a file
17
+ * @returns {object } Parsed file content
18
+ * @throws Will throw an error if neither file exists.
19
+ */
20
+ function parseFile ( filePath ) {
21
+ let resolvedPath = filePath ;
22
+ if ( ! fs . existsSync ( resolvedPath ) ) {
23
+ // Try the '.json5' fallback
24
+ const { dir, name } = path . parse ( filePath ) ;
25
+ const altPath = path . join ( dir , `${ name } .json5` ) ;
26
+ if ( fs . existsSync ( altPath ) ) {
27
+ resolvedPath = altPath ;
28
+ } else {
29
+ throw new Error ( `Neither ${ filePath } nor ${ altPath } exist.` ) ;
30
+ }
31
+ }
32
+ const fileContent = fs . readFileSync ( resolvedPath , 'utf8' ) ;
33
+ return JSON5 . parse ( fileContent ) ;
34
+ }
35
+
36
+ /**
37
+ * Builds a file path from the main pixel directory and the given filename,
38
+ * then parses the file.
39
+ *
40
+ * @param {string } mainPixelDir - path to the main pixels directory
41
+ * @param {string } filename - file name (with extension) to read
42
+ * @returns {object } Parsed file content
43
+ */
44
+ function readSchemaFile ( mainPixelDir , filename ) {
45
+ const filePath = path . join ( mainPixelDir , filename ) ;
46
+ return parseFile ( filePath ) ;
47
+ }
10
48
11
49
/**
12
50
* Read common parameters
13
51
* @param {string } mainPixelDir - path to the main pixels directory
14
52
* @returns {object } common parameters
15
53
*/
16
54
export function readCommonParams ( mainPixelDir ) {
17
- console . log ( mainPixelDir ) ;
18
- return parseFile ( path . join ( mainPixelDir , 'common_params.json' ) ) ;
55
+ return readSchemaFile ( mainPixelDir , 'params_dictionary.json' ) ;
19
56
}
20
57
21
58
/**
@@ -24,7 +61,7 @@ export function readCommonParams(mainPixelDir) {
24
61
* @returns {object } common suffixes
25
62
*/
26
63
export function readCommonSuffixes ( mainPixelDir ) {
27
- return parseFile ( path . join ( mainPixelDir , 'common_suffixes .json' ) ) ;
64
+ return readSchemaFile ( mainPixelDir , 'suffixes_dictionary .json' ) ;
28
65
}
29
66
30
67
/**
@@ -33,13 +70,13 @@ export function readCommonSuffixes(mainPixelDir) {
33
70
* @returns {object } ignore parameters
34
71
*/
35
72
export function readIgnoreParams ( mainPixelDir ) {
36
- return parseFile ( path . join ( mainPixelDir , 'ignore_params.json' ) ) ;
73
+ return readSchemaFile ( mainPixelDir , 'ignore_params.json' ) ;
37
74
}
38
75
39
76
/**
40
- * Get product definition path
77
+ * Get product definition path based on mainPixelDir.
41
78
* @param {string } mainPixelDir - path to the main pixels directory
42
- * @returns {string } product definition path
79
+ * @returns {string } product definition file path
43
80
*/
44
81
export function getProductDefPath ( mainPixelDir ) {
45
82
return path . join ( mainPixelDir , 'product.json' ) ;
@@ -67,13 +104,24 @@ export function getResultsDir(mainPixelDir) {
67
104
return resultsDir ;
68
105
}
69
106
107
+ /**
108
+ * Get path to a file inside the results directory.
109
+ *
110
+ * @param {string } mainPixelDir - path to the main pixels directory
111
+ * @param {string } filename - file name within the results directory
112
+ * @returns {string } Absolute path to the file in results
113
+ */
114
+ function getResultsFilePath ( mainPixelDir , filename ) {
115
+ return path . join ( getResultsDir ( mainPixelDir ) , filename ) ;
116
+ }
117
+
70
118
/**
71
119
* Get path to pixel errors encountered during live validation
72
120
* @param {string } mainPixelDir - path to the main pixels directory
73
121
* @returns {string } pixel errors path
74
122
*/
75
123
export function getPixelErrorsPath ( mainPixelDir ) {
76
- return path . join ( getResultsDir ( mainPixelDir ) , 'pixel_errors.json' ) ;
124
+ return getResultsFilePath ( mainPixelDir , 'pixel_errors.json' ) ;
77
125
}
78
126
79
127
/**
@@ -82,16 +130,16 @@ export function getPixelErrorsPath(mainPixelDir) {
82
130
* @returns {string } undocumented pixels path
83
131
*/
84
132
export function getUndocumentedPixelsPath ( mainPixelDir ) {
85
- return path . join ( getResultsDir ( mainPixelDir ) , 'undocumented_pixels.json' ) ;
133
+ return getResultsFilePath ( mainPixelDir , 'undocumented_pixels.json' ) ;
86
134
}
87
135
88
136
/**
89
- * Get tokenized pixels path and creates the path if it doesn't exist
137
+ * Get tokenized pixels path
90
138
* @param {string } mainPixelDir - path to the main pixels directory
91
139
* @returns {string } tokenized pixels path
92
140
*/
93
141
export function getTokenizedPixelsPath ( mainPixelDir ) {
94
- return path . join ( getResultsDir ( mainPixelDir ) , 'tokenized_pixels.json' ) ;
142
+ return getResultsFilePath ( mainPixelDir , 'tokenized_pixels.json' ) ;
95
143
}
96
144
97
145
/**
@@ -102,7 +150,3 @@ export function getTokenizedPixelsPath(mainPixelDir) {
102
150
export function readTokenizedPixels ( mainPixelDir ) {
103
151
return parseFile ( getTokenizedPixelsPath ( mainPixelDir ) ) ;
104
152
}
105
-
106
- function parseFile ( filePath ) {
107
- return JSON5 . parse ( fs . readFileSync ( filePath ) . toString ( ) ) ;
108
- }
0 commit comments