@@ -3,7 +3,7 @@ const path = require('path')
33const fs = require ( 'fs-extra' )
44const opta = require ( 'opta' )
55const parseList = require ( 'safe-parse-list' )
6- const scopeAndName = require ( './lib/scope-and -name' )
6+ const packageName = require ( './lib/package -name' )
77const git = require ( './lib/git' )
88const npm = require ( './lib/npm' )
99
@@ -16,7 +16,8 @@ function initOpts () {
1616 prompt : false ,
1717 flag : {
1818 alias : 'd' ,
19- defaultDescription : 'process.cwd()'
19+ defaultDescription : 'process.cwd()' ,
20+ default : ( ) => process . cwd ( )
2021 }
2122 } ,
2223
@@ -33,16 +34,23 @@ function initOpts () {
3334 type : 'string' ,
3435 prompt : {
3536 message : 'Package name:' ,
36- validate : npm . validatePackageName
37+ validate : npm . validatePackageName ,
38+ default : ( promptInput , allInput ) => {
39+ return packageName ( allInput . name , allInput . cwd )
40+ }
3741 }
3842 } ,
3943 version : {
4044 type : 'string' ,
4145 flag : {
42- key : 'package-version'
46+ key : 'package-version' ,
47+ alias : 'V'
4348 } ,
4449 prompt : {
45- message : 'Version:'
50+ message : 'Initial version:' ,
51+ default : ( promptInput , allInput ) => {
52+ return allInput . version || '1.0.0'
53+ }
4654 }
4755 } ,
4856 description : {
@@ -54,13 +62,19 @@ function initOpts () {
5462 author : {
5563 type : 'string' ,
5664 prompt : {
57- message : 'Author:'
65+ message : 'Author:' ,
66+ default : ( promptInput , allInput ) => {
67+ return allInput . author || git . author ( { cwd : allInput . cwd } )
68+ }
5869 }
5970 } ,
6071 repository : {
6172 type : 'string' ,
6273 prompt : {
63- message : 'Repository:'
74+ message : 'Repository:' ,
75+ default : ( promptInput , allInput ) => {
76+ return allInput . repository || git . remote ( { cwd : allInput . cwd } )
77+ }
6478 }
6579 } ,
6680 keywords : {
@@ -84,14 +98,19 @@ function initOpts () {
8498 prompt : {
8599 message : 'Module Type:' ,
86100 type : 'list' ,
87- choices : [ 'commonjs' , 'module' ]
101+ choices : [ 'commonjs' , 'module' ] ,
102+ default : ( promptInput , allInput ) => {
103+ return allInput . type || 'commonjs'
104+ }
88105 }
89106 } ,
90107 main : {
91108 type : 'string' ,
92- default : 'index.js' ,
93109 prompt : {
94- message : 'Main:'
110+ message : 'Main:' ,
111+ default : ( promptInput , allInput ) => {
112+ return allInput . main || 'index.js'
113+ }
95114 }
96115 } ,
97116 private : {
@@ -157,29 +176,14 @@ async function main (input, _opts = {}) {
157176 let opts = options . values ( )
158177
159178 // 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 )
176180
177181 await options . prompt ( {
178182 promptor : _opts . promptor
179183 } ) ( )
180184
181185 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 ) )
183187}
184188
185189module . exports . options = initOpts ( ) . options
@@ -190,14 +194,27 @@ module.exports.cli = function () {
190194}
191195
192196module . exports . readPackageJson = readPackageJson
193- async function readPackageJson ( pkgPath , opts = { } ) {
197+ async function readPackageJson ( options ) {
198+ const opts = options . values ( )
194199 let pkg = { }
195200 try {
196- pkg = await fs . readJSON ( pkgPath )
201+ pkg = await fs . readJSON ( path . resolve ( opts . cwd , 'package.json' ) )
197202 } catch ( e ) {
198203 // @TODO log this?
199204 // ignore if missing or unreadable
200205 }
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+
201218 return pkg
202219}
203220
0 commit comments