From 7f307b4eda4f762e5f9618132b10b7d532d5fc99 Mon Sep 17 00:00:00 2001 From: Cheng Liu Date: Sat, 2 Apr 2022 05:51:58 +0800 Subject: [PATCH] feat: merge-git-branch-lockfiles-branch-pattern --- packages/config/package.json | 2 + packages/config/src/Config.ts | 1 + packages/config/src/index.ts | 15 +++- packages/config/test/index.ts | 90 +++++++++++++++++++ packages/config/tsconfig.json | 6 ++ .../src/install.ts | 3 +- pnpm-lock.yaml | 4 + 7 files changed, 119 insertions(+), 2 deletions(-) diff --git a/packages/config/package.json b/packages/config/package.json index f2bea352b3d..aab31e7fa8f 100644 --- a/packages/config/package.json +++ b/packages/config/package.json @@ -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", diff --git a/packages/config/src/Config.ts b/packages/config/src/Config.ts index eba4290e380..5c0242e8057 100644 --- a/packages/config/src/Config.ts +++ b/packages/config/src/Config.ts @@ -132,6 +132,7 @@ export interface Config { useLockfile: boolean useGitBranchLockfile: boolean mergeGitBranchLockfiles?: boolean + mergeGitBranchLockfilesBranchPattern?: string[] globalPnpmfile?: string npmPath?: string gitChecks?: boolean diff --git a/packages/config/src/index.ts b/packages/config/src/index.ts index 92fe54cc305..ab6b29ff89d 100644 --- a/packages/config/src/index.ts +++ b/packages/config/src/index.ts @@ -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' @@ -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], @@ -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'] @@ -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"') diff --git a/packages/config/test/index.ts b/packages/config/test/index.ts index 4f22e63feb7..9032abbc27d 100644 --- a/packages/config/test/index.ts +++ b/packages/config/test/index.ts @@ -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' @@ -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) + } +}) \ No newline at end of file diff --git a/packages/config/tsconfig.json b/packages/config/tsconfig.json index 9660d8adfdb..7a6e0ef56be 100644 --- a/packages/config/tsconfig.json +++ b/packages/config/tsconfig.json @@ -18,6 +18,12 @@ { "path": "../error" }, + { + "path": "../git-utils" + }, + { + "path": "../matcher" + }, { "path": "../pnpmfile" }, diff --git a/packages/plugin-commands-installation/src/install.ts b/packages/plugin-commands-installation/src/install.ts index aa3e7d2ed0d..a9e45b8771c 100644 --- a/packages/plugin-commands-installation/src/install.ts +++ b/packages/plugin-commands-installation/src/install.ts @@ -12,7 +12,6 @@ export function rcOptionsTypes () { return pick([ 'cache-dir', 'child-concurrency', - 'merge-git-branch-lockfiles', 'dev', 'engine-strict', 'fetch-retries', @@ -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', diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7506bc3628b..a0b2450572c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -341,6 +341,8 @@ importers: '@pnpm/config': workspace:14.0.0 '@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/prepare': workspace:* '@pnpm/read-project-manifest': workspace:3.0.0 @@ -360,6 +362,8 @@ importers: dependencies: '@pnpm/constants': link:../constants '@pnpm/error': link:../error + '@pnpm/git-utils': link:../git-utils + '@pnpm/matcher': link:../matcher '@pnpm/pnpmfile': link:../pnpmfile '@pnpm/read-project-manifest': link:../read-project-manifest '@pnpm/types': link:../types