1
1
// external dependencies
2
- const inquirer = require ( 'inquirer ' ) ;
2
+ const prompts = require ( 'prompts ' ) ;
3
3
4
4
// internal modules
5
5
const path = require ( 'path' ) ;
@@ -8,8 +8,10 @@ const util = require('util');
8
8
const exec = util . promisify ( require ( 'child_process' ) . exec ) ;
9
9
const os = require ( 'os' ) ;
10
10
11
+
11
12
// helper functions
12
13
const color = require ( './colors.js' ) ;
14
+ // require('./helpers.js');
13
15
14
16
// Default settings.
15
17
const settingsData = {
@@ -66,53 +68,78 @@ function writeSettings(data, command = '<command>', successMessage = "Settings u
66
68
} )
67
69
}
68
70
71
+ async function openURL ( url ) {
72
+ let stderr ;
73
+ switch ( process . platform ) {
74
+ case "darwin" :
75
+ ( { stderr} = await exec ( `open ${ url } ` ) ) ;
76
+ break ;
77
+ case "win32" :
78
+ ( { stderr} = await exec ( `start ${ url } ` ) ) ;
79
+ break ;
80
+ default :
81
+ ( { stderr} = await exec ( `xdg-open ${ url } ` ) ) ;
82
+ break ;
83
+ }
84
+ console . log ( 'here ' , stderr )
85
+ return stderr ;
86
+ }
87
+
88
+ const suggestFilter = ( input , choices ) => {
89
+ return Promise . resolve (
90
+ choices . filter ( choice =>
91
+ choice . title . toLowerCase ( ) . includes ( input . toLowerCase ( ) )
92
+ )
93
+ )
94
+ }
95
+
96
+
97
+ const onCancel = ( ) => {
98
+ console . error ( "See ya ('__') /" ) ;
99
+ process . exit ( ) ;
100
+ return false ;
101
+ }
102
+
103
+ function getChoices ( ) {
104
+ const projects = [ ...settings . projects ] ;
105
+ const result = projects . map ( ( { ...project } ) => { // Spreading project to make it immutable
106
+ project . title = project . name ;
107
+ project . value = { name : project . name , path : project . path } ;
108
+ if ( project . editor && project . editor !== settings . commandToOpen ) {
109
+ project . title += color . grey ( ` (${ color . boldGrey ( 'editor:' ) } ${ color . grey ( project . editor + ')' ) } ` ) ;
110
+ project . value . editor = project . editor ;
111
+ }
112
+ return project ;
113
+ } ) ;
114
+
115
+ return result ;
116
+ }
117
+
69
118
async function selectProject ( projectName , action ) {
70
119
let selectedProject ;
71
120
if ( ! projectName ) {
72
- const projects = [ ...settings . projects ] ;
73
121
// Ask which project he wants to open
74
122
const questions = [
75
123
{
76
- type : 'list ' ,
124
+ type : 'autocomplete ' ,
77
125
message : `Select project to ${ action } :` ,
78
126
name : 'selectedProject' ,
79
- choices : projects . map ( ( { ...project } ) => { // Spreading project to make it immutable
80
- project . value = { name : project . name , path : project . path } ;
81
- if ( project . editor && project . editor !== settings . commandToOpen ) {
82
- project . name += color . grey ( ` (${ color . boldGrey ( 'editor:' ) } ${ project . editor } )` ) ;
83
- project . value . editor = project . editor ;
84
- }
85
- return project ;
86
- } )
127
+ choices :getChoices ( ) ,
128
+ limit :40 ,
129
+ suggest :suggestFilter
87
130
}
88
131
] ;
132
+
89
133
// Redirecting to stderr in order for it to be used with command substitution
90
- var promptModule = inquirer . createPromptModule ( { output : process . stderr } ) ;
91
- ( { selectedProject } = await promptModule ( questions ) ) ;
134
+ ( { selectedProject } = await prompts ( questions , { onCancel } ) ) ;
135
+
92
136
} else {
93
137
// If project name is mentioned then open directly
94
138
selectedProject = settings . projects . find ( project => project . name . toLowerCase ( ) == projectName . toLowerCase ( ) ) ;
95
139
}
96
140
return selectedProject ;
97
141
}
98
142
99
- async function openURL ( url ) {
100
- let stderr ;
101
- switch ( process . platform ) {
102
- case "darwin" :
103
- ( { stderr} = await exec ( `open ${ url } ` ) ) ;
104
- break ;
105
- case "win32" :
106
- ( { stderr} = await exec ( `start ${ url } ` ) ) ;
107
- break ;
108
- default :
109
- ( { stderr} = await exec ( `xdg-open ${ url } ` ) ) ;
110
- break ;
111
- }
112
- console . log ( 'here ' , stderr )
113
- return stderr ;
114
- }
115
-
116
143
// Helper funcitions [END]
117
144
118
145
// oncmd: projectman open [projectName]
@@ -178,12 +205,12 @@ async function addProject(projectDirectory = '.', cmdObj = undefined) {
178
205
name = newProject . path . split ( path . sep ) . pop ( ) ;
179
206
}
180
207
181
- ( { finalName : newProject . name } = await inquirer . prompt ( [ {
182
- type : 'input ' ,
208
+ ( { finalName : newProject . name } = await prompts ( [ {
209
+ type : 'text ' ,
183
210
message : 'Project Name :' ,
184
211
name : 'finalName' ,
185
- default : name
186
- } ] ) ) ;
212
+ initial : name
213
+ } ] , { onCancel } ) ) ;
187
214
188
215
if ( cmdObj . repo ) {
189
216
newProject . name = newProject . name . concat ( ` (${ name . split ( '.' ) [ 0 ] } )` )
@@ -213,7 +240,6 @@ async function removeProject(projectName) {
213
240
writeSettings ( settings , 'remove' , "Project Removed" ) ;
214
241
}
215
242
216
-
217
243
// pm seteditor [command]
218
244
async function setEditor ( command , cmdObj = undefined ) {
219
245
let commandToOpen ;
@@ -231,47 +257,47 @@ async function setEditor(command, cmdObj = undefined) {
231
257
if ( ! command ) {
232
258
const questions = [
233
259
{
234
- type : 'list ' ,
260
+ type : 'select ' ,
235
261
message : 'Select text editor' ,
236
262
name : 'selectedEditor' ,
237
263
choices : [
238
264
{
239
- name : 'VSCode' ,
265
+ title : 'VSCode' ,
240
266
value : 'code'
241
267
} ,
242
268
{
243
- name : 'Sublime' ,
269
+ title : 'Sublime' ,
244
270
value : 'subl'
245
271
} ,
246
272
{
247
- name : 'Atom' ,
273
+ title : 'Atom' ,
248
274
value : 'atom'
249
275
} ,
250
276
{
251
- name : 'Vim' ,
277
+ title : 'Vim' ,
252
278
value : 'vim'
253
279
} ,
254
280
{
255
- name : 'Other' ,
281
+ title : 'Other' ,
256
282
value : 'other'
257
283
}
258
284
]
259
285
}
260
286
] ;
261
- const { selectedEditor} = await inquirer . prompt ( questions ) ;
287
+ const { selectedEditor} = await prompts ( questions , { onCancel } ) ;
262
288
if ( selectedEditor == 'other' ) {
263
289
console . warn ( "Enter command that you use to open Editor from Terminal" ) ;
264
290
console . log ( `E.g With VSCode Installed, you can type ${ color . yellow ( 'code <directory>' ) } in terminal to open directory` ) ;
265
291
console . log ( `In this case, the command would be ${ color . yellow ( 'code' ) } \n` ) ;
266
292
const question = {
267
- type : 'input ' ,
293
+ type : 'text ' ,
268
294
message : 'Enter command :' ,
269
295
name : 'command' ,
270
296
validate : function ( val ) {
271
297
return val !== ''
272
298
}
273
299
}
274
- const { command} = await inquirer . prompt ( [ question ] )
300
+ const { command} = await prompts ( [ question ] , { onCancel } )
275
301
commandToOpen = command ;
276
302
} else {
277
303
commandToOpen = selectedEditor ;
@@ -351,13 +377,29 @@ async function getProjectPath(projectName) {
351
377
return ;
352
378
}
353
379
354
- const selectedProject = await selectProject ( projectName , "get directory" ) ;
355
-
356
- if ( ! selectedProject ) {
357
- console . error ( `Project does not exist. Add it using ${ color . yellow ( 'pm add [projectPath]' ) } or cd till the project folder and type ${ color . yellow ( 'pm add' ) } ` ) ;
358
- return ;
380
+ if ( ! projectName ) {
381
+ const question = {
382
+ name :'selectedProject' ,
383
+ message :'Enter number of project you want to cd to:' ,
384
+ type :'autocomplete' ,
385
+ out :process . stderr ,
386
+ choices :getChoices ( ) ,
387
+ onRender :( ) => {
388
+ process . stderr . write ( '\033c' ) ;
389
+ }
390
+ }
391
+
392
+ const { selectedProject} = await prompts ( [ question ] , { onCancel} )
393
+ if ( ! selectedProject ) {
394
+ console . error ( `Project does not exist. Add it using ${ color . yellow ( 'pm add [projectPath]' ) } or cd till the project folder and type ${ color . yellow ( 'pm add' ) } ` ) ;
395
+ return ;
396
+ }
397
+
398
+ } else {
399
+ selectedProject = settings . projects . find ( project => project . name . toLowerCase ( ) == projectName . toLowerCase ( ) ) ;
359
400
}
360
401
402
+ // Print path
361
403
console . log ( selectedProject . path ) ;
362
404
}
363
405
0 commit comments