fix(make-dedicated-lockfile): prepublishOnly script is automatically … (#5083)

close #5061
close #5062
This commit is contained in:
Zoltan Kochan
2022-07-24 03:19:01 +03:00
committed by GitHub
parent 107d01109a
commit 01c5834bff
8 changed files with 38 additions and 8 deletions

View File

@@ -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).

View File

@@ -0,0 +1,5 @@
---
"@pnpm/make-dedicated-lockfile": patch
---
Read the dependency versions before moving node_modules.

View File

@@ -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')
@@ -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',

View File

@@ -4,6 +4,6 @@
"dependencies": {
"ramda": "0.26.0",
"request": "^2.0.0",
"is-positive": "workspace:1.0.0"
"is-positive": "workspace:^"
}
}

View File

@@ -4,7 +4,7 @@ importers:
packages/is-negative:
specifiers:
is-positive: workspace:1.0.0
is-positive: workspace:^
ramda: 0.26.0
request: 2.0.0
dependencies:

View File

@@ -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'
@@ -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)

View File

@@ -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)
}
@@ -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)) {

View File

@@ -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' }))
})