diff --git a/src/cmd.ts b/src/cmd.ts index cb06eb87..deb7364c 100755 --- a/src/cmd.ts +++ b/src/cmd.ts @@ -130,6 +130,24 @@ export async function setUp() { const Command = await getCommand(process.argv) + /** + * If you run `gh pr 1 -s node-gh --remote=origin --user protoEvangelion`, nopt will return + * + * { + * remote: 'origin', + * submit: 'node-gh', + * user: 'protoEvangelion', + * argv: { + * original: ['pr', '1', '-s', 'pr', 'node-gh', '--remote', 'origin', '--user', 'protoEvangelion'], + * remain: ['pr', '1'], + * cooked: ['pr', '1', '--submit', 'node-gh', '--remote', 'origin', '--user', 'protoEvangelion'], + * }, + * } + * + * Historically we passed every arg after 2nd arg (gh pr 1 -s user; everything after 'pr') + * and all parsed options to each cmd's payload function to figure out positional args and allow for neat shortcuts like: + * gh is 'new issue' 'new issue description' + */ const args = nopt(Command.DETAILS.options, Command.DETAILS.shorthands, process.argv, 2) setAutoFreeze(false) diff --git a/src/cmds/gists.ts b/src/cmds/gists.ts index 903d0410..76ef9c71 100644 --- a/src/cmds/gists.ts +++ b/src/cmds/gists.ts @@ -7,12 +7,11 @@ // -- Requires ------------------------------------------------------------------------------------- import * as inquirer from 'inquirer' -import { openUrl } from '../utils' +import { openUrl, userRanValidFlags } from '../utils' import * as base from '../base' import { getGitHubInstance } from '../github' import { afterHooks, beforeHooks } from '../hooks' import * as logger from '../logger' -import { hasCmdInOptions } from '../utils' const config = base.getConfig() const testing = process.env.NODE_ENV === 'testing' @@ -65,7 +64,7 @@ Gists.prototype.run = async function(done) { instance.config = config instance.GitHub = await getGitHubInstance() - if (!hasCmdInOptions(Gists.DETAILS.commands, options)) { + if (!userRanValidFlags(Gists.DETAILS.commands, options)) { options.list = true } diff --git a/src/cmds/notification.ts b/src/cmds/notification.ts index 7baf0b6f..e875705c 100644 --- a/src/cmds/notification.ts +++ b/src/cmds/notification.ts @@ -8,7 +8,7 @@ import { getGitHubInstance } from '../github' import * as logger from '../logger' -import { hasCmdInOptions } from '../utils' +import { userRanValidFlags } from '../utils' const printed = {} @@ -54,7 +54,7 @@ Notifications.prototype.run = async function(done) { const options = instance.options instance.GitHub = await getGitHubInstance() - if (!hasCmdInOptions(Notifications.DETAILS.commands, options)) { + if (!userRanValidFlags(Notifications.DETAILS.commands, options)) { options.latest = true } diff --git a/src/cmds/pull-request.ts b/src/cmds/pull-request.ts index b9abec8c..937a2e5f 100755 --- a/src/cmds/pull-request.ts +++ b/src/cmds/pull-request.ts @@ -10,14 +10,13 @@ import * as Table from 'cli-table3' import { startsWith } from 'lodash' import * as marked from 'marked' import * as TerminalRenderer from 'marked-terminal' -import { openUrl } from '../utils' +import { openUrl, userRanValidFlags } from '../utils' import * as wrap from 'wordwrap' import * as base from '../base' import * as git from '../git' import { getGitHubInstance } from '../github' import { afterHooks, beforeHooks } from '../hooks' import * as logger from '../logger' -import { hasCmdInOptions } from '../utils' const config = base.getConfig() const testing = process.env.NODE_ENV === 'testing' @@ -139,7 +138,7 @@ PullRequest.prototype.run = async function(done) { instance.config = config instance.GitHub = await getGitHubInstance() - if (!hasCmdInOptions(PullRequest.DETAILS.commands, options)) { + if (!userRanValidFlags(PullRequest.DETAILS.commands, options)) { const payload = options.argv.remain && options.argv.remain.concat().slice(1) if (payload && payload[0]) { diff --git a/src/cmds/repo.ts b/src/cmds/repo.ts index c2027e8b..918a1a81 100644 --- a/src/cmds/repo.ts +++ b/src/cmds/repo.ts @@ -9,14 +9,13 @@ import * as Octokit from '@octokit/rest' import * as fs from 'fs' import * as inquirer from 'inquirer' -import { openUrl } from '../utils' +import { openUrl, getCurrentFolderName, userRanValidFlags } from '../utils' import * as url from 'url' import * as base from '../base' import * as git from '../git' import { getGitHubInstance } from '../github' import { afterHooks, beforeHooks } from '../hooks' import * as logger from '../logger' -import { getCurrentFolderName, hasCmdInOptions } from '../utils' const config = base.getConfig() const testing = process.env.NODE_ENV === 'testing' @@ -97,7 +96,7 @@ Repo.prototype.run = async function(done) { instance.GitHub = await getGitHubInstance() if ( - !hasCmdInOptions(Repo.DETAILS.commands, options) && + !userRanValidFlags(Repo.DETAILS.commands, options) && options.browser !== false && options.argv.cooked.length === 1 ) { diff --git a/src/cmds/user.ts b/src/cmds/user.ts index ab98f19e..8ec96529 100644 --- a/src/cmds/user.ts +++ b/src/cmds/user.ts @@ -9,7 +9,7 @@ import * as configs from '../configs' import { getGitHubInstance, tokenExists } from '../github' import * as logger from '../logger' -import { hasCmdInOptions } from '../utils' +import { userRanValidFlags } from '../utils' const testing = process.env.NODE_ENV === 'testing' @@ -42,9 +42,9 @@ User.DETAILS = { User.prototype.run = async function(done) { const instance = this const options = instance.options - let login + let login = options.login - if (!hasCmdInOptions(User.DETAILS.commands, options)) { + if (!userRanValidFlags(User.DETAILS.commands, options)) { login = true } diff --git a/src/utils.ts b/src/utils.ts index 47e23a3d..ffab246e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -27,7 +27,10 @@ export function getCurrentFolderName(): string { return cwdArr[cwdArr.length - 1] } -export function hasCmdInOptions(commands, options) { +/** + * Checks to see if the cli arguments are one of the accepted flags + */ +export function userRanValidFlags(commands, options) { if (commands) { return commands.some(c => { return options[c] !== undefined