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

Add Better Error Messages #56

Merged
merged 2 commits into from
Jan 27, 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
6 changes: 3 additions & 3 deletions bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import linkInspector from "./index";
import {dirname, basename} from 'path';
import fs from 'fs';

const args = process.argv.slice(2);
const args: string[] = process.argv.slice(2);

if (args.length === 0) {
console.error("no link or path given");
}

async function writeLink(link: string, path: any) {
async function writeLink(link: string, path: string) {
console.log("Broken Link:", link);

if (path) {
Expand All @@ -23,7 +23,7 @@ async function writeLink(link: string, path: any) {
}

for (const arg of args) {
let path = '';
let path: string = '';

try {new URL(arg)}
catch {
Expand Down
2 changes: 1 addition & 1 deletion checkLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const axios = require("axios");

// Return true if link is broken
export async function checkLink(link: string): Promise<boolean> {
const params = {
const params: object = {
headers: {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
Expand Down
20 changes: 10 additions & 10 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { checkLink } from "./checkLink";
import fs from 'fs';

let PROCESSES: string[][] = [];
let CURRENTPROCESS = 0;
let RUNNINGPROCESSES = 0;
const MAXPROCESSES = 100;
let CURRENTPROCESS: number = 0;
let RUNNINGPROCESSES: number = 0;
const MAXPROCESSES: number = 100;

async function runProcess(callback: any) {
if (MAXPROCESSES <= RUNNINGPROCESSES || PROCESSES.length === CURRENTPROCESS) return;
Expand All @@ -24,7 +24,7 @@ async function runProcess(callback: any) {
runProcess(callback);
}

export default async function linkInspector(arg: string, callback: any, path='') {
export default async function linkInspector(arg: string, callback: any, path: string = '') {
try { // If arg is a link
new URL(arg);
PROCESSES.push([arg, path]);
Expand All @@ -33,7 +33,7 @@ export default async function linkInspector(arg: string, callback: any, path='')
} catch {}

try { // If arg is a path
const stats = fs.statSync(arg);
const stats: fs.Stats = fs.statSync(arg);

// Skip files over 100mb
if (100*1024*1024 < stats.size) return
Expand All @@ -48,17 +48,17 @@ export default async function linkInspector(arg: string, callback: any, path='')
}

// Handle file
const content: any = fs.readFileSync(arg, 'utf8');
const content: string = fs.readFileSync(arg, 'utf8');

// Skip binary files
if (!/^[\x00-\x7F]*$/.test(content)) return;

// Get all the links
const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
const urlRegex: RegExp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
const links: string[] = content.match(urlRegex) || [];

const directoryIndex = arg.indexOf(path);
const pathAfterDirectory = arg.substring(directoryIndex);
const directoryIndex: number = arg.indexOf(path);
const pathAfterDirectory: string = arg.substring(directoryIndex);

for (const link of links) {
try { // Runs link inspector on each link
Expand All @@ -68,6 +68,6 @@ export default async function linkInspector(arg: string, callback: any, path='')
} catch {}
}
} catch {
console.error("Error: Not a valid link or path")
console.error(`Error: ${arg} is not a valid link or path`);
}
}
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "link-inspector",
"version": "2.0.1",
"version": "2.0.2",
"author": "Justin Dhillon",
"license": "AGPL-3.0-or-later",

Expand Down
Loading