Skip to content

Commit

Permalink
feat!: fixed and separated subcommands & global options. added tempor…
Browse files Browse the repository at this point in the history
…ary banner
  • Loading branch information
wilfreud committed Apr 12, 2024
1 parent 76efe00 commit beae1f2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 74 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"quirgo": "./dist/index.js"
},
"scripts": {
"link-cli": "pnpm --global unlink quirgo-cli && pnpm link --global",
"init": "pnpm --global unlink quirgo-cli && pnpm link --global",
"build": "tsup src/**/*.ts --format esm,cjs --dts",
"dev": "tsc -w",
"test": "node --env-file=config.env test/test.js",
Expand All @@ -20,7 +20,7 @@
"author": "wilfreud",
"license": "MIT",
"dependencies": {
"chalk": "^5.3.0",
"chalk": "^4.1.2",
"cli-table3": "^0.6.4",
"commander": "^12.0.0",
"dotenv": "^16.4.5",
Expand Down
11 changes: 3 additions & 8 deletions pnpm-lock.yaml

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

Empty file removed src/commands/variables.command.ts
Empty file.
126 changes: 62 additions & 64 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,31 @@ import { RepoManager } from "./lib/repository-manager";
import { Configuration as RepositoryManagerConfiguration } from "@/types/repository-manager";
import { envParser, jsonParser } from "./lib/parsers";
import { BANNER } from "./lib/ui/banner";
import chalk from "chalk";

console.log(BANNER);

// Declare the program
const program = new Command("quirgo");
const program = new Command();

// Init repo manager
let repoManager: RepoManager | null = null;
const config: RepositoryManagerConfiguration = {
repositoryName: "",
repositoryOwner: "",
verbose: false,
};
let parsedKeyValues: ReturnType<typeof envParser> = {};

/**
* If no .env file provided,it means only one key-value pair is provided
* Use console.table() to display listed secrets/variables
* Use console.table() to display listed secrets/variables (consider removing tabler)
*/

// Add options
program
.name("quirgo")
.option("--verbose", "Verbose output", false)
.version(
require("../package.json").version,
"-v, --version",
Expand All @@ -34,83 +38,77 @@ program
.description(
"A simple CLI to manage your GitHub repositories secrets and variables."
)
.option("--verbose", "Verbose output")
.option("-t, --token <string>", "GitHub access token")
.action((opts) => {
if (opts.token) {
repoManager = new RepoManager(opts.token);
}
})
.option("-r, --repo <string>", "GitHub repository name")
.action((opts) => {
if (opts.repo) {
config.repositoryName = opts.repo;
}
})
.option("-o, --owner <string>", "GitHub repository owner")
.action((opts) => {
if (opts.owner) {
config.repositoryOwner = opts.owner;
}
})
.option("-e, --env <string>", "Path to a .env file to parse")
.action((opts) => {
console.log("parsing....");
if (opts.env) {
parsedKeyValues = envParser(opts.env);
}
})
.option("-e, --env <path>", "Path to a .env file to parse")
.option("-j, --json", "Path to a JSON file to parse")
.action((opts) => {
if (opts.json) {
parsedKeyValues = jsonParser(opts.json);
}
const { env, token, repo, owner, json } = opts;
if (token) repoManager = new RepoManager(token);

if (repo) config.repositoryName = repo;

if (owner) config.repositoryOwner = owner;

if (env)
parsedKeyValues = envParser(env, { verbose: config.verbose || false });

if (json)
parsedKeyValues = jsonParser(opts.json, {
verbose: config.verbose || false,
});
});

// Add commands
program
.command("vars")
.description("Manage repository variables")
.alias("v")
.option("-f, --file <filepath>", "Path to a .env file to parse")
.command("list", "List all variables")
.command("create [name] [value]", "Create a new variable")
.command("update [name] [value]", "Update an existing variable")
.command("remove [name]", "Remove an existing variable");
const varsCommand = new Command("vars");
varsCommand.alias("v").description("Manage repository variables");

program
.command("secrets")
.description("Manage repository secrets")
.alias("s")
.option("-f, --file <filepath>", "Path to a .env file to parse")
.command("list", "List all secrets")
.command("set [name] [value]", "Set a new secret")
.command("remove [name]", "Remove an existing secret");
varsCommand.command("list").description("List all variables");
varsCommand
.command("create [name] [value]")
.description("Create a new variable");
varsCommand
.command("update [name] [value]")
.description("Update an existing variable");
varsCommand.command("remove [name]").description("Remove an existing variable");

// Add secrets commands
const secretsCommand = new Command("secrets");
secretsCommand.description("Manage repository secrets").alias("s");

secretsCommand
.command("list")
.description("List all secrets")
.action(() => {
console.log("Listing secrets....");
});

secretsCommand
.command("set [name] [value]")
.description("Set a new secret")
.action((name, value) => {
console.log(`Setting secret ${name}.... value: ${value}`);
});

secretsCommand
.command("remove [name]")
.description("Remove an existing secret")
.action((name) => {
console.log(`Removing secret ${name}....`);
});

// Add subcommands to program
program.addCommand(varsCommand).addCommand(secretsCommand);

// Add hooks & custom events
program.on("option:verbose", () => {
console.log("Verbose mode activated");
console.log(chalk.cyan("Verbose mode activated"));
config.verbose = true;
});

program.on("preAction", () => {
if (!repoManager) throw new Error("No repository manager found");
});

program.on("*", () => {
program.help();
});

// Execute CLI with arguments
program.parse();
program.parse(process.argv);

// Treatment here
const options = program.opts();
if (program.args.length === 0) {
program.help();
}

/**
* Next Step: get input from user
* Properly understand sub commands
*/
console.table(options);

0 comments on commit beae1f2

Please sign in to comment.