Skip to content

Commit

Permalink
fix: replace commander with util.parseArgs (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Dec 18, 2023
1 parent 98638a0 commit d69b632
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 33 deletions.
85 changes: 62 additions & 23 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
// LICENSE file in the root directory of this source tree.
//

const { program } = require("commander");
program
.version(require("./package.json").version)
.description("submit code reviews with suggestions based on your diffs")
.option("-m, --message <msg>", "use the specified message as the PR comment")
.option("-f, --fail", "fail if comments could not be posted")
.argument("[diff]", "the diff to create suggestions from")
.addHelpText(
"after",
const path = require("node:path");
const { parseArgs } = require("node:util");

function printHelp() {
console.log(
[
`Usage: ${path.basename(__filename)} [options] [diff]`,
"",
"Submit code reviews with suggestions based on your diffs",
"",
"Arguments:",
" diff the diff to create suggestions from",
"",
"Options:",
" -h, --help display help for command",
" -v, --version output the version number",
" -m, --message <msg> use the specified message as the PR comment",
" -f, --fail fail if comments could not be posted",
"",
"Examples:",
" # Submit current changes as suggestions",
Expand All @@ -29,21 +37,52 @@ program
"If your CI is hosted by Azure DevOps, replace `GITHUB_TOKEN` with `AZURE_PERSONAL_ACCESS_TOKEN`.",
].join("\n")
);
}

const { values, positionals } = parseArgs({
args: process.argv.slice(2),
options: {
help: {
type: "boolean",
short: "h",
},
version: {
type: "boolean",
short: "v",
},
message: {
type: "string",
short: "m",
},
fail: {
type: "boolean",
short: "f",
},
},
allowPositionals: true,
});

const suggest = require("./src/index");
if (process.stdin.isTTY) {
const { [0]: diff } = program.parse(process.argv).args;
if (diff) {
suggest(diff, program.opts());
if (values.help) {
printHelp();
} else if (values.version) {
const { name, version } = require("./package.json");
console.log(name, version);
} else {
const suggest = require("./src/index");
if (process.stdin.isTTY) {
const diff = positionals[0];
if (diff) {
suggest(diff, values);
} else {
printHelp();
}
} else {
program.help();
let data = "";
const stdin = process.openStdin();
stdin.setEncoding("utf8");
stdin.on("data", (chunk) => {
data += chunk;
});
stdin.on("end", () => suggest(data, values));
}
} else {
let data = "";
const stdin = process.openStdin();
stdin.setEncoding("utf8");
stdin.on("data", (chunk) => {
data += chunk;
});
stdin.on("end", () => suggest(data, program.parse(process.argv).opts()));
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@octokit/core": "^5.0",
"@octokit/plugin-rest-endpoint-methods": "^10.0",
"azure-devops-node-api": "^12.0",
"commander": "^8.1",
"parse-diff": "^0.11"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function getClient() {
return require("./GitHubClient");
}

throw "No access token was set";
throw new Error("No access token was set");
}

/**
Expand Down
8 changes: 0 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -790,13 +790,6 @@ __metadata:
languageName: node
linkType: hard

"commander@npm:^8.1":
version: 8.3.0
resolution: "commander@npm:8.3.0"
checksum: 0f82321821fc27b83bd409510bb9deeebcfa799ff0bf5d102128b500b7af22872c0c92cb6a0ebc5a4cf19c6b550fba9cedfa7329d18c6442a625f851377bacf0
languageName: node
linkType: hard

"compare-func@npm:^2.0.0":
version: 2.0.0
resolution: "compare-func@npm:2.0.0"
Expand Down Expand Up @@ -2987,7 +2980,6 @@ __metadata:
"@types/node": ^20.0.0
azure-devops-node-api: ^12.0
codecov: ^3.0
commander: ^8.1
eslint: ^8.0
parse-diff: ^0.11
prettier: ^3.0
Expand Down

0 comments on commit d69b632

Please sign in to comment.