Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve cli with commander #68

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/many-frogs-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"google-indexing-script": minor
---

Improve CLI with commander
20 changes: 18 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "google-indexing-script",
"description": "Script to get your site indexed on Google in less than 48 hours",
"version": "0.3.0",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -27,7 +28,9 @@
"release": "changeset publish"
},
"dependencies": {
"commander": "^12.1.0",
"googleapis": "131.0.0",
"picocolors": "^1.0.1",
"sitemapper": "3.2.8"
},
"prettier": {
Expand Down
32 changes: 30 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
const { index } = require(".");
import { index } from ".";
import { Command } from "commander";
import packageJson from "../package.json";
import { green } from "picocolors";

index();
const program = new Command(packageJson.name);

program
.alias("gis")
.version(packageJson.version, "-v, --version", "Output the current version.")
.description(packageJson.description)
.argument("[input]")
.usage(`${green("[input]")} [options]`)
.helpOption("-h, --help", "Output usage information.")
.option("-c, --client-email <email>", "The client email for the Google service account.")
.option("-k, --private-key <key>", "The private key for the Google service account.")
.option("-p, --path <path>", "The path to the Google service account credentials file.")
.option("-u, --urls <urls>", "A comma-separated list of URLs to index.")
.option("--rpm-retry", "Retry when the rate limit is exceeded.")
.action((input, options) => {
index(input, {
client_email: options.clientEmail,
private_key: options.privateKey,
path: options.path,
urls: options.urls ? options.urls.split(",") : undefined,
quota: {
rpmRetry: options.rpmRetry,
},
});
})
.parse(process.argv);
13 changes: 6 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "./shared/gsc";
import { getSitemapPages } from "./shared/sitemap";
import { Status } from "./shared/types";
import { batch, parseCommandLineArgs } from "./shared/utils";
import { batch } from "./shared/utils";
import { readFileSync, existsSync, mkdirSync, writeFileSync } from "fs";
import path from "path";

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

const args = parseCommandLineArgs(process.argv.slice(2));
if (!options.client_email) {
options.client_email = args["client-email"] || process.env.GIS_CLIENT_EMAIL;
options.client_email = process.env.GIS_CLIENT_EMAIL;
}
if (!options.private_key) {
options.private_key = args["private-key"] || process.env.GIS_PRIVATE_KEY;
options.private_key = process.env.GIS_PRIVATE_KEY;
}
if (!options.path) {
options.path = args["path"] || process.env.GIS_PATH;
options.path = process.env.GIS_PATH;
}
if (!options.urls) {
options.urls = args["urls"] ? args["urls"].split(",") : undefined;
options.urls = process.env.GIS_URLS ? process.env.GIS_URLS.split(",") : undefined;
}
if (!options.quota) {
options.quota = {
rpmRetry: args["rpm-retry"] === "true" || process.env.GIS_QUOTA_RPM_RETRY === "true",
rpmRetry: process.env.GIS_QUOTA_RPM_RETRY === "true",
};
}

Expand Down
21 changes: 0 additions & 21 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,3 @@ export async function fetchRetry(url: string, options: RequestInit, retries: num
return fetchRetry(url, options, retries - 1);
}
}

/**
* Parses command-line arguments and returns key-value pairs.
* @param argv The array of command-line arguments.
* @returns An object containing parsed key-value pairs.
*/
export function parseCommandLineArgs(argv: string[]) {
const parsedArgs: { [key: string]: string } = {};

argv.forEach((arg, index) => {
// Check if the argument is in the format --key=value or --key value
const matches = arg.match(/^--([^=]+)(?:=(.+))?$/);
if (matches) {
const key = matches[1];
const value = matches[2] || argv[index + 1]; // Use next argument if value is not provided
parsedArgs[key] = value;
}
});

return parsedArgs;
}