5etools-utils 0.1.10 → 0.1.13

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 CHANGED
@@ -17,13 +17,11 @@ class JsonTester {
17
17
  constructor (
18
18
  {
19
19
  dirSchema,
20
- dir = ".",
21
20
  tagLog = LOG_TAG,
22
21
  fnGetSchemaId = () => "homebrew.json",
23
22
  },
24
23
  ) {
25
24
  this._dirSchema = dirSchema;
26
- this._dir = dir;
27
25
  this._tagLog = tagLog;
28
26
  this._fnGetSchemaId = fnGetSchemaId;
29
27
 
@@ -44,6 +42,8 @@ class JsonTester {
44
42
  },
45
43
  );
46
44
  // endregion
45
+
46
+ this._isSchemaLoaded = false;
47
47
  }
48
48
 
49
49
  /**
@@ -92,8 +92,8 @@ class JsonTester {
92
92
  errors.push(error);
93
93
  }
94
94
 
95
- getErrors () {
96
- um.info(this._tagLog, `Validating JSON against schema`);
95
+ _doLoadSchema () {
96
+ if (this._isSchemaLoaded) return;
97
97
 
98
98
  uf.listJsonFiles(this._dirSchema)
99
99
  .forEach(filePath => {
@@ -106,22 +106,49 @@ class JsonTester {
106
106
  this._ajv.addSchema(contents, relativeFilePath);
107
107
  });
108
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
+
109
135
  const errors = [];
110
136
  const errorsFull = [];
111
137
 
112
- uf.runOnDirs(
113
- (dir) => {
114
- uf.listJsonFiles(dir)
115
- .forEach(filePath => {
116
- const data = uf.readJSON(filePath);
117
- this._addImplicits(data);
118
- const valid = this._ajv.validate(this._fnGetSchemaId(filePath), data);
138
+ this._doRunOnDir({errors, errorsFull, dir, opts});
119
139
 
120
- if (!valid) this._handleError({filePath, errors, errorsFull, data});
121
- });
122
- },
123
- this._dir,
124
- );
140
+ return {errors, errorsFull};
141
+ }
142
+
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}));
125
152
 
126
153
  return {errors, errorsFull};
127
154
  }
package/lib/UtilFs.js CHANGED
@@ -28,23 +28,30 @@ function readJSON (path, {isIncludeRaw = false} = {}) {
28
28
  }
29
29
  }
30
30
 
31
- function listJsonFiles (dir) {
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)) acc.push(...listJsonFiles(file));
36
- else {
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, root = ".") {
44
- fs.readdirSync(root, "utf8")
45
- .map(dir => ({dir, dirPath: root === "." ? dir : `${root}/${dir}`}))
46
- .filter(({dir, dirPath}) => isDirectory(dirPath) && !dir.startsWith(".") && !dir.startsWith("_") && dir !== "node_modules")
47
- .forEach(({dirPath}) => fn(dirPath));
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));
48
55
  }
49
56
 
50
57
  function mkDirs (pathToCreate) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "5etools-utils",
3
- "version": "0.1.10",
3
+ "version": "0.1.13",
4
4
  "description": "Shared utilities for the 5etools ecosystem.",
5
5
  "type": "module",
6
6
  "main": "lib/Api.js",