mirror of
https://github.com/pnpm/pnpm.git
synced 2026-07-01 19:35:22 -04:00
BREAKING CHANGE: - @pnpm/build-modules - @pnpm/filter-lockfile - @pnpm/headless - @pnpm/lifecycle - @pnpm/modules-cleaner - @pnpm/package-is-installable - @pnpm/read-importers-context - @pnpm/resolve-dependencies - supi
75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
import {
|
|
removalLogger,
|
|
rootLogger,
|
|
} from '@pnpm/core-loggers'
|
|
import binify from '@pnpm/package-bins'
|
|
import { DependenciesField, DependencyManifest } from '@pnpm/types'
|
|
import { safeReadPackageFromDir } from '@pnpm/utils'
|
|
import rimraf = require('@zkochan/rimraf')
|
|
import path = require('path')
|
|
|
|
export default async function removeDirectDependency (
|
|
dependency: {
|
|
dependenciesField?: DependenciesField | undefined,
|
|
name: string,
|
|
},
|
|
opts: {
|
|
binsDir: string,
|
|
dryRun?: boolean,
|
|
modulesDir: string,
|
|
muteLogs?: boolean,
|
|
rootDir: string,
|
|
},
|
|
) {
|
|
const results = await Promise.all([
|
|
removeBins(dependency.name, opts),
|
|
!opts.dryRun && remove(path.join(opts.modulesDir, dependency.name)) as any, // tslint:disable-line:no-any
|
|
])
|
|
|
|
const uninstalledPkg = results[0]
|
|
if (!opts.muteLogs) {
|
|
rootLogger.debug({
|
|
prefix: opts.rootDir,
|
|
removed: {
|
|
dependencyType: dependency.dependenciesField === 'devDependencies' && 'dev' ||
|
|
dependency.dependenciesField === 'optionalDependencies' && 'optional' ||
|
|
dependency.dependenciesField === 'dependencies' && 'prod' ||
|
|
undefined,
|
|
name: dependency.name,
|
|
version: uninstalledPkg?.version,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
async function removeBins (
|
|
uninstalledPkg: string,
|
|
opts: {
|
|
dryRun?: boolean,
|
|
modulesDir: string,
|
|
binsDir: string,
|
|
},
|
|
) {
|
|
const uninstalledPkgPath = path.join(opts.modulesDir, uninstalledPkg)
|
|
const uninstalledPkgJson = await safeReadPackageFromDir(uninstalledPkgPath) as DependencyManifest
|
|
|
|
if (!uninstalledPkgJson) return
|
|
const cmds = await binify(uninstalledPkgJson, uninstalledPkgPath)
|
|
|
|
if (!opts.dryRun) {
|
|
// TODO: what about the .cmd bin files on Windows?
|
|
await Promise.all(
|
|
cmds
|
|
.map((cmd) => path.join(opts.binsDir, cmd.name))
|
|
.map(remove),
|
|
)
|
|
}
|
|
|
|
return uninstalledPkgJson
|
|
}
|
|
|
|
function remove (p: string) {
|
|
removalLogger.debug(p)
|
|
return rimraf(p)
|
|
}
|