File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -163,7 +163,11 @@ class PackageJson {
163
163
return this
164
164
}
165
165
166
+ // Manually set data from an existing object
166
167
fromContent ( data ) {
168
+ if ( ! data || typeof data !== 'object' ) {
169
+ throw new Error ( 'Content data must be an object' )
170
+ }
167
171
this . #manifest = data
168
172
this . #canSave = false
169
173
return this
Original file line number Diff line number Diff line change @@ -355,3 +355,18 @@ t.test('reversion can still save', async t => {
355
355
JSON . parse ( fs . readFileSync ( resolve ( path , 'package.json' ) , 'utf8' ) )
356
356
)
357
357
} )
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