Skip to content

Commit 2d320bc

Browse files
committed
fix: save when reverting content
When a package.json is changed, saved, and then reverted back to what it was before, save was not running because this module was not updating its local copy of what the contents of the file was. This has been fixed.
1 parent 1464adc commit 2d320bc

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ class PackageJson {
252252
.replace(/\n/g, eol)
253253

254254
if (fileContent.trim() !== this.#readFileContent.trim()) {
255-
return await writeFile(this.filename, fileContent)
255+
const written = await writeFile(this.filename, fileContent)
256+
this.#readFileContent = fileContent
257+
return written
256258
}
257259
}
258260

test/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,31 @@ t.test('empty props at bottom', async t => {
327327
JSON.parse(fs.readFileSync(resolve(path, 'package.json'), 'utf8'))
328328
)
329329
})
330+
331+
t.test('reversion can still save', async t => {
332+
const path = t.testdir({
333+
'package.json': JSON.stringify({
334+
name: '@npmcli/test',
335+
version: '1.0.0',
336+
}),
337+
})
338+
const pkgJson = await PackageJson.load(path)
339+
pkgJson.update({ version: '2.0.0' })
340+
await pkgJson.save()
341+
t.strictSame(
342+
{
343+
name: '@npmcli/test',
344+
version: '2.0.0',
345+
},
346+
JSON.parse(fs.readFileSync(resolve(path, 'package.json'), 'utf8'))
347+
)
348+
pkgJson.update({ version: '1.0.0' })
349+
await pkgJson.save()
350+
t.strictSame(
351+
{
352+
name: '@npmcli/test',
353+
version: '1.0.0',
354+
},
355+
JSON.parse(fs.readFileSync(resolve(path, 'package.json'), 'utf8'))
356+
)
357+
})

0 commit comments

Comments
 (0)