-
-
Notifications
You must be signed in to change notification settings - Fork 933
/
updateWorkspaceDependencies.ts
34 lines (32 loc) · 1.45 KB
/
updateWorkspaceDependencies.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { PnpmError } from '@pnpm/error'
import { parseWantedDependency } from '@pnpm/parse-wanted-dependency'
import { WorkspacePackages } from '@pnpm/resolver-base'
import { IncludedDependencies, ProjectManifest } from '@pnpm/types'
export function updateToWorkspacePackagesFromManifest (
manifest: ProjectManifest,
include: IncludedDependencies,
workspacePackages: WorkspacePackages
) {
const allDeps = {
...(include.devDependencies ? manifest.devDependencies : {}),
...(include.dependencies ? manifest.dependencies : {}),
...(include.optionalDependencies ? manifest.optionalDependencies : {}),
} as Record<string, string>
const updateSpecs = Object.keys(allDeps).reduce((acc: string[], depName) => {
if (workspacePackages[depName]) {
acc.push(`${depName}@workspace:*`)
}
return acc
}, [])
return updateSpecs
}
export function createWorkspaceSpecs (specs: string[], workspacePackages: WorkspacePackages) {
return specs.map((spec) => {
const parsed = parseWantedDependency(spec)
if (!parsed.alias) throw new PnpmError('NO_PKG_NAME_IN_SPEC', `Cannot update/install from workspace through "${spec}"`)
if (!workspacePackages[parsed.alias]) throw new PnpmError('WORKSPACE_PACKAGE_NOT_FOUND', `"${parsed.alias}" not found in the workspace`)
if (!parsed.pref) return `${parsed.alias}@workspace:>=0.0.0`
if (parsed.pref.startsWith('workspace:')) return spec
return `${parsed.alias}@workspace:${parsed.pref}`
})
}