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

Commit 92a013a

Browse files
author
Ryan Garant
committed
fix(plugins): rewrite cmd/plugin resolution to support typescript & commonjs environments
1 parent 301e975 commit 92a013a

23 files changed

+128
-156
lines changed

default.gh.json

-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,5 @@
117117

118118
"ssh": true,
119119

120-
"color": true,
121-
122120
"signature": ""
123121
}

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"dependencies": {
9090
"@babel/polyfill": "^7.2.5",
9191
"async": "~1.5.0",
92-
"colors": "~1.1.2",
92+
"colors": "~1.3.3",
9393
"github": "~0.2.4",
9494
"handlebars": "~4.0.4",
9595
"inquirer": "~6.2.0",

src/base.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7-
import * as configs from './configs'
87
import * as fs from 'fs'
98
import * as Github from 'github'
109
import * as path from 'path'
1110
import * as updateNotifier from 'update-notifier'
11+
import * as configs from './configs'
1212

1313
// -- Config -------------------------------------------------------------------
1414

15+
const config = configs.getConfig()
16+
1517
export function clone(o) {
1618
return JSON.parse(JSON.stringify(o))
1719
}
@@ -20,7 +22,7 @@ export function clone(o) {
2022

2123
export function load() {}
2224

23-
export const github = setupGithubClient(configs.getConfig())
25+
export const github = setupGithubClient(config)
2426

2527
export function asyncReadPackages(callback) {
2628
function read(err, data) {
@@ -51,8 +53,6 @@ export function checkVersion() {
5153
}
5254

5355
export function expandAliases(options) {
54-
const config = configs.getConfig()
55-
5656
if (config.alias) {
5757
options.fwd = config.alias[options.fwd] || options.fwd
5858
options.submit = config.alias[options.submit] || options.submit
@@ -67,7 +67,7 @@ export function find(filepath, opt_pattern) {
6767
}
6868

6969
export function getUser() {
70-
return configs.getConfig().github_user
70+
return config.github_user
7171
}
7272

7373
// Export some config methods to allow plugins to access them

src/cmd.ts

+41-47
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
// export const hi = 'hi'
1010

1111
import * as async from 'async'
12-
import { checkVersion, clone, find, getUser, expandAliases, load } from './base'
13-
import * as configs from './configs'
1412
import * as fs from 'fs'
15-
import * as git from './git'
1613
import * as nopt from 'nopt'
1714
import * as path from 'path'
15+
import { checkVersion, clone, expandAliases, find, getUser, load } from './base'
1816
import User from './cmds/user'
17+
import * as configs from './configs'
18+
import * as git from './git'
1919

2020
const config = configs.getConfig()
2121

@@ -41,63 +41,57 @@ function invokePayload(options, command, cooked, remain) {
4141
}
4242
}
4343

44-
async function resolveCmd(name) {
45-
let Command
44+
async function resolveCmd(name, commandDir) {
45+
const commandFiles = find(commandDir, /\.js$/i)
46+
47+
const commandName = commandFiles.filter(file => {
48+
switch (file) {
49+
case 'milestone.js':
50+
if (name === 'ms') return true
51+
break
52+
case 'notification.js':
53+
if (name === 'nt') return true
54+
break
55+
case 'pull-request.js':
56+
if (name === 'pr') return true
57+
break
58+
}
4659

47-
const commandDir = path.join(__dirname, 'cmds')
48-
const commandPath = path.join(commandDir, `${name}.js`)
60+
if (file.startsWith(name)) {
61+
return true
62+
}
4963

50-
if (fs.existsSync(commandPath)) {
51-
Command = await import(commandPath)
52-
} else {
53-
const commandFiles = find(commandDir, /\.js$/i)
54-
55-
const commandName = commandFiles.filter(file => {
56-
switch (file) {
57-
case 'milestone.js':
58-
if (name === 'ms') return true
59-
break
60-
case 'notification.js':
61-
if (name === 'nt') return true
62-
break
63-
case 'pull-request.js':
64-
if (name === 'pr') return true
65-
break
66-
}
64+
return false
65+
})[0]
6766

68-
if (file.startsWith(name)) {
69-
return true
70-
}
67+
if (commandName) {
68+
return await import(path.join(commandDir, commandName))
69+
}
7170

72-
return false
73-
})
71+
return resolvePlugin(name)
72+
}
7473

75-
Command = await import(path.join(commandDir, commandName[0]))
76-
}
74+
function resolvePlugin(name) {
75+
// If plugin command exists, register the executed plugin name on
76+
// process.env. This may simplify core plugin infrastructure.
77+
process.env.NODEGH_PLUGIN = name
7778

78-
return Command.default
79+
return { default: configs.getPlugin(name).Impl }
7980
}
8081

8182
async function loadCommand(name) {
82-
let Command = await resolveCmd(name)
83-
let plugin
84-
85-
// If command was not found, check if it is registered as a plugin.
86-
if (!Command) {
87-
try {
88-
plugin = configs.getPlugin(name)
89-
} catch (e) {
90-
return null
91-
}
83+
let Command
9284

93-
Command = plugin.Impl
85+
const commandDir = path.join(__dirname, 'cmds')
86+
const commandPath = path.join(commandDir, `${name}.js`)
9487

95-
// If plugin command exists, register the executed plugin name on
96-
// process.env. This may simplify core plugin infrastructure.
97-
process.env.NODEGH_PLUGIN = name
88+
if (fs.existsSync(commandPath)) {
89+
Command = await import(commandPath)
90+
} else {
91+
Command = await resolveCmd(name, commandDir)
9892
}
9993

100-
return Command
94+
return Command.default
10195
}
10296

10397
export function setUp() {

src/cmds/alias.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import * as base from '../base'
1010
import * as configs from '../configs'
11-
import logger from '../logger'
11+
import * as logger from '../logger'
1212

1313
const config = base.getConfig()
1414

src/cmds/gists.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
// -- Requires -------------------------------------------------------------------------------------
88

9-
import * as base from '../base'
10-
import * as hooks from '../hooks'
119
import * as inquirer from 'inquirer'
1210
import * as openUrl from 'opn'
13-
import logger from '../logger'
11+
import * as base from '../base'
12+
import * as hooks from '../hooks'
13+
import * as logger from '../logger'
1414

1515
const config = base.getConfig()
1616
const testing = process.env.NODE_ENV === 'testing'

src/cmds/hello.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// -- Requires -------------------------------------------------------------------------------------
88

9-
import logger from '../logger'
9+
import * as logger from '../logger'
1010

1111
// -- Constructor ----------------------------------------------------------------------------------
1212

src/cmds/help.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
// -- Requires -------------------------------------------------------------------------------------
88

9-
import * as base from '../base'
10-
import * as configs from '../configs'
119
import * as nopt from 'nopt'
1210
import * as path from 'path'
1311
import * as stream from 'stream'
1412
import * as url from 'url'
15-
import logger from '../logger'
13+
import * as base from '../base'
14+
import * as configs from '../configs'
15+
import * as logger from '../logger'
1616

1717
// -- Constructor ----------------------------------------------------------------------------------
1818

src/cmds/issue.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
// -- Requires -------------------------------------------------------------------------------------
88

99
import * as async from 'async'
10+
import { isArray } from 'lodash'
11+
import * as openUrl from 'opn'
1012
import * as base from '../base'
1113
import * as hooks from '../hooks'
12-
import * as openUrl from 'opn'
13-
import * as _ from 'lodash'
14-
import logger from '../logger'
14+
import * as logger from '../logger'
1515

1616
const config = base.getConfig()
1717

@@ -584,7 +584,7 @@ function formatIssues(issues, showDetailed) {
584584
`
585585
}
586586

587-
if (_.isArray(issue.labels) && issue.labels.length > 0) {
587+
if (isArray(issue.labels) && issue.labels.length > 0) {
588588
const labels = issue.labels.map(label => label.name)
589589
const labelHeading = labels.length > 1 ? 'labels: ' : 'label: '
590590

src/cmds/milestone.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import * as async from 'async'
1010
import * as base from '../base'
11-
import logger from '../logger'
11+
import * as logger from '../logger'
1212

1313
// -- Constructor ----------------------------------------------------------------------------------
1414

src/cmds/notification.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import * as async from 'async'
1010
import * as base from '../base'
11-
import logger from '../logger'
11+
import * as logger from '../logger'
1212

1313
const printed = {}
1414

src/cmds/pull-request.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
// -- Requires -------------------------------------------------------------------------------------
88

99
import * as async from 'async'
10+
import { startsWith } from 'lodash'
11+
import * as openUrl from 'opn'
1012
import * as base from '../base'
1113
import * as git from '../git'
1214
import * as hooks from '../hooks'
13-
import * as openUrl from 'opn'
14-
import * as _ from 'lodash'
15+
import * as logger from '../logger'
1516
import Issues from './issue'
16-
import logger from '../logger'
1717

1818
const config = base.getConfig()
1919

@@ -420,7 +420,7 @@ PullRequest.prototype.getBranchNameFromPullNumber_ = function(number) {
420420
}
421421

422422
PullRequest.prototype.getPullRequestNumberFromBranch_ = function(currentBranch, prefix) {
423-
if (currentBranch && _.startsWith(currentBranch, prefix)) {
423+
if (currentBranch && startsWith(currentBranch, prefix)) {
424424
return currentBranch.replace(prefix, '')
425425
}
426426
}

src/cmds/repo.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
// -- Requires -------------------------------------------------------------------------------------
88

9-
import * as base from '../base'
109
import * as fs from 'fs'
11-
import * as git from '../git'
12-
import * as hooks from '../hooks'
13-
import * as openUrl from 'opn'
1410
import * as inquirer from 'inquirer'
11+
import * as openUrl from 'opn'
1512
import * as url from 'url'
16-
import logger from '../logger'
13+
import * as base from '../base'
14+
import * as git from '../git'
15+
import * as hooks from '../hooks'
16+
import * as logger from '../logger'
1717

1818
const config = base.getConfig()
1919
const testing = process.env.NODE_ENV === 'testing'

src/cmds/user.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
// -- Requires -------------------------------------------------------------------------------------
88

9-
import * as base from '../base'
10-
import * as configs from '../configs'
119
import * as fs from 'fs'
1210
import * as inquirer from 'inquirer'
1311
import * as moment from 'moment'
1412
import * as userhome from 'userhome'
15-
import logger from '../logger'
13+
import * as base from '../base'
14+
import * as configs from '../configs'
15+
import * as logger from '../logger'
1616

1717
const config = configs.getConfig()
1818
const testing = process.env.NODE_ENV === 'testing'

src/cmds/version.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import * as base from '../base'
8-
import logger from '../logger'
8+
import * as logger from '../logger'
99

1010
export default function Version() {}
1111

src/configs.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import * as fs from 'fs'
88
import * as path from 'path'
99
import * as userhome from 'userhome'
1010
import * as which from 'which'
11-
import { spawnSync } from './exec'
12-
import logger from './logger'
11+
import * as exec from './exec'
12+
import * as logger from './logger'
1313

1414
let cache = {}
1515
let plugins
@@ -23,7 +23,7 @@ export function getNodeModulesGlobalPath() {
2323
let path = getConfig()[PLUGINS_PATH_KEY]
2424

2525
if (path === undefined) {
26-
result = spawnSync('npm', ['root', '-g'])
26+
result = exec.spawnSync('npm', ['root', '-g'])
2727

2828
if (result.stdout) {
2929
path = result.stdout

src/exec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import * as child_process from 'child_process'
8-
import logger from './logger'
8+
import * as logger from './logger'
99

1010
export function spawnSync(cmd, args, options?: object) {
1111
var exec

src/git.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import * as exec from './exec'
8-
import logger from './logger'
8+
import * as logger from './logger'
99

1010
const git_command = process.env.GH_GIT_COMMAND || 'git'
1111

0 commit comments

Comments
 (0)