Skip to content

Commit d1ab3c8

Browse files
committed
Add willSave to EditablePackageJson
1 parent 456eadd commit d1ab3c8

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

registry/lib/packages.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ declare namespace Packages {
2020
// @ts-ignore TypeScript doesn't like an override with a different return type.
2121
override save: (options?: SaveOptions | undefined) => Promise<boolean>
2222
saveSync: (options?: SaveOptions | undefined) => boolean
23+
willSave: (options?: SaveOptions | undefined) => boolean
2324
}
2425
export type Exports = Exclude<PackageJson['exports'], undefined>
2526
export type ExtractOptions = PacoteOptions & {

registry/lib/packages.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,14 @@ function getEditablePackageJsonClass() {
182182
return this
183183
}
184184

185-
async save({ ignoreWhitespace = false, sort = false } = {}) {
186-
if (!this._canSave) {
185+
async save(options) {
186+
if (!this._canSave || this.content === undefined) {
187187
throw new Error('No package.json to save to')
188188
}
189+
const { ignoreWhitespace = false, sort = false } = {
190+
__proto__: null,
191+
...options
192+
}
189193
const {
190194
[identSymbol]: indent,
191195
[newlineSymbol]: newline,
@@ -227,10 +231,14 @@ function getEditablePackageJsonClass() {
227231
return true
228232
}
229233

230-
async saveSync({ ignoreWhitespace = false, sort = false } = {}) {
234+
async saveSync(options) {
231235
if (!this._canSave || this.content === undefined) {
232236
throw new Error('No package.json to save to')
233237
}
238+
const { ignoreWhitespace = false, sort = false } = {
239+
__proto__: null,
240+
...options
241+
}
234242
const {
235243
[Symbol.for('indent')]: indent,
236244
[Symbol.for('newline')]: newline,
@@ -271,6 +279,45 @@ function getEditablePackageJsonClass() {
271279
super.update(content)
272280
return this
273281
}
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+
}
274321
}
275322
}
276323
return _EditablePackageJsonClass

0 commit comments

Comments
 (0)