@beauraines/node-helpers 2.4.4 → 2.5.2
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 +14 -1
- package/index.js +2 -0
- package/package.json +1 -1
- package/src/config.js +80 -0
- package/src/database.js +3 -3
- package/src/database.test.js +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,12 +2,25 @@
|
|
|
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
|
-
### [2.
|
|
5
|
+
### [2.5.2](https://github.com/beauraines/node-helpers/compare/v2.5.0...v2.5.2) (2023-06-03)
|
|
6
|
+
|
|
7
|
+
## [2.5.0](https://github.com/beauraines/node-helpers/compare/v2.4.3...v2.5.0) (2023-06-03)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* new config module ([#32](https://github.com/beauraines/node-helpers/issues/32)) ([514df50](https://github.com/beauraines/node-helpers/commit/514df509ca33527a8b18e5efe43d1b772963e879))
|
|
13
|
+
* supports cross OS file paths ([#33](https://github.com/beauraines/node-helpers/issues/33)) ([27f5e27](https://github.com/beauraines/node-helpers/commit/27f5e27d02408bff07220c3e82bd90186023b324)), closes [#24](https://github.com/beauraines/node-helpers/issues/24)
|
|
6
14
|
|
|
7
15
|
|
|
8
16
|
### Bug Fixes
|
|
9
17
|
|
|
18
|
+
* adds export of new config module ([#34](https://github.com/beauraines/node-helpers/issues/34)) ([81c7a27](https://github.com/beauraines/node-helpers/commit/81c7a27696da3d9a0b6fb3a215418185c9b1d153))
|
|
10
19
|
* **deps:** bump @azure/storage-queue from 12.12.0 to 12.13.0 ([45acd3d](https://github.com/beauraines/node-helpers/commit/45acd3d808ab064c1dbc4b492a013c7b60ebf5b2))
|
|
20
|
+
* **deps:** bump dayjs from 1.11.7 to 1.11.8 ([2208985](https://github.com/beauraines/node-helpers/commit/22089855cd90758273bac28fa1d8669d21f661b5))
|
|
21
|
+
* **deps:** bump node-fetch from 2.6.9 to 2.6.11 ([d9fbed2](https://github.com/beauraines/node-helpers/commit/d9fbed2e6ade418caada9cc68f6ac05ba4d3159d))
|
|
22
|
+
* **deps:** bump sqlite from 4.1.2 to 4.2.0 ([5634e40](https://github.com/beauraines/node-helpers/commit/5634e400a0dea024a40a890da639a167df01cf25))
|
|
23
|
+
* **deps:** bump sqlite from 4.2.0 to 4.2.1 ([61b6533](https://github.com/beauraines/node-helpers/commit/61b6533d95c456114de9d8ca01dd30be204af2f9))
|
|
11
24
|
|
|
12
25
|
### [2.4.3](https://github.com/beauraines/node-helpers/compare/v2.4.0...v2.4.3) (2023-03-06)
|
|
13
26
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
const AzureStorage = require("./src/azure")
|
|
3
|
+
const config = require('./src/config.js')
|
|
3
4
|
const credentials = require("./src/credentials.js");
|
|
4
5
|
const database = require("./src/database");
|
|
5
6
|
const helpers = require("./src/helpers");
|
|
@@ -7,6 +8,7 @@ const jira = require("./src/jira");
|
|
|
7
8
|
|
|
8
9
|
module.exports = {
|
|
9
10
|
AzureStorage,
|
|
11
|
+
config,
|
|
10
12
|
credentials,
|
|
11
13
|
database,
|
|
12
14
|
helpers,
|
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
|
+
}
|
package/src/database.js
CHANGED
|
@@ -2,15 +2,15 @@ const {homedir} = require('os');
|
|
|
2
2
|
const sqlite = require('sqlite');
|
|
3
3
|
const sqlite3 = require('sqlite3');
|
|
4
4
|
const {fileExists} = require('./helpers');
|
|
5
|
+
const path = require('path');
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Opens the BurnDownStatus SQLite3 Database
|
|
8
|
-
* @param file file name of the SQLite3 DB. If not provided, defaults to
|
|
9
|
+
* @param file file name of the SQLite3 DB. If not provided, defaults to BurnDownStatus.db in the users home
|
|
9
10
|
* @returns SQLite database connection
|
|
10
11
|
*/
|
|
11
12
|
async function getDBConnection(file) {
|
|
12
|
-
|
|
13
|
-
file = file ? file : `${homeDir}/BurnDownStatus.db`;
|
|
13
|
+
file = file ? file : path.join(homedir(),'BurnDownStatus.db');
|
|
14
14
|
if (! await fileExists(file)){
|
|
15
15
|
console.error(`${file} not found`);
|
|
16
16
|
// ! Separation of concerns - this should probably not be doing the exiting, but it is.
|
package/src/database.test.js
CHANGED
|
@@ -3,6 +3,7 @@ const sqlite = require('sqlite');
|
|
|
3
3
|
const sqlite3 = require('sqlite3');
|
|
4
4
|
const helpers = require('./helpers');
|
|
5
5
|
const { getDBConnection } = require('./database');
|
|
6
|
+
const path = require('path');
|
|
6
7
|
|
|
7
8
|
jest.mock('sqlite');
|
|
8
9
|
jest.mock('os');
|
|
@@ -32,7 +33,7 @@ describe('database module', () => {
|
|
|
32
33
|
helpers.fileExists.mockReturnValue(true);
|
|
33
34
|
|
|
34
35
|
|
|
35
|
-
const expectedDefaultFile =
|
|
36
|
+
const expectedDefaultFile = path.join(os.homedir(),'BurnDownStatus.db')
|
|
36
37
|
|
|
37
38
|
const file = undefined;
|
|
38
39
|
// call function with null file
|
|
@@ -56,7 +57,7 @@ describe('database module', () => {
|
|
|
56
57
|
os.homedir.mockReturnValue(expectedHomeDir);
|
|
57
58
|
helpers.fileExists.mockReturnValue(true);
|
|
58
59
|
|
|
59
|
-
const expectedDefaultFile =
|
|
60
|
+
const expectedDefaultFile = path.join(os.homedir(),'BurnDownStatus.db')
|
|
60
61
|
|
|
61
62
|
const db = await getDBConnection(expectedDefaultFile)
|
|
62
63
|
|