Skip to content

Commit

Permalink
fix: manifest with no changes is not resaved
Browse files Browse the repository at this point in the history
Differences in order of keys is not considered a change
  • Loading branch information
zkochan committed Aug 29, 2019
1 parent 93a5c28 commit f8cc4b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 31 deletions.
2 changes: 2 additions & 0 deletions packages/read-importer-manifest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
"@pnpm/types": "workspace:^3.0.0",
"@pnpm/write-importer-manifest": "workspace:2.0.3",
"detect-indent": "6.0.0",
"fast-deep-equal": "2.0.1",
"graceful-fs": "4.2.1",
"is-windows": "1.0.2",
"json5": "2.1.0",
"parse-json": "5.0.0",
"read-yaml-file": "1.1.0",
"sort-keys": "4.0.0",
"strip-bom": "4.0.0"
},
"devDependencies": {
Expand Down
28 changes: 26 additions & 2 deletions packages/read-importer-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import PnpmError from '@pnpm/error'
import { ImporterManifest } from '@pnpm/types'
import writeImporterManifest from '@pnpm/write-importer-manifest'
import detectIndent = require('detect-indent')
import equal = require('fast-deep-equal')
import fs = require('fs')
import { Stats } from 'fs'
import isWindows = require('is-windows')
import path = require('path')
import readYamlFile from 'read-yaml-file'
import sortKeys = require('sort-keys')
import { promisify } from 'util'
import {
readJson5File,
Expand Down Expand Up @@ -165,10 +167,32 @@ function createManifestWriter (
manifestPath: string,
},
): (WriteImporterManifest) {
const stringifiedInitialManifest = JSON.stringify(opts.initialManifest)
const initialManifest = normalize(JSON.parse(JSON.stringify(opts.initialManifest)))
return async (updatedManifest: ImporterManifest, force?: boolean) => {
if (force === true || stringifiedInitialManifest !== JSON.stringify(updatedManifest)) {
updatedManifest = normalize(updatedManifest)
if (force === true || !equal(initialManifest, updatedManifest)) {
return writeImporterManifest(opts.manifestPath, updatedManifest, { indent: opts.indent })
}
}
}

const dependencyKeys = new Set([
'dependencies',
'devDependencies',
'optionalDependencies',
'peerDependencies',
])

function normalize (manifest: ImporterManifest) {
const result = {}

for (const key of Object.keys(manifest)) {
if (!dependencyKeys.has(key)) {
result[key] = manifest[key]
} else if (Object.keys(manifest[key]).length !== 0) {
result[key] = sortKeys(manifest[key])
}
}

return result
}
16 changes: 13 additions & 3 deletions packages/read-importer-manifest/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,23 @@ test('preserve space indentation in json5 file', async (t) => {
test('do not save manifest if it had no changes', async (t) => {
process.chdir(tempy.directory())

await writeFile('package.json5', JSON.stringify({ dependencies: { foo: '*', bar: '*' } }), 'utf8')
await writeFile(
'package.json5',
JSON.stringify({
dependencies: { foo: '*', bar: '*' },
devDependencies: {},
}),
'utf8',
)

const { manifest, writeImporterManifest } = await readImporterManifest(process.cwd())
const { writeImporterManifest } = await readImporterManifest(process.cwd())

const stat1 = await stat('package.json5')

await writeImporterManifest(manifest)
await writeImporterManifest({
dependencies: { bar: '*', foo: '*' },
peerDependencies: {},
})

const stat2 = await stat('package.json5')

Expand Down
1 change: 0 additions & 1 deletion packages/write-importer-manifest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/write-importer-manifest#readme",
"dependencies": {
"@pnpm/types": "workspace:^3.0.0",
"sort-keys": "4.0.0",
"write-json-file": "4.0.0",
"write-json5-file": "2.1.2",
"write-yaml-file": "3.0.1"
Expand Down
23 changes: 0 additions & 23 deletions packages/write-importer-manifest/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ImporterManifest } from '@pnpm/types'
import sortKeys = require('sort-keys')
import writeJsonFile = require('write-json-file')
import writeJson5File = require('write-json5-file')
import writeYamlFile = require('write-yaml-file')
Expand All @@ -14,7 +13,6 @@ export default function writeImporterManifest (
manifest: ImporterManifest,
opts?: { indent?: string | number | undefined },
): Promise<void> {
manifest = normalize(manifest)
switch (filePath.substr(filePath.lastIndexOf('.') + 1).toLowerCase()) {
case 'json5':
return writeJson5File(filePath, manifest, opts)
Expand All @@ -25,24 +23,3 @@ export default function writeImporterManifest (
return writeJsonFile(filePath, manifest, opts)
}
}

const dependencyKeys = new Set([
'dependencies',
'devDependencies',
'optionalDependencies',
'peerDependencies',
])

function normalize (manifest: ImporterManifest) {
const result = {}

for (const key of Object.keys(manifest)) {
if (!dependencyKeys.has(key)) {
result[key] = manifest[key]
} else if (Object.keys(manifest[key]).length !== 0) {
result[key] = sortKeys(manifest[key])
}
}

return result
}
6 changes: 4 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f8cc4b4

Please sign in to comment.