Skip to content

Commit

Permalink
Refactoring findBreakUrl to improve code maintainability
Browse files Browse the repository at this point in the history
*extract fileDirectory(), put that into fileRead.js

*variables name changed

*split index.js to fileRead.js and index.js
  • Loading branch information
klee214 committed Oct 23, 2020
1 parent 5cc4ecb commit d740f80
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
44 changes: 44 additions & 0 deletions fileRead.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const fs = require("fs");
const chalk = require('chalk')

const fileRead = (file) => {
let fileData = null;

try {
fileData = fs.readFileSync(`${__dirname}\\${file}`, {
encoding: "utf-8",
});

// wrong file name
} catch (error) {
console.log(file + " is a WRONG file name");
return process.exit(1);
}
return fileData;
}

const readDirectory = (yargs) => {
let files = [];
if (yargs.argv._.length === 1 && yargs.argv._[0] === "start") {

// decide the option if it is -f or -a
if (yargs.argv.a && typeof yargs.argv.f !== "string") {
const tmpFiles = fs.readdirSync(__dirname, { encoding: "utf-8" });

// if -a, store all files into the files variable
files = tmpFiles.filter((file) => {
return file.toLowerCase().endsWith(".html");
});

} else if (typeof yargs.argv.f === "string") {
files = [...yargs.argv.f.split(",")];
} else {
console.log(chalk.yellow("Please enter filenames or -a following -f"))
}
return files;
} else {
console.log(chalk.yellow("Wrong argument: use [start]"));
}
}

module.exports = { fileRead, readDirectory }
18 changes: 3 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
#!/usr/bin/env node
const { fetchFunction } = require("./fetch");
const fs = require("fs");
const validator = require("validator");
const { files, arg } = require("./yargs");
const { arg, files } = require("./yargs");
const { jsonFetch } = require("./json");
const { exit } = require("process");

let fileData = null;
const { fileRead } = require('./fileRead')

// Start testing files each
files.map((file) => {
try {
fileData = fs.readFileSync(`${__dirname}\\${file}`, {
encoding: "utf-8",
});

// wrong file name
} catch (error) {
console.log(file + " is a WRONG file name");
return process.exit(1);
}
const fileData = fileRead(file);

const regex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
const findURL = fileData.match(regex);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

27 changes: 3 additions & 24 deletions yargs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const yargs = require("yargs");
const chalk = require('chalk')
const fs = require("fs");
const { readDirectory } = require('./fileRead')

/*
The purpose of this module:
Expand Down Expand Up @@ -44,29 +43,9 @@ yargs
.alias("h", "help")
.demandCommand().argv;

let files = [];
const files = readDirectory(yargs);

if (yargs.argv._.length === 1 && yargs.argv._[0] === "start") {
// decide the option if it is -f or -a
if (yargs.argv.a && typeof yargs.argv.f !== "string") {
const tmpFiles = fs.readdirSync(__dirname, { encoding: "utf-8" });


// if -a, store all files into the files variable
files = tmpFiles.filter((file) => {
return file.toLowerCase().endsWith(".html");
});
} else if (typeof yargs.argv.f === "string") {
files = [...yargs.argv.f.split(",")];
} else{
console.log(chalk.yellow("Please enter filenames or -a following -f"))
}
} else {
console.log(chalk.yellow("Wrong argument: use [start]"));
}
const yargObj = {
module.exports = {
files,
arg: yargs.argv,
};

module.exports = yargObj;

0 comments on commit d740f80

Please sign in to comment.