Skip to content

Commit

Permalink
fix(make-dedicated-lockfile): prepublishOnly script is automatically … (
Browse files Browse the repository at this point in the history
#5083)

close #5061
close #5062
  • Loading branch information
zkochan committed Jul 24, 2022
1 parent 107d011 commit 01c5834
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-apricots-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/read-project-manifest": patch
---

It should be possible to rewrite a manifest back to its initial content [#5061](https://github.com/pnpm/pnpm/issues/5061).
5 changes: 5 additions & 0 deletions .changeset/famous-humans-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/make-dedicated-lockfile": patch
---

Read the dependency versions before moving node_modules.
9 changes: 5 additions & 4 deletions packages/make-dedicated-lockfile/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export default async function (lockfileDir: string, projectDir: string) {
const dedicatedLockfile = pruneSharedLockfile(lockfile)

await writeWantedLockfile(projectDir, dedicatedLockfile)

const { manifest, writeProjectManifest } = await readProjectManifest(projectDir)
const publishManifest = await exportableManifest(projectDir, manifest)
await writeProjectManifest(publishManifest)

const modulesDir = path.join(projectDir, 'node_modules')
const tmp = path.join(projectDir, 'tmp_node_modules')
const tempModulesDir = path.join(projectDir, 'node_modules/.tmp')
Expand All @@ -46,10 +51,6 @@ export default async function (lockfileDir: string, projectDir: string) {
if (err['code'] !== 'ENOENT') throw err
}

const { manifest, writeProjectManifest } = await readProjectManifest(projectDir)
const publishManifest = await exportableManifest(projectDir, manifest)
await writeProjectManifest(publishManifest)

try {
await pnpmExec([
'install',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"dependencies": {
"ramda": "0.26.0",
"request": "^2.0.0",
"is-positive": "workspace:1.0.0"
"is-positive": "workspace:^"
}
}

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

2 changes: 2 additions & 0 deletions packages/make-dedicated-lockfile/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import pnpmExec from '@pnpm/exec'
import { readWantedLockfile } from '@pnpm/lockfile-file'
import fixtures from '@pnpm/test-fixtures'
import makeDedicatedLockfile from '../lib'
Expand All @@ -9,6 +10,7 @@ const f = fixtures(__dirname)
test('makeDedicatedLockfile()', async () => {
const tmp = f.prepare('fixture')
fs.writeFileSync('.npmrc', 'store-dir=store\ncache-dir=cache', 'utf8')
await pnpmExec(['install', '--no-frozen-lockfile'], { cwd: tmp })
const projectDir = path.join(tmp, 'packages/is-negative')
await makeDedicatedLockfile(tmp, projectDir)

Expand Down
7 changes: 5 additions & 2 deletions packages/read-project-manifest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,16 @@ function createManifestWriter (
manifestPath: string
}
): (WriteProjectManifest) {
const initialManifest = normalize(JSON.parse(JSON.stringify(opts.initialManifest)))
let initialManifest = normalize(opts.initialManifest)
return async (updatedManifest: ProjectManifest, force?: boolean) => {
updatedManifest = normalize(updatedManifest)
if (force === true || !equal(initialManifest, updatedManifest)) {
return writeProjectManifest(opts.manifestPath, updatedManifest, {
await writeProjectManifest(opts.manifestPath, updatedManifest, {
indent: opts.indent,
insertFinalNewline: opts.insertFinalNewline,
})
initialManifest = normalize(updatedManifest)
return Promise.resolve(undefined)
}
return Promise.resolve(undefined)
}
Expand All @@ -200,6 +202,7 @@ const dependencyKeys = new Set([
])

function normalize (manifest: ProjectManifest) {
manifest = JSON.parse(JSON.stringify(manifest))
const result = {}

for (const key of Object.keys(manifest)) {
Expand Down
14 changes: 14 additions & 0 deletions packages/read-project-manifest/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,17 @@ test('preserve trailing new line at the end of package.json5', async () => {
const rawManifest = await fs.readFile('package.json5', 'utf8')
expect(rawManifest).toBe("{dependencies:{bar:'1.0.0'}}")
})

test('canceling changes to a manifest', async () => {
process.chdir(tempy.directory())

await fs.writeFile('package.json', JSON.stringify({ name: 'foo' }), 'utf8')

const { writeProjectManifest } = await readProjectManifest(process.cwd())

await writeProjectManifest({ name: 'bar' })
expect(await fs.readFile('package.json', 'utf8')).toBe(JSON.stringify({ name: 'bar' }))

await writeProjectManifest({ name: 'foo' })
expect(await fs.readFile('package.json', 'utf8')).toBe(JSON.stringify({ name: 'foo' }))
})

0 comments on commit 01c5834

Please sign in to comment.