5etools-utils 0.5.18 → 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.18",
3
+ "version": "0.6.0",
4
4
  "description": "Shared utilities for the 5etools ecosystem.",
5
5
  "type": "module",
6
6
  "main": "lib/Api.js",