5etools-utils 0.5.17 → 0.6.0

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.
@@ -23,7 +23,7 @@ class BrewCleaner {
23
23
 
24
24
  const files = Uf.listJsonFiles(folder);
25
25
  for (const file of files) {
26
- let contents = Uf.readJSON(file);
26
+ let contents = Uf.readJsonSync(file);
27
27
 
28
28
  if (this._RE_INVALID_WINDOWS_CHARS.test(file.split("/").slice(1).join("/"))) {
29
29
  ALL_ERRORS.push(`${file} contained invalid characters!`);
@@ -144,7 +144,7 @@ class BrewIndexGenerator {
144
144
  folder,
145
145
  name: file,
146
146
  cleanName: file.replace(/#/g, "%23"),
147
- contents: Uf.readJSON(file),
147
+ contents: Uf.readJsonSync(file),
148
148
  }))
149
149
  .forEach(fileInfo => {
150
150
  if (!fileInfo.contents._meta) {
@@ -17,7 +17,7 @@ class BrewTimestamper {
17
17
  static async _pUpdateDir (dir) {
18
18
  const promises = Uf.listJsonFiles(dir)
19
19
  .map(async file => {
20
- const fileData = Uf.readJSON(file, {isIncludeRaw: true});
20
+ const fileData = Uf.readJsonSync(file, {isIncludeRaw: true});
21
21
 
22
22
  if (!fileData.json._meta) {
23
23
  throw new Error(`File "${file}" did not have metadata!`);
package/lib/TestData.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import fs from "fs";
2
- import {readJSON} from "./UtilFs.js";
2
+ import {readJsonSync} from "./UtilFs.js";
3
3
  import {ObjectWalker} from "./ObjectWalker.js";
4
4
 
5
5
  /** Runs multiple handlers on each file, to avoid re-reading each file for each handler */
@@ -37,7 +37,7 @@ class _ParsedJsonChecker {
37
37
 
38
38
  if (!filePath.endsWith(".json") || (fnIsIgnoredFile && fnIsIgnoredFile(filePath))) return;
39
39
 
40
- const contents = readJSON(filePath);
40
+ const contents = readJsonSync(filePath);
41
41
 
42
42
  ObjectWalker.walk({
43
43
  obj: contents,
package/lib/TestJson.js CHANGED
@@ -118,7 +118,7 @@ class JsonTester {
118
118
  const dir = type === "site" ? this._dirSchemaSite : this._dirSchemaBrew;
119
119
 
120
120
  this._ajv.addSchema(
121
- Uf.readJSON(path.join(dir, relativeFilePath)),
121
+ Uf.readJsonSync(path.join(dir, relativeFilePath)),
122
122
  relativeFilePath,
123
123
  );
124
124
  }
@@ -133,7 +133,7 @@ class JsonTester {
133
133
  const relativeFilePath = path.relative(this._dirSchema, filePath)
134
134
  .replace(/\\/g, "/");
135
135
 
136
- this._ajv.addSchema(Uf.readJSON(filePath), relativeFilePath);
136
+ this._ajv.addSchema(Uf.readJsonSync(filePath), relativeFilePath);
137
137
  });
138
138
 
139
139
  this._isSchemaLoaded = true;
@@ -149,7 +149,7 @@ class JsonTester {
149
149
 
150
150
  Um.info(this._tagLog, `\tValidating "${filePath}"...`);
151
151
 
152
- const data = Uf.readJSON(filePath);
152
+ const data = Uf.readJsonSync(filePath);
153
153
  this._addImplicits(data);
154
154
 
155
155
  const isValid = this._ajv.validate(this._fnGetSchemaId(filePath), data);
package/lib/UtilFs.js CHANGED
@@ -5,7 +5,7 @@ function isDirectory (path) {
5
5
  return fs.lstatSync(path).isDirectory();
6
6
  }
7
7
 
8
- function readJSON (path, {isIncludeRaw = false} = {}) {
8
+ function readJsonSync (path, {isIncludeRaw = false} = {}) {
9
9
  try {
10
10
  if (fs.existsSync(path)) {
11
11
  let str = fs.readFileSync(path, "utf8");
@@ -21,7 +21,7 @@ function readJSON (path, {isIncludeRaw = false} = {}) {
21
21
  const filenames = fs.readdirSync(dir, "utf8");
22
22
  const filename = filenames.find(it => it.toLowerCase() === originalName.toLowerCase());
23
23
  if (!filename) throw new Error(`Could not find file "${path}"`);
24
- return readJSON(`${dir}/${filename}`, {isIncludeRaw});
24
+ return readJsonSync(`${dir}/${filename}`, {isIncludeRaw});
25
25
  }
26
26
  } catch (e) {
27
27
  e.message += ` (Path: ${path})`;
@@ -29,6 +29,10 @@ function readJSON (path, {isIncludeRaw = false} = {}) {
29
29
  }
30
30
  }
31
31
 
32
+ function writeJsonSync (filePath, data) {
33
+ return fs.writeFileSync(filePath, `${JSON.stringify(data, null, "\t")}\n`, "utf-8");
34
+ }
35
+
32
36
  /**
33
37
  * @param dir
34
38
  * @param [opts]
@@ -86,10 +90,66 @@ function mkDirs (pathToCreate) {
86
90
  }, "");
87
91
  }
88
92
 
93
+ function removeSync (path) {
94
+ if (fs.existsSync(path)) {
95
+ fs.readdirSync(path).forEach(file => {
96
+ const curPath = `${path}/${file}`;
97
+ if (fs.lstatSync(curPath).isDirectory()) removeSync(curPath);
98
+ else fs.unlinkSync(curPath);
99
+ });
100
+ fs.rmdirSync(path);
101
+ }
102
+ }
103
+
104
+ /**
105
+ * @param src
106
+ * @param dest
107
+ * @param [opts]
108
+ * @param [opts.isForce]
109
+ * @param [opts.isDryRun]
110
+ */
111
+ function copySync (src, dest, opts) {
112
+ opts = opts || {};
113
+ if (fs.existsSync(src) && fs.statSync(src).isDirectory()) {
114
+ if (opts.isDryRun) console.log(`Creating directory ${dest}`);
115
+ else fs.mkdirSync(dest, {recursive: true});
116
+
117
+ fs.readdirSync(src).forEach(child => copySync(`${src}/${child}`, `${dest}/${child}`, opts));
118
+ } else {
119
+ if (opts.isForce) {
120
+ if (opts.isDryRun) {
121
+ console.log(`\tRemoving ${dest}`);
122
+ } else {
123
+ if (fs.existsSync(dest)) fs.unlinkSync(dest);
124
+ }
125
+ }
126
+
127
+ if (opts.isDryRun) console.log(`\tCopying ${src} to ${dest}`);
128
+ else {
129
+ const dirName = path.dirname(dest);
130
+ if (dirName && !fs.existsSync(dirName)) fs.mkdirSync(dirName, {recursive: true});
131
+ fs.copyFileSync(src, dest);
132
+ }
133
+ }
134
+ }
135
+
136
+ function lsRecursiveSync (dir, fileList = []) {
137
+ fs.readdirSync(dir).forEach(file => {
138
+ fileList = fs.statSync(path.join(dir, file)).isDirectory()
139
+ ? lsRecursiveSync(path.join(dir, file), fileList)
140
+ : fileList.concat(path.join(dir, file));
141
+ });
142
+ return fileList;
143
+ }
144
+
89
145
  export {
90
- readJSON,
146
+ readJsonSync,
147
+ writeJsonSync,
91
148
  listJsonFiles,
92
149
  runOnDirs,
93
150
  pRunOnDirs,
94
151
  mkDirs,
152
+ removeSync,
153
+ copySync,
154
+ lsRecursiveSync,
95
155
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "5etools-utils",
3
- "version": "0.5.17",
3
+ "version": "0.6.0",
4
4
  "description": "Shared utilities for the 5etools ecosystem.",
5
5
  "type": "module",
6
6
  "main": "lib/Api.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "version": "1.21.10",
3
+ "version": "1.21.11",
4
4
  "title": "Bestiary Schema",
5
5
  "$id": "bestiary.json",
6
6
  "type": "object",
@@ -1146,6 +1146,26 @@
1146
1146
  "additionalProperties": false
1147
1147
  }
1148
1148
  },
1149
+ "resource": {
1150
+ "type": "array",
1151
+ "items": {
1152
+ "type": "object",
1153
+ "properties": {
1154
+ "value": {
1155
+ "type": "integer"
1156
+ },
1157
+ "formula": {
1158
+ "type": "string"
1159
+ }
1160
+ },
1161
+ "required": [
1162
+ "value"
1163
+ ],
1164
+ "additionalProperties": false
1165
+ },
1166
+ "uniqueItems": true,
1167
+ "minItems": 1
1168
+ },
1149
1169
  "fluff": {
1150
1170
  "description": "This is intended to be used for Homebrew only - site data should include a fluff file per source",
1151
1171
  "anyOf": [
@@ -2248,6 +2268,26 @@
2248
2268
  "additionalProperties": false
2249
2269
  }
2250
2270
  },
2271
+ "resource": {
2272
+ "type": "array",
2273
+ "items": {
2274
+ "type": "object",
2275
+ "properties": {
2276
+ "value": {
2277
+ "type": "integer"
2278
+ },
2279
+ "formula": {
2280
+ "type": "string"
2281
+ }
2282
+ },
2283
+ "required": [
2284
+ "value"
2285
+ ],
2286
+ "additionalProperties": false
2287
+ },
2288
+ "uniqueItems": true,
2289
+ "minItems": 1
2290
+ },
2251
2291
  "fluff": {
2252
2292
  "description": "This is intended to be used for Homebrew only - site data should include a fluff file per source",
2253
2293
  "anyOf": [
@@ -3365,6 +3405,26 @@
3365
3405
  "additionalProperties": false
3366
3406
  }
3367
3407
  },
3408
+ "resource": {
3409
+ "type": "array",
3410
+ "items": {
3411
+ "type": "object",
3412
+ "properties": {
3413
+ "value": {
3414
+ "type": "integer"
3415
+ },
3416
+ "formula": {
3417
+ "type": "string"
3418
+ }
3419
+ },
3420
+ "required": [
3421
+ "value"
3422
+ ],
3423
+ "additionalProperties": false
3424
+ },
3425
+ "uniqueItems": true,
3426
+ "minItems": 1
3427
+ },
3368
3428
  "fluff": {
3369
3429
  "description": "This is intended to be used for Homebrew only - site data should include a fluff file per source",
3370
3430
  "anyOf": [
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "$id": "spells.json",
4
- "version": "1.9.4",
4
+ "version": "1.9.5",
5
5
  "title": "Spells",
6
6
  "type": "object",
7
7
  "$defs": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "version": "1.21.10",
3
+ "version": "1.21.11",
4
4
  "title": "Bestiary Schema",
5
5
  "$id": "bestiary.json",
6
6
  "type": "object",
@@ -1146,6 +1146,26 @@
1146
1146
  "additionalProperties": false
1147
1147
  }
1148
1148
  },
1149
+ "resource": {
1150
+ "type": "array",
1151
+ "items": {
1152
+ "type": "object",
1153
+ "properties": {
1154
+ "value": {
1155
+ "type": "integer"
1156
+ },
1157
+ "formula": {
1158
+ "type": "string"
1159
+ }
1160
+ },
1161
+ "required": [
1162
+ "value"
1163
+ ],
1164
+ "additionalProperties": false
1165
+ },
1166
+ "uniqueItems": true,
1167
+ "minItems": 1
1168
+ },
1149
1169
  "fluff": {
1150
1170
  "description": "This is intended to be used for Homebrew only - site data should include a fluff file per source",
1151
1171
  "anyOf": [
@@ -2248,6 +2268,26 @@
2248
2268
  "additionalProperties": false
2249
2269
  }
2250
2270
  },
2271
+ "resource": {
2272
+ "type": "array",
2273
+ "items": {
2274
+ "type": "object",
2275
+ "properties": {
2276
+ "value": {
2277
+ "type": "integer"
2278
+ },
2279
+ "formula": {
2280
+ "type": "string"
2281
+ }
2282
+ },
2283
+ "required": [
2284
+ "value"
2285
+ ],
2286
+ "additionalProperties": false
2287
+ },
2288
+ "uniqueItems": true,
2289
+ "minItems": 1
2290
+ },
2251
2291
  "fluff": {
2252
2292
  "description": "This is intended to be used for Homebrew only - site data should include a fluff file per source",
2253
2293
  "anyOf": [
@@ -3365,6 +3405,26 @@
3365
3405
  "additionalProperties": false
3366
3406
  }
3367
3407
  },
3408
+ "resource": {
3409
+ "type": "array",
3410
+ "items": {
3411
+ "type": "object",
3412
+ "properties": {
3413
+ "value": {
3414
+ "type": "integer"
3415
+ },
3416
+ "formula": {
3417
+ "type": "string"
3418
+ }
3419
+ },
3420
+ "required": [
3421
+ "value"
3422
+ ],
3423
+ "additionalProperties": false
3424
+ },
3425
+ "uniqueItems": true,
3426
+ "minItems": 1
3427
+ },
3368
3428
  "fluff": {
3369
3429
  "description": "This is intended to be used for Homebrew only - site data should include a fluff file per source",
3370
3430
  "anyOf": [
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "$id": "spells.json",
4
- "version": "1.9.4",
4
+ "version": "1.9.5",
5
5
  "title": "Spells",
6
6
  "type": "object",
7
7
  "$defs": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "version": "1.21.10",
3
+ "version": "1.21.11",
4
4
  "title": "Bestiary Schema",
5
5
  "$id": "bestiary.json",
6
6
  "type": "object",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "$id": "spells.json",
4
- "version": "1.9.4",
4
+ "version": "1.9.5",
5
5
  "title": "Spells",
6
6
  "type": "object",
7
7
  "$defs": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "version": "1.21.10",
3
+ "version": "1.21.11",
4
4
  "title": "Bestiary Schema",
5
5
  "$id": "bestiary.json",
6
6
  "type": "object",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "$id": "spells.json",
4
- "version": "1.9.4",
4
+ "version": "1.9.5",
5
5
  "title": "Spells",
6
6
  "type": "object",
7
7
  "$defs": {