File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -163,7 +163,11 @@ class PackageJson {
163163 return this
164164 }
165165
166+ // Manually set data from an existing object
166167 fromContent ( data ) {
168+ if ( ! data || typeof data !== 'object' ) {
169+ throw new Error ( 'Content data must be an object' )
170+ }
167171 this . #manifest = data
168172 this . #canSave = false
169173 return this
Original file line number Diff line number Diff line change @@ -355,3 +355,18 @@ t.test('reversion can still save', async t => {
355355 JSON . parse ( fs . readFileSync ( resolve ( path , 'package.json' ) , 'utf8' ) )
356356 )
357357} )
358+
359+ t . test ( 'fromContent' , async t => {
360+ t . test ( 'object' , async t => {
361+ const pkgJson = new PackageJson ( )
362+ const data = { name : '@npmcli/test' , version : '1.0.0-fromContent' }
363+ pkgJson . fromContent ( data )
364+ t . deepEqual ( pkgJson . content , data )
365+ } )
366+ t . test ( 'string' , async t => {
367+ const pkgJson = new PackageJson ( )
368+ await t . throws ( ( ) => {
369+ pkgJson . fromContent ( 'a string' )
370+ } , 'Content data must be a string' )
371+ } )
372+ } )
You can’t perform that action at this time.
0 commit comments