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 new feather for arg --ignore #13

Merged
merged 5 commits into from
Oct 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ If you enter -s argument, this tool checks both http:// and https://
When the link starts with https://, this tool checks whether the link is broken or not if it changes to http://.
<br/>Also, when the link starts with http://, this tool checks whether the lilnk is broken or not if the changes to https://.

### ignore URL pattern(s) using a text file (Argument -s)
For example, if you write 'https://www.google.com" in the text file (ignore-urls.txt), then run the command $ tool-name --ignore ignore-urls.txt index.html. Your tool will check all URLs in index.html, but ignore the URLs in ignore-url.txt which is "https://www.google.com" in this case.

### Helping option
When you enter -h argument, this tool prints out the usage of this tool.

Expand Down
1 change: 1 addition & 0 deletions ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# http://s9y.org
34 changes: 34 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ if (process.argv.length === 2) {
// If user enter -s, the program checks whether http:// actually work using https://
// If user enter -h, the program prints out the usage of this tool
let sFlag = false;
let ignore = false;
let ignoredLink = [];


for (let i = 2; i < process.argv.length; i++) {
let arg = process.argv[i];
if (arg.startsWith('-')) {
Expand All @@ -40,6 +44,10 @@ for (let i = 2; i < process.argv.length; i++) {
console.log("Tool Name: url-fi")
console.log("Version: 0.1")
}

if (arg.includes("i")) {
ignore = true;
}
}

if (arg.match(goodRegex)) {
Expand All @@ -49,13 +57,37 @@ for (let i = 2; i < process.argv.length; i++) {
} else if (arg.match(allRegex)) {
statusFlag = 1;
}

}

// If the user enters any arguments/filenames, starts process.
// --version or -v: prints tool name & version
// filename: checks broken links
for (let i = 2; i < process.argv.length; i++) {
let arg = process.argv[i];
if (ignore) {
arg = process.argv[++i];
data = fs.readFileSync(arg, 'utf8');
array = data.split("\n");
string = "";
for(let i = 0; i < array.length; i++){

if(!array[i].trim().startsWith("#")){
string += array[i];
}
}
ignoredLink = string.match(linkRegex);
if (ignoredLink == null){
console.log(colors.red("The ignore file is invalid. It doesn't use http:// or https://"));
process.exitCode = 1;
process.exit(1);
}
ignore = false;
i++;
}

arg = process.argv[i];

if (!arg.startsWith("-")) {
fs.readFile(path.normalize(arg), 'utf8', function (err, data) {
if (err) {
Expand All @@ -64,6 +96,8 @@ for (let i = 2; i < process.argv.length; i++) {
process.exit(1);
}
let links = data.match(linkRegex);
//ignore the links
links = links.filter(val => !ignoredLink.includes(val));
for (let i = 0; i < links.length; i++) {
let link = links[i];
if (link.startsWith("https://")) {
Expand Down