Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to run in debug mode #390

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Scorecard](https://api.securityscorecards.dev/projects/github.com/NodeSecure/rep

</div>

This project is designed to generate periodic security reports in both HTML and PDF formats. It leverages the [@nodesecure/scanner](https://github.com/NodeSecure/scanner) to retrieve all necessary data.
This project is designed to generate periodic security reports in both HTML and PDF formats. It leverages the [@nodesecure/scanner](https://github.com/NodeSecure/scanner) to retrieve all necessary data.

| Screen1 | Screen2 |
| :----------------------------------: | :----------------------------------: |
Expand Down Expand Up @@ -85,9 +85,7 @@ This uses the official NodeSecure [runtime configuration](https://github.com/Nod
},
"git": {
"organizationUrl": "https://github.com/NodeSecure",
"repositories": [
"vulnera"
]
"repositories": ["vulnera"]
},
"charts": [
{
Expand Down Expand Up @@ -230,6 +228,14 @@ $ npm run preview:light
$ npm run preview:dark
```

## Debug mode

You can write in the file "reports/debug-pkg-repo.txt", all data generated from NPM package and GIT repository scanners using the following option. Usefull if you want to get a preview from this data set.

```bash
$ nreport exec --debug
```

## Contributors ✨

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
Expand Down
26 changes: 24 additions & 2 deletions bin/commands/execute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Import Node.js Dependencies
import fs from "node:fs/promises";
import { writeFileSync } from "node:fs";
import path from "node:path";
import { inspect } from "node:util";

// Import Third-party Dependencies
import * as rc from "@nodesecure/rc";
Expand All @@ -12,7 +15,13 @@ import { fetchPackagesAndRepositoriesData } from "../../src/analysis/fetch.js";
import * as CONSTANTS from "../../src/constants.js";
import * as reporting from "../../src/reporting/index.js";

export async function execute() {
export async function execute(options = {}) {
const { debug: debugMode } = options;

if (debugMode) {
console.log(kleur.bgMagenta().bold(` > Debug mode enabled \n`));
}

const [configResult] = await Promise.all([
rc.read(
process.cwd()
Expand All @@ -22,6 +31,7 @@ export async function execute() {

const config = configResult.unwrap();
const { report } = config;

if (report.reporters.length === 0) {
throw new Error("At least one reporter must be selected (either 'HTML' or 'PDF')");
}
Expand All @@ -31,7 +41,13 @@ export async function execute() {

store.run(config, () => {
fetchPackagesAndRepositoriesData()
.then((data) => reporting.proceed(data))
.then((data) => {
if (debugMode) {
debug(data);
}

return reporting.proceed(data);
})
.catch((error) => {
console.error(error);
process.exit(0);
Expand Down Expand Up @@ -59,3 +75,9 @@ function teardown() {
recursive: true, force: true
});
}

function debug(obj) {
const filePath = path.join(CONSTANTS.DIRS.REPORTS, `debug-pkg-repo.txt`);
writeFileSync(filePath, inspect(obj, { showHidden: true, depth: null }), "utf8");
}

1 change: 1 addition & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const cli = sade("nreport").version(version);

cli
.command("execute")
.option("-d, --debug", "Enable debug mode", false)
.alias("exec")
.describe("Execute report at the current working dir with current configuration.")
.example("nreport exec")
Expand Down
12 changes: 8 additions & 4 deletions src/analysis/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ async function fetchRepositoriesStats(
},
async(spinner) => {
const repos = await Promise.all(
repositories.map((repositoryName) => utils.cloneGITRepository(
path.join(CONSTANTS.DIRS.CLONES, repositoryName),
`${organizationUrl}/${repositoryName}.git`
))
repositories.map((repositoryName) => {
const trimmedRepositoryName = repositoryName.trim();

return utils.cloneGITRepository(
path.join(CONSTANTS.DIRS.CLONES, trimmedRepositoryName),
`${organizationUrl}/${trimmedRepositoryName}.git`
);
})
);
spinner.text = "Fetching repositories metadata on the NPM Registry";

Expand Down