@ygracs/chn-alias-list 0.0.3

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.
@@ -0,0 +1,245 @@
1
+ // [v0.1.026-20241021]
2
+
3
+ // === module init block ===
4
+
5
+ const fs = require('fs');
6
+
7
+ const {
8
+ isPlainObject,
9
+ } = require('@ygracs/bsfoc-lib-js');
10
+
11
+ // === module extra block (helper functions) ===
12
+
13
+ /**
14
+ * @typedef {Object} fsoDescr
15
+ * @property {boolean} isERR - flag
16
+ * @property {number|undefined} errCode - error code
17
+ * @property {string} errEvent - event ID
18
+ * @property {string} errMsg - event message
19
+ * @property {string} source - path to file
20
+ * @property {any} content - file content
21
+ * @description A fs ops description.
22
+ */
23
+
24
+ /**
25
+ * @typedef {Object} VCOR_evalerrfs
26
+ * @property {boolean} isSucceed - ops flag
27
+ * @property {fsoDescr} descr - description
28
+ * @description A result of an error check ops.
29
+ */
30
+
31
+ /**
32
+ * @function checkFsError
33
+ * @param {fsoDescr} descr
34
+ * @param {Error} err
35
+ * @returns {VCOR_evalerrfs}
36
+ * @inner
37
+ * @description Checks an error code of a fs ops and sets descr info if succeed
38
+ */
39
+ function checkFsError(descr, err) {
40
+ let isSucceed = false;
41
+ if (isPlainObject(err) && isPlainObject(descr)) {
42
+ switch (err.code) {
43
+ case 'ENOENT':
44
+ case 'EISDIR':
45
+ case 'ENOTDIR':
46
+ case 'EPERM': {
47
+ descr.errCode = err.errno;
48
+ descr.errEvent = err.code;
49
+ descr.errMsg = `ERR_FILE_${err.code}`;
50
+ isSucceed = true;
51
+ break;
52
+ }
53
+ default: {}
54
+ };
55
+ if (isSucceed) descr.isERR = true;
56
+ };
57
+ return { isSucceed, descr };
58
+ };
59
+
60
+ /**
61
+ * @function loadFileSync
62
+ * @param {string} src - a path to some file
63
+ * @returns {fsoDescr}
64
+ * @inner
65
+ * @description Loads file by a given path
66
+ */
67
+ function loadFileSync(src) {
68
+ /** @type {fsoDescr} */
69
+ const data = {
70
+ isERR: false,
71
+ errCode: 0,
72
+ errEvent: '',
73
+ errMsg: '',
74
+ source: '',
75
+ content: '',
76
+ };
77
+ if (typeof src !== 'string') {
78
+ if (src !== undefined) {
79
+ data.isERR = true;
80
+ data.errEvent = 'ERR_INVARG';
81
+ data.errMsg = 'The "source" argument must be a string';
82
+ };
83
+ } else {
84
+ const srcPath = src.trim();
85
+ if (srcPath !== '') {
86
+ data.source = srcPath;
87
+ try {
88
+ data.content = fs.readFileSync(srcPath, 'utf8');
89
+ } catch (err) {
90
+ const { isSucceed } = checkFsError(data, err);
91
+ if (!isSucceed) {
92
+ // // TODO: [?] consider check others
93
+ //console.log(`TEST_ERR-MSG:loadFileSync: ${err}`);
94
+ //throw err;
95
+ };
96
+ };
97
+ };
98
+ };
99
+ return data;
100
+ };
101
+
102
+ /**
103
+ * @function saveToFileSync
104
+ * @param {string} src - a path to some file
105
+ * @param {string} content - some file content
106
+ * @param {any} [opt] - <reserved>
107
+ * @returns {fsoDescr}
108
+ * @inner
109
+ * @description Saves a content to a file
110
+ */
111
+ function saveToFileSync(src, content = '', opt) {
112
+ /** @type {fsoDescr} */
113
+ const data = {
114
+ isERR: false,
115
+ errCode: 0,
116
+ errEvent: '',
117
+ errMsg: '',
118
+ source: '',
119
+ content: '',
120
+ };
121
+ if (typeof src !== 'string') {
122
+ data.isERR = true;
123
+ data.errEvent = src === undefined ? 'ERR_NOSRC' : 'ERR_INVARG';
124
+ data.errMsg = 'The "source" argument must be a string';
125
+ } else if (typeof content === 'string') {
126
+ const srcPath = src.trim();
127
+ if (srcPath === '') {
128
+ data.isERR = true;
129
+ data.errEvent = 'ERR_NOSRC';
130
+ data.errMsg = 'No "source" path given';
131
+ } else {
132
+ data.source = srcPath;
133
+ try {
134
+ fs.writeFileSync(srcPath, content, 'utf8');
135
+ } catch (err) {
136
+ const { isSucceed } = checkFsError(data, err);
137
+ if (!isSucceed) {
138
+ // // TODO: [?] consider check others
139
+ //console.log(`TEST_ERR-MSG:saveToFileSync: ${err}`);
140
+ //throw err;
141
+ };
142
+ };
143
+ };
144
+ } else {
145
+ data.isERR = true;
146
+ data.errEvent = 'ERR_INVARG';
147
+ data.errMsg = 'The "content" argument must be a string';
148
+ };
149
+ return data;
150
+ };
151
+
152
+ // === module main block ===
153
+
154
+ /***
155
+ * (* constant definitions *)
156
+ */
157
+
158
+ /***
159
+ * (* function definitions *)
160
+ */
161
+
162
+ /**
163
+ * @typedef {Object} RVAL_loadjsonff
164
+ * @property {fsoDescr} descr - ops description
165
+ * @property {any} obj - loaded content
166
+ * @description A result of `loadJSONFromFileSync`
167
+ */
168
+
169
+ /**
170
+ * @function loadJSONFromFileSync
171
+ * @param {string} src - a path to some file
172
+ * @param {any} [opt] - <reserved>
173
+ * @returns {RVAL_loadjsonff}
174
+ * @inner
175
+ * @description Loads a JSON-object from a file
176
+ */
177
+ function loadJSONFromFileSync(src, opt) {
178
+ const data = loadFileSync(src);
179
+ let obj = null;
180
+ if (!data.isERR) {
181
+ if (data.content !== '') {
182
+ try {
183
+ obj = JSON.parse(data.content);
184
+ // if succeed clear <data.content>
185
+ data.content = '';
186
+ } catch (err) {
187
+ data.isERR = true;
188
+ //console.log(`TEST_ERR-CODE: [${err.code}](${err.errno})`);
189
+ //console.log(`TEST_ERR-MSG: ${err}`);
190
+ if (err instanceof SyntaxError) {
191
+ data.errEvent = 'ERR_JSON_BADDATA';
192
+ data.errMsg = err.message;
193
+ } else {
194
+ data.errEvent = 'ERR_JSON_UNKNOWN';
195
+ };
196
+ };
197
+ };
198
+ };
199
+ return { descr: data, obj };
200
+ };
201
+
202
+ /**
203
+ * @function saveJSONToFileSync
204
+ * @param {string} src - a path to some file
205
+ * @param {object} obj - some content
206
+ * @param {any} [opt] - <reserved>
207
+ * @returns {fsoDescr}
208
+ * @inner
209
+ * @description Saves a JSON-object to a file
210
+ */
211
+ function saveJSONToFileSync(src, obj, opt) {
212
+ /** @type {fsoDescr} */
213
+ let data = {
214
+ isERR: false,
215
+ errCode: 0,
216
+ errEvent: '',
217
+ errMsg: '',
218
+ source: '',
219
+ content: '',
220
+ };
221
+ try {
222
+ data.content = JSON.stringify(obj, null, 2);
223
+ } catch (err) {
224
+ data.isERR = true;
225
+ //console.log(`TEST_ERR-CODE: [${err.code}](${err.errno})`);
226
+ //console.log(`TEST_ERR-MSG: ${err}`);
227
+ data.errEvent = 'ERR_JSON_UNKNOWN';
228
+ };
229
+ if (!data.isERR) {
230
+ data = saveToFileSync(src, data.content, opt);
231
+ };
232
+ return data;
233
+ };
234
+
235
+ /***
236
+ * (* class definitions *)
237
+ */
238
+
239
+ // === module exports block ===
240
+
241
+ module.exports.checkFsError = checkFsError;
242
+ module.exports.loadFileSync = loadFileSync;
243
+ module.exports.saveToFileSync = saveToFileSync;
244
+ module.exports.loadJSONFromFileSync = loadJSONFromFileSync;
245
+ module.exports.saveJSONToFileSync = saveJSONToFileSync;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@ygracs/chn-alias-list",
3
+ "version": "0.0.3",
4
+ "description": "A small library which provides some helper classes for EPG-tools",
5
+ "author": "ygracs <cs70th-om@rambler.ru>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://gitlab.com/ygracs/chn-alias-list.git"
10
+ },
11
+ "main": "./index.js",
12
+ "files": [
13
+ "doc/chn-alias-list.md",
14
+ "lib/chn-alias-list.js",
15
+ "lib/file-helper.js",
16
+ "index.js",
17
+ "CHANGELOG.md"
18
+ ],
19
+ "scripts": {
20
+ "test": "jest",
21
+ "build-doc-md": "jsdoc2md",
22
+ "build-doc-html": "jsdoc"
23
+ },
24
+ "imports": {
25
+ "#lib/*": "./lib/*",
26
+ "#test-dir/*": "./__test__/*"
27
+ },
28
+ "dependencies": {
29
+ "@ygracs/bsfoc-lib-js": "^0.2.1"
30
+ },
31
+ "devDependencies": {
32
+ "jest": "^29.7.0",
33
+ "jsdoc-to-markdown": "^9.0.4",
34
+ "minimist": "^1.2.8"
35
+ }
36
+ }