1
1
#!/usr/bin/env node
2
2
3
- 'use strict' ;
3
+ const app = require ( './lib/app' ) ;
4
4
5
- const chalk = require ( 'chalk' ) ;
6
- const path = require ( 'path' ) ;
7
- const program = require ( 'commander' ) ;
8
- const updateNotifier = require ( 'update-notifier' ) ;
9
-
10
- const git = require ( path . resolve ( __dirname , 'utils/git' ) ) ;
11
- const dialogue = require ( path . resolve ( __dirname , 'utils/interface' ) ) ;
12
- const { getRemoteTabs } = require ( path . resolve ( __dirname , 'utils/utils' ) ) ;
13
-
14
- // Checks for available update and returns an instance
15
- const pkg = require ( path . resolve ( __dirname , 'package.json' ) ) ;
16
- const notifier = updateNotifier ( { pkg } ) ;
17
-
18
- program . version ( '0.6.1' , '-v, --version' ) ;
19
-
20
- program . parse ( process . argv ) ;
21
-
22
- if ( notifier . update ) {
23
- notifier . notify ( ) ;
24
- }
25
-
26
- if ( ! process . argv . slice ( 2 ) . length ) {
27
- const screen = dialogue . screen ( ) ;
28
-
29
- const branchTable = dialogue . branchTable ( ) ;
30
- const statusBarText = dialogue . statusBarText ( ) ;
31
- const helpDialogue = dialogue . helpDialogue ( ) ;
32
- const question = dialogue . question ( ) ;
33
- const statusBar = dialogue . statusBar ( ) ;
34
- const statusHelpText = dialogue . statusHelpText ( ) ;
35
-
36
- var currentRemote = 'local' ;
37
- var remoteList = [ ] ;
38
-
39
- const toggleHelp = ( ) => {
40
- helpDialogue . toggle ( ) ;
41
- screen . render ( ) ;
42
- } ;
43
-
44
- /**
45
- * @todo : Build a keyMap utility
46
- * @body : Add left and right functionality to change remote. Add getUniqueRemotes method here.
47
- */
48
- screen . key ( '?' , toggleHelp ) ;
49
- screen . key ( [ 'escape' , 'q' , 'C-c' ] , ( ) => process . exit ( 0 ) ) ;
50
- screen . key ( 'r' , ( ) => {
51
- branchTable . clearItems ( ) ;
52
-
53
- git . doFetchBranches ( ) . then ( ( ) => refreshTable ( currentRemote ) ) ;
54
- } ) ;
55
-
56
- screen . append ( branchTable ) ;
57
- screen . append ( statusBar ) ;
58
- screen . append ( helpDialogue ) ;
59
-
60
- statusBar . append ( statusBarText ) ;
61
- statusBar . append ( statusHelpText ) ;
62
-
63
- process . on ( 'SIGWINCH' , ( ) => {
64
- screen . emit ( 'resize' ) ;
65
- } ) ;
66
-
67
- /**
68
- * Trim and remove whitespace from selected line.
69
- *
70
- * @param {String } selectedLine String representation of selected line.
71
- * @return {Array } Array of selected line.
72
- */
73
- const parseSelection = selectedLine => {
74
- const selection = selectedLine . split ( / \s * \s / ) . map ( column => {
75
- return column === 'local' ? '' : column ;
76
- } ) ;
77
-
78
- return selection ;
79
- } ;
80
-
81
- branchTable . on ( 'select' , async selectedLine => {
82
- const selection = parseSelection ( selectedLine . content ) ;
83
-
84
- const gitBranch = selection [ 2 ] ;
85
- const gitRemote = selection [ 1 ] ;
86
-
87
- /**
88
- * @todo : Identify and handle unhandledRejections
89
- * @body : Some errors are not handled and the following rejection is passed is the workaround
90
- */
91
- process . on ( 'unhandledRejection' , reason => {
92
- console . log ( chalk . yellow ( '[LOG] ' ) + reason ) ;
93
- } ) ;
94
-
95
- // If selection is a remote, prompt if new branch is to be created.
96
- if ( gitRemote ) {
97
- question . setData ( [
98
- [
99
- 'Create local branch named: ' +
100
- chalk . white . bold ( `${ gitBranch } ` ) +
101
- '?' ,
102
- ] ,
103
- [ 'Yes' ] ,
104
- [ 'No' ] ,
105
- ] ) ;
106
-
107
- screen . append ( question ) ;
108
- question . focus ( ) ;
109
- screen . render ( ) ;
110
-
111
- question . on ( 'select' , async ( val , key ) => {
112
- const answer = val . content . trim ( ) ;
113
-
114
- if ( answer === 'Yes' ) {
115
- await git
116
- . doCheckoutBranch ( gitBranch , gitRemote )
117
- . then ( git . doCreateBranch ( gitBranch ) )
118
- . then ( screen . destroy ( ) ) ;
119
- } else if ( answer === 'No' ) {
120
- await git
121
- . doCheckoutBranch ( gitBranch , gitRemote )
122
- . then ( screen . destroy ( ) ) ;
123
- }
124
- } ) ;
125
- } else {
126
- await git . doCheckoutBranch ( gitBranch , gitRemote ) . then ( screen . destroy ( ) ) ;
127
- }
128
- } ) ;
129
-
130
- /**
131
- * @todo : Build a keybind utility
132
- */
133
- branchTable . key ( [ 'left' , 'h' ] , ( ) => {
134
- currentRemote = getPrevRemote ( currentRemote , remoteList ) ;
135
- } ) ;
136
-
137
- branchTable . key ( [ 'right' , 'l' ] , ( ) => {
138
- currentRemote = getNextRemote ( currentRemote , remoteList ) ;
139
- } ) ;
140
-
141
- branchTable . key ( 'j' , ( ) => {
142
- branchTable . down ( ) ;
143
-
144
- screen . render ( ) ;
145
- } ) ;
146
-
147
- branchTable . key ( 'k' , ( ) => {
148
- branchTable . up ( ) ;
149
-
150
- screen . render ( ) ;
151
- } ) ;
152
-
153
- branchTable . key ( 'space' , function ( ) {
154
- const selection = parseSelection ( this . items [ this . selected ] . content ) ;
155
-
156
- const gitBranch = selection [ 2 ] ;
157
- const gitRemote = selection [ 1 ] ;
158
-
159
- var args = [ ] ;
160
-
161
- if ( gitRemote ) {
162
- args . push ( gitRemote ) ;
163
- }
164
-
165
- args . push ( gitBranch ) ;
166
-
167
- if ( args . length > 1 ) {
168
- args = args . join ( '/' ) ;
169
- }
170
-
171
- screen . spawn ( 'git' , [ 'log' , args , '--color=always' ] ) ;
172
- } ) ;
173
-
174
- branchTable . focus ( ) ;
175
-
176
- /**
177
- * Cycle to previous remote
178
- *
179
- * @param currentRemote {String} Current displayed remote
180
- * @param remoteList {Array} Unique remotes for current project
181
- * @return {String }
182
- */
183
- function getPrevRemote ( currentRemote , remoteList ) {
184
- var currIndex = remoteList . indexOf ( currentRemote ) ;
185
-
186
- if ( currIndex > 0 ) {
187
- currIndex -= 1 ;
188
- }
189
-
190
- currentRemote = remoteList [ currIndex ] ;
191
-
192
- refreshTable ( currentRemote ) ;
193
-
194
- return currentRemote ;
195
- }
196
-
197
- /**
198
- * Cycle to next remote
199
- *
200
- * @param currentRemote {String} Current displayed remote
201
- * @param remoteList {Array} Unique remotes for current project
202
- * @return {String }
203
- */
204
- function getNextRemote ( currentRemote , remoteList ) {
205
- var currIndex = remoteList . indexOf ( currentRemote ) ;
206
-
207
- if ( currIndex < remoteList . length - 1 ) {
208
- currIndex += 1 ;
209
- }
210
-
211
- currentRemote = remoteList [ currIndex ] ;
212
-
213
- refreshTable ( currentRemote ) ;
214
-
215
- return currentRemote ;
216
- }
217
-
218
- /**
219
- * Build array of branches for main interface
220
- *
221
- * @param {String } currentRemote Current displayed remote
222
- */
223
- async function refreshTable ( currentRemote ) {
224
- const results = await git . buildListArray ( currentRemote ) ;
225
-
226
- branchTable . setData ( [ [ '' , 'Remote' , 'Branch Name' , 'Path' ] , ...results ] ) ;
227
-
228
- remoteList = await git . buildRemoteList ( ) ;
229
-
230
- statusBarText . content = getRemoteTabs ( remoteList , currentRemote ) ;
231
-
232
- screen . render ( ) ;
233
- }
234
-
235
- refreshTable ( currentRemote ) ;
236
- }
5
+ app . start ( process . argv . slice ( 2 ) ) ;
0 commit comments