Skip to content

Commit

Permalink
feat: merge-git-branch-lockfiles-branch-pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
chengcyber committed Apr 1, 2022
1 parent 4e2f5c2 commit 7f307b4
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/config/package.json
Expand Up @@ -34,6 +34,8 @@
"dependencies": {
"@pnpm/constants": "workspace:6.0.0",
"@pnpm/error": "workspace:3.0.0",
"@pnpm/git-utils": "workspace:0.0.0",
"@pnpm/matcher": "workspace:3.0.0",
"@pnpm/pnpmfile": "workspace:2.0.0",
"@pnpm/read-project-manifest": "workspace:3.0.0",
"@pnpm/types": "workspace:8.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/Config.ts
Expand Up @@ -132,6 +132,7 @@ export interface Config {
useLockfile: boolean
useGitBranchLockfile: boolean
mergeGitBranchLockfiles?: boolean
mergeGitBranchLockfilesBranchPattern?: string[]
globalPnpmfile?: string
npmPath?: string
gitChecks?: boolean
Expand Down
15 changes: 14 additions & 1 deletion packages/config/src/index.ts
Expand Up @@ -4,6 +4,8 @@ import { LAYOUT_VERSION } from '@pnpm/constants'
import PnpmError from '@pnpm/error'
import { requireHooks } from '@pnpm/pnpmfile'
import { safeReadProjectManifestOnly } from '@pnpm/read-project-manifest'
import { getCurrentBranch } from '@pnpm/git-utils'
import matcher from '@pnpm/matcher'
import camelcase from 'camelcase'
import loadNpmConf from '@zkochan/npm-conf'
import npmTypes from '@zkochan/npm-conf/lib/types'
Expand Down Expand Up @@ -36,6 +38,7 @@ export const types = Object.assign({
'cache-dir': String,
'child-concurrency': Number,
'merge-git-branch-lockfiles': Boolean,
'merge-git-branch-lockfiles-branch-pattern': Array,
color: ['always', 'auto', 'never'],
'config-dir': String,
dev: [null, true],
Expand Down Expand Up @@ -266,6 +269,17 @@ export default async (
if (typeof pnpmConfig['gitBranchLockfile'] === 'boolean') return pnpmConfig['gitBranchLockfile']
return false
})()
pnpmConfig.mergeGitBranchLockfiles = await (async () => {
if (typeof pnpmConfig['mergeGitBranchLockfiles'] === 'boolean') return pnpmConfig['mergeGitBranchLockfiles']
if (pnpmConfig['mergeGitBranchLockfilesBranchPattern'] != null && pnpmConfig['mergeGitBranchLockfilesBranchPattern'].length > 0) {
const branch = await getCurrentBranch()
if (branch) {
const branchMatcher = matcher(pnpmConfig['mergeGitBranchLockfilesBranchPattern'])
return branchMatcher(branch)
}
}
return undefined
})()
pnpmConfig.lockfileOnly = typeof pnpmConfig['lockfileOnly'] === 'undefined'
? pnpmConfig.shrinkwrapOnly
: pnpmConfig['lockfileOnly']
Expand Down Expand Up @@ -331,7 +345,6 @@ export default async (
}
delete pnpmConfig.lockfileDir
}
pnpmConfig.useGitBranchLockfile = false
if (opts.cliOptions['virtual-store-dir']) {
throw new PnpmError('CONFIG_CONFLICT_VIRTUAL_STORE_DIR_WITH_GLOBAL',
'Configuration conflict. "virtual-store-dir" may not be used with "global"')
Expand Down
90 changes: 90 additions & 0 deletions packages/config/test/index.ts
Expand Up @@ -2,6 +2,7 @@
import { promises as fs } from 'fs'
import path from 'path'
import PATH from 'path-name'
import { getCurrentBranch } from '@pnpm/git-utils/test/utils/mock'
import getConfig from '@pnpm/config'
import PnpmError from '@pnpm/error'
import prepare, { prepareEmpty } from '@pnpm/prepare'
Expand Down Expand Up @@ -786,3 +787,92 @@ test('getConfig() sets sideEffectsCacheRead and sideEffectsCacheWrite when side-
expect(config.sideEffectsCacheRead).toBeTruthy()
expect(config.sideEffectsCacheWrite).toBeTruthy()
})

test('respect merge-git-branch-lockfiles-branch-pattern', async () => {
{
const { config } = await getConfig({
cliOptions: {},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})

expect(config.mergeGitBranchLockfilesBranchPattern).toBeUndefined()
expect(config.mergeGitBranchLockfiles).toBeUndefined()
}
{
prepareEmpty()

const npmrc = [
'merge-git-branch-lockfiles-branch-pattern[]=main',
'merge-git-branch-lockfiles-branch-pattern[]=release/**',
].join('\n')

await fs.writeFile('.npmrc', npmrc, 'utf8')

const { config } = await getConfig({
cliOptions: {
global: false,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})

expect(config.mergeGitBranchLockfilesBranchPattern).toEqual(['main', 'release/**'])
}
})

test('getConfig() sets merge-git-branch-lockfiles when branch matches merge-git-branch-lockfiles-branch-pattern', async () => {
prepareEmpty()
{
const npmrc = [
'merge-git-branch-lockfiles-branch-pattern[]=main',
'merge-git-branch-lockfiles-branch-pattern[]=release/**',
].join('\n')

await fs.writeFile('.npmrc', npmrc, 'utf8')

getCurrentBranch.mockReturnValue('develop')
const { config } = await getConfig({
cliOptions: {
global: false,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})

expect(config.mergeGitBranchLockfilesBranchPattern).toEqual(['main', 'release/**'])
expect(config.mergeGitBranchLockfiles).toBe(false)
}
{
getCurrentBranch.mockReturnValue('main')
const { config } = await getConfig({
cliOptions: {
global: false,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})
expect(config.mergeGitBranchLockfiles).toBe(true)
}
{
getCurrentBranch.mockReturnValue('release/1.0.0')
const { config } = await getConfig({
cliOptions: {
global: false,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})
expect(config.mergeGitBranchLockfiles).toBe(true)
}
})
6 changes: 6 additions & 0 deletions packages/config/tsconfig.json
Expand Up @@ -18,6 +18,12 @@
{
"path": "../error"
},
{
"path": "../git-utils"
},
{
"path": "../matcher"
},
{
"path": "../pnpmfile"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-commands-installation/src/install.ts
Expand Up @@ -12,7 +12,6 @@ export function rcOptionsTypes () {
return pick([
'cache-dir',
'child-concurrency',
'merge-git-branch-lockfiles',
'dev',
'engine-strict',
'fetch-retries',
Expand All @@ -34,6 +33,8 @@ export function rcOptionsTypes () {
'lockfile-directory',
'lockfile-only',
'lockfile',
'merge-git-branch-lockfiles',
'merge-git-branch-lockfiles-branch-pattern',
'modules-dir',
'network-concurrency',
'node-linker',
Expand Down
4 changes: 4 additions & 0 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 7f307b4

Please sign in to comment.