Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to install dependencies with a frozen lockfile #121

Merged
merged 6 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getWorkspaceArgs,
doesDependencyExist,
} from "./_utils";
import type { OperationOptions } from "./types";
import type { OperationOptions, PackageManagerName } from "./types";

/**
* Installs project dependencies.
Expand All @@ -13,13 +13,27 @@ 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<OperationOptions, "cwd" | "silent" | "packageManager"> = {},
options: Pick<OperationOptions, "cwd" | "silent" | "packageManager"> & {
frozenLockFile?: boolean;
} = {},
) {
const resolvedOptions = await resolveOperationOptions(options);

await executeCommand(resolvedOptions.packageManager.command, ["install"], {
const pmToInstallCommandMap: Record<PackageManagerName, string[]> = {
npm: ["ci"],
yarn: ["install", "--immutable"],
bun: ["install", "--frozen-lockfile"],
pnpm: ["install", "--frozen-lockfile"],
};

const commandArgs = options.frozenLockFile
? pmToInstallCommandMap[resolvedOptions.packageManager.name]
: ["install"];

await executeCommand(resolvedOptions.packageManager.command, commandArgs, {
cwd: resolvedOptions.cwd,
silent: resolvedOptions.silent,
});
Expand Down
12 changes: 12 additions & 0 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ describe("api (workspace)", () => {
expect(installDependenciesSpy).toHaveReturned();
}, 60_000);

it("installs dependencies with lockfile", async () => {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
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 = () =>
Expand Down