boma 2.0.4 → 2.0.5
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/dist/index.d.ts +5 -0
- package/dist/index.js +14 -10
- package/package.json +1 -1
- package/readme.md +21 -2
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface ReadJSONProps {
|
|
|
10
10
|
filePath: string;
|
|
11
11
|
parseJSON?: boolean;
|
|
12
12
|
createIfNotFound?: boolean | SerializableObject | SerializableArray;
|
|
13
|
+
silent?: boolean;
|
|
13
14
|
}
|
|
14
15
|
export interface SaveJSONProps {
|
|
15
16
|
filePath: string;
|
|
@@ -17,6 +18,7 @@ export interface SaveJSONProps {
|
|
|
17
18
|
format?: boolean;
|
|
18
19
|
logSaving?: boolean;
|
|
19
20
|
replaceNonSerializable?: boolean;
|
|
21
|
+
silent?: boolean;
|
|
20
22
|
}
|
|
21
23
|
export interface addToJSONProps {
|
|
22
24
|
filePath: string;
|
|
@@ -24,6 +26,7 @@ export interface addToJSONProps {
|
|
|
24
26
|
format?: boolean;
|
|
25
27
|
logSaving?: boolean;
|
|
26
28
|
replaceNonSerializable?: boolean;
|
|
29
|
+
silent?: boolean;
|
|
27
30
|
}
|
|
28
31
|
export type ErrorWithCode = Error & {
|
|
29
32
|
code: string;
|
|
@@ -41,6 +44,8 @@ export interface SerializationIssue {
|
|
|
41
44
|
export declare const isErrorWithCode: (error: unknown) => error is ErrorWithCode;
|
|
42
45
|
export declare const isErrorWithMessage: (error: unknown) => error is ErrorWithMessage;
|
|
43
46
|
export declare const isSyntaxError: (error: unknown) => error is SyntaxError;
|
|
47
|
+
export declare const isObjectLike: (value: unknown) => value is object;
|
|
48
|
+
export declare const isPlainMergeableObject: (value: unknown) => value is Record<string, unknown>;
|
|
44
49
|
export declare const getSerializationIssues: (value: unknown, path?: string, ancestors?: WeakMap<object, string>, issues?: SerializationIssue[]) => SerializationIssue[];
|
|
45
50
|
export declare const isSerializable: (value: unknown) => value is Serializable;
|
|
46
51
|
export declare const sanitizeNonSerializable: (value: unknown, path?: string, ancestors?: WeakMap<object, string>) => Serializable;
|
package/dist/index.js
CHANGED
|
@@ -12,10 +12,10 @@ const getDate = () => {
|
|
|
12
12
|
const castedDate = new Date();
|
|
13
13
|
return castedDate.toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit' });
|
|
14
14
|
};
|
|
15
|
-
const isObjectLike = (value) => {
|
|
15
|
+
export const isObjectLike = (value) => {
|
|
16
16
|
return typeof value === 'object' && value !== null;
|
|
17
17
|
};
|
|
18
|
-
const isPlainMergeableObject = (value) => {
|
|
18
|
+
export const isPlainMergeableObject = (value) => {
|
|
19
19
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
20
20
|
};
|
|
21
21
|
const pathJoin = (basePath, key) => {
|
|
@@ -35,7 +35,7 @@ const getIssueMessage = (issue) => {
|
|
|
35
35
|
}
|
|
36
36
|
return `Field "${issue.path}" has non-serializable value of type "${issue.kind}"`;
|
|
37
37
|
};
|
|
38
|
-
export const getSerializationIssues = (value, path = '
|
|
38
|
+
export const getSerializationIssues = (value, path = '[OBJECT]', ancestors = new WeakMap(), issues = []) => {
|
|
39
39
|
if (value === null) {
|
|
40
40
|
return issues;
|
|
41
41
|
}
|
|
@@ -185,7 +185,7 @@ const logSerializationIssues = (issues) => {
|
|
|
185
185
|
}
|
|
186
186
|
};
|
|
187
187
|
export const saveJSON = (saveInput) => {
|
|
188
|
-
const { filePath, objToSave, format = false, logSaving = false, replaceNonSerializable = false, } = saveInput;
|
|
188
|
+
const { filePath, objToSave, format = false, logSaving = false, replaceNonSerializable = false, silent = true, } = saveInput;
|
|
189
189
|
const issues = getSerializationIssues(objToSave);
|
|
190
190
|
if (issues.length > 0) {
|
|
191
191
|
logSerializationIssues(issues);
|
|
@@ -202,7 +202,7 @@ export const saveJSON = (saveInput) => {
|
|
|
202
202
|
try {
|
|
203
203
|
const json = format ? JSON.stringify(valueToSave, null, 2) : JSON.stringify(valueToSave);
|
|
204
204
|
writeFileSync(filePath, json, 'utf8');
|
|
205
|
-
logSaving && console.log(`[${getDate()}] Write file ${filePath} successfully`);
|
|
205
|
+
!silent && logSaving && console.log(`[${getDate()}] Write file ${filePath} successfully`);
|
|
206
206
|
}
|
|
207
207
|
catch (error) {
|
|
208
208
|
if (isErrorWithCode(error)) {
|
|
@@ -212,7 +212,7 @@ export const saveJSON = (saveInput) => {
|
|
|
212
212
|
}
|
|
213
213
|
};
|
|
214
214
|
export const readJSON = (props) => {
|
|
215
|
-
const { filePath, createIfNotFound = false, parseJSON = true } = props;
|
|
215
|
+
const { filePath, createIfNotFound = false, parseJSON = true, silent = true } = props;
|
|
216
216
|
try {
|
|
217
217
|
const savedfile = readFileSync(filePath, 'utf8');
|
|
218
218
|
if (parseJSON) {
|
|
@@ -225,7 +225,7 @@ export const readJSON = (props) => {
|
|
|
225
225
|
switch (err.code) {
|
|
226
226
|
case 'ENOENT':
|
|
227
227
|
if (createIfNotFound) {
|
|
228
|
-
console.log('Try to create: ', filePath);
|
|
228
|
+
!silent && console.log('Try to create: ', filePath);
|
|
229
229
|
try {
|
|
230
230
|
const initialContent = typeof createIfNotFound === 'boolean' ? '{}' : JSON.stringify(createIfNotFound);
|
|
231
231
|
writeFileSync(filePath, initialContent, 'utf8');
|
|
@@ -235,7 +235,7 @@ export const readJSON = (props) => {
|
|
|
235
235
|
}
|
|
236
236
|
return typeof createIfNotFound === 'boolean' ? {} : createIfNotFound;
|
|
237
237
|
}
|
|
238
|
-
console.
|
|
238
|
+
console.info("Boma can't find file: ", filePath);
|
|
239
239
|
return parseJSON ? {} : null;
|
|
240
240
|
case 'EACCES':
|
|
241
241
|
console.error(`Access denied for ${filePath}`);
|
|
@@ -254,8 +254,8 @@ export const readJSON = (props) => {
|
|
|
254
254
|
}
|
|
255
255
|
};
|
|
256
256
|
export const addToJSON = (saveInput) => {
|
|
257
|
-
const { filePath, dataToAdd, format = false, logSaving = false, replaceNonSerializable = false, } = saveInput;
|
|
258
|
-
const oldJSON = readJSON({ filePath, createIfNotFound: true });
|
|
257
|
+
const { filePath, dataToAdd, format = false, logSaving = false, replaceNonSerializable = false, silent = true, } = saveInput;
|
|
258
|
+
const oldJSON = readJSON({ filePath, createIfNotFound: true, silent });
|
|
259
259
|
if (oldJSON === null || typeof oldJSON !== 'object') {
|
|
260
260
|
return saveJSON({
|
|
261
261
|
filePath,
|
|
@@ -263,6 +263,7 @@ export const addToJSON = (saveInput) => {
|
|
|
263
263
|
format,
|
|
264
264
|
logSaving,
|
|
265
265
|
replaceNonSerializable,
|
|
266
|
+
silent
|
|
266
267
|
});
|
|
267
268
|
}
|
|
268
269
|
if (Array.isArray(oldJSON) && Array.isArray(dataToAdd)) {
|
|
@@ -272,6 +273,7 @@ export const addToJSON = (saveInput) => {
|
|
|
272
273
|
format,
|
|
273
274
|
logSaving,
|
|
274
275
|
replaceNonSerializable,
|
|
276
|
+
silent
|
|
275
277
|
});
|
|
276
278
|
return;
|
|
277
279
|
}
|
|
@@ -282,6 +284,7 @@ export const addToJSON = (saveInput) => {
|
|
|
282
284
|
format,
|
|
283
285
|
logSaving,
|
|
284
286
|
replaceNonSerializable,
|
|
287
|
+
silent
|
|
285
288
|
});
|
|
286
289
|
return;
|
|
287
290
|
}
|
|
@@ -292,6 +295,7 @@ export const addToJSON = (saveInput) => {
|
|
|
292
295
|
format,
|
|
293
296
|
logSaving,
|
|
294
297
|
replaceNonSerializable,
|
|
298
|
+
silent
|
|
295
299
|
});
|
|
296
300
|
return;
|
|
297
301
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -6,7 +6,12 @@
|
|
|
6
6
|
# Usage
|
|
7
7
|
|
|
8
8
|
```JS
|
|
9
|
-
const logs = readJSON({
|
|
9
|
+
const logs = readJSON({
|
|
10
|
+
filePath: '/support.log',
|
|
11
|
+
createIfNotFound: {},
|
|
12
|
+
parseJSON: true,
|
|
13
|
+
silent: true, // Do not log warnings (only file not found will be logged)
|
|
14
|
+
});
|
|
10
15
|
/*
|
|
11
16
|
logs = {
|
|
12
17
|
any: {}, //
|
|
@@ -18,7 +23,8 @@ saveJSON({
|
|
|
18
23
|
filePath: '/test.json',
|
|
19
24
|
objToSave: { any: {}, key: {} },
|
|
20
25
|
format: true,
|
|
21
|
-
logSaving: false
|
|
26
|
+
logSaving: false,
|
|
27
|
+
silent: true, // Do not log warnings
|
|
22
28
|
}); // Format for save line break's
|
|
23
29
|
/* Will save:
|
|
24
30
|
{
|
|
@@ -40,6 +46,8 @@ addToJSON({
|
|
|
40
46
|
```
|
|
41
47
|
|
|
42
48
|
## Types
|
|
49
|
+
Main types:
|
|
50
|
+
|
|
43
51
|
```typescript
|
|
44
52
|
export type SerializablePrimitive = string | number | boolean | null;
|
|
45
53
|
export type SerializableArray = Serializable[];
|
|
@@ -58,6 +66,7 @@ export interface SaveJSONProps {
|
|
|
58
66
|
format?: boolean
|
|
59
67
|
logSaving?: boolean;
|
|
60
68
|
replaceNonSerializable?: boolean; // Заменять несереализуемые значения их типом
|
|
69
|
+
silent?: boolean; // Не выводим предупреждения
|
|
61
70
|
}
|
|
62
71
|
|
|
63
72
|
export interface addToJSONProps {
|
|
@@ -65,5 +74,15 @@ export interface addToJSONProps {
|
|
|
65
74
|
dataToAdd: SerializableObject | SerializableArray;
|
|
66
75
|
format?: boolean
|
|
67
76
|
logSaving?: boolean;
|
|
77
|
+
silent?: boolean; // Не выводим предупреждения
|
|
68
78
|
}
|
|
79
|
+
|
|
80
|
+
// We also export some helpers:
|
|
81
|
+
export const isSerializable = (value: unknown) => boolean;
|
|
82
|
+
export const isObjectLike = (value: unknown) => boolean;
|
|
83
|
+
export const isPlainMergeableObject = (value: unknown) => boolean;
|
|
84
|
+
|
|
85
|
+
export const isErrorWithCode = (error: unknown) => boolean;
|
|
86
|
+
export const isErrorWithMessage = (error: unknown) => boolean;
|
|
87
|
+
export const isSyntaxError = (error: unknown) => boolean;
|
|
69
88
|
```
|