mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-28 20:11:48 -04:00
fix: uninstall w/o type argument should find dependency
This commit is contained in:
@@ -29,8 +29,6 @@ export default async function uninstallCmd (pkgsToUninstall: string[], maybeOpts
|
||||
throw new Error('No package.json found - cannot uninstall')
|
||||
}
|
||||
|
||||
const pkg = ctx.pkg
|
||||
|
||||
if (opts.lock === false) {
|
||||
return run()
|
||||
}
|
||||
@@ -38,32 +36,30 @@ export default async function uninstallCmd (pkgsToUninstall: string[], maybeOpts
|
||||
return lock(ctx.storePath, run, {stale: opts.lockStaleDuration})
|
||||
|
||||
function run () {
|
||||
return uninstallInContext(pkgsToUninstall, pkg, ctx, opts)
|
||||
return uninstallInContext(pkgsToUninstall, ctx, opts)
|
||||
}
|
||||
}, {stale: opts.lockStaleDuration})
|
||||
}
|
||||
|
||||
export async function uninstallInContext (pkgsToUninstall: string[], pkg: Package, ctx: PnpmContext, opts: StrictPnpmOptions) {
|
||||
const saveType = getSaveType(opts) || 'dependencies'
|
||||
if (saveType) {
|
||||
const pkgJsonPath = path.join(ctx.root, 'package.json')
|
||||
const pkg = await removeDeps(pkgJsonPath, pkgsToUninstall, saveType)
|
||||
for (let depName in ctx.shrinkwrap.dependencies) {
|
||||
if (!isDependentOn(pkg, depName)) {
|
||||
delete ctx.shrinkwrap.dependencies[depName]
|
||||
delete ctx.shrinkwrap.specifiers[depName]
|
||||
}
|
||||
export async function uninstallInContext (pkgsToUninstall: string[], ctx: PnpmContext, opts: StrictPnpmOptions) {
|
||||
const pkgJsonPath = path.join(ctx.root, 'package.json')
|
||||
const saveType = getSaveType(opts)
|
||||
const pkg = await removeDeps(pkgJsonPath, pkgsToUninstall, saveType)
|
||||
for (let depName in ctx.shrinkwrap.dependencies) {
|
||||
if (!isDependentOn(pkg, depName)) {
|
||||
delete ctx.shrinkwrap.dependencies[depName]
|
||||
delete ctx.shrinkwrap.specifiers[depName]
|
||||
}
|
||||
const newShr = await pruneShrinkwrap(ctx.shrinkwrap, pkg)
|
||||
const removedPkgIds = await removeOrphanPkgs(ctx.privateShrinkwrap, newShr, ctx.root, ctx.storePath)
|
||||
await saveShrinkwrap(ctx.root, newShr)
|
||||
await saveModules(path.join(ctx.root, 'node_modules'), {
|
||||
packageManager: `${pnpmPkgJson.name}@${pnpmPkgJson.version}`,
|
||||
storePath: ctx.storePath,
|
||||
skipped: Array.from(ctx.skipped).filter(pkgId => removedPkgIds.indexOf(pkgId) === -1),
|
||||
})
|
||||
await removeOuterLinks(pkgsToUninstall, path.join(ctx.root, 'node_modules'), {storePath: ctx.storePath})
|
||||
}
|
||||
const newShr = await pruneShrinkwrap(ctx.shrinkwrap, pkg)
|
||||
const removedPkgIds = await removeOrphanPkgs(ctx.privateShrinkwrap, newShr, ctx.root, ctx.storePath)
|
||||
await saveShrinkwrap(ctx.root, newShr)
|
||||
await saveModules(path.join(ctx.root, 'node_modules'), {
|
||||
packageManager: `${pnpmPkgJson.name}@${pnpmPkgJson.version}`,
|
||||
storePath: ctx.storePath,
|
||||
skipped: Array.from(ctx.skipped).filter(pkgId => removedPkgIds.indexOf(pkgId) === -1),
|
||||
})
|
||||
await removeOuterLinks(pkgsToUninstall, path.join(ctx.root, 'node_modules'), {storePath: ctx.storePath})
|
||||
}
|
||||
|
||||
function isDependentOn (pkg: Package, depName: string): boolean {
|
||||
|
||||
@@ -1,21 +1,32 @@
|
||||
import loadJsonFile = require('load-json-file')
|
||||
import writePkg = require('write-pkg')
|
||||
import {DependenciesType} from './getSaveType'
|
||||
import {DependenciesType, dependenciesTypes} from './getSaveType'
|
||||
import {Package} from './types'
|
||||
|
||||
export default async function (
|
||||
pkgJsonPath: string,
|
||||
removedPackages: string[],
|
||||
saveType: DependenciesType
|
||||
saveType?: DependenciesType
|
||||
): Promise<Package> {
|
||||
const packageJson = await loadJsonFile(pkgJsonPath)
|
||||
packageJson[saveType] = packageJson[saveType]
|
||||
|
||||
if (!packageJson[saveType]) return packageJson
|
||||
if (saveType) {
|
||||
packageJson[saveType] = packageJson[saveType]
|
||||
|
||||
removedPackages.forEach(dependency => {
|
||||
delete packageJson[saveType][dependency]
|
||||
})
|
||||
if (!packageJson[saveType]) return packageJson
|
||||
|
||||
removedPackages.forEach(dependency => {
|
||||
delete packageJson[saveType][dependency]
|
||||
})
|
||||
} else {
|
||||
dependenciesTypes
|
||||
.filter(deptype => packageJson[deptype])
|
||||
.forEach(deptype => {
|
||||
removedPackages.forEach(dependency => {
|
||||
delete packageJson[deptype][dependency]
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
await writePkg(pkgJsonPath, packageJson)
|
||||
return packageJson
|
||||
|
||||
@@ -35,6 +35,19 @@ test('uninstall package with no dependencies', async function (t) {
|
||||
t.equal(pkgJson.dependencies, undefined, 'is-negative has been removed from dependencies')
|
||||
})
|
||||
|
||||
test('uninstall package and remove from appropriate property', async function (t: tape.Test) {
|
||||
const project = prepare(t)
|
||||
await installPkgs(['is-negative@2.1.0'], testDefaults({ saveDev: true }))
|
||||
await uninstall(['is-negative'], testDefaults())
|
||||
|
||||
await project.storeHasNot('is-negative', '2.1.0')
|
||||
|
||||
await project.hasNot('is-negative')
|
||||
|
||||
const pkgJson = await readPkg()
|
||||
t.equal(pkgJson.dependencies, undefined, 'is-negative has been removed from dependencies')
|
||||
})
|
||||
|
||||
test('uninstall scoped package', async function (t) {
|
||||
const project = prepare(t)
|
||||
await installPkgs(['@zkochan/logger@0.1.0'], testDefaults({ save: true }))
|
||||
|
||||
Reference in New Issue
Block a user