From aafa4f64c4de7e6f87f13d28f7673a4fced462b3 Mon Sep 17 00:00:00 2001 From: Ryan Clements Date: Fri, 28 Jun 2024 04:15:53 -0400 Subject: [PATCH] feat: add option to install dependencies with a frozen lockfile (#121) --- src/api.ts | 21 ++++++++++++++++++--- src/cli.ts | 4 ++++ test/api.test.ts | 12 ++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/api.ts b/src/api.ts index c4ad05a..457021b 100644 --- a/src/api.ts +++ b/src/api.ts @@ -5,7 +5,7 @@ import { getWorkspaceArgs, doesDependencyExist, } from "./_utils"; -import type { OperationOptions } from "./types"; +import type { OperationOptions, PackageManagerName } from "./types"; /** * Installs project dependencies. @@ -14,13 +14,28 @@ import type { OperationOptions } from "./types"; * @param options.cwd - The directory to run the command in. * @param options.silent - Whether to run the command in silent mode. * @param options.packageManager - The package manager info to use (auto-detected). + * @param options.frozenLockFile - Whether to install dependencies with frozen lock file. */ export async function installDependencies( - options: Pick = {}, + options: Pick & { + frozenLockFile?: boolean; + } = {}, ) { const resolvedOptions = await resolveOperationOptions(options); - await executeCommand(resolvedOptions.packageManager.command, ["install"], { + const pmToFrozenLockfileInstallCommand: Record = + { + npm: ["ci"], + yarn: ["install", "--immutable"], + bun: ["install", "--frozen-lockfile"], + pnpm: ["install", "--frozen-lockfile"], + }; + + const commandArgs = options.frozenLockFile + ? pmToFrozenLockfileInstallCommand[resolvedOptions.packageManager.name] + : ["install"]; + + await executeCommand(resolvedOptions.packageManager.command, commandArgs, { cwd: resolvedOptions.cwd, silent: resolvedOptions.silent, }); diff --git a/src/cli.ts b/src/cli.ts index af51985..0ba8094 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -42,6 +42,10 @@ const install = defineCommand({ alias: "g", description: "Add globally", }, + "frozen-lockfile": { + type: "boolean", + description: "Install dependencies with frozen lock file", + }, }, run: async ({ args }) => { await (args._.length > 0 diff --git a/test/api.test.ts b/test/api.test.ts index 95ed66d..6b385f2 100644 --- a/test/api.test.ts +++ b/test/api.test.ts @@ -21,6 +21,18 @@ describe("api (workspace)", () => { expect(installDependenciesSpy).toHaveReturned(); }, 60_000); + it("installs dependencies with lockfile", async () => { + const installDependenciesSpy = vi.fn(installDependencies); + const executeInstallDependenciesSpy = () => + installDependenciesSpy({ + cwd: fixture.dir, + silent: !process.env.DEBUG, + frozenLockFile: true, + }); + await executeInstallDependenciesSpy(); + expect(installDependenciesSpy).toHaveReturned(); + }, 60_000); + it("adds dependency", async () => { const addDependencySpy = vi.fn(addDependency); const executeAddDependencySpy = () =>