Skip to content

Commit

Permalink
feat: respect lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
imcuttle committed Mar 14, 2022
1 parent d1f7edd commit 11d6cbb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
4 changes: 3 additions & 1 deletion __tests__/fixture/pnpm-workspace-wild/pnpm-lock.yaml

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

53 changes: 49 additions & 4 deletions __tests__/patches/pnpm-workspace-wild.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
const { fixture } = require('../helper')
const fs = require('fs')
const cp = require('child_process')
const { tarPackEntry } = require('tar-stream')
const readYaml = require('read-yaml-file')
const isCI = require('is-ci')

jest.mock('@lerna/version/lib/git-add', () => {
Expand Down Expand Up @@ -44,6 +47,22 @@ jest.mock('@lerna/npm-publish', () => {
}
})

jest.mock('tar-stream', () => {
const tar = jest.requireActual('tar-stream')
const tarPackEntry = jest.fn((a, b, cb) => {
cb && cb(null)
})
return {
...tar,
tarPackEntry,
pack: () => {
const p = tar.pack()
p.entry = tarPackEntry
return p
}
}
})

describe('lerna patches: pnpm-workspace-wild', function () {
let unpatchs = []
const cwd = process.cwd()
Expand Down Expand Up @@ -100,18 +119,22 @@ describe('lerna patches: pnpm-workspace-wild', function () {
)
)

process.chdir(fixture('pnpm-workspace-wild'))
cp.execSync(`pnpm i`, { cwd: fixture('pnpm-workspace-wild') })

unpatchs = [
require('./../../src/patches/pnpm-workspace-deps')(),
require('./../../src/patches/pnpm-workspace')(),
require('./../../src/patches/nested-packages')(),
require('./../../src/patches/fix-conventional-commit')(),
require('./../../src/patches/after-lerna-version-update-lockfile')()
]
process.chdir(fixture('pnpm-workspace-wild'))
})
afterEach(() => {
unpatchs.forEach((fn) => fn())
process.chdir(cwd)

tarPackEntry.mockClear()
})
if (isCI) {
return
Expand All @@ -125,9 +148,31 @@ describe('lerna patches: pnpm-workspace-wild', function () {
})

await publishCmd.runner
const [aArgs, bArgs] = tarPackEntry.mock.calls
expect(JSON.parse(aArgs[1])).toMatchObject({
name: 'normal-c',
version: '1.0.1'
})
expect(JSON.parse(bArgs[1])).toMatchObject({
name: 'normal-a',
version: '1.0.1',
dependencies: {
'normal-c': '1.0.1'
}
})

const normalAPkg = JSON.parse(
await fs.promises.readFile(fixture('pnpm-workspace-wild/packages/normal-a/package.json'), 'utf-8')
)
expect(normalAPkg).toMatchObject({
name: 'normal-a',
version: '1.0.1',
dependencies: {
'normal-c': 'workspace:*'
}
})

const { npmPublish } = require('@lerna/npm-publish')
const [pkg] = npmPublish.mock.calls[1]
console.log(pkg, pkg.packed)
const lockData = await readYaml(fixture('pnpm-workspace-wild/pnpm-lock.yaml'))
expect(lockData.importers['packages/normal-a']['specifiers']['normal-c']).toBe('workspace:*')
})
})
9 changes: 8 additions & 1 deletion src/patches/after-lerna-version-update-lockfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fs = require('fs')
const readYaml = require('read-yaml-file')
const writeYaml = require('write-yaml-file')
const { gitAdd } = require('@lerna/version/lib/git-add')
const { parseWorkspaceVersionAlias } = require('./pnpm-workspace-deps/patched-package')

const fixPnpmLockFile = async (filename, updatesVersions) => {
const lockData = await readYaml(filename)
Expand All @@ -22,7 +23,13 @@ const fixPnpmLockFile = async (filename, updatesVersions) => {
const nextVersion = updatesVersions.get(name)
if (nextVersion) {
if (spec.startsWith('link:') && lockItem.specifiers[name]) {
const output = lockItem.specifiers[name].match(/^([\D]*)\d/)
const prevSpec = lockItem.specifiers[name]
if (parseWorkspaceVersionAlias(prevSpec)) {
lockItem.specifiers[name] = prevSpec
continue
}

const output = prevSpec.match(/^([\D]*)\d/)
if (output) {
lockItem.specifiers[name] = output[1] + nextVersion
} else {
Expand Down
1 change: 1 addition & 0 deletions src/patches/pnpm-workspace-deps/patched-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,4 @@ class PatchedPackage {
}

module.exports.Package = PatchedPackage
module.exports.parseWorkspaceVersionAlias = parseWorkspaceVersionAlias

0 comments on commit 11d6cbb

Please sign in to comment.