5etools-utils 0.1.11 → 0.1.14

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,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
- getErrors () {
98
- um.info(this._tagLog, `Validating JSON against schema`);
95
+ _doLoadSchema () {
96
+ if (this._isSchemaLoaded) return;
99
97
 
100
98
  uf.listJsonFiles(this._dirSchema)
101
99
  .forEach(filePath => {
@@ -108,25 +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
- uf.runOnDirs(
115
- (dir) => {
116
- uf.listJsonFiles(dir)
117
- .forEach(filePath => {
118
- const data = uf.readJSON(filePath);
119
- this._addImplicits(data);
120
- const valid = this._ajv.validate(this._fnGetSchemaId(filePath), data);
138
+ this._doRunOnDir({...opts, errors, errorsFull, dir});
121
139
 
122
- if (!valid) this._handleError({filePath, errors, errorsFull, data});
123
- });
124
- },
125
- {
126
- root: this._dir,
127
- dirWhitelist: this._dirWhitelist,
128
- },
129
- );
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}));
130
152
 
131
153
  return {errors, errorsFull};
132
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 = ".", dirWhitelist = null} = {}) {
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" && (!dirWhitelist || dirWhitelist.has(dir)))
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.11",
3
+ "version": "0.1.14",
4
4
  "description": "Shared utilities for the 5etools ecosystem.",
5
5
  "type": "module",
6
6
  "main": "lib/Api.js",