@@ -182,10 +182,14 @@ function getEditablePackageJsonClass() {
182
182
return this
183
183
}
184
184
185
- async save ( { ignoreWhitespace = false , sort = false } = { } ) {
186
- if ( ! this . _canSave ) {
185
+ async save ( options ) {
186
+ if ( ! this . _canSave || this . content === undefined ) {
187
187
throw new Error ( 'No package.json to save to' )
188
188
}
189
+ const { ignoreWhitespace = false , sort = false } = {
190
+ __proto__ : null ,
191
+ ...options
192
+ }
189
193
const {
190
194
[ identSymbol ] : indent ,
191
195
[ newlineSymbol ] : newline ,
@@ -227,10 +231,14 @@ function getEditablePackageJsonClass() {
227
231
return true
228
232
}
229
233
230
- async saveSync ( { ignoreWhitespace = false , sort = false } = { } ) {
234
+ async saveSync ( options ) {
231
235
if ( ! this . _canSave || this . content === undefined ) {
232
236
throw new Error ( 'No package.json to save to' )
233
237
}
238
+ const { ignoreWhitespace = false , sort = false } = {
239
+ __proto__ : null ,
240
+ ...options
241
+ }
234
242
const {
235
243
[ Symbol . for ( 'indent' ) ] : indent ,
236
244
[ Symbol . for ( 'newline' ) ] : newline ,
@@ -271,6 +279,45 @@ function getEditablePackageJsonClass() {
271
279
super . update ( content )
272
280
return this
273
281
}
282
+
283
+ willSave ( options ) {
284
+ const { ignoreWhitespace = false , sort = false } = {
285
+ __proto__ : null ,
286
+ ...options
287
+ }
288
+ if ( ! this . _canSave || this . content === undefined ) {
289
+ return false
290
+ }
291
+ const {
292
+ [ Symbol . for ( 'indent' ) ] : indent ,
293
+ [ Symbol . for ( 'newline' ) ] : newline ,
294
+ ...rest
295
+ } = this . content
296
+ const content = sort ? packageSort ( rest ) : rest
297
+
298
+ if (
299
+ ignoreWhitespace &&
300
+ getUtil ( ) . isDeepStrictEqual ( content , this . _readFileJson )
301
+ ) {
302
+ return false
303
+ }
304
+
305
+ const format = indent === undefined ? ' ' : indent
306
+ const eol = newline === undefined ? '\n' : newline
307
+ const fileContent = `${ JSON . stringify (
308
+ content ,
309
+ null ,
310
+ format
311
+ ) } \n`. replace ( / \n / g, eol )
312
+
313
+ if (
314
+ ! ignoreWhitespace &&
315
+ fileContent . trim ( ) === this . _readFileContent . trim ( )
316
+ ) {
317
+ return false
318
+ }
319
+ return true
320
+ }
274
321
}
275
322
}
276
323
return _EditablePackageJsonClass
0 commit comments