Skip to content

Commit 1a4cd9f

Browse files
committed
CLI enhancement with commander.js, http.request replaced with fetch api, Code optimised with class based implementation, node_modules folder ignored indefinitely. Look up and ignore patterns by default loaded from the configuration for CLI and API, Load FTP credentials from configuration if not available-take from environmental variable.
1 parent 9145052 commit 1a4cd9f

File tree

8 files changed

+946
-902
lines changed

8 files changed

+946
-902
lines changed

bin/CLIDriver.ts

Lines changed: 90 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,103 @@
11
#!/usr/bin/env node
22

3-
import yargs from "yargs";
4-
import { hawk, hawkStrategy } from "../hawk";
5-
import { makeSitemap } from "../lib/utils";
3+
import { Command } from "commander";
4+
import { Hawk } from "../lib/core";
5+
import { SuppotredStrategies } from "../lib/types";
66

7-
async function _genMapHandler(argv: any): Promise<void> {
8-
if (argv.commit) {
9-
/* Make sitemap and update to Google search console */
10-
await hawkStrategy.gWebmaster2(
11-
argv.prettify,
12-
argv.include,
13-
argv.exclude,
7+
const program = new Command();
8+
const hawkInstance = new Hawk();
9+
10+
const strategyOptions =
11+
"Strategy to use \n1.Index-Now\n2.G-Webmaster\n3.G-Webmaster2\n4.G-Index";
12+
13+
async function handleGenMap(options: Record<string, any>) {
14+
console.log(
15+
"🕸️ Making sitemap",
16+
options.commit ? "and uploading..." : "",
17+
);
18+
19+
await hawkInstance.utils.makeSitemap(
20+
options.include,
21+
options.exclude,
22+
options.prettify,
23+
options.commit,
24+
);
25+
}
26+
27+
async function handleMain(options: Record<string, any>) {
28+
if (
29+
!Boolean(options.strategy) ||
30+
!["1", "2", "3", "4"].includes(options.strategy)
31+
) {
32+
console.log(
33+
"⭕ Must Choose any one number from below\n",
34+
strategyOptions,
1435
);
15-
} else {
16-
/* Only making site map */
17-
await makeSitemap(argv.prettify, argv.include, argv.exclude);
36+
process.exit(1);
1837
}
19-
}
2038

21-
async function _mainHandler(argv: any): Promise<void> {
22-
await hawk(argv.strategy, argv.include, argv.exclude, argv.prettify);
39+
const strategyMap: Record<string, SuppotredStrategies> = {
40+
"1": "IndexNow",
41+
"2": "GWebmaster",
42+
"3": "GWebmaster2",
43+
"4": "GIndex",
44+
};
45+
46+
await hawkInstance.hawk(
47+
strategyMap[options.strategy],
48+
options.include,
49+
options.exclude,
50+
options.prettify,
51+
);
52+
53+
console.log(
54+
`🚀 Hawk employing "${strategyMap[options.strategy]}" strategy..`,
55+
);
2356
}
2457

25-
async function main(): Promise<void> {
26-
// Configure yargs options
27-
const argv = await yargs
28-
.scriptName("hawk")
29-
.usage("$0 [options] [args]")
30-
.option("strategy", {
31-
alias: "s",
32-
describe: "Strategy to use",
33-
choices: ["GIndex", "IndexNow", "GWebmaster", "GWebmaster2"],
34-
default: "IndexNow",
35-
})
36-
.option("include", {
37-
alias: "i",
38-
describe: "Include pattern",
39-
type: "array",
40-
default: [],
41-
})
42-
.option("exclude", {
43-
alias: "e",
44-
describe: "Exclude pattern",
45-
type: "array",
46-
default: [],
47-
})
48-
.option("prettify", {
49-
alias: "p",
50-
describe: "Prettify sitemap.xml output",
51-
type: "boolean",
52-
default: false,
53-
})
54-
.command(
55-
"genmap [option]",
56-
"Generate sitemap.xml & upload to Google search console.",
57-
(yargs) => {
58-
yargs.option("commit", {
59-
alias: "c",
60-
describe: "Upload to Google search console",
61-
type: "boolean",
62-
default: false,
63-
});
64-
},
58+
async function main() {
59+
program
60+
.name("hawk")
61+
.description(
62+
"CLI for generating sitemaps and feeding to search engines",
6563
)
66-
.command("secret", "Set secret credentials")
67-
.strict()
68-
.help().argv;
64+
.version("1.5.0");
6965

70-
const isGenMap: boolean = argv._.includes("genmap");
71-
if (isGenMap) {
72-
_genMapHandler(argv);
73-
} else {
74-
_mainHandler(argv);
75-
}
66+
// Global options
67+
program
68+
.option("-s, --strategy <number>", strategyOptions)
69+
.option(
70+
"-i, --include <patterns...>",
71+
"Include patterns",
72+
hawkInstance.configurations.lookupPatterns,
73+
)
74+
.option(
75+
"-e, --exclude <patterns...>",
76+
"Exclude patterns",
77+
hawkInstance.configurations.ignorePattern,
78+
)
79+
.option("-p, --prettify", "Prettify sitemap.xml output", false);
80+
81+
// Genmap command
82+
program
83+
.command("genmap")
84+
.option("-c, --commit", "Upload to FTP Server", false)
85+
.description(
86+
"Generate sitemap.xml and optionally upload to FTP server",
87+
)
88+
.action(async (options) => {
89+
await handleGenMap({
90+
...program.opts(),
91+
...options,
92+
});
93+
});
94+
95+
// Default command (no subcommand provided)
96+
program.action(async () => {
97+
await handleMain(program.opts());
98+
});
99+
100+
await program.parseAsync(process.argv);
76101
}
77102

78103
main().catch((error) => {

hawk.ts

Lines changed: 0 additions & 124 deletions
This file was deleted.

0 commit comments

Comments
 (0)