/
link.ts
265 lines (212 loc) · 7.74 KB
/
link.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { promises as fs } from 'fs'
import path from 'path'
import readYamlFile from 'read-yaml-file'
import { install, link } from '@pnpm/plugin-commands-installation'
import prepare, { preparePackages } from '@pnpm/prepare'
import assertProject, { isExecutable } from '@pnpm/assert-project'
import fixtures from '@pnpm/test-fixtures'
import PATH from 'path-name'
import writePkg from 'write-pkg'
import { DEFAULT_OPTS } from './utils'
const f = fixtures(__dirname)
test('linking multiple packages', async () => {
const project = prepare()
process.chdir('..')
const globalDir = path.resolve('global')
await writePkg('linked-foo', { name: 'linked-foo', version: '1.0.0' })
await writePkg('linked-bar', { name: 'linked-bar', version: '1.0.0', dependencies: { 'is-positive': '1.0.0' } })
await fs.writeFile('linked-bar/.npmrc', 'shamefully-hoist = true')
process.chdir('linked-foo')
console.log('linking linked-foo to global package')
const linkOpts = {
...DEFAULT_OPTS,
bin: path.join(globalDir, 'bin'),
dir: globalDir,
}
await link.handler({
...linkOpts,
})
process.chdir('..')
process.chdir('project')
await link.handler({
...linkOpts,
}, ['linked-foo', '../linked-bar'])
await project.has('linked-foo')
await project.has('linked-bar')
const modules = await readYamlFile<object>('../linked-bar/node_modules/.modules.yaml')
expect(modules['hoistPattern']).toStrictEqual(['*']) // the linked package used its own configs during installation // eslint-disable-line @typescript-eslint/dot-notation
})
test('link global bin', async function () {
prepare()
process.chdir('..')
const globalDir = path.resolve('global')
const globalBin = path.join(globalDir, 'bin')
const oldPath = process.env[PATH]
process.env[PATH] = `${globalBin}${path.delimiter}${oldPath ?? ''}`
await fs.mkdir(globalBin, { recursive: true })
await writePkg('package-with-bin', { name: 'package-with-bin', version: '1.0.0', bin: 'bin.js' })
await fs.writeFile('package-with-bin/bin.js', '#!/usr/bin/env node\nconsole.log(/hi/)\n', 'utf8')
process.chdir('package-with-bin')
await link.handler({
...DEFAULT_OPTS,
cliOptions: {
global: true,
},
bin: globalBin,
dir: globalDir,
})
process.env[PATH] = oldPath
await isExecutable((value) => expect(value).toBeTruthy(), path.join(globalBin, 'package-with-bin'))
})
test('link to global bin in dir', async function () {
prepare()
process.chdir('..')
const globalDir = path.resolve('global')
const globalBin = path.join(globalDir, 'bin')
const oldPath = process.env[PATH]
process.env[PATH] = `${globalBin}${path.delimiter}${oldPath ?? ''}`
await fs.mkdir(globalBin, { recursive: true })
await writePkg('./dir/package-with-bin-in-dir', { name: 'package-with-bin-in-dir', version: '1.0.0', bin: 'bin.js' })
await fs.writeFile('./dir/package-with-bin-in-dir/bin.js', '#!/usr/bin/env node\nconsole.log(/hi/)\n', 'utf8')
await link.handler({
...DEFAULT_OPTS,
cliOptions: {
global: true,
dir: path.resolve('./dir/package-with-bin-in-dir'),
},
bin: globalBin,
dir: globalDir,
})
process.env[PATH] = oldPath
await isExecutable((value) => expect(value).toBeTruthy(), path.join(globalBin, 'package-with-bin-in-dir'))
})
test('link from global package in dir', async function () {
const project = prepare()
process.chdir('..')
const globalDir = path.resolve('global')
const globalBin = path.join(globalDir, 'bin')
const oldPath = process.env[PATH]
process.env[PATH] = `${globalBin}${path.delimiter}${oldPath ?? ''}`
await fs.mkdir(globalBin, { recursive: true })
await writePkg('global-package-with-bin', { name: 'global-package-with-bin', version: '1.0.0', bin: 'bin.js' })
await fs.writeFile('global-package-with-bin/bin.js', '#!/usr/bin/env node\nconsole.log(/hi/)\n', 'utf8')
process.chdir('global-package-with-bin')
// link to global
await link.handler({
...DEFAULT_OPTS,
cliOptions: {
global: true,
},
bin: globalBin,
dir: globalDir,
})
process.chdir('..')
// link from global
await link.handler({
...DEFAULT_OPTS,
cliOptions: {
global: true,
dir: path.resolve('./project'),
},
bin: globalBin,
dir: globalDir,
}, ['global-package-with-bin'])
process.env[PATH] = oldPath
await project.has('global-package-with-bin')
})
test('relative link', async () => {
const project = prepare({
dependencies: {
'@pnpm.e2e/hello-world-js-bin': '*',
},
})
const linkedPkgName = 'hello-world-js-bin'
const linkedPkgPath = path.resolve('..', linkedPkgName)
f.copy(linkedPkgName, linkedPkgPath)
await link.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [`../${linkedPkgName}`])
await project.isExecutable('.bin/hello-world-js-bin')
// The linked package has been installed successfully as well with bins linked
// to node_modules/.bin
const linkedProject = assertProject(linkedPkgPath)
await linkedProject.isExecutable('.bin/cowsay')
const wantedLockfile = await project.readLockfile()
expect(wantedLockfile.dependencies['@pnpm.e2e/hello-world-js-bin']).toBe('link:../hello-world-js-bin') // link added to wanted lockfile
expect(wantedLockfile.specifiers['@pnpm.e2e/hello-world-js-bin']).toBe('*') // specifier of linked dependency added to ${WANTED_LOCKFILE}
const currentLockfile = await project.readCurrentLockfile()
expect(currentLockfile.dependencies['@pnpm.e2e/hello-world-js-bin']).toBe('link:../hello-world-js-bin') // link added to wanted lockfile
})
test('absolute link', async () => {
const project = prepare({
dependencies: {
'@pnpm.e2e/hello-world-js-bin': '*',
},
})
const linkedPkgName = 'hello-world-js-bin'
const linkedPkgPath = path.resolve('..', linkedPkgName)
f.copy(linkedPkgName, linkedPkgPath)
await link.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [linkedPkgPath])
await project.isExecutable('.bin/hello-world-js-bin')
// The linked package has been installed successfully as well with bins linked
// to node_modules/.bin
const linkedProject = assertProject(linkedPkgPath)
await linkedProject.isExecutable('.bin/cowsay')
const wantedLockfile = await project.readLockfile()
expect(wantedLockfile.dependencies['@pnpm.e2e/hello-world-js-bin']).toBe('link:../hello-world-js-bin') // link added to wanted lockfile
expect(wantedLockfile.specifiers['@pnpm.e2e/hello-world-js-bin']).toBe('*') // specifier of linked dependency added to ${WANTED_LOCKFILE}
const currentLockfile = await project.readCurrentLockfile()
expect(currentLockfile.dependencies['@pnpm.e2e/hello-world-js-bin']).toBe('link:../hello-world-js-bin') // link added to wanted lockfile
})
test('link --production', async () => {
const projects = preparePackages([
{
name: 'target',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
devDependencies: {
'is-negative': '1.0.0',
},
},
{
name: 'source',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
devDependencies: {
'is-negative': '1.0.0',
},
},
])
process.chdir('target')
await install.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
})
await link.handler({
...DEFAULT_OPTS,
cliOptions: { production: true },
dir: process.cwd(),
}, ['../source'])
await projects['source'].has('is-positive')
await projects['source'].hasNot('is-negative')
// --production should not have effect on the target
await projects['target'].has('is-positive')
await projects['target'].has('is-negative')
})
test('link fails if nothing is linked', async () => {
prepare()
await expect(
link.handler({
...DEFAULT_OPTS,
dir: '',
}, [])
).rejects.toThrow(/You must provide a parameter/)
})