Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
refactor: rename hasCmdInOptions to userRanValidFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Garant committed Sep 25, 2019
1 parent e378e4f commit c1f8cf2
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
18 changes: 18 additions & 0 deletions src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions src/cmds/gists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions src/cmds/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { getGitHubInstance } from '../github'
import * as logger from '../logger'
import { hasCmdInOptions } from '../utils'
import { userRanValidFlags } from '../utils'

const printed = {}

Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 2 additions & 3 deletions src/cmds/pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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]) {
Expand Down
5 changes: 2 additions & 3 deletions src/cmds/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
) {
Expand Down
6 changes: 3 additions & 3 deletions src/cmds/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 4 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c1f8cf2

Please sign in to comment.