Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
wantedDependencies: [],
})
break
case 'install': {
await installCase(importer)
break
}
case 'installSome': {
await installSome({
...importer,
updatePackageManifest: true,
})
break
}
case 'unlink': {
const packageDirs = await readModulesDirs(importer.modulesDir)
const externalPackages = await pFilter(
packageDirs!,
(packageDir: string) => isExternalLink(ctx.storeDir, importer.modulesDir, packageDir),
)
const allDeps = getAllDependenciesFromPackage(importer.manifest)
const packagesToInstall: string[] = []
for (const pkgName of externalPackages) {
await rimraf(path.join(importer.modulesDir, pkgName))
if (allDeps[pkgName]) {
packagesToInstall.push(pkgName)
}
}
if (!packagesToInstall.length) return importers
// TODO: install only those that were unlinked
// but don't update their version specs in package.json
await Promise.all(importers.map(async ({ binsDir, id, modulesDir, pruneDirectDependencies, removePackages, rootDir }) => {
const currentImporter = opts.currentLockfile.importers[id] || {} as LockfileImporter
const currentPkgs = R.toPairs(mergeDependencies(currentImporter))
const wantedPkgs = R.toPairs(mergeDependencies(wantedLockfile.importers[id]))
const allCurrentPackages = new Set(
(pruneDirectDependencies || removePackages?.length)
? (await readModulesDir(modulesDir) || [])
: [],
)
const depsToRemove = new Set([
...(removePackages || []).filter((removePackage) => allCurrentPackages.has(removePackage)),
...R.difference(currentPkgs, wantedPkgs).map(([depName]) => depName),
])
if (pruneDirectDependencies) {
if (allCurrentPackages.size > 0) {
const newPkgsSet = new Set(wantedPkgs.map(([depName]) => depName))
for (const currentPackage of Array.from(allCurrentPackages)) {
if (!newPkgsSet.has(currentPackage)) {
depsToRemove.add(currentPackage)
}
}
}
}
export async function unlink (maybeOpts: InstallOptions) {
const reporter = maybeOpts && maybeOpts.reporter
if (reporter) {
streamParser.on('data', reporter)
}
const opts = await _extendOptions(maybeOpts)
const ctx = await getContext(opts)
opts.store = ctx.storePath
if (ctx.importers.length > 1) throw new Error('Unlink not implemented for multiple importers yet')
const importer = ctx.importers[0]
const packageDirs = await readModulesDirs(importer.modulesDir)
const externalPackages = await pFilter(
packageDirs,
(packageDir: string) => isExternalLink(opts.store, importer.modulesDir, packageDir),
)
await _unlinkPkgs(externalPackages, opts, ctx.importers)
if (reporter) {
streamParser.removeListener('data', reporter)
}
}