This repository has been archived by the owner on Dec 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
97 lines (92 loc) · 2.89 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use strict";
const fetch = require("node-fetch");
const chalk = require("chalk");
const terminalLink = require("terminal-link");
const { error, success, debug } = require("util-box").outputUtil;
const { httpUtil } = require("util-box");
const config = require("./config");
const wait = require("./wait");
// fetch alternate domain-name suggestions
async function fetchSuggestions(input) {
let reqUrl = httpUtil.makeQueryString({ domain: input }, `${config.API_ENDPOINT}/suggestions`);
const stopSpinner = wait("Hunting Domain Suggestions");
try {
let res = await fetch(reqUrl);
if (!/2[0-9]/.test(res.status)) {
error("Error! Unable to fetch suggestions.", res.status);
} else {
let body = await res.json();
console.log(
` ${chalk.white(`
🌎 Here are some suggestions similar to ${chalk.cyan.underline(input)}`)} `
);
body.map(domains => {
console.log(
`${chalk.greenBright(terminalLink(domains.domain, `${config.PROVIDER_ENDPOINT}${domains.domain}`))} `
);
});
stopSpinner();
}
} catch (err) {
error(`Sorry! Unable to fetch suggestions due to Error ${err}`);
stopSpinner();
}
}
// check domain name availability
async function checkAvailability(input) {
debug(`Checking availability for ${input}`);
let reqUrl = httpUtil.makeQueryString({ domain: input }, `${config.API_ENDPOINT}/available`);
const stopSpinner = wait("Hunting domain availability");
try {
let res = await fetch(reqUrl);
if (!/2[0-9]/.test(res.status)) {
debug(`Error! Expected 2xx, found ${res.status}`);
error(`
Unable to fetch Domain Availability! Error: ${res.status}`);
stopSpinner();
} else {
let body = await res.json();
if (body.available) {
if (body.period > 1) body.numberofYears = "years";
else body.numberofYears = "year";
let domainLink = terminalLink(body.domain, `${config.PROVIDER_ENDPOINT}${body.domain}`);
console.log(`
${chalk.bold.underline(`Available`)}
${chalk.dim("The prices are subject to vary based on discounts, taxes and other fees")}
Domain : ${chalk.greenBright(domainLink)}
Price : ${chalk.white(body.price / 1000000)} ${chalk.yellowBright(body.currency)}
Period : ${chalk.white(body.period)} ${body.numberofYears}
`);
} else {
console.log(`
Oh no! That domain seems to be taken.
`);
fetchSuggestions(input);
}
stopSpinner();
}
} catch (err) {
error(`
Unable to Fetch domain availability ${err}`);
stopSpinner();
}
}
const performComparison = input => {
debug(`comparing the ${input}`);
};
const app = (input, flags) => {
if (!input) {
console.log(`
${chalk.red(
`Missing or invalid input: domain-name
Please enter a valid domain name`
)}
`);
console.log(`${chalk.gray("Use the --help to view usage instructions")}`);
} else {
if (flags.compare || flags.c) performComparison(input);
if (flags.suggestions || flags.s) fetchSuggestions(input);
else checkAvailability(input);
}
};
module.exports = app;