@@ -3,6 +3,7 @@ const path = require('path')
33const fs = require ( 'fs-extra' )
44const opta = require ( 'opta' )
55const parseList = require ( 'safe-parse-list' )
6+ const { Loggerr } = require ( 'loggerr' )
67const packageName = require ( './lib/package-name' )
78const git = require ( './lib/git' )
89const npm = require ( './lib/npm' )
@@ -20,6 +21,20 @@ function initOpts () {
2021 default : ( ) => process . cwd ( )
2122 }
2223 } ,
24+ silent : {
25+ type : 'boolean' ,
26+ prompt : false ,
27+ flag : {
28+ conflicts : [ 'verbose' ]
29+ }
30+ } ,
31+ verbose : {
32+ type : 'boolean' ,
33+ prompt : false ,
34+ flag : {
35+ conflicts : [ 'silent' ]
36+ }
37+ } ,
2338
2439 ignoreExisting : {
2540 description : 'Ignore existing files (& overwrite them)' ,
@@ -138,14 +153,22 @@ function initOpts () {
138153 }
139154 } ,
140155 peerDependencies : {
141- advanced : true ,
142156 type : 'string' ,
143157 flag : {
144158 key : 'peer-dependencies'
145159 } ,
146160 prompt : false
147161 } ,
148162
163+ // Scripts is an odd one
164+ scripts : {
165+ flag : false ,
166+ prompt : false ,
167+ default : {
168+ test : 'echo "Error: no test specified" && exit 1'
169+ }
170+ } ,
171+
149172 // @TODO detect from existing file
150173 spacer : {
151174 type : 'string' ,
@@ -175,15 +198,20 @@ async function main (input, _opts = {}) {
175198 } )
176199 let opts = options . values ( )
177200
201+ const log = _opts . logger || new Loggerr ( {
202+ level : ( opts . silent && 'silent' ) || ( opts . verbose && 'debug' ) || 'info' ,
203+ formatter : 'cli'
204+ } )
205+
178206 // Read current state and set defaults
179- const pkg = opts . ignoreExisting ? { } : await readPackageJson ( options )
207+ const pkg = opts . ignoreExisting ? { } : await readPackageJson ( options , { log } )
180208
181209 await options . prompt ( {
182210 promptor : _opts . promptor
183211 } ) ( )
184212
185213 opts = options . values ( )
186- return write ( path . resolve ( opts . cwd , 'package.json' ) , opts , await format ( opts , pkg ) )
214+ return write ( path . resolve ( opts . cwd , 'package.json' ) , opts , await format ( opts , pkg ) , { log } )
187215}
188216
189217module . exports . options = initOpts ( ) . options
@@ -194,11 +222,12 @@ module.exports.cli = function () {
194222}
195223
196224module . exports . readPackageJson = readPackageJson
197- async function readPackageJson ( options ) {
225+ async function readPackageJson ( options , { log } = { } ) {
198226 const opts = options . values ( )
199227 let pkg = { }
200228 try {
201229 pkg = await fs . readJSON ( path . resolve ( opts . cwd , 'package.json' ) )
230+ log . debug ( 'Read existing package.json' , pkg )
202231 } catch ( e ) {
203232 // @TODO log this?
204233 // ignore if missing or unreadable
@@ -212,7 +241,8 @@ async function readPackageJson (options) {
212241 author : pkg . author ,
213242 description : pkg . description ,
214243 repository : pkg . repository && pkg . repository . url ,
215- keywords : pkg . keywords
244+ keywords : pkg . keywords ,
245+ scripts : Object . assign ( { } , pkg . scripts , opts . scripts )
216246 } )
217247
218248 return pkg
@@ -232,12 +262,9 @@ async function format (opts, pkg = {}) {
232262 }
233263
234264 // Scripts
235- // pkg.scripts = Object.assign({}, {
236- // test: opts.scriptsTest,
237- // prepare: opts.scriptsPrepare,
238- // preversion: opts.scriptsPreVersion,
239- // postpublish: opts.scriptsPostPublish
240- // }, opts.scripts)
265+ if ( Object . keys ( opts . scripts ) . length ) {
266+ pkg . scripts = opts . scripts
267+ }
241268
242269 pkg . author = opts . author || ''
243270 pkg . license = opts . license
@@ -279,23 +306,30 @@ async function format (opts, pkg = {}) {
279306}
280307
281308module . exports . write = write
282- async function write ( pkgPath , opts , pkg ) {
309+ async function write ( pkgPath , opts , pkg , { log } = { } ) {
283310 // Write package json
311+ log . info ( `Writing package.json\n${ pkgPath } ` )
284312 await fs . outputJSON ( pkgPath , pkg , {
285313 spaces : opts . spacer || 2
286314 } )
287315
288316 // Run installs
289- await npm . install ( opts . dependencies , {
290- save : 'prod' ,
291- directory : opts . cwd ,
292- exact : opts . saveExact
293- } )
294- await npm . install ( opts . devDependencies , {
295- save : 'dev' ,
296- directory : opts . cwd ,
297- exact : opts . saveExact
298- } )
317+ if ( opts . dependencies && opts . dependencies . length ) {
318+ log . info ( 'Installing dependencies' , opts . dependencies )
319+ await npm . install ( opts . dependencies , {
320+ save : 'prod' ,
321+ directory : opts . cwd ,
322+ exact : opts . saveExact
323+ } )
324+ }
325+ if ( opts . devDependencies && opts . devDependencies . length ) {
326+ log . info ( 'Installing dev dependencies' , opts . devDependencies )
327+ await npm . install ( opts . devDependencies , {
328+ save : 'dev' ,
329+ directory : opts . cwd ,
330+ exact : opts . saveExact
331+ } )
332+ }
299333
300334 // Read full package back to return
301335 return fs . readJSON ( pkgPath )
0 commit comments