fix: uninstall should remove dependency from any property

This commit is contained in:
zkochan
2017-06-26 22:36:36 +03:00
parent 03264e5557
commit 8ca612477d
5 changed files with 13 additions and 24 deletions

View File

@@ -46,14 +46,6 @@ export async function uninstallInContext (pkgsToUninstall: string[], ctx: PnpmCo
const pkgJsonPath = path.join(ctx.root, 'package.json')
const saveType = getSaveType(opts)
const pkg = await removeDeps(pkgJsonPath, pkgsToUninstall, saveType)
if (ctx.shrinkwrap.dependencies) {
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)
@@ -67,15 +59,6 @@ export async function uninstallInContext (pkgsToUninstall: string[], ctx: PnpmCo
await removeOuterLinks(pkgsToUninstall, path.join(ctx.root, 'node_modules'), {storePath: ctx.storePath})
}
function isDependentOn (pkg: Package, depName: string): boolean {
return [
'dependencies',
'devDependencies',
'optionalDependencies',
]
.some(deptype => pkg[deptype] && pkg[deptype][depName])
}
async function removeOuterLinks (
pkgsToUninstall: string[],
modules: string,

View File

@@ -6,6 +6,6 @@ export const dependenciesTypes: DependenciesType[] = ['dependencies', 'devDepend
export default function getSaveType (opts: PnpmOptions): DependenciesType | undefined {
if (opts.saveDev) return 'devDependencies'
if (opts.saveOptional) return 'optionalDependencies'
if (opts.save) return 'dependencies'
if (opts.saveProd) return 'dependencies'
return undefined
}

View File

@@ -8,6 +8,7 @@ export type PnpmOptions = {
bin?: string,
ignoreScripts?: boolean
save?: boolean,
saveProd?: boolean,
saveDev?: boolean,
saveOptional?: boolean,
production?: boolean,
@@ -63,6 +64,7 @@ export type StrictPnpmOptions = {
bin: string,
ignoreScripts: boolean
save: boolean,
saveProd: boolean,
saveDev: boolean,
saveOptional: boolean,
production: boolean,

View File

@@ -135,7 +135,7 @@ test('dependency should be removed from the old field when installing it as a di
},
})
await installPkgs(['foo'], testDefaults({saveOptional: true}))
await installPkgs(['bar'], testDefaults({save: true}))
await installPkgs(['bar'], testDefaults({saveProd: true}))
await installPkgs(['qar'], testDefaults({saveDev: true}))
const pkgJson = await readPkg({normalize: false})

View File

@@ -19,6 +19,7 @@ import {
link,
} from '../src'
import thenify = require('thenify')
import pnpmCli from '../src/bin/pnpm'
const ncp = thenify(ncpCB.ncp)
@@ -37,15 +38,18 @@ test('uninstall package with no dependencies', async function (t) {
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 installPkgs(['is-positive@3.1.0'], testDefaults({ saveOptional: true }))
await project.storeHasNot('is-negative', '2.1.0')
// testing the CLI directly as there was an issue where `npm.config` started to set save = true by default
// npm@5 introduced --save-prod that bahaves the way --save worked in pre 5 versions
await pnpmCli(['uninstall', 'is-positive'])
await project.hasNot('is-negative')
await project.storeHasNot('is-positive', '3.1.0')
await project.hasNot('is-positive')
const pkgJson = await readPkg()
t.equal(pkgJson.dependencies, undefined, 'is-negative has been removed from dependencies')
t.equal(pkgJson.optionalDependencies, undefined, 'is-negative has been removed from optionalDependencies')
})
test('uninstall scoped package', async function (t) {