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

Do not write package.json if nothing changed #234

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/update-root-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down