5etools-utils 0.1.6 → 0.1.9
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 +3 -1
- package/lib/UtilFs.js +9 -6
- package/package.json +1 -1
package/lib/TestJson.js
CHANGED
|
@@ -19,11 +19,13 @@ class JsonTester {
|
|
|
19
19
|
dirSchema,
|
|
20
20
|
dir = ".",
|
|
21
21
|
tagLog = LOG_TAG,
|
|
22
|
+
fnGetSchemaId = () => "homebrew.json",
|
|
22
23
|
},
|
|
23
24
|
) {
|
|
24
25
|
this._dirSchema = dirSchema;
|
|
25
26
|
this._dir = dir;
|
|
26
27
|
this._tagLog = tagLog;
|
|
28
|
+
this._fnGetSchemaId = fnGetSchemaId;
|
|
27
29
|
|
|
28
30
|
// region Set up validator
|
|
29
31
|
this._ajv = new Ajv({
|
|
@@ -113,7 +115,7 @@ class JsonTester {
|
|
|
113
115
|
.forEach(filePath => {
|
|
114
116
|
const data = uf.readJSON(filePath);
|
|
115
117
|
this._addImplicits(data);
|
|
116
|
-
const valid = this._ajv.validate(
|
|
118
|
+
const valid = this._ajv.validate(this._fnGetSchemaId(filePath), data);
|
|
117
119
|
|
|
118
120
|
if (!valid) this._handleError({filePath, errors, errorsFull, data});
|
|
119
121
|
});
|
package/lib/UtilFs.js
CHANGED
|
@@ -4,21 +4,23 @@ function isDirectory (path) {
|
|
|
4
4
|
return fs.lstatSync(path).isDirectory();
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
function readJSON (path) {
|
|
7
|
+
function readJSON (path, {isIncludeRaw = false} = {}) {
|
|
8
8
|
try {
|
|
9
9
|
if (fs.existsSync(path)) {
|
|
10
10
|
let str = fs.readFileSync(path, "utf8");
|
|
11
11
|
// Strip BOM(s)
|
|
12
12
|
while (str.charCodeAt(0) === 0xFEFF) str = str.slice(1);
|
|
13
|
-
|
|
13
|
+
const out = JSON.parse(str);
|
|
14
|
+
if (isIncludeRaw) return {raw: str, json: out};
|
|
15
|
+
return out;
|
|
14
16
|
} else {
|
|
15
17
|
const parts = path.split(/[\\/]+/g);
|
|
16
18
|
const dir = parts.slice(0, -1).join("/");
|
|
17
19
|
const originalName = parts[parts.length - 1];
|
|
18
20
|
const filenames = fs.readdirSync(dir, "utf8");
|
|
19
21
|
const filename = filenames.find(it => it.toLowerCase() === originalName.toLowerCase());
|
|
20
|
-
if (filename)
|
|
21
|
-
|
|
22
|
+
if (!filename) throw new Error(`Could not find file "${path}"`);
|
|
23
|
+
return readJSON(`${dir}/${filename}`, {isIncludeRaw});
|
|
22
24
|
}
|
|
23
25
|
} catch (e) {
|
|
24
26
|
e.message += ` (Path: ${path})`;
|
|
@@ -40,8 +42,9 @@ function listJsonFiles (dir) {
|
|
|
40
42
|
|
|
41
43
|
function runOnDirs (fn, root = ".") {
|
|
42
44
|
fs.readdirSync(root, "utf8")
|
|
43
|
-
.
|
|
44
|
-
.
|
|
45
|
+
.map(dir => ({dir, dirPath: `${root}/${dir}`}))
|
|
46
|
+
.filter(({dir, dirPath}) => isDirectory(dirPath) && !dir.startsWith(".") && !dir.startsWith("_") && dir !== "node_modules")
|
|
47
|
+
.forEach(({dirPath}) => fn(dirPath));
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
function mkDirs (pathToCreate) {
|