improve cli with commander

This commit is contained in:
Antoine Kingue 2024-07-08 14:13:18 +02:00
parent adabe73e8e
commit c8b9303e6e
3 changed files with 32 additions and 29 deletions

View File

@ -1,3 +1,28 @@
const { index } = require(".");
const { Command } = require("commander");
const packageJson = require("../package.json");
const { green } = require("picocolors");
index();
const program = new Command();
program
.version(packageJson.version, "-v, --version")
.argument("[input]")
.usage(`${green("[input]")} [options]`)
.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);

View File

@ -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";
@ -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",
};
}

View File

@ -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;
}