-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·66 lines (49 loc) · 2.14 KB
/
index.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
#!/usr/bin/env node
const program = require("commander");
const ora = require("ora");
const chalk = require("chalk");
const path = require("path");
const { parse } = require("./lib/parse");
const { saveFile, isFileAlreadyExist } = require("./lib/file-system");
const { getCurrentDate } = require("./lib/time");
const URL_UTILS = require("./lib/url");
const main = async () => {
program
.version("0.0.2")
.option("-u, --url [Site URL*]", "Site URL")
.option("-o, --output [Output path]", "Output path")
.option("-f, --fileName [File name]", "File name")
.parse(process.argv);
const PARSE_SITE_URL = program.url;
if (PARSE_SITE_URL) {
if (URL_UTILS.isUrlValid(PARSE_SITE_URL)) {
const formatedURL = URL_UTILS.formatToStandard(PARSE_SITE_URL);
const spinner = ora();
spinner.start(`Starting parse ${formatedURL}`);
try {
const siteName = URL_UTILS.splitURL(formatedURL).authority;
const outputPath = program.output || path.join(__dirname, "/output");
const fileName = program.fileName || siteName + "_" + getCurrentDate() + ".json";
if (isFileAlreadyExist(outputPath, fileName)) {
throw new Error(
chalk.red("File with name ") +
chalk.cyan(fileName) +
chalk.red(" already exist at ") +
chalk.cyan(outputPath));
}
const parsedData = await parse(formatedURL);
await saveFile(outputPath, fileName, JSON.stringify(parsedData));
spinner.succeed(`Site ${chalk.green(formatedURL)} have been parsed`);
spinner.succeed(`Info saved at ${chalk.cyan(outputPath)}/${chalk.cyan(fileName)}`);
} catch (e) {
spinner.fail(e.message);
}
} else {
console.log("URL is not valid!");
}
} else {
console.log("Please specify the URL for parsing\n");
program.help();
}
};
main();