@@ -7,10 +7,11 @@ var findPkg = require('pkg-up');
7
7
var script = process . argv [ 2 ] ;
8
8
9
9
// help message:
10
- if ( script === '--help' ) {
10
+ if ( process . argv [ 2 ] === '--help' ) {
11
11
console . log (
12
12
`Edit npm scripts from the command line without worrying about json escaping.
13
13
14
+ edit-script
14
15
edit-script <script>`
15
16
) ;
16
17
process . exit ( ) ;
@@ -20,37 +21,24 @@ var pkgPath;
20
21
var pkg = { }
21
22
var scripts = { } ;
22
23
23
- if ( ! script ) {
24
- console . error ( 'Error: Must pass a script name' ) ;
25
- process . exit ( 1 ) ;
26
- }
24
+ const NEW_SCRIPT_SYMBOL = Symbol ( 'Create new script' ) ;
25
+ const EXIT_SYMBOL = Symbol ( 'Exit' ) ;
27
26
27
+ // Find package.json path:
28
28
findPkg ( )
29
29
. then ( function ( p ) {
30
30
if ( ! p ) throw new Error ( 'No package.json file found!' ) ;
31
31
pkgPath = p ;
32
+ // Load package.json:
32
33
return fs . readJson ( pkgPath ) ;
33
34
} )
34
35
. then ( function ( data ) {
36
+ // Assign global variables:
35
37
pkg = data ;
36
38
scripts = pkg . scripts ;
37
-
38
- return inquirer . prompt ( [
39
- {
40
- type : 'confirm' ,
41
- name : 'create' ,
42
- message : `The script "${ script } " does not exist. Create it?` ,
43
- when : ! scripts [ script ] ,
44
- } ,
45
- ] ) ;
46
- } )
47
- . then ( function ( answers ) {
48
- if ( ! scripts [ script ] && ! answers . create ) {
49
- console . log ( 'Aborting' ) ;
50
- process . exit ( ) ;
51
- }
52
39
} )
53
- . then ( function ( ) {
40
+ . then ( getScriptName )
41
+ . then ( function editScript ( ) {
54
42
return inquirer . prompt ( [
55
43
{
56
44
type : 'editor' ,
@@ -59,18 +47,100 @@ findPkg()
59
47
default : scripts [ script ] ,
60
48
} ,
61
49
] )
62
- } )
63
- . then ( function ( answers ) {
64
- var val = answers . script . trim ( ) ;
65
- if ( ! val ) {
66
- console . log ( 'Deleting script.' ) ;
67
- delete scripts [ script ] ;
50
+ . then ( function ( answers ) {
51
+ var val = answers . script . trim ( ) ;
52
+ if ( ! val ) {
53
+ console . log ( 'Deleting script.' ) ;
54
+ delete scripts [ script ] ;
55
+ } else {
56
+ scripts [ script ] = val ;
57
+ }
58
+ return fs . writeJson ( pkgPath , pkg )
59
+ } )
60
+ . catch ( function ( err ) {
61
+ console . error ( err ) ;
62
+ process . exit ( 1 ) ;
63
+ } ) ;
64
+ } ) ;
65
+
66
+ function getScriptName ( ) {
67
+ if ( script ) {
68
+ // Verify creation if the script does not exist:
69
+ return inquirer . prompt ( [
70
+ {
71
+ type : 'confirm' ,
72
+ name : 'create' ,
73
+ message : `The script "${ script } " does not exist. Create it?` ,
74
+ when : ! scripts [ script ] ,
75
+ } ,
76
+ ] )
77
+ . then ( function ( answers ) {
78
+ if ( ! script && ! scripts [ script ] && ! answers . create ) {
79
+ console . log ( 'Aborting' ) ;
80
+ process . exit ( ) ;
81
+ }
82
+ } ) ;
68
83
} else {
69
- scripts [ script ] = val ;
84
+ // Get choices:
85
+ var choices = Object . keys ( scripts ) . map ( function ( key ) {
86
+ return {
87
+ name : pad ( key , scripts [ key ] ) ,
88
+ value : key ,
89
+ short : key ,
90
+ }
91
+ } ) ;
92
+ // Add aditional choices:
93
+ choices . push ( new inquirer . Separator ( ) ) ;
94
+ choices . push ( {
95
+ name : 'Create a new script' ,
96
+ value : NEW_SCRIPT_SYMBOL ,
97
+ } ) ;
98
+ choices . push ( {
99
+ name : 'Exit edit-script' ,
100
+ value : EXIT_SYMBOL ,
101
+ } )
102
+ // Prompt for script name:
103
+ return inquirer . prompt ( [
104
+ {
105
+ type : 'list' ,
106
+ name : 'script' ,
107
+ message : 'Select a script to edit:' ,
108
+ choices : choices ,
109
+ } ,
110
+ ] )
111
+ . then ( function ( answers ) {
112
+ switch ( answers . script ) {
113
+ case NEW_SCRIPT_SYMBOL :
114
+ // Get script name:
115
+ return inquirer . prompt ( [ {
116
+ type : 'input' ,
117
+ name : 'name' ,
118
+ message : 'Enter the script name:' ,
119
+ validate : function ( val ) {
120
+ if ( ! val ) return 'Script name must not be empty' ;
121
+ else return true ;
122
+ } ,
123
+ } ] )
124
+ . then ( function ( answers ) {
125
+ // Set it:
126
+ script = answers . name ;
127
+ } )
128
+ break ;
129
+ case EXIT_SYMBOL :
130
+ process . exit ( ) ;
131
+ default :
132
+ script = answers . script ;
133
+ }
134
+ } )
70
135
}
71
- return fs . writeJson ( pkgPath , pkg )
72
- } )
73
- . catch ( function ( err ) {
74
- console . error ( err ) ;
75
- process . exit ( 1 ) ;
76
- } ) ;
136
+ }
137
+
138
+ function pad ( str1 , str2 ) {
139
+ var padLen = 60 - ( str1 . length + str2 . length ) ;
140
+ // Ensure at least one space:
141
+ var pad = ' ' ;
142
+ for ( var i = 1 ; i < padLen ; i ++ ) {
143
+ pad += ' ' ;
144
+ }
145
+ return str1 + pad + str2 ;
146
+ }
0 commit comments