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

Commit

Permalink
refactor(hooks): pass options object directly into hooks instead of i…
Browse files Browse the repository at this point in the history
…nstance
  • Loading branch information
Ryan Garant committed Sep 25, 2019
1 parent c1f8cf2 commit f556b87
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 65 deletions.
12 changes: 10 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "node",
"request": "launch",
"runtimeArgs": ["-r", "ts-node/register"],
"args": ["${workspaceFolder}/src/debug.ts", "us", "--whoami"],
"args": ["${workspaceFolder}/src/debug.ts", "user", "--login"],
// "args": ["${workspaceFolder}/src/debug.ts", "ji", "LWM-117", "--status"],
"console": "integratedTerminal"
},
Expand All @@ -19,7 +19,15 @@
"request": "launch",
"env": { "NODE_ENV": "testing", "GH_USER": "protoEvangelion", "GH_TOKEN": "0001" },
"runtimeArgs": ["-r", "ts-node/register"],
"args": ["${workspaceFolder}/src/debug.ts", "re", "--new", "foo", "--init"],
"args": [
"${workspaceFolder}/src/debug.ts",
"is",
"-N",
"-t",
"'Node GH rocks!'",
"-L",
"bug,question,test"
],
"console": "integratedTerminal"
}
]
Expand Down
12 changes: 6 additions & 6 deletions src/cmds/gists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ Gists.prototype.run = async function(done) {
for (const gist_id of options.delete) {
logger.log(`Deleting gist ${logger.colors.green(`${options.loggedUser}/${gist_id}`)}`)

await beforeHooks('gists.delete', instance)
await beforeHooks('gists.delete', { options })

await _deleteHandler(gist_id, instance)

await afterHooks('gists.delete', instance)
await afterHooks('gists.delete', { options })
}
}

if (options.fork) {
await beforeHooks('gists.fork', instance)
await beforeHooks('gists.fork', { options })

logger.log(`Forking gist on ${logger.colors.green(options.loggedUser)}`)

Expand All @@ -115,7 +115,7 @@ Gists.prototype.run = async function(done) {

logger.log(data.html_url)

await afterHooks('gists.fork', instance)
await afterHooks('gists.fork', { options })
}

if (options.list) {
Expand All @@ -133,7 +133,7 @@ Gists.prototype.run = async function(done) {
if (options.new) {
const privacy = options.private ? 'private' : 'public'

await beforeHooks('gists.new', instance)
await beforeHooks('gists.new', { options })

logger.log(
`Creating ${logger.colors.magenta(privacy)} gist on ${logger.colors.green(
Expand All @@ -152,7 +152,7 @@ Gists.prototype.run = async function(done) {
logger.log(data.html_url)
}

await afterHooks('gists.new', instance)
await afterHooks('gists.new', { options })
}

done && done()
Expand Down
54 changes: 27 additions & 27 deletions src/cmds/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
// -- Requires -------------------------------------------------------------------------------------

import { isArray } from 'lodash'
import { openUrl } from '../utils'
import { produce } from 'immer'
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'

// -- Constructor ----------------------------------------------------------------------------------

Expand Down Expand Up @@ -89,34 +88,35 @@ Issue.STATE_OPEN = 'open'

Issue.prototype.run = async function(done) {
const instance = this
const options = instance.options
let options = instance.options
const number = logger.colors.green(`#${options.number}`)

instance.config = config
instance.GitHub = await getGitHubInstance()

options.state = options.state || Issue.STATE_OPEN
options = produce(options, draft => {
draft.state = draft.state || Issue.STATE_OPEN

if (!hasCmdInOptions(Issue.DETAILS.commands, options)) {
const payload = options.argv.remain && options.argv.remain.slice(1)
if (!userRanValidFlags(Issue.DETAILS.commands, draft)) {
const payload = draft.argv.remain && draft.argv.remain.slice(1)

if (payload && payload[0]) {
if (/^\d+$/.test(payload[0])) {
options.browser = true
options.number = payload[0]
return
if (payload && payload[0]) {
if (/^\d+$/.test(payload[0])) {
draft.browser = true
draft.number = payload[0]
} else {
draft.new = true
draft.title = draft.title || payload[0]
draft.message = draft.message || payload[1]
}
} else {
draft.list = true
}

options.new = true
options.title = options.title || payload[0]
options.message = options.message || payload[1]
} else {
options.list = true
}
}
})

if (options.assign) {
await beforeHooks('issue.assign', instance)
await beforeHooks('issue.assign', { options })

logger.log(
`Assigning issue ${number} on ${getUserRepo(options)} to ${logger.colors.magenta(
Expand All @@ -132,7 +132,7 @@ Issue.prototype.run = async function(done) {

logger.log(logger.colors.cyan(data.html_url))

await afterHooks('issue.assign', instance)
await afterHooks('issue.assign', { options })
}

if (options.browser) {
Expand Down Expand Up @@ -176,7 +176,7 @@ Issue.prototype.run = async function(done) {
}

if (options.new) {
await beforeHooks('issue.new', instance)
await beforeHooks('issue.new', { options })

logger.log(`Creating a new issue on ${getUserRepo(options)}`)

Expand All @@ -192,21 +192,21 @@ Issue.prototype.run = async function(done) {

logger.log(data.html_url)

await afterHooks('issue.new', instance)
await afterHooks('issue.new', { options })
}

if (options.open) {
await beforeHooks('issue.open', instance)
await beforeHooks('issue.open', { options })

await openHandler(instance, options)

await afterHooks('issue.open', instance)
await afterHooks('issue.open', { options })
} else if (options.close) {
await beforeHooks('issue.close', instance)
await beforeHooks('issue.close', { options })

await closeHandler(instance, options)

await afterHooks('issue.close', instance)
await afterHooks('issue.close', { options })
}

if (options.search) {
Expand Down
24 changes: 12 additions & 12 deletions src/cmds/pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ PullRequest.prototype.list = async function(user, repo) {
let options = instance.options
let json

await beforeHooks('pull-request.list', instance)
await beforeHooks('pull-request.list', { options })

let sort = options.sort

Expand Down Expand Up @@ -766,7 +766,7 @@ PullRequest.prototype.list = async function(user, repo) {
}
}

await afterHooks('pull-request.list', instance)
await afterHooks('pull-request.list', { options })
}

PullRequest.prototype.listFromAllRepositories = async function() {
Expand Down Expand Up @@ -925,7 +925,7 @@ PullRequest.prototype._fetchHandler = async function() {
fetchType = PullRequest.FETCH_TYPE_REBASE
}

await beforeHooks('pull-request.fetch', instance)
await beforeHooks('pull-request.fetch', { options })

let operation = ''
let branch = options.pullBranch
Expand All @@ -952,14 +952,14 @@ PullRequest.prototype._fetchHandler = async function() {
throw new Error(`Can't fetch pull request ${options.number}.\n${err}`)
}

await afterHooks('pull-request.fetch', instance)
await afterHooks('pull-request.fetch', { options })
}

PullRequest.prototype._fwdHandler = async function() {
const instance = this
const options = this.options

await beforeHooks('pull-request.fwd', instance)
await beforeHooks('pull-request.fwd', { options })

logger.log(
`Forwarding pull request ${logger.colors.green(
Expand All @@ -982,14 +982,14 @@ PullRequest.prototype._fwdHandler = async function() {

instance.setMergeCommentRequiredOptions_()

await afterHooks('pull-request.fwd', instance)
await afterHooks('pull-request.fwd', { options })
}

PullRequest.prototype._closeHandler = async function() {
const instance = this
const options = this.options

await beforeHooks('pull-request.close', instance)
await beforeHooks('pull-request.close', { options })

logger.log(`Closing pull request ${logger.colors.green(`#${options.number}`)}`)

Expand All @@ -1003,7 +1003,7 @@ PullRequest.prototype._closeHandler = async function() {

instance.setMergeCommentRequiredOptions_()

await afterHooks('pull-request.close', instance)
await afterHooks('pull-request.close', { options })
}

PullRequest.prototype._commentHandler = async function() {
Expand Down Expand Up @@ -1081,7 +1081,7 @@ PullRequest.prototype._openHandler = async function() {
const instance = this
const options = this.options

await beforeHooks('pull-request.open', instance)
await beforeHooks('pull-request.open', { options })

logger.log(`Opening pull request ${logger.colors.green(`#${options.number}`)}`)

Expand All @@ -1093,14 +1093,14 @@ PullRequest.prototype._openHandler = async function() {

logger.log(data.html_url)

await afterHooks('pull-request.open', instance)
await afterHooks('pull-request.open', { options })
}

PullRequest.prototype._submitHandler = async function() {
const instance = this
const options = this.options

await beforeHooks('pull-request.submit', instance)
await beforeHooks('pull-request.submit', { options })

logger.log(`Submitting pull request to ${logger.colors.magenta(`@${options.submit}`)}`)

Expand All @@ -1118,5 +1118,5 @@ PullRequest.prototype._submitHandler = async function() {

instance.setMergeCommentRequiredOptions_()

await afterHooks('pull-request.submit', instance)
await afterHooks('pull-request.submit', { options })
}
Loading

0 comments on commit f556b87

Please sign in to comment.