9
9
// export const hi = 'hi'
10
10
11
11
import * as async from 'async'
12
- import { checkVersion , clone , find , getUser , expandAliases , load } from './base'
13
- import * as configs from './configs'
14
12
import * as fs from 'fs'
15
- import * as git from './git'
16
13
import * as nopt from 'nopt'
17
14
import * as path from 'path'
15
+ import { checkVersion , clone , expandAliases , find , getUser , load } from './base'
18
16
import User from './cmds/user'
17
+ import * as configs from './configs'
18
+ import * as git from './git'
19
19
20
20
const config = configs . getConfig ( )
21
21
@@ -41,63 +41,57 @@ function invokePayload(options, command, cooked, remain) {
41
41
}
42
42
}
43
43
44
- async function resolveCmd ( name ) {
45
- let Command
44
+ async function resolveCmd ( name , commandDir ) {
45
+ const commandFiles = find ( commandDir , / \. j s $ / 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
+ }
46
59
47
- const commandDir = path . join ( __dirname , 'cmds' )
48
- const commandPath = path . join ( commandDir , `${ name } .js` )
60
+ if ( file . startsWith ( name ) ) {
61
+ return true
62
+ }
49
63
50
- if ( fs . existsSync ( commandPath ) ) {
51
- Command = await import ( commandPath )
52
- } else {
53
- const commandFiles = find ( commandDir , / \. j s $ / 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 ]
67
66
68
- if ( file . startsWith ( name ) ) {
69
- return true
70
- }
67
+ if ( commandName ) {
68
+ return await import ( path . join ( commandDir , commandName ) )
69
+ }
71
70
72
- return false
73
- } )
71
+ return resolvePlugin ( name )
72
+ }
74
73
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
77
78
78
- return Command . default
79
+ return { default : configs . getPlugin ( name ) . Impl }
79
80
}
80
81
81
82
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
92
84
93
- Command = plugin . Impl
85
+ const commandDir = path . join ( __dirname , 'cmds' )
86
+ const commandPath = path . join ( commandDir , `${ name } .js` )
94
87
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 )
98
92
}
99
93
100
- return Command
94
+ return Command . default
101
95
}
102
96
103
97
export function setUp ( ) {
0 commit comments