@@ -3,7 +3,7 @@ const path = require('path')
3
3
const fs = require ( 'fs-extra' )
4
4
const opta = require ( 'opta' )
5
5
const parseList = require ( 'safe-parse-list' )
6
- const scopeAndName = require ( './lib/scope-and -name' )
6
+ const packageName = require ( './lib/package -name' )
7
7
const git = require ( './lib/git' )
8
8
const npm = require ( './lib/npm' )
9
9
@@ -16,7 +16,8 @@ function initOpts () {
16
16
prompt : false ,
17
17
flag : {
18
18
alias : 'd' ,
19
- defaultDescription : 'process.cwd()'
19
+ defaultDescription : 'process.cwd()' ,
20
+ default : ( ) => process . cwd ( )
20
21
}
21
22
} ,
22
23
@@ -33,16 +34,23 @@ function initOpts () {
33
34
type : 'string' ,
34
35
prompt : {
35
36
message : 'Package name:' ,
36
- validate : npm . validatePackageName
37
+ validate : npm . validatePackageName ,
38
+ default : ( promptInput , allInput ) => {
39
+ return packageName ( allInput . name , allInput . cwd )
40
+ }
37
41
}
38
42
} ,
39
43
version : {
40
44
type : 'string' ,
41
45
flag : {
42
- key : 'package-version'
46
+ key : 'package-version' ,
47
+ alias : 'V'
43
48
} ,
44
49
prompt : {
45
- message : 'Version:'
50
+ message : 'Initial version:' ,
51
+ default : ( promptInput , allInput ) => {
52
+ return allInput . version || '1.0.0'
53
+ }
46
54
}
47
55
} ,
48
56
description : {
@@ -54,13 +62,19 @@ function initOpts () {
54
62
author : {
55
63
type : 'string' ,
56
64
prompt : {
57
- message : 'Author:'
65
+ message : 'Author:' ,
66
+ default : ( promptInput , allInput ) => {
67
+ return allInput . author || git . author ( { cwd : allInput . cwd } )
68
+ }
58
69
}
59
70
} ,
60
71
repository : {
61
72
type : 'string' ,
62
73
prompt : {
63
- message : 'Repository:'
74
+ message : 'Repository:' ,
75
+ default : ( promptInput , allInput ) => {
76
+ return allInput . repository || git . remote ( { cwd : allInput . cwd } )
77
+ }
64
78
}
65
79
} ,
66
80
keywords : {
@@ -84,14 +98,19 @@ function initOpts () {
84
98
prompt : {
85
99
message : 'Module Type:' ,
86
100
type : 'list' ,
87
- choices : [ 'commonjs' , 'module' ]
101
+ choices : [ 'commonjs' , 'module' ] ,
102
+ default : ( promptInput , allInput ) => {
103
+ return allInput . type || 'commonjs'
104
+ }
88
105
}
89
106
} ,
90
107
main : {
91
108
type : 'string' ,
92
- default : 'index.js' ,
93
109
prompt : {
94
- message : 'Main:'
110
+ message : 'Main:' ,
111
+ default : ( promptInput , allInput ) => {
112
+ return allInput . main || 'index.js'
113
+ }
95
114
}
96
115
} ,
97
116
private : {
@@ -157,29 +176,14 @@ async function main (input, _opts = {}) {
157
176
let opts = options . values ( )
158
177
159
178
// Read current state and set defaults
160
- const pkgPath = path . resolve ( opts . cwd , 'package.json' )
161
- const pkg = opts . ignoreExisting ? { } : await readPackageJson ( pkgPath )
162
-
163
- // Set defaults
164
- options . defaults ( {
165
- version : pkg . version || '1.0.0' ,
166
- name : scopeAndName ( input . name || pkg . name , opts . cwd ) ,
167
- type : pkg . type || 'commonjs' ,
168
- author : pkg . author || await git . author ( { cwd : opts . cwd } ) ,
169
- description : pkg . description ,
170
- repository : async ( ) => {
171
- // @TODO Do more here, read from git, etc
172
- return ( pkg . repository && pkg . repository . url ) || git . remote ( { cwd : opts . cwd } )
173
- } ,
174
- keywords : parseList ( pkg . keywords )
175
- } )
179
+ const pkg = opts . ignoreExisting ? { } : await readPackageJson ( options )
176
180
177
181
await options . prompt ( {
178
182
promptor : _opts . promptor
179
183
} ) ( )
180
184
181
185
opts = options . values ( )
182
- return write ( pkgPath , opts , await format ( opts , pkg ) )
186
+ return write ( path . resolve ( opts . cwd , 'package.json' ) , opts , await format ( opts , pkg ) )
183
187
}
184
188
185
189
module . exports . options = initOpts ( ) . options
@@ -190,14 +194,27 @@ module.exports.cli = function () {
190
194
}
191
195
192
196
module . exports . readPackageJson = readPackageJson
193
- async function readPackageJson ( pkgPath , opts = { } ) {
197
+ async function readPackageJson ( options ) {
198
+ const opts = options . values ( )
194
199
let pkg = { }
195
200
try {
196
- pkg = await fs . readJSON ( pkgPath )
201
+ pkg = await fs . readJSON ( path . resolve ( opts . cwd , 'package.json' ) )
197
202
} catch ( e ) {
198
203
// @TODO log this?
199
204
// ignore if missing or unreadable
200
205
}
206
+
207
+ // Set defaults from the package.json
208
+ options . defaults ( {
209
+ version : pkg . version ,
210
+ name : pkg . name ,
211
+ type : pkg . type ,
212
+ author : pkg . author ,
213
+ description : pkg . description ,
214
+ repository : pkg . repository && pkg . repository . url ,
215
+ keywords : pkg . keywords
216
+ } )
217
+
201
218
return pkg
202
219
}
203
220
0 commit comments