From 31b287b7fc73b60bc237b2ec8d8090886ac76728 Mon Sep 17 00:00:00 2001 From: Erwan Date: Tue, 16 Jul 2024 18:51:24 +0200 Subject: [PATCH] feat: add option to run in debug mode (fix #375) * fix: repository name with spaces breaks git clone command --- bin/commands/execute.js | 26 ++++++++++++++++++++++++-- bin/index.js | 1 + src/analysis/fetch.js | 12 ++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/bin/commands/execute.js b/bin/commands/execute.js index d079f78..369e2bf 100644 --- a/bin/commands/execute.js +++ b/bin/commands/execute.js @@ -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"; @@ -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 { d: debugMode } = options; + + if (debugMode) { + console.log(kleur.bgMagenta().bold(` > Debug mode enabled \n`)); + } + const [configResult] = await Promise.all([ rc.read( process.cwd() @@ -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')"); } @@ -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); @@ -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"); +} + diff --git a/bin/index.js b/bin/index.js index 9e2eab2..86e9de9 100755 --- a/bin/index.js +++ b/bin/index.js @@ -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") diff --git a/src/analysis/fetch.js b/src/analysis/fetch.js index 6427147..77cd276 100644 --- a/src/analysis/fetch.js +++ b/src/analysis/fetch.js @@ -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";