Skip to content

Commit

Permalink
feat: add dedupe util
Browse files Browse the repository at this point in the history
  • Loading branch information
OrbisK committed Dec 28, 2024
1 parent fa96ead commit c1abb52
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
doesDependencyExist,
} from "./_utils";
import type { OperationOptions, PackageManagerName } from "./types";
import { consola } from "consola";
import * as fs from "node:fs";
import { resolve } from "pathe";

/**
* Installs project dependencies.
Expand Down Expand Up @@ -233,15 +236,29 @@ export async function ensureDependencyInstalled(
await addDependency(name, resolvedOptions);
}

export async function dedupeDependencies(options: OperationOptions = {}) {
export async function dedupeDependencies(options: Pick<OperationOptions, 'cwd' | 'silent'> & {recreateLockfile?: boolean} = {}) {
const resolvedOptions = await resolveOperationOptions(options);
if (["bun", "deno"].includes(resolvedOptions.packageManager?.name ?? "")) {
throw new Error(
`Deduping is currently not supported for \`${resolvedOptions.packageManager?.name ?? "unknown package manager"}\``,
const isSupported = ["bun", "deno"].includes(resolvedOptions.packageManager?.name ?? "");
const recreateLockfile = options.recreateLockfile ?? !isSupported;
if(!isSupported) {
consola.log(
`Deduping is currently not supported for \`${resolvedOptions.packageManager?.name ?? "unknown package manager"}\`.`,
);
}
await executeCommand(resolvedOptions.packageManager.command, ["dedupe"], {
cwd: resolvedOptions.cwd,
silent: resolvedOptions.silent,
});
if (recreateLockfile) {
consola.log("Removing lockfile(s) and reinstalling dependencies...");
const lockfiles = Array.isArray(resolvedOptions.packageManager.lockFile)
? resolvedOptions.packageManager.lockFile
: [resolvedOptions.packageManager.lockFile];
for (const lockfile of lockfiles) {
if (lockfile)
fs.rmSync(resolve(resolvedOptions.cwd, lockfile), { force: true });
}
await installDependencies(resolvedOptions);
} else if (isSupported) {
await executeCommand(resolvedOptions.packageManager.command, ["dedupe"], {
cwd: resolvedOptions.cwd,
silent: resolvedOptions.silent,
});
}
}
4 changes: 4 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ const dedupe = defineCommand({
type: "boolean",
description: "Run in silent mode",
},
recreateLockFile: {
type: "boolean",
description: "Recreate lock file",
},
},
run: async ({ args }) => {
await dedupeDependencies(args);
Expand Down

0 comments on commit c1abb52

Please sign in to comment.