Skip to content

Commit

Permalink
Added ability to exclude urls. Closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Feb 8, 2017
1 parent 4a76f7b commit a883caa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
7 changes: 7 additions & 0 deletions bin/hyperlink
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ var optimist = require('optimist'),
describe: 'Crawl all HTML-pages linked with relative and root relative links. This stays inside your domain.',
type: 'boolean'
})
.options('exclude', {
describe: 'Url pattern to exclude from the build. Supports * wildcards. You can create multiple of these: --exclude *.php --exclude http://example.com/*.gif',
type: 'string',
demand: false
})
.options('concurrency', {
alias: 'c',
describe: 'The maximum number of assets that can be loading at once (defaults to 100)',
Expand All @@ -46,6 +51,7 @@ if (commandLineOptions.h) {
var urlTools = require('urltools'),
// canonicalUrl = commandLineOptions.canonicalurl && urlTools.ensureTrailingSlash(commandLineOptions.canonicalurl),
rootUrl = commandLineOptions.root && urlTools.urlOrFsPathToUrl(commandLineOptions.root, true),
excludePatterns = commandLineOptions.exclude && [].concat(commandLineOptions.exclude),
inputUrls;

var hyperlink = require('../lib/index');
Expand All @@ -72,6 +78,7 @@ if (commandLineOptions._.length > 0) {
hyperlink({
root: rootUrl,
inputUrls: inputUrls,
excludePatterns: excludePatterns,
recursive: commandLineOptions.recursive,
verbose: commandLineOptions.verbose,
concurrency: commandLineOptions.concurrency
Expand Down
27 changes: 25 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ var TapRender = require('tap-render');

var t = new TapRender();

function regexEscape(pattern) {
return pattern.replace(/[\.\+\{\}\[\]\(\)\?\^\$]/g, '\\$&').replace(/\*/g, '.*?');
}

module.exports = function (options) {
options = options || {};

Expand All @@ -20,6 +24,20 @@ module.exports = function (options) {
root: options.root
});

var excludePattern;

if (Array.isArray(options.excludePatterns)) {
excludePattern = new RegExp('^(:?' + options.excludePatterns.map(regexEscape).join('|') + ')');
}

function shouldSkip(url) {
if (excludePattern) {
return excludePattern.test(url);
}

return false;
}

var relationsQuery = {
type: query.not('HtmlAnchor'),
crossorigin: false
Expand All @@ -33,9 +51,12 @@ module.exports = function (options) {
redirects = redirects || [];
relations = relations || [];

var skip = shouldSkip(url);

if (status !== 200) {
var invalidStatusReport = {
ok: false,
skip: skip,
name: 'should respond with HTTP status 200',
operator: 'error',
expected: [200, url].join(' '),
Expand All @@ -51,6 +72,7 @@ module.exports = function (options) {

var report = {
ok: true,
skip: skip,
name: 'URI should have no redirects - ' + url,
operator: 'noRedirects',
expected: [200, url].join(' '),
Expand Down Expand Up @@ -116,6 +138,7 @@ module.exports = function (options) {

var insecureReport = {
ok: false,
skip: skip,
name: 'URI should be secure - ' + url,
operator: 'mixed-content',
expected: insecureLogLine.replace(/\bhttps?:/g, 'https:'),
Expand Down Expand Up @@ -275,7 +298,7 @@ module.exports = function (options) {
})
// .writeStatsToStderr()
.run(function () {
t.close();
process.exit(errorCount ? 1 : 0);
var results = t.close();
process.exit(results.fail ? 1 : 0);
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"lodash": "^4.16.4",
"optimist": "^0.6.1",
"request": "^2.75.0",
"tap-render": "^0.1.7",
"tap-render": "Munter/tap-render#0.1.7-patch1",
"urltools": "^0.3.0"
},
"devDependencies": {
Expand Down

0 comments on commit a883caa

Please sign in to comment.