Skip to content

Commit 1995602

Browse files
authored
feat(lockfile-check): change default behavior to auto-install dependencies and add skip option (#47)
- Change lockfile-check to automatically install dependencies by default when lockfile changes are detected - Add `--skip-install` option to allow users to skip automatic installation - Update documentation and test cases to reflect this behavior change
1 parent b40a7e5 commit 1995602

6 files changed

Lines changed: 48 additions & 30 deletions

File tree

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ commitLint({
297297

298298
### lockfile-check
299299

300-
**Description**: Detect and visually output in the console whether the lockfile of frontend dependencies has been modified.
300+
**Description**: Detect and visually output in the console whether the lockfile of frontend dependencies has been modified, and automatically install dependencies by default to keep in sync.
301301

302-
**Use cases**: Recommended to use after Git operations such as pulling updates (`git pull`), switching branches (`git checkout`), or merging code. It helps detect if upstream dependency lockfiles (like `pnpm-lock.yaml`) have changed. If a change is detected, you can invoke a package manager's installation command through specific options to synchronize the local environment with upstream instantly, preventing obscure bugs caused by outdated dependencies.
302+
**Use cases**: Recommended to use after Git operations such as pulling updates (`git pull`), switching branches (`git checkout`), or merging code. It helps detect if upstream dependency lockfiles (like `pnpm-lock.yaml`) have changed. If a change is detected, it will automatically invoke a package manager's installation command to synchronize the local environment with upstream instantly, preventing obscure bugs caused by outdated dependencies.
303303

304-
**Core Workflow**: Execute a Git diff comparing the project's lockfile (e.g. `pnpm-lock.yaml` or corresponding environment lockfile) from the original HEAD. Print its modification status in the console. Furthermore, trigger the package manager to reinstall dependencies to sync with upstream if directed by the command options.
304+
**Core Workflow**: Execute a Git diff comparing the project's lockfile (e.g. `pnpm-lock.yaml` or corresponding environment lockfile) from the original HEAD. Print its modification status in the console. Furthermore, trigger the package manager to reinstall dependencies to sync with upstream by default, or skip installation if directed by the command options.
305305

306306
**CLI Commands**:
307307

@@ -312,17 +312,17 @@ Usage: vr lockfile-check [flags...]
312312
313313
Flags:
314314
--package-manager string Package manager (npm, yarn, pnpm) # default: 'pnpm'
315-
--install Auto install dependencies if lockfile changed
315+
--skip-install Skip install dependencies when lockfile changed
316316
```
317317

318318
_Example_:
319319

320320
```shell
321-
# Check the synchronization status of the current lockfile
321+
# Check the synchronization status of the current lockfile and install dependencies if changed
322322
pnpm exec vr lockfile-check
323323

324-
# Check current status, forcefully run installation to sync dependencies if updates exist
325-
pnpm exec vr lockfile-check --install
324+
# Check current status but skip installation even if updates exist
325+
pnpm exec vr lockfile-check --skip-install
326326

327327
# Specify other package managers for checking
328328
pnpm exec vr lockfile-check --package-manager npm
@@ -333,7 +333,7 @@ _It is also recommended to integrate with `simple-git-hooks` or `husky` in `pack
333333
```json
334334
{
335335
"simple-git-hooks": {
336-
"post-merge": "pnpm exec vr lockfile-check --install"
336+
"post-merge": "pnpm exec vr lockfile-check"
337337
}
338338
}
339339
```
@@ -345,7 +345,7 @@ import { lockfileCheck } from '@varlet/release'
345345

346346
lockfileCheck({
347347
packageManager?: 'npm' | 'yarn' | 'pnpm' // Choose package manager, defaults to 'pnpm'
348-
install?: boolean // Whether to automatically run install if lockfile is out of sync
348+
skipInstall?: boolean // Whether to skip installation if lockfile is out of sync
349349
})
350350
```
351351

README.zh-CN.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ commitLint({
297297
298298
### lockfile-check
299299
300-
**作用**:检测并在控制台直观地输出前端依赖包的 lockfile 文件是否发生变更。
300+
**作用**:检测并在控制台直观地输出前端依赖包的 lockfile 文件是否发生变更,并默认自动安装依赖以保持同步
301301
302-
**使用场景**:通常在进行 `git 操作` (如 `git pull`拉取最新代码、`git checkout`切换分支、或合并代码) 之后使用。它能帮助你检测上游的依赖锁文件(如 `pnpm-lock.yaml`)是否发生了变动。如果检测到有变更,可以通过可选操作直接执行 install 安装命令,从而确保本地环境快速与上游依赖保持一致,避免因依赖版本陈旧导致的疑难 bug。
302+
**使用场景**:通常在进行 `git 操作` (如 `git pull`拉取最新代码、`git checkout`切换分支、或合并代码) 之后使用。它能帮助你检测上游的依赖锁文件(如 `pnpm-lock.yaml`)是否发生了变动。如果检测到有变更,默认会自动执行 install 安装命令,从而确保本地环境快速与上游依赖保持一致,避免因依赖版本陈旧导致的疑难 bug。
303303
304-
**核心流程**:比对项目原始游标的锁文件(如 pnpm-lock.yaml 或对应环境的 lockfile)的 Git diff,在控制台输出其是否存在变动;并根据指令决定是否随后触发包管理器的依赖重装程序以同步上游
304+
**核心流程**:比对项目原始游标的锁文件(如 pnpm-lock.yaml 或对应环境的 lockfile)的 Git diff,在控制台输出其是否存在变动;默认会触发包管理器的依赖重装程序以同步上游,也可以通过指令跳过安装
305305
306306
**CMD 命令**:
307307
@@ -312,17 +312,17 @@ _标志参考_:
312312

313313
标志:
314314
--package-manager string 包管理器 (npm, yarn, pnpm) # 默认: 'pnpm'
315-
--install 如果 lockfile 发生变化则自动安装依赖
315+
--skip-install lockfile 发生变化时跳过安装依赖
316316
```
317317
318318
_使用示例_:
319319
320320
```shell
321-
# 检查当前 lockfile 的同步状态
321+
# 检查当前 lockfile 的同步状态,若有变化则自动安装依赖
322322
pnpm exec vr lockfile-check
323323

324-
# 检查当前状态,若存在更新则强制运行安装命令同步依赖
325-
pnpm exec vr lockfile-check --install
324+
# 检查当前状态,即使有更新也跳过安装
325+
pnpm exec vr lockfile-check --skip-install
326326

327327
# 指定其他包管理器进行检查
328328
pnpm exec vr lockfile-check --package-manager npm
@@ -333,7 +333,7 @@ _建议配合并在 `package.json` 中的 `simple-git-hooks` 或 `husky` 一同
333333
```json
334334
{
335335
"simple-git-hooks": {
336-
"post-merge": "pnpm exec vr lockfile-check --install"
336+
"post-merge": "pnpm exec vr lockfile-check"
337337
}
338338
}
339339
```
@@ -345,7 +345,7 @@ import { lockfileCheck } from '@varlet/release'
345345

346346
lockfileCheck({
347347
packageManager?: 'npm' | 'yarn' | 'pnpm' // 选择包管理器,默认为 'pnpm'
348-
install?: boolean // 检测到 lock 不同步时是否自动运行对应的 install 重新安装
348+
skipInstall?: boolean // 检测到 lock 不同步时是否跳过安装
349349
})
350350
```
351351

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ cli({
7979
default: 'pnpm',
8080
description: 'Package manager (npm, yarn, pnpm), default pnpm',
8181
},
82-
install: { type: Boolean, alias: 'i', description: 'Auto install dependencies if lockfile changed' },
82+
skipInstall: { type: Boolean, alias: 's', description: 'Skip install dependencies when lockfile changed' },
8383
},
8484
help: {
8585
description: 'Check if lockfile has been updated and optionally install dependencies',

src/lockfileCheck.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type PackageManager = 'npm' | 'yarn' | 'pnpm'
66

77
export interface LockfileCheckOptions {
88
packageManager?: PackageManager
9-
install?: boolean
9+
skipInstall?: boolean
1010
}
1111

1212
export function getLockfilePath(packageManager: PackageManager): string {
@@ -56,7 +56,7 @@ export async function installDependencies(packageManager: PackageManager): Promi
5656
export async function lockfileCheck(options: LockfileCheckOptions = {}): Promise<void> {
5757
try {
5858
const pkgManager = (options.packageManager || 'pnpm') as PackageManager
59-
const installFlag = options.install || false
59+
const skipInstallFlag = options.skipInstall || false
6060

6161
if (!['npm', 'yarn', 'pnpm'].includes(pkgManager)) {
6262
logger.error(`Unsupported package manager: ${pkgManager}`)
@@ -67,7 +67,7 @@ export async function lockfileCheck(options: LockfileCheckOptions = {}): Promise
6767

6868
if (needSync) {
6969
logger.warn('Lockfile has been updated!')
70-
if (installFlag) {
70+
if (!skipInstallFlag) {
7171
await installDependencies(pkgManager)
7272
}
7373
}

test/cli.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,23 @@ describe('cli', () => {
155155
})
156156

157157
it('runs lockfile-check command with options', async () => {
158-
await runCli(['lockfile-check', '--packageManager', 'npm', '--install'])
158+
await runCli(['lockfile-check', '--packageManager', 'npm', '--skip-install'])
159159

160160
expect(indexMock.lockfileCheck).toHaveBeenCalledWith(
161161
expect.objectContaining({
162162
packageManager: 'npm',
163-
install: true,
163+
skipInstall: true,
164164
}),
165165
)
166166
})
167167

168168
it('supports short flags for lockfile-check', async () => {
169-
await runCli(['lockfile-check', '-m', 'yarn', '-i'])
169+
await runCli(['lockfile-check', '-m', 'yarn', '-s'])
170170

171171
expect(indexMock.lockfileCheck).toHaveBeenCalledWith(
172172
expect.objectContaining({
173173
packageManager: 'yarn',
174-
install: true,
174+
skipInstall: true,
175175
}),
176176
)
177177
})

test/lockfileCheck.test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,40 @@ describe('lockfileCheck', () => {
131131
await expect(lockfileCheck({ packageManager: 'yarn' })).resolves.toBeUndefined()
132132
})
133133

134-
it('should install dependencies when install flag is true and lockfile updated', async () => {
134+
it('should install dependencies by default when lockfile updated', async () => {
135135
const { x: mockExec } = await import('tinyexec')
136136
vi.mocked(mockExec)
137137
.mockResolvedValueOnce({ stdout: 'pnpm-lock.yaml\n' } as any)
138138
.mockResolvedValueOnce(undefined as any)
139139

140-
await lockfileCheck({ install: true })
140+
await lockfileCheck()
141141

142142
expect(mockExec).toHaveBeenCalledWith('pnpm', ['install'], { throwOnError: true })
143143
})
144144

145+
it('should not install when skipInstall flag is true and lockfile updated', async () => {
146+
const { x: mockExec } = await import('tinyexec')
147+
vi.mocked(mockExec).mockResolvedValue({ stdout: 'pnpm-lock.yaml\n' } as any)
148+
149+
const loggerMock = await import('rslog')
150+
const mockWarn = vi.spyOn(loggerMock.logger, 'warn')
151+
152+
const lockfileModule = await import('../src/lockfileCheck')
153+
const spyInstall = vi.spyOn(lockfileModule, 'installDependencies')
154+
155+
await lockfileCheck({ skipInstall: true })
156+
157+
expect(mockExec).toHaveBeenCalledWith('git', ['diff', '--name-only', 'ORIG_HEAD', 'HEAD'], { throwOnError: true })
158+
expect(mockExec).not.toHaveBeenCalledWith('pnpm', ['install'], { throwOnError: true })
159+
expect(spyInstall).not.toHaveBeenCalled()
160+
expect(mockWarn).toHaveBeenCalledWith('Lockfile has been updated!')
161+
})
162+
145163
it('should not install when lockfile is not updated', async () => {
146164
const { x: mockExec } = await import('tinyexec')
147165
vi.mocked(mockExec).mockResolvedValue({ stdout: 'test.txt\n' } as any)
148166

149-
await lockfileCheck({ install: true })
167+
await lockfileCheck()
150168

151169
expect(mockExec).toHaveBeenCalledWith('git', ['diff', '--name-only', 'ORIG_HEAD', 'HEAD'], { throwOnError: true })
152170
expect(mockExec).not.toHaveBeenCalledWith('pnpm', ['install'], { throwOnError: true })
@@ -161,7 +179,7 @@ describe('lockfileCheck', () => {
161179
const loggerMock = await import('rslog')
162180
const mockError = vi.spyOn(loggerMock.logger, 'error')
163181

164-
await expect(lockfileCheck({ install: true })).resolves.toBeUndefined()
182+
await expect(lockfileCheck()).resolves.toBeUndefined()
165183
expect(mockError).toHaveBeenCalledWith('Error checking lockfile sync:', expect.any(Error))
166184
})
167185
})

0 commit comments

Comments
 (0)