5etools-utils 0.1.12 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/TestJson.js +42 -22
- package/lib/UtilFs.js +24 -8
- package/package.json +1 -1
package/lib/TestJson.js
CHANGED
|
@@ -17,15 +17,11 @@ class JsonTester {
|
|
|
17
17
|
constructor (
|
|
18
18
|
{
|
|
19
19
|
dirSchema,
|
|
20
|
-
dir = ".",
|
|
21
|
-
dirWhitelist = null,
|
|
22
20
|
tagLog = LOG_TAG,
|
|
23
21
|
fnGetSchemaId = () => "homebrew.json",
|
|
24
22
|
},
|
|
25
23
|
) {
|
|
26
24
|
this._dirSchema = dirSchema;
|
|
27
|
-
this._dir = dir;
|
|
28
|
-
this._dirWhitelist = dirWhitelist;
|
|
29
25
|
this._tagLog = tagLog;
|
|
30
26
|
this._fnGetSchemaId = fnGetSchemaId;
|
|
31
27
|
|
|
@@ -46,6 +42,8 @@ class JsonTester {
|
|
|
46
42
|
},
|
|
47
43
|
);
|
|
48
44
|
// endregion
|
|
45
|
+
|
|
46
|
+
this._isSchemaLoaded = false;
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
/**
|
|
@@ -94,8 +92,8 @@ class JsonTester {
|
|
|
94
92
|
errors.push(error);
|
|
95
93
|
}
|
|
96
94
|
|
|
97
|
-
|
|
98
|
-
|
|
95
|
+
_doLoadSchema () {
|
|
96
|
+
if (this._isSchemaLoaded) return;
|
|
99
97
|
|
|
100
98
|
uf.listJsonFiles(this._dirSchema)
|
|
101
99
|
.forEach(filePath => {
|
|
@@ -108,27 +106,49 @@ class JsonTester {
|
|
|
108
106
|
this._ajv.addSchema(contents, relativeFilePath);
|
|
109
107
|
});
|
|
110
108
|
|
|
109
|
+
this._isSchemaLoaded = true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
_doRunOnDir ({dir, errors, errorsFull, ...opts} = {}) {
|
|
113
|
+
uf.listJsonFiles(dir, opts)
|
|
114
|
+
.forEach(filePath => {
|
|
115
|
+
um.info(this._tagLog, `\tValidating "${filePath}"...`);
|
|
116
|
+
|
|
117
|
+
const data = uf.readJSON(filePath);
|
|
118
|
+
this._addImplicits(data);
|
|
119
|
+
const valid = this._ajv.validate(this._fnGetSchemaId(filePath), data);
|
|
120
|
+
|
|
121
|
+
if (!valid) this._handleError({filePath, errors, errorsFull, data});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @param dir
|
|
127
|
+
* @param [opts]
|
|
128
|
+
* @param [opts.dirBlacklist]
|
|
129
|
+
*/
|
|
130
|
+
getErrors (dir, opts = {}) {
|
|
131
|
+
um.info(this._tagLog, `Validating JSON against schema`);
|
|
132
|
+
|
|
133
|
+
this._doLoadSchema();
|
|
134
|
+
|
|
111
135
|
const errors = [];
|
|
112
136
|
const errorsFull = [];
|
|
113
137
|
|
|
114
|
-
|
|
115
|
-
(dir) => {
|
|
116
|
-
uf.listJsonFiles(dir)
|
|
117
|
-
.forEach(filePath => {
|
|
118
|
-
um.info(this._tagLog, `\tValidating "${filePath}"...`);
|
|
138
|
+
this._doRunOnDir({...opts, errors, errorsFull, dir});
|
|
119
139
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
const valid = this._ajv.validate(this._fnGetSchemaId(filePath), data);
|
|
140
|
+
return {errors, errorsFull};
|
|
141
|
+
}
|
|
123
142
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
143
|
+
getErrorsOnDirs () {
|
|
144
|
+
um.info(this._tagLog, `Validating JSON against schema`);
|
|
145
|
+
|
|
146
|
+
this._doLoadSchema();
|
|
147
|
+
|
|
148
|
+
const errors = [];
|
|
149
|
+
const errorsFull = [];
|
|
150
|
+
|
|
151
|
+
uf.runOnDirs((dir) => this._doRunOnDir({errors, errorsFull, dir}));
|
|
132
152
|
|
|
133
153
|
return {errors, errorsFull};
|
|
134
154
|
}
|
package/lib/UtilFs.js
CHANGED
|
@@ -28,23 +28,38 @@ function readJSON (path, {isIncludeRaw = false} = {}) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
/**
|
|
32
|
+
* @param dir
|
|
33
|
+
* @param [opts]
|
|
34
|
+
* @param [opts.dirBlacklist]
|
|
35
|
+
*/
|
|
36
|
+
function listJsonFiles (dir, opts) {
|
|
37
|
+
const {dirBlacklist = null} = opts || {};
|
|
38
|
+
|
|
32
39
|
const dirContent = fs.readdirSync(dir, "utf8")
|
|
33
40
|
.map(file => dir === "." ? file : `${dir}/${file}`);
|
|
34
41
|
return dirContent.reduce((acc, file) => {
|
|
35
|
-
if (isDirectory(file))
|
|
36
|
-
|
|
42
|
+
if (isDirectory(file)) {
|
|
43
|
+
if (dirBlacklist == null || !dirBlacklist.has(file)) acc.push(...listJsonFiles(file, opts));
|
|
44
|
+
} else {
|
|
37
45
|
if (file.toLowerCase().endsWith(".json")) acc.push(file);
|
|
38
46
|
}
|
|
39
47
|
return acc;
|
|
40
48
|
}, []);
|
|
41
49
|
}
|
|
42
50
|
|
|
43
|
-
function runOnDirs (fn
|
|
44
|
-
fs.readdirSync(
|
|
45
|
-
.
|
|
46
|
-
.
|
|
47
|
-
|
|
51
|
+
function runOnDirs (fn) {
|
|
52
|
+
fs.readdirSync(".", "utf8")
|
|
53
|
+
.filter(dir => isDirectory(dir) && !dir.startsWith(".") && !dir.startsWith("_") && dir !== "node_modules")
|
|
54
|
+
.forEach(dir => fn(dir));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function pRunOnDirs (pFn) {
|
|
58
|
+
await Promise.allSettled(
|
|
59
|
+
fs.readdirSync(".", "utf8")
|
|
60
|
+
.filter(dir => isDirectory(dir) && !dir.startsWith(".") && !dir.startsWith("_") && dir !== "node_modules")
|
|
61
|
+
.map(dir => pFn(dir)),
|
|
62
|
+
);
|
|
48
63
|
}
|
|
49
64
|
|
|
50
65
|
function mkDirs (pathToCreate) {
|
|
@@ -63,5 +78,6 @@ export {
|
|
|
63
78
|
readJSON,
|
|
64
79
|
listJsonFiles,
|
|
65
80
|
runOnDirs,
|
|
81
|
+
pRunOnDirs,
|
|
66
82
|
mkDirs,
|
|
67
83
|
};
|