Skip to content

Commit 50065be

Browse files
authored
Merge pull request #68 from ArkeeAgency/antoinekm/commander
improve cli with commander
2 parents 38479e9 + 208a723 commit 50065be

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed

.changeset/many-frogs-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"google-indexing-script": minor
3+
---
4+
5+
Improve CLI with commander

package-lock.json

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "google-indexing-script",
3+
"description": "Script to get your site indexed on Google in less than 48 hours",
34
"version": "0.3.0",
45
"main": "./dist/index.js",
56
"types": "./dist/index.d.ts",
@@ -27,7 +28,9 @@
2728
"release": "changeset publish"
2829
},
2930
"dependencies": {
31+
"commander": "^12.1.0",
3032
"googleapis": "131.0.0",
33+
"picocolors": "^1.0.1",
3134
"sitemapper": "3.2.8"
3235
},
3336
"prettier": {

src/cli.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1-
const { index } = require(".");
1+
import { index } from ".";
2+
import { Command } from "commander";
3+
import packageJson from "../package.json";
4+
import { green } from "picocolors";
25

3-
index();
6+
const program = new Command(packageJson.name);
7+
8+
program
9+
.alias("gis")
10+
.version(packageJson.version, "-v, --version", "Output the current version.")
11+
.description(packageJson.description)
12+
.argument("[input]")
13+
.usage(`${green("[input]")} [options]`)
14+
.helpOption("-h, --help", "Output usage information.")
15+
.option("-c, --client-email <email>", "The client email for the Google service account.")
16+
.option("-k, --private-key <key>", "The private key for the Google service account.")
17+
.option("-p, --path <path>", "The path to the Google service account credentials file.")
18+
.option("-u, --urls <urls>", "A comma-separated list of URLs to index.")
19+
.option("--rpm-retry", "Retry when the rate limit is exceeded.")
20+
.action((input, options) => {
21+
index(input, {
22+
client_email: options.clientEmail,
23+
private_key: options.privateKey,
24+
path: options.path,
25+
urls: options.urls ? options.urls.split(",") : undefined,
26+
quota: {
27+
rpmRetry: options.rpmRetry,
28+
},
29+
});
30+
})
31+
.parse(process.argv);

src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from "./shared/gsc";
1212
import { getSitemapPages } from "./shared/sitemap";
1313
import { Status } from "./shared/types";
14-
import { batch, parseCommandLineArgs } from "./shared/utils";
14+
import { batch } from "./shared/utils";
1515
import { readFileSync, existsSync, mkdirSync, writeFileSync } from "fs";
1616
import path from "path";
1717

@@ -45,22 +45,21 @@ export const index = async (input: string = process.argv[2], options: IndexOptio
4545
process.exit(1);
4646
}
4747

48-
const args = parseCommandLineArgs(process.argv.slice(2));
4948
if (!options.client_email) {
50-
options.client_email = args["client-email"] || process.env.GIS_CLIENT_EMAIL;
49+
options.client_email = process.env.GIS_CLIENT_EMAIL;
5150
}
5251
if (!options.private_key) {
53-
options.private_key = args["private-key"] || process.env.GIS_PRIVATE_KEY;
52+
options.private_key = process.env.GIS_PRIVATE_KEY;
5453
}
5554
if (!options.path) {
56-
options.path = args["path"] || process.env.GIS_PATH;
55+
options.path = process.env.GIS_PATH;
5756
}
5857
if (!options.urls) {
59-
options.urls = args["urls"] ? args["urls"].split(",") : undefined;
58+
options.urls = process.env.GIS_URLS ? process.env.GIS_URLS.split(",") : undefined;
6059
}
6160
if (!options.quota) {
6261
options.quota = {
63-
rpmRetry: args["rpm-retry"] === "true" || process.env.GIS_QUOTA_RPM_RETRY === "true",
62+
rpmRetry: process.env.GIS_QUOTA_RPM_RETRY === "true",
6463
};
6564
}
6665

src/shared/utils.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,3 @@ export async function fetchRetry(url: string, options: RequestInit, retries: num
5050
return fetchRetry(url, options, retries - 1);
5151
}
5252
}
53-
54-
/**
55-
* Parses command-line arguments and returns key-value pairs.
56-
* @param argv The array of command-line arguments.
57-
* @returns An object containing parsed key-value pairs.
58-
*/
59-
export function parseCommandLineArgs(argv: string[]) {
60-
const parsedArgs: { [key: string]: string } = {};
61-
62-
argv.forEach((arg, index) => {
63-
// Check if the argument is in the format --key=value or --key value
64-
const matches = arg.match(/^--([^=]+)(?:=(.+))?$/);
65-
if (matches) {
66-
const key = matches[1];
67-
const value = matches[2] || argv[index + 1]; // Use next argument if value is not provided
68-
parsedArgs[key] = value;
69-
}
70-
});
71-
72-
return parsedArgs;
73-
}

0 commit comments

Comments
 (0)