import ArgsParser from "@bp/service/args/args-parser"; import { Args } from "@bp/service/args/args.types"; import { Command } from "commander"; import { name, version, description } from "@bp/../package.json"; import { readConfigFile } from "@bp/service/args/args-utils"; function commaSeparatedList(value: string, _prev: unknown): string[] { // remove all whitespaces const cleanedValue: string = value.trim(); return cleanedValue !== "" ? cleanedValue.replace(/\s/g, "").split(",") : []; } export default class CLIArgsParser extends ArgsParser { private getCommand(): Command { return new Command(name) .version(version) .description(description) .option("-tb, --target-branch ", "branch where changes must be backported to.") .option("-pr, --pull-request ", "pull request url, e.g., https://github.com/lampajr/backporting/pull/1.") .option("-d, --dry-run", "if enabled the tool does not create any pull request nor push anything remotely") .option("-a, --auth ", "git service authentication string, e.g., github token.") .option("-gu, --git-user ", "local git user name, default is 'GitHub'.") .option("-ge, --git-email ", "local git user email, default is 'noreply@github.com'.") .option("-f, --folder ", "local folder where the repo will be checked out, e.g., /tmp/folder.") .option("--title ", "backport pr title, default original pr title prefixed by target branch.") .option("--body ", "backport pr title, default original pr body prefixed by bodyPrefix.") .option("--body-prefix ", "backport pr body prefix, default `backport `.") .option("--bp-branch-name ", "backport pr branch name, default auto-generated by the commit.") .option("--reviewers ", "comma separated list of reviewers for the backporting pull request.", commaSeparatedList) .option("--assignees ", "comma separated list of assignees for the backporting pull request.", commaSeparatedList) .option("--no-inherit-reviewers", "if provided and reviewers option is empty then inherit them from original pull request") .option("-cf, --config-file ", "configuration file containing all valid options, the json must match Args interface."); } readArgs(): Args { const opts = this.getCommand() .parse() .opts(); let args: Args; if (opts.configFile) { // if config file is set ignore all other options args = readConfigFile(opts.configFile); } else { args = { dryRun: opts.dryRun, auth: opts.auth, pullRequest: opts.pullRequest, targetBranch: opts.targetBranch, folder: opts.folder, gitUser: opts.gitUser, gitEmail: opts.gitEmail, title: opts.title, body: opts.body, bodyPrefix: opts.bodyPrefix, bpBranchName: opts.bpBranchName, reviewers: opts.reviewers, assignees: opts.assignees, inheritReviewers: opts.inheritReviewers, }; } return args; } }