Skip to content

Instantly share code, notes, and snippets.

@klee214
Created November 7, 2020 20:09
Show Gist options
  • Save klee214/81c3906a0b1fa138902bfdbd0e035efa to your computer and use it in GitHub Desktop.
Save klee214/81c3906a0b1fa138902bfdbd0e035efa to your computer and use it in GitHub Desktop.
diff --git a/index.js b/index.js
index 3972bbf..c031329 100644
--- a/index.js
+++ b/index.js
@@ -1,38 +1,55 @@
#!/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");
+const { fileRead } = require("./fileRead");
+const axios = require("axios");
+const chalk = require("chalk");
+const path = require("path")
-let fileData = null;
-
-// Start testing files each
-files.map((file) => {
- try {
- fileData = fs.readFileSync(`${__dirname}\\${file}`, {
- encoding: "utf-8",
+if (arg.a) {
+ console.log(chalk.bgBlueBright("*******API Request start*******"))
+ axios
+ .get(arg.a)
+ .then(async (response) => {
+ console.log(chalk.bgGreenBright("*******API Request Success*******"))
+ if (arg.j) {
+ response.data.map(res=>{
+ await jsonFetch(path.resolve(arg.a, `./${res[id]}`));
+ })
+ } else {
+ response.data.map(res=>{
+ await fetchFunction(path.resolve(arg.a, `./${res[id]}`), arg.a);
+ })
+ }
+ })
+ .catch((error) => {
+ console.log(chalk.bgRedBright("*******API Request Fails*******"))
+ console.log(
+ chalk.yellowBright(
+ `Cannot found: ${arg.a}. Probably because of network connection problem or wrong API address`
+ )
+ );
});
+} else {
+ // Start testing files each
+ files.map((file) => {
+ const fileData = fileRead(file);
- // wrong file name
- } catch (error) {
- console.log(file + " is a WRONG file name");
- return process.exit(1);
- }
-
- 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);
+ 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);
- if (findURL) {
- findURL.forEach(async (url) => {
- if (validator.isURL(url)) {
- if (arg.j) {
- await jsonFetch(url);
- } else {
- await fetchFunction(url, file);
+ if (findURL) {
+ findURL.forEach(async (url) => {
+ if (validator.isURL(url)) {
+ if (arg.j) {
+ await jsonFetch(url);
+ } else {
+ await fetchFunction(url, file);
+ }
}
- }
- });
- }
-});
+ });
+ }
+ });
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment