@beauraines/node-helpers 5.0.0 → 5.1.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 CHANGED
@@ -2,6 +2,13 @@
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.1.0](https://github.com/beauraines/node-helpers/compare/v5.0.0...v5.1.0) (2024-10-16)
6
+
7
+
8
+ ### Features
9
+
10
+ * adds cli argument module ([#155](https://github.com/beauraines/node-helpers/issues/155)) ([b7943b6](https://github.com/beauraines/node-helpers/commit/b7943b6cf55a221ecc1a55afec2ab9505ed88347))
11
+
5
12
  ## [5.0.0](https://github.com/beauraines/node-helpers/compare/v4.3.0...v5.0.0) (2024-10-14)
6
13
 
7
14
 
package/eslint.config.mjs CHANGED
@@ -22,6 +22,7 @@ export default [...compat.extends("eslint:recommended"), {
22
22
  globals: {
23
23
  ...globals.browser,
24
24
  ...globals.commonjs,
25
+ ...globals.node,
25
26
  ...jest.environments.globals.globals,
26
27
  },
27
28
 
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
 
2
2
  const ado = require("./src/ado");
3
3
  const AzureStorage = require("./src/azure")
4
+ const cliArguments = require('./src/cli-arguments.js');
4
5
  const config = require('./src/config.js')
5
6
  const credentials = require("./src/credentials.js");
6
7
  const database = require("./src/database");
@@ -8,12 +9,13 @@ const helpers = require("./src/helpers");
8
9
  const jira = require("./src/jira");
9
10
 
10
11
  module.exports = {
12
+ ado,
11
13
  AzureStorage,
14
+ cliArguments,
12
15
  config,
13
16
  credentials,
14
17
  database,
15
18
  helpers,
16
- jira,
17
- ado
19
+ jira
18
20
  }
19
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beauraines/node-helpers",
3
- "version": "5.0.0",
3
+ "version": "5.1.0",
4
4
  "description": "Collection of node helpers",
5
5
  "main": "index.js",
6
6
  "repository": {
package/src/ado.js CHANGED
@@ -82,7 +82,7 @@ async function getDistinctParentWorkItems(workItemAPI, ids) {
82
82
  async function callRestApi(url, username, token) {
83
83
  // console.log(url) // Only display this in verbose mode
84
84
  // Bearer token format for ADO
85
- // eslint-disable-next-line no-undef
85
+
86
86
  let bearerToken = Buffer.from(`${username}:${token}`).toString('base64');
87
87
 
88
88
  let response;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Parses command-line parameters and returns an array of the parameters. This is really getting the
3
+ * "arguments" passed from the command-line, but there is no additional parsing or formatting, so
4
+ * this is best suited to just parameters. You could handle each element in the array yourself for
5
+ * involved processing
6
+ *
7
+ * `node script.js beau 42`
8
+ *
9
+ * returns
10
+ * `[ 'beau', '42' ]`
11
+ *
12
+ * *
13
+ * @returns Array
14
+ */
15
+ const getParameters = () => {
16
+ // process.argv is an array where:
17
+ // - The first element is the path to the Node.js executable (e.g., '/usr/local/bin/node')
18
+ // - The second element is the path to the script file (e.g., '/path/to/script.js')
19
+ // - The subsequent elements are the command-line arguments
20
+
21
+ // Get the arguments starting from the third element
22
+ const args = process.argv.slice(2).length > 0 ? process.argv.slice(2) : []
23
+ return args
24
+ }
25
+
26
+ /**
27
+ * Parses named command-line arguments. This only supports long-named arguments prefixed with a `--`.
28
+ * Short arguments .e.g. `-d` are not supported. The value can either be space separated or with an
29
+ * equals sign
30
+ *
31
+ * `node name-arguments.js --age=42 --name=Beau`
32
+ *
33
+ * Using this function this will return
34
+ *
35
+ * { age: '42', name: 'Beau' }
36
+ *
37
+ *
38
+ * @returns Object
39
+ */
40
+ const getNamedArguments = () => {
41
+ const args = {};
42
+
43
+ const argv =process.argv;
44
+ // Start from index 2 to skip the node executable and script file paths
45
+ for (let i = 2; i < argv.length; i++) {
46
+ let arg = argv[i];
47
+
48
+ if (arg.startsWith('--')) {
49
+ // Remove the leading --
50
+ arg = arg.slice(2);
51
+
52
+ // Handle --key=value
53
+ if (arg.includes('=')) {
54
+ const [key, value] = arg.split('=');
55
+ args[key] = value;
56
+ } else {
57
+ // Handle --key value
58
+ const key = arg;
59
+ const value = argv[i + 1];
60
+
61
+ if (!value || value.startsWith('--')) {
62
+ args[key] = true; // For flags without values
63
+ } else {
64
+ args[key] = value;
65
+ i++; // Skip the next item as it's the value for this key
66
+ }
67
+ }
68
+ }
69
+ }
70
+
71
+ return args;
72
+ }
73
+
74
+
75
+ module.exports = {
76
+ getNamedArguments,
77
+ getParameters
78
+ }
@@ -1,6 +1,5 @@
1
1
  const fs = require('fs');
2
2
  const {fileExists} = require('./helpers');
3
- const process = require('node:process')
4
3
 
5
4
  /**
6
5
  * Reads a file for credentials and validates that the file has the required attributes.
package/src/database.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const {homedir} = require('os');
2
- const process = require('node:process')
3
2
  const sqlite = require('sqlite');
4
3
  const sqlite3 = require('sqlite3');
5
4
  const {fileExists} = require('./helpers');
@@ -4,7 +4,6 @@ const sqlite3 = require('sqlite3');
4
4
  const helpers = require('./helpers');
5
5
  const { getDBConnection } = require('./database');
6
6
  const path = require('path');
7
- const process = require('node:process')
8
7
 
9
8
  jest.mock('sqlite');
10
9
  jest.mock('os');
package/src/helpers.js CHANGED
@@ -192,11 +192,11 @@ async function streamToBuffer(readableStream) {
192
192
  return new Promise((resolve, reject) => {
193
193
  const chunks = [];
194
194
  readableStream.on("data", (data) => {
195
- // eslint-disable-next-line no-undef
195
+
196
196
  chunks.push(data instanceof Buffer ? data : Buffer.from(data));
197
197
  });
198
198
  readableStream.on("end", () => {
199
- // eslint-disable-next-line no-undef
199
+
200
200
  resolve(Buffer.concat(chunks));
201
201
  });
202
202
  readableStream.on("error", reject);
package/src/jira.js CHANGED
@@ -9,7 +9,7 @@ const fetch = require('node-fetch');
9
9
  * @returns String
10
10
  */
11
11
  function credentialsToToken(email,token) {
12
- // eslint-disable-next-line no-undef
12
+
13
13
  let bearerToken = Buffer.from(`${email}:${token}`).toString('base64');
14
14
  return bearerToken
15
15
  }