Skip to content

Commit

Permalink
feat: adds cli argument module (#155)
Browse files Browse the repository at this point in the history
* ci(linter): adds Node globals to linters

Prevents things like `process` from being flagged as undeclared.

* feat: new module for reading cli arguments

- reads arguments e.g. `command arg1 arg2`
- reads named arguments .e.g `command --foo=bar --baz flower'

* fix: corrects linter issues

These were due to adding the node globals to the linter configuration
  • Loading branch information
beauraines authored Oct 16, 2024
1 parent dcf5a70 commit b7943b6
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 9 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default [...compat.extends("eslint:recommended"), {
globals: {
...globals.browser,
...globals.commonjs,
...globals.node,
...jest.environments.globals.globals,
},

Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@

const ado = require("./src/ado");
const AzureStorage = require("./src/azure")
const cliArguments = require('./src/cli-arguments.js');
const config = require('./src/config.js')
const credentials = require("./src/credentials.js");
const database = require("./src/database");
const helpers = require("./src/helpers");
const jira = require("./src/jira");

module.exports = {
ado,
AzureStorage,
cliArguments,
config,
credentials,
database,
helpers,
jira,
ado
jira
}

2 changes: 1 addition & 1 deletion src/ado.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async function getDistinctParentWorkItems(workItemAPI, ids) {
async function callRestApi(url, username, token) {
// console.log(url) // Only display this in verbose mode
// Bearer token format for ADO
// eslint-disable-next-line no-undef
let bearerToken = Buffer.from(`${username}:${token}`).toString('base64');

let response;
Expand Down
78 changes: 78 additions & 0 deletions src/cli-arguments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Parses command-line parameters and returns an array of the parameters. This is really getting the
* "arguments" passed from the command-line, but there is no additional parsing or formatting, so
* this is best suited to just parameters. You could handle each element in the array yourself for
* involved processing
*
* `node script.js beau 42`
*
* returns
* `[ 'beau', '42' ]`
*
* *
* @returns Array
*/
const getParameters = () => {
// process.argv is an array where:
// - The first element is the path to the Node.js executable (e.g., '/usr/local/bin/node')
// - The second element is the path to the script file (e.g., '/path/to/script.js')
// - The subsequent elements are the command-line arguments

// Get the arguments starting from the third element
const args = process.argv.slice(2).length > 0 ? process.argv.slice(2) : []
return args
}

/**
* Parses named command-line arguments. This only supports long-named arguments prefixed with a `--`.
* Short arguments .e.g. `-d` are not supported. The value can either be space separated or with an
* equals sign
*
* `node name-arguments.js --age=42 --name=Beau`
*
* Using this function this will return
*
* { age: '42', name: 'Beau' }
*
*
* @returns Object
*/
const getNamedArguments = () => {
const args = {};

const argv =process.argv;
// Start from index 2 to skip the node executable and script file paths
for (let i = 2; i < argv.length; i++) {
let arg = argv[i];

if (arg.startsWith('--')) {
// Remove the leading --
arg = arg.slice(2);

// Handle --key=value
if (arg.includes('=')) {
const [key, value] = arg.split('=');
args[key] = value;
} else {
// Handle --key value
const key = arg;
const value = argv[i + 1];

if (!value || value.startsWith('--')) {
args[key] = true; // For flags without values
} else {
args[key] = value;
i++; // Skip the next item as it's the value for this key
}
}
}
}

return args;
}


module.exports = {
getNamedArguments,
getParameters
}
1 change: 0 additions & 1 deletion src/credentials.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fs = require('fs');
const {fileExists} = require('./helpers');
const process = require('node:process')

/**
* Reads a file for credentials and validates that the file has the required attributes.
Expand Down
1 change: 0 additions & 1 deletion src/database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const {homedir} = require('os');
const process = require('node:process')
const sqlite = require('sqlite');
const sqlite3 = require('sqlite3');
const {fileExists} = require('./helpers');
Expand Down
1 change: 0 additions & 1 deletion src/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const sqlite3 = require('sqlite3');
const helpers = require('./helpers');
const { getDBConnection } = require('./database');
const path = require('path');
const process = require('node:process')

jest.mock('sqlite');
jest.mock('os');
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ async function streamToBuffer(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
// eslint-disable-next-line no-undef
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
});
readableStream.on("end", () => {
// eslint-disable-next-line no-undef
resolve(Buffer.concat(chunks));
});
readableStream.on("error", reject);
Expand Down
2 changes: 1 addition & 1 deletion src/jira.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const fetch = require('node-fetch');
* @returns String
*/
function credentialsToToken(email,token) {
// eslint-disable-next-line no-undef
let bearerToken = Buffer.from(`${email}:${token}`).toString('base64');
return bearerToken
}
Expand Down

0 comments on commit b7943b6

Please sign in to comment.