@beauraines/node-helpers 2.4.4 → 2.5.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/CHANGELOG.md +10 -1
- package/package.json +1 -1
- package/src/config.js +80 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,12 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## [2.5.0](https://github.com/beauraines/node-helpers/compare/v2.4.3...v2.5.0) (2023-06-03)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* new config module ([#32](https://github.com/beauraines/node-helpers/issues/32)) ([514df50](https://github.com/beauraines/node-helpers/commit/514df509ca33527a8b18e5efe43d1b772963e879))
|
|
6
11
|
|
|
7
12
|
|
|
8
13
|
### Bug Fixes
|
|
9
14
|
|
|
10
15
|
* **deps:** bump @azure/storage-queue from 12.12.0 to 12.13.0 ([45acd3d](https://github.com/beauraines/node-helpers/commit/45acd3d808ab064c1dbc4b492a013c7b60ebf5b2))
|
|
16
|
+
* **deps:** bump dayjs from 1.11.7 to 1.11.8 ([2208985](https://github.com/beauraines/node-helpers/commit/22089855cd90758273bac28fa1d8669d21f661b5))
|
|
17
|
+
* **deps:** bump node-fetch from 2.6.9 to 2.6.11 ([d9fbed2](https://github.com/beauraines/node-helpers/commit/d9fbed2e6ade418caada9cc68f6ac05ba4d3159d))
|
|
18
|
+
* **deps:** bump sqlite from 4.1.2 to 4.2.0 ([5634e40](https://github.com/beauraines/node-helpers/commit/5634e400a0dea024a40a890da639a167df01cf25))
|
|
19
|
+
* **deps:** bump sqlite from 4.2.0 to 4.2.1 ([61b6533](https://github.com/beauraines/node-helpers/commit/61b6533d95c456114de9d8ca01dd30be204af2f9))
|
|
11
20
|
|
|
12
21
|
### [2.4.3](https://github.com/beauraines/node-helpers/compare/v2.4.0...v2.4.3) (2023-03-06)
|
|
13
22
|
|
package/package.json
CHANGED
package/src/config.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const {fileExists} = require('./helpers');
|
|
3
|
+
const {homedir} = require('os');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Reads the specified config file from the users home directory
|
|
8
|
+
*
|
|
9
|
+
* @param {string} configFile file name for the JSON config file, from the users home directory
|
|
10
|
+
*
|
|
11
|
+
* @throws {Config file not found. You must create one using the config command}
|
|
12
|
+
* @returns {object} the configuration object
|
|
13
|
+
*/
|
|
14
|
+
const readConfig = async (configFile) => {
|
|
15
|
+
configFile = path.join(homedir(),configFile)
|
|
16
|
+
let config
|
|
17
|
+
if ( await fileExists(configFile) ) {
|
|
18
|
+
config = fs.readFileSync(configFile,
|
|
19
|
+
{ encoding: 'utf8', flag: 'r' });
|
|
20
|
+
config = JSON.parse(config);
|
|
21
|
+
} else {
|
|
22
|
+
throw new Error(`Config file not found. You must create one using the config command`)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return config
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Validates the config file to ensure that it has all of the required properties specified. This
|
|
30
|
+
* is only schema validation, it does not check if the properties are valid values or data types.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} configFile file name for the JSON config file, from the users home directory
|
|
33
|
+
* @param {Array} configProps an array of properties that defines a valid config file
|
|
34
|
+
*
|
|
35
|
+
* @throws {Configuration file not found}
|
|
36
|
+
* @throws {Invalid configuration file}
|
|
37
|
+
* @returns {boolean}
|
|
38
|
+
*/
|
|
39
|
+
const validateConfig = async (configFile, configProps) => {
|
|
40
|
+
configFile = path.join(homedir(),configFile)
|
|
41
|
+
if (! fs.existsSync(configFile) ) {
|
|
42
|
+
throw new Error('Configuration file not found')
|
|
43
|
+
}
|
|
44
|
+
let config
|
|
45
|
+
config = fs.readFileSync(configFile,
|
|
46
|
+
{ encoding: 'utf8', flag: 'r' });
|
|
47
|
+
config = JSON.parse(config);
|
|
48
|
+
// Check for properties
|
|
49
|
+
let validConfig = true
|
|
50
|
+
for (key of configProps) {
|
|
51
|
+
validConfig = config[key] ? true : false
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!validConfig) {
|
|
55
|
+
throw Error('Invalid configuration file')
|
|
56
|
+
}
|
|
57
|
+
return validConfig
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Creates a boilerplate config file in the user's home directory
|
|
62
|
+
*
|
|
63
|
+
* @param {string} configFile file name for the JSON config file, from the users home directory
|
|
64
|
+
* @param {Array} configProps an array of properties that defines a valid config file
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
const createConfig = async (configFile,configProps) => {
|
|
68
|
+
configFile = path.join(homedir(),configFile)
|
|
69
|
+
let config = {}
|
|
70
|
+
for (key of configProps) {
|
|
71
|
+
config[key] = ''
|
|
72
|
+
}
|
|
73
|
+
fs.writeFileSync(configFile,JSON.stringify(config))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = {
|
|
77
|
+
createConfig,
|
|
78
|
+
readConfig,
|
|
79
|
+
validateConfig
|
|
80
|
+
}
|