boma 1.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Gratio-tech
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,23 @@
1
+ export type SerializablePrimitive = string | number | boolean | null;
2
+ export type SerializableArray = Serializable[];
3
+ export type SerializableObject = {
4
+ [key: string]: Serializable;
5
+ };
6
+ export type Serializable = SerializablePrimitive | SerializableArray | SerializableObject;
7
+ export interface ReadJSONProps {
8
+ filePath: string;
9
+ parseJSON?: boolean;
10
+ createIfNotFound?: boolean | SerializableObject | SerializableArray;
11
+ }
12
+ export type ErrorWithCode = Error & {
13
+ code: string;
14
+ };
15
+ export type ErrorWithMessage = Error & {
16
+ message: any;
17
+ };
18
+ export declare const isErrorWithCode: (error: unknown) => error is ErrorWithCode;
19
+ export declare const isErrorWithMessage: (error: unknown) => error is ErrorWithMessage;
20
+ export declare const isSyntaxError: (error: unknown) => error is SyntaxError;
21
+ export declare const saveJSON: (filePath: string, objToSave: Serializable, format?: boolean) => void;
22
+ export declare const readJSON: (props: ReadJSONProps) => any;
23
+ export declare const addToJSON: (filePath: string, objToAdd: SerializableObject | SerializableArray) => void;
package/dist/index.js ADDED
@@ -0,0 +1,82 @@
1
+ import { readFileSync, writeFileSync } from 'fs';
2
+ export const isErrorWithCode = (error) => {
3
+ return error instanceof Error && 'code' in error;
4
+ };
5
+ export const isErrorWithMessage = (error) => {
6
+ return error instanceof Error && 'message' in error;
7
+ };
8
+ export const isSyntaxError = (error) => {
9
+ return error instanceof SyntaxError;
10
+ };
11
+ export const saveJSON = (filePath, objToSave, format) => {
12
+ try {
13
+ const json = format ? JSON.stringify(objToSave, null, 2) : JSON.stringify(objToSave);
14
+ writeFileSync(filePath, json, 'utf8');
15
+ }
16
+ catch (error) {
17
+ if (isErrorWithCode(error)) {
18
+ console.error(`Filesystem error for ${filePath}:`, error);
19
+ }
20
+ throw error;
21
+ }
22
+ };
23
+ export const readJSON = (props) => {
24
+ const { filePath, createIfNotFound = false, parseJSON = true } = props;
25
+ try {
26
+ const savedfile = readFileSync(filePath, 'utf8');
27
+ if (parseJSON) {
28
+ return savedfile.trim() === "" ? null : JSON.parse(savedfile);
29
+ }
30
+ return savedfile;
31
+ }
32
+ catch (err) {
33
+ if (isErrorWithCode(err)) {
34
+ switch (err.code) {
35
+ case 'ENOENT':
36
+ if (createIfNotFound) {
37
+ console.log('Try to create: ', filePath);
38
+ try {
39
+ const initialContent = typeof createIfNotFound === 'boolean' ? '{}' : JSON.stringify(createIfNotFound);
40
+ writeFileSync(filePath, initialContent, 'utf8');
41
+ }
42
+ catch (writeErr) {
43
+ console.error(`Error creating file ${filePath}: `, writeErr, '\n');
44
+ }
45
+ return typeof createIfNotFound === 'boolean' ? {} : createIfNotFound;
46
+ }
47
+ console.log('File not found: ', filePath);
48
+ return parseJSON ? {} : null;
49
+ case 'EACCES':
50
+ console.error(`Access denied for ${filePath}`);
51
+ return null;
52
+ default:
53
+ console.error(`Some filesystem error when try readJSON: ${filePath}`);
54
+ return null;
55
+ }
56
+ }
57
+ if (isSyntaxError(err)) {
58
+ console.error('File ', filePath, ' has incorrect JSON syntax', '\n');
59
+ return null;
60
+ }
61
+ console.error('Function readJSON error:', err, '\n');
62
+ return null;
63
+ }
64
+ };
65
+ export const addToJSON = (filePath, objToAdd) => {
66
+ const oldJSON = readJSON({ filePath, createIfNotFound: true });
67
+ if (oldJSON === null || typeof oldJSON !== 'object') {
68
+ return saveJSON(filePath, objToAdd);
69
+ }
70
+ if (Array.isArray(oldJSON) && Array.isArray(objToAdd)) {
71
+ saveJSON(filePath, [...oldJSON, ...objToAdd]);
72
+ }
73
+ else if (!Array.isArray(oldJSON) && !Array.isArray(objToAdd)) {
74
+ saveJSON(filePath, { ...oldJSON, ...objToAdd });
75
+ }
76
+ else if (Array.isArray(objToAdd) && Object.keys(oldJSON).length === 0) {
77
+ saveJSON(filePath, [...objToAdd]);
78
+ }
79
+ else {
80
+ throw new Error('Cannot merge array with object');
81
+ }
82
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "boma",
3
+ "version": "1.0.0",
4
+ "description": "Primitive JSON helper",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "type": "module",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "prepublishOnly": "npm run build"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/Psychosynthesis/Boma.git"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "import": {
19
+ "default": "./dist/index.js",
20
+ "types": "./dist/index.d.ts"
21
+ }
22
+ }
23
+ },
24
+ "author": "Nick",
25
+ "license": "MIT",
26
+ "bugs": {
27
+ "url": "https://github.com/Psychosynthesis/Boma/issues"
28
+ },
29
+ "homepage": "https://github.com/Psychosynthesis/Boma#readme",
30
+ "devDependencies": {
31
+ "@types/node": "^22.13.10",
32
+ "typescript": "^5.8.x"
33
+ }
34
+ }
package/readme.md ADDED
@@ -0,0 +1,2 @@
1
+ # Boma JSON helper
2
+ Super-simple helper for working with JSON