forked from pnpm/pnpm
/
index.ts
183 lines (164 loc) Β· 6.08 KB
/
index.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import path from 'path'
import {
summaryLogger,
} from '@pnpm/core-loggers'
import PnpmError from '@pnpm/error'
import { getContextForSingleImporter } from '@pnpm/get-context'
import { linkBinsOfPackages } from '@pnpm/link-bins'
import {
getLockfileImporterId,
ProjectSnapshot,
writeCurrentLockfile,
writeLockfiles,
} from '@pnpm/lockfile-file'
import logger, { streamParser } from '@pnpm/logger'
import {
getPref,
getSpecFromPackageManifest,
guessDependencyType,
PackageSpecObject,
updateProjectManifestObject,
} from '@pnpm/manifest-utils'
import { pruneSharedLockfile } from '@pnpm/prune-lockfile'
import readProjectManifest from '@pnpm/read-project-manifest'
import { symlinkDirectRootDependency } from '@pnpm/symlink-dependency'
import {
DependenciesField,
DEPENDENCIES_FIELDS,
DependencyManifest,
ProjectManifest,
} from '@pnpm/types'
import normalize from 'normalize-path'
import {
extendOptions,
LinkOptions,
} from './options'
type LinkFunctionOptions = LinkOptions & {
linkToBin?: string
dir: string
}
export { LinkFunctionOptions }
export default async function link (
linkFromPkgs: Array<{alias: string, path: string} | string>,
destModules: string,
maybeOpts: LinkFunctionOptions
) {
const reporter = maybeOpts?.reporter
if ((reporter != null) && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
const opts = await extendOptions(maybeOpts)
const ctx = await getContextForSingleImporter(opts.manifest, {
...opts,
extraBinPaths: [], // ctx.extraBinPaths is not needed, so this is fine
}, true)
const importerId = getLockfileImporterId(ctx.lockfileDir, opts.dir)
const linkedPkgs: Array<{path: string, manifest: DependencyManifest, alias: string}> = []
const specsToUpsert = [] as PackageSpecObject[]
for (const linkFrom of linkFromPkgs) {
let linkFromPath: string
let linkFromAlias: string | undefined
if (typeof linkFrom === 'string') {
linkFromPath = linkFrom
} else {
linkFromPath = linkFrom.path
linkFromAlias = linkFrom.alias
}
const { manifest } = await readProjectManifest(linkFromPath) as { manifest: DependencyManifest }
if (typeof linkFrom === 'string' && manifest.name === undefined) {
throw new PnpmError('INVALID_PACKAGE_NAME', `Package in ${linkFromPath} must have a name field to be linked`)
}
specsToUpsert.push({
alias: manifest.name,
pref: getPref(manifest.name, manifest.name, manifest.version, {
pinnedVersion: opts.pinnedVersion,
}),
saveType: (opts.targetDependenciesField ?? (ctx.manifest && guessDependencyType(manifest.name, ctx.manifest))) as DependenciesField,
})
const packagePath = normalize(path.relative(opts.dir, linkFromPath))
const addLinkOpts = {
linkedPkgName: linkFromAlias ?? manifest.name,
manifest: ctx.manifest,
packagePath,
}
addLinkToLockfile(ctx.currentLockfile.importers[importerId], addLinkOpts)
addLinkToLockfile(ctx.wantedLockfile.importers[importerId], addLinkOpts)
linkedPkgs.push({
alias: linkFromAlias ?? manifest.name,
manifest,
path: linkFromPath,
})
}
const updatedCurrentLockfile = pruneSharedLockfile(ctx.currentLockfile)
const warn = (message: string) => logger.warn({ message, prefix: opts.dir })
const updatedWantedLockfile = pruneSharedLockfile(ctx.wantedLockfile, { warn })
// Linking should happen after removing orphans
// Otherwise would've been removed
for (const { alias, manifest, path } of linkedPkgs) {
// TODO: cover with test that linking reports with correct dependency types
const stu = specsToUpsert.find((s) => s.alias === manifest.name)
await symlinkDirectRootDependency(path, destModules, alias, {
fromDependenciesField: stu?.saveType ?? opts.targetDependenciesField,
linkedPackage: manifest,
prefix: opts.dir,
})
}
const linkToBin = maybeOpts?.linkToBin ?? path.join(destModules, '.bin')
await linkBinsOfPackages(linkedPkgs.map((p) => ({ manifest: p.manifest, location: p.path })), linkToBin, {
extraNodePaths: ctx.extraNodePaths,
})
let newPkg!: ProjectManifest
if (opts.targetDependenciesField) {
newPkg = await updateProjectManifestObject(opts.dir, opts.manifest, specsToUpsert)
for (const { alias } of specsToUpsert) {
updatedWantedLockfile.importers[importerId].specifiers[alias] = getSpecFromPackageManifest(newPkg, alias)
}
} else {
newPkg = opts.manifest
}
const lockfileOpts = { forceSharedFormat: opts.forceSharedLockfile, useGitBranchLockfile: opts.useGitBranchLockfile, mergeGitBranchLockfiles: opts.mergeGitBranchLockfiles }
if (opts.useLockfile) {
await writeLockfiles({
currentLockfile: updatedCurrentLockfile,
currentLockfileDir: ctx.virtualStoreDir,
wantedLockfile: updatedWantedLockfile,
wantedLockfileDir: ctx.lockfileDir,
...lockfileOpts,
})
} else {
await writeCurrentLockfile(ctx.virtualStoreDir, updatedCurrentLockfile, lockfileOpts)
}
summaryLogger.debug({ prefix: opts.dir })
if ((reporter != null) && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}
return newPkg
}
function addLinkToLockfile (
projectSnapshot: ProjectSnapshot,
opts: {
linkedPkgName: string
packagePath: string
manifest?: ProjectManifest
}
) {
const id = `link:${opts.packagePath}`
let addedTo: DependenciesField | undefined
for (const depType of DEPENDENCIES_FIELDS) {
if (!addedTo && opts.manifest?.[depType]?.[opts.linkedPkgName]) {
addedTo = depType
projectSnapshot[depType] = projectSnapshot[depType] ?? {}
projectSnapshot[depType]![opts.linkedPkgName] = id
} else if (projectSnapshot[depType] != null) {
delete projectSnapshot[depType]![opts.linkedPkgName]
}
}
// package.json might not be available when linking to global
if (opts.manifest == null) return
const availableSpec = getSpecFromPackageManifest(opts.manifest, opts.linkedPkgName)
if (availableSpec) {
projectSnapshot.specifiers[opts.linkedPkgName] = availableSpec
} else {
delete projectSnapshot.specifiers[opts.linkedPkgName]
}
}