Skip to content

Commit

Permalink
Merge pull request #54 from gaurav-nelson/link-as-arg-fix
Browse files Browse the repository at this point in the history
fixes Invalid argument errors
  • Loading branch information
tcort authored Jul 26, 2018
2 parents 2202813 + b00a9b1 commit 95d8ec9
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions markdown-link-check
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@ program
.action(function (filenameOrUrl) {
filenameForOutput = filenameOrUrl;
if (/https?:/.test(filenameOrUrl)) {
stream = request.get(filenameOrUrl);
request(filenameOrUrl, function (error, response, body) {
if (error) {
console.error(chalk.red('\nERROR: Unable to connect! Please provide a vaild URL as an argument.'));
process.exit(1);
}
else if (response.statusCode === 404){
console.error(chalk.red('\nERROR: 404 - File not found! Please provide a vaild URL as an argument.'));
process.exit(1);
} else {
stream = request.get(filenameOrUrl);
}

});
try { // extract baseUrl from supplied URL
const parsed = url.parse(filenameOrUrl);
delete parsed.search;
Expand All @@ -38,11 +50,26 @@ program
parsed.pathname = parsed.pathname.substr(0, parsed.pathname.lastIndexOf('/') + 1);
}
opts.baseUrl = url.format(parsed);
} catch (err) { /* ignore error */ }
} catch (err) { /* ignore error */
}
} else {
opts.baseUrl = 'file://' + path.dirname(path.resolve(filenameOrUrl));
stream = fs.createReadStream(filenameOrUrl);
fs.stat(filenameOrUrl, function(error , stats){
if (stats.isDirectory()){
console.error(chalk.red('\nERROR: ' + filenameOrUrl + ' is a directory! Please provide a vaild filename as an argument.'));
process.exit(1);
}
});
fs.access(filenameOrUrl, fs.constants.F_OK, function(fileerror){
if (fileerror){
console.error(chalk.red('\nERROR: File not found! Please provide a vaild filename as an argument.'));
process.exit(1);
} else {
opts.baseUrl = 'file://' + path.dirname(path.resolve(filenameOrUrl));
stream = fs.createReadStream(filenameOrUrl);
}
});
}

}).parse(process.argv);

opts.showProgressBar = (program.progress === true); // force true or undefined to be true or false.
Expand Down

0 comments on commit 95d8ec9

Please sign in to comment.