Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Do not write package.json if nothing changed
Browse files Browse the repository at this point in the history
Need a similar fix for Shrinkwrap class.

Fix: #232

PR-URL: #234
Credit: @isaacs
Close: #234
Reviewed-by: @nlf
  • Loading branch information
isaacs committed Feb 18, 2021
1 parent aa99153 commit 7e8b824
Showing 2 changed files with 82 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/update-root-package-json.js
Original file line number Diff line number Diff line change
@@ -15,11 +15,18 @@ const depTypes = new Set([
'peerDependencies',
])

const parseJsonSafe = json => {
try {
return parseJSON(json)
} catch (er) {
return null
}
}

const updateRootPackageJson = async tree => {
const filename = resolve(tree.path, 'package.json')
const originalContent = await readFile(filename, 'utf8')
.then(data => parseJSON(data))
.catch(() => null)
const originalJson = await readFile(filename, 'utf8').catch(() => null)
const originalContent = parseJsonSafe(originalJson)

const depsData = orderDeps({
...tree.package,
@@ -52,7 +59,8 @@ const updateRootPackageJson = async tree => {
const content = (JSON.stringify(packageJsonContent, null, format) + '\n')
.replace(/\n/g, eol)

return writeFile(filename, content)
if (content !== originalJson)
return writeFile(filename, content)
}

module.exports = updateRootPackageJson
70 changes: 70 additions & 0 deletions test/update-root-package-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {resolve} = require('path')
const {statSync} = require('fs')

const t = require('tap')

@@ -29,6 +30,33 @@ t.test('missing package.json', async t => {
)
})

t.test('invalid package.json', async t => {
const path = t.testdir({
'package.json': 'this! is! not! json!',
})
await updateRootPackageJson({
path: path,
package: {
name: 'invalid-package-json-test',
version: '1.0.0',
dependencies: {
abbrev: '^1.0.0',
},
},
})
t.match(
require(resolve(path, 'package.json')),
{
name: 'invalid-package-json-test',
version: '1.0.0',
dependencies: {
abbrev: '^1.0.0',
},
},
'should write new package.json with tree data'
)
})

t.test('existing package.json', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
@@ -62,6 +90,48 @@ t.test('existing package.json', async t => {
)
})

t.test('unchanged package.json', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
name: 'existing-package-json-test',
version: '1.0.0',
bin: './file.js',
funding: 'http://example.com',
dependencies: {
abbrev: '^1.0.0',
},
}, null, 2) + '\n',
})
const { mtime } = statSync(path + '/package.json')
await updateRootPackageJson({
path: path,
package: {
name: 'existing-package-json-test',
version: '1.0.0',
bin: './file.js',
funding: 'http://example.com',
dependencies: {
abbrev: '^1.0.0',
},
},
})
t.match(
require(resolve(path, 'package.json')),
{
name: 'existing-package-json-test',
version: '1.0.0',
bin: './file.js',
funding: 'http://example.com',
dependencies: {
abbrev: '^1.0.0',
},
},
'should write new package.json with tree data'
)
const { mtime: newMtime } = statSync(path + '/package.json')
t.equal(newMtime.toISOString(), mtime.toISOString(), 'mtime not changed')
})

t.test('existing package.json with optionalDependencies', async t => {
const path = t.testdir({
'package.json': JSON.stringify({

0 comments on commit 7e8b824

Please sign in to comment.