diff --git a/README.md b/README.md index 6e52b2b..a612885 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,12 @@ To check hyperlinks in your markup language files, follow these steps: linkspector check -c /path/to/custom-config.yml ``` + To get the output in JSON format: + + ```bash + linkspector check --json + ``` + 1. Linkspector starts checking the hyperlinks in your files based on the configuration provided in the configuration file or using the default configuration. It then displays the results in your terminal. 1. After the check is complete, Linkspector provides a summary of the results. If any dead links are found, they are listed in the terminal, along with their status codes and error messages. @@ -213,12 +219,12 @@ If there are failed links, linkspector shows the output as comma-seprated values ``` REDISTRIBUTED.md, https://unlicense.org/, null, 186, net::ERR_SSL_VERSION_OR_CIPHER_MISMATCH at https://unlicense.org/] -❌ Found link errors in one or more files. +💥 Error: Some hyperlinks in the specified files are invalid. ``` If there are no errors, linkspector shows the following message: ``` -✅ All links are working. +✨ Success: All hyperlinks in the specified files are valid. ``` ## What's planned diff --git a/index.js b/index.js index 67f4757..db8a27f 100755 --- a/index.js +++ b/index.js @@ -4,64 +4,89 @@ import { program } from "commander"; import kleur from "kleur"; import ora from "ora"; import { linkspector } from "./linkspector.js"; -import { createRequire } from 'module'; +import { createRequire } from "module"; const require = createRequire(import.meta.url); -const pkg = require('./package.json'); +const pkg = require("./package.json"); -// Define the program and its options program .version(pkg.version) .description("🔍 Uncover broken links in your content.") .command("check") .description("Check hyperlinks based on the configuration file.") .option("-c, --config ", "Specify a custom configuration file path") + .option("-j, --json", "Output the results in JSON format") .action(async (cmd) => { const configFile = cmd.config || ".linkspector.yml"; // Use custom config file path if provided let currentFile = ""; // Variable to store the current file name + let results = []; // Array to store the results if json is true + + const spinner = cmd.json ? null : ora().start(); try { let hasErrorLinks = false; - // Start the loading spinner with the first file name - const spinner = ora().start(); - - for await (const { file, result } of linkspector(configFile)) { + for await (const { file, result } of linkspector(configFile, cmd)) { // Update the current file name currentFile = file; + if (!cmd.json) { + spinner.text = `Checking ${currentFile}...\n`; + } for (const linkStatusObj of result) { - spinner.text = `Checking ${currentFile}...`; + // If json is true, store the results in the results array + if (cmd.json) { + results.push({ + file: currentFile, + link: linkStatusObj.link, + status_code: linkStatusObj.status_code, + line_number: linkStatusObj.line_number, + position: linkStatusObj.position, + status: linkStatusObj.status, + error_message: linkStatusObj.error_message, + }); + } else { + // If json is false, print the results in the console + if (linkStatusObj.status === "error") { + spinner.stop(); + console.log( + kleur.red( + `💥 ${currentFile} - Line ${linkStatusObj.line_number}: ${linkStatusObj.error_message}` + ) + ); + spinner.start(`Checking ${currentFile}...\n`); + } + } + if (linkStatusObj.status === "error") { hasErrorLinks = true; - // Stop the spinner before printing an error message - spinner.stop(); - console.error( - kleur.red( - `🚫 ${currentFile}, ${linkStatusObj.link} , ${linkStatusObj.status_code}, ${linkStatusObj.line_number}, ${linkStatusObj.error_message}` - ) - ); - // Start the spinner again after printing an error message - spinner.start(`Checking ${currentFile}...`); } } } - spinner.stop(); + if (cmd.json) { + console.log(JSON.stringify(results, null, 2)); + } if (!hasErrorLinks) { - console.log( - kleur.green( - "✨ Success: All hyperlinks in the specified files are valid." - ) - ); + if (!cmd.json) { + spinner.stop(); + console.log( + kleur.green( + "✨ Success: All hyperlinks in the specified files are valid." + ) + ); + } process.exit(0); } else { - console.error( - kleur.red( - "❌ Error: Some links in the specified files are not valid." - ) - ); + if (!cmd.json) { + spinner.stop(); + console.error( + kleur.red( + "💥 Error: Some hyperlinks in the specified files are invalid." + ) + ); + } process.exit(1); } } catch (error) { diff --git a/linkspector.js b/linkspector.js index 531dd94..28f6521 100644 --- a/linkspector.js +++ b/linkspector.js @@ -20,7 +20,7 @@ function isGitInstalled() { } } -export async function* linkspector(configFile) { +export async function* linkspector(configFile, cmd) { //Use default configuration if no config file is specified let config = {}; let defaultConfig = { @@ -56,7 +56,9 @@ export async function* linkspector(configFile) { } } catch (err) { if (err.code === "ENOENT") { - console.log("Configuration file not found. Using default configuration."); + if (!cmd.json) { + console.log("Configuration file not found. Using default configuration."); + } config = defaultConfig; } else { throw new Error(err); diff --git a/package.json b/package.json index d8b5844..0a0a537 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@umbrelladocs/linkspector", - "version": "0.3.1", + "version": "0.3.2", "description": "Uncover broken links in your content.", "type": "module", "main": "linkspector.js",