From 5fe03ec02b286e0d69ea48cfd0822f33b5b439e8 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 31 Aug 2024 15:31:31 +0800 Subject: [PATCH 1/2] feat: implement uninstall --- src/uninstall.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/uninstall.ts diff --git a/src/uninstall.ts b/src/uninstall.ts new file mode 100644 index 0000000..112a5b3 --- /dev/null +++ b/src/uninstall.ts @@ -0,0 +1,45 @@ +import { existsSync } from 'node:fs' +import process from 'node:process' +import { resolve } from 'node:path' +import { x } from 'tinyexec' +import { detectPackageManager } from './detect' + +export interface UninstallPackageOptions { + cwd?: string + dev?: boolean + silent?: boolean + packageManager?: string + additionalArgs?: string[] +} + +export async function uninstallPackage(names: string | string[], options: UninstallPackageOptions = {}) { + const detectedAgent = options.packageManager || await detectPackageManager(options.cwd) || 'npm' + const [agent] = detectedAgent.split('@') + + if (!Array.isArray(names)) + names = [names] + + const args = options.additionalArgs || [] + + if (agent === 'pnpm' && existsSync(resolve(options.cwd ?? process.cwd(), 'pnpm-workspace.yaml'))) + args.unshift('-w') + + return x( + agent, + [ + agent === 'yarn' + ? 'remove' + : 'uninstall', + options.dev ? '-D' : '', + ...args, + ...names, + ].filter(Boolean), + { + nodeOptions: { + stdio: options.silent ? 'ignore' : 'inherit', + cwd: options.cwd, + }, + throwOnError: true, + }, + ) +} From 639a6ba34b8d671a780ce0ab2dd576fc0e62e145 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 2 Sep 2024 03:13:20 +0800 Subject: [PATCH 2/2] fix: re-export in the entrypoint --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index cf3d395..8dd8d61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export * from './detect' export * from './install' +export * from './uninstall'