Skip to content

Commit

Permalink
feat: help option
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorHo committed Feb 16, 2024
1 parent ff5d653 commit 127a2d3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ npm install -g audit-export

## Usage

You can use the tool in two different main ways:
The tool is packed with a help function to see the usage ways. To see it, just pass the `--help` option.

The simplest usage is as the following:

```
npm audit --json | audit-export
```

But with more customizations you can use it in two different main ways:

```
npm audit --json | audit-export --folder <folder_path> --name <file_name.html> --title <HTML_file_title>
npm audit --json | audit-export --folder <folder_path> --file <file_name.html> --title <HTML_file_title>
```

or:
Expand Down
32 changes: 25 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ const OPTIONS = {
title: "NPM Audit Report",
template: join([__dirname, "template.ejs"])
};
const HELP_TEXT = `
Usage:
// Option 1
$ npm audit --json | audit-export
// Option 2
$ npm audit --json | audit-export <path> <file_name>
// Option 3
$ npm audit --json | audit-export --folder <folder_path> --file <file_name.html> --title <HTML_file_title>
// All parameters are optional
`;

/**
* Processes the input data and writes it to the specified file or folder.
Expand Down Expand Up @@ -197,21 +211,25 @@ function processArgument(index) {
}
const argumentNameOffsetStart = (process.argv[index].startsWith("--") ? 2 : 1);
const argumentName = process.argv[index].substring(argumentNameOffsetStart);

if (argumentName === "help") {
console.log(HELP_TEXT);
process.exit(0);
}

OPTIONS[argumentName] = process.argv[index + 1];
return index + 1;
} else {
if(!OPTIONS.folder) {
OPTIONS.folder = process.argv[index]; // First unnamed arg
} else if(!OPTIONS.file) {
OPTIONS.file = process.argv[index]; // Second unnamed arg (all other will be skipped)
}
} else if(!OPTIONS.folder) {
OPTIONS.folder = process.argv[index]; // First unnamed arg
} else if(!OPTIONS.file) {
OPTIONS.file = process.argv[index]; // Second unnamed arg (all other will be skipped)
}
return index;
}

// Command-line arguments (simple processor)
for (let argIndex = 2; argIndex < process.argv.length; argIndex++) {
argIndex = processArgument(argIndex);
processArgument(argIndex);
}

if(!OPTIONS.folder) {
Expand Down

0 comments on commit 127a2d3

Please sign in to comment.