Skip to content

Commit

Permalink
Merge pull request #35 from UmbrellaDocs/json-output
Browse files Browse the repository at this point in the history
Feature: Add JSON output option
  • Loading branch information
gaurav-nelson authored May 5, 2024
2 parents 5643ed7 + dfa07d5 commit c35eb55
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 33 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
81 changes: 53 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path>", "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) {
Expand Down
6 changes: 4 additions & 2 deletions linkspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit c35eb55

Please sign in to comment.