Skip to content

Commit fa3d763

Browse files
authored
Merge pull request #12 from duckduckgo/la/global-ignore-and-json5-support
Support JSON5, rename common_* files, add global ignore list
2 parents 16a592f + b6f491d commit fa3d763

File tree

12 files changed

+87
-66
lines changed

12 files changed

+87
-66
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"kp": {
3+
"key": "kp",
4+
"description": "",
5+
"enum": [1]
6+
},
7+
"p": {
8+
"key": "p",
9+
"description": "",
10+
"enum": [1]
11+
},
12+
"test": {
13+
"key": "test",
14+
"description": "",
15+
"enum": [1]
16+
},
17+
"scrlybrkr": {
18+
"key": "scrlybrkr",
19+
"description": "Added by 3rd party school proxy",
20+
"type": "number"
21+
}
22+
}

live_validation_scripts/validate_live_pixel.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ function main(mainDir, csvFile) {
1717
const productDef = fileUtils.readProductDef(mainDir);
1818
const commonParams = fileUtils.readCommonParams(mainDir);
1919
const commonSuffixes = fileUtils.readCommonSuffixes(mainDir);
20+
const globalIgnoreParams = fileUtils.readIgnoreParams(fileUtils.GLOBAL_PIXEL_DIR);
2021

2122
const tokenizedPixels = fileUtils.readTokenizedPixels(mainDir);
2223
const paramsValidator = new ParamsValidator(commonParams, commonSuffixes);
23-
const ignoreParams = fileUtils.readIgnoreParams(mainDir);
24+
const pixelIgnoreParams = fileUtils.readIgnoreParams(mainDir);
25+
26+
const ignoreParams = [...(Object.values(pixelIgnoreParams) || []), ...Object.values(globalIgnoreParams)];
2427

2528
const liveValidator = new LivePixelsValidator(tokenizedPixels, productDef, ignoreParams, paramsValidator);
2629
let processedPixels = 0;

src/file_utils.mjs

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,52 @@ import path from 'path';
77
import JSON5 from 'json5';
88

99
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+
}
1048

1149
/**
1250
* Read common parameters
1351
* @param {string} mainPixelDir - path to the main pixels directory
1452
* @returns {object} common parameters
1553
*/
1654
export function readCommonParams(mainPixelDir) {
17-
console.log(mainPixelDir);
18-
return parseFile(path.join(mainPixelDir, 'common_params.json'));
55+
return readSchemaFile(mainPixelDir, 'params_dictionary.json');
1956
}
2057

2158
/**
@@ -24,7 +61,7 @@ export function readCommonParams(mainPixelDir) {
2461
* @returns {object} common suffixes
2562
*/
2663
export function readCommonSuffixes(mainPixelDir) {
27-
return parseFile(path.join(mainPixelDir, 'common_suffixes.json'));
64+
return readSchemaFile(mainPixelDir, 'suffixes_dictionary.json');
2865
}
2966

3067
/**
@@ -33,13 +70,13 @@ export function readCommonSuffixes(mainPixelDir) {
3370
* @returns {object} ignore parameters
3471
*/
3572
export function readIgnoreParams(mainPixelDir) {
36-
return parseFile(path.join(mainPixelDir, 'ignore_params.json'));
73+
return readSchemaFile(mainPixelDir, 'ignore_params.json');
3774
}
3875

3976
/**
40-
* Get product definition path
77+
* Get product definition path based on mainPixelDir.
4178
* @param {string} mainPixelDir - path to the main pixels directory
42-
* @returns {string} product definition path
79+
* @returns {string} product definition file path
4380
*/
4481
export function getProductDefPath(mainPixelDir) {
4582
return path.join(mainPixelDir, 'product.json');
@@ -67,13 +104,24 @@ export function getResultsDir(mainPixelDir) {
67104
return resultsDir;
68105
}
69106

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+
70118
/**
71119
* Get path to pixel errors encountered during live validation
72120
* @param {string} mainPixelDir - path to the main pixels directory
73121
* @returns {string} pixel errors path
74122
*/
75123
export function getPixelErrorsPath(mainPixelDir) {
76-
return path.join(getResultsDir(mainPixelDir), 'pixel_errors.json');
124+
return getResultsFilePath(mainPixelDir, 'pixel_errors.json');
77125
}
78126

79127
/**
@@ -82,16 +130,16 @@ export function getPixelErrorsPath(mainPixelDir) {
82130
* @returns {string} undocumented pixels path
83131
*/
84132
export function getUndocumentedPixelsPath(mainPixelDir) {
85-
return path.join(getResultsDir(mainPixelDir), 'undocumented_pixels.json');
133+
return getResultsFilePath(mainPixelDir, 'undocumented_pixels.json');
86134
}
87135

88136
/**
89-
* Get tokenized pixels path and creates the path if it doesn't exist
137+
* Get tokenized pixels path
90138
* @param {string} mainPixelDir - path to the main pixels directory
91139
* @returns {string} tokenized pixels path
92140
*/
93141
export function getTokenizedPixelsPath(mainPixelDir) {
94-
return path.join(getResultsDir(mainPixelDir), 'tokenized_pixels.json');
142+
return getResultsFilePath(mainPixelDir, 'tokenized_pixels.json');
95143
}
96144

97145
/**
@@ -102,7 +150,3 @@ export function getTokenizedPixelsPath(mainPixelDir) {
102150
export function readTokenizedPixels(mainPixelDir) {
103151
return parseFile(getTokenizedPixelsPath(mainPixelDir));
104152
}
105-
106-
function parseFile(filePath) {
107-
return JSON5.parse(fs.readFileSync(filePath).toString());
108-
}
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1 @@
1-
{
2-
"kp": {
3-
"key": "kp",
4-
"description": "",
5-
"enum": [1]
6-
},
7-
"p": {
8-
"key": "p",
9-
"description": "",
10-
"enum": [1]
11-
},
12-
"test": {
13-
"key": "test",
14-
"description": "",
15-
"enum": [1]
16-
}
17-
}
1+
{}
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1 @@
1-
{
2-
"kp": {
3-
"key": "kp",
4-
"description": "",
5-
"enum": [1]
6-
},
7-
"p": {
8-
"key": "p",
9-
"description": "",
10-
"enum": [1]
11-
},
12-
"test": {
13-
"key": "test",
14-
"description": "",
15-
"enum": [1]
16-
}
17-
}
1+
{}
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1 @@
1-
{
2-
"kp": {
3-
"key": "kp",
4-
"description": "",
5-
"enum": [1]
6-
},
7-
"p": {
8-
"key": "p",
9-
"description": "",
10-
"enum": [1]
11-
},
12-
"test": {
13-
"key": "test",
14-
"description": "",
15-
"enum": [1]
16-
}
17-
}
1+
{}

0 commit comments

Comments
 (0)