Skip to content

Commit

Permalink
feat: add option to install dependencies with a frozen lockfile (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanClementsHax authored Jun 28, 2024
1 parent b3d731d commit aafa4f6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getWorkspaceArgs,
doesDependencyExist,
} from "./_utils";
import type { OperationOptions } from "./types";
import type { OperationOptions, PackageManagerName } from "./types";

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

await executeCommand(resolvedOptions.packageManager.command, ["install"], {
const pmToFrozenLockfileInstallCommand: Record<PackageManagerName, string[]> =
{
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,
});
Expand Down
4 changes: 4 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 () => {
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

0 comments on commit aafa4f6

Please # to comment.