-
-
Notifications
You must be signed in to change notification settings - Fork 936
/
import.ts
136 lines (110 loc) · 3.89 KB
/
import.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
/// <reference path="../../../typings/index.d.ts" />
import { promisify } from 'util'
import path from 'path'
import assertProject from '@pnpm/assert-project'
import PnpmError from '@pnpm/error'
import { importCommand } from '@pnpm/plugin-commands-installation'
import prepare, { tempDir } from '@pnpm/prepare'
import { addDistTag, REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import tempy from 'tempy'
import ncpCB from 'ncp'
const ncp = promisify(ncpCB)
const fixtures = path.join(__dirname, '../../../fixtures')
const REGISTRY = `http://localhost:${REGISTRY_MOCK_PORT}`
const TMP = tempy.directory()
const DEFAULT_OPTS = {
alwaysAuth: false,
ca: undefined,
cacheDir: path.join(TMP, 'cache'),
cert: undefined,
fetchRetries: 2,
fetchRetryFactor: 90,
fetchRetryMaxtimeout: 90,
fetchRetryMintimeout: 10,
httpsProxy: undefined,
key: undefined,
localAddress: undefined,
lock: false,
lockStaleDuration: 90,
networkConcurrency: 16,
offline: false,
proxy: undefined,
rawConfig: { registry: REGISTRY },
registries: { default: REGISTRY },
registry: REGISTRY,
storeDir: path.join(TMP, 'store'),
strictSsl: false,
userAgent: 'pnpm',
useRunningStoreServer: false,
useStoreServer: false,
}
test('import from package-lock.json', async () => {
await addDistTag({ package: 'dep-of-pkg-with-1-dep', version: '100.1.0', distTag: 'latest' })
tempDir()
await ncp(path.join(fixtures, 'has-package-lock-json'), process.cwd())
await importCommand.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [])
const project = assertProject(process.cwd())
const lockfile = await project.readLockfile()
expect(lockfile.packages).toHaveProperty(['/dep-of-pkg-with-1-dep/100.0.0'])
expect(lockfile.packages).not.toHaveProperty(['/dep-of-pkg-with-1-dep/100.1.0'])
// node_modules is not created
await project.hasNot('dep-of-pkg-with-1-dep')
await project.hasNot('pkg-with-1-dep')
})
test('import from yarn.lock', async () => {
await addDistTag({ package: 'dep-of-pkg-with-1-dep', version: '100.1.0', distTag: 'latest' })
tempDir()
await ncp(path.join(fixtures, 'has-yarn-lock'), process.cwd())
await importCommand.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [])
const project = assertProject(process.cwd())
const lockfile = await project.readLockfile()
expect(lockfile.packages).toHaveProperty(['/dep-of-pkg-with-1-dep/100.1.0'])
expect(lockfile.packages).not.toHaveProperty(['/dep-of-pkg-with-1-dep/100.0.0'])
// node_modules is not created
await project.hasNot('dep-of-pkg-with-1-dep')
await project.hasNot('pkg-with-1-dep')
})
test('import from yarn2 lock file', async () => {
tempDir()
await ncp(path.join(fixtures, 'has-yarn2-lock'), process.cwd())
await importCommand.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [])
const project = assertProject(process.cwd())
const lockfile = await project.readLockfile()
expect(lockfile.packages).toMatchSnapshot();
})
test('import from npm-shrinkwrap.json', async () => {
await addDistTag({ package: 'dep-of-pkg-with-1-dep', version: '100.1.0', distTag: 'latest' })
tempDir()
await ncp(path.join(fixtures, 'has-npm-shrinkwrap-json'), process.cwd())
await importCommand.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [])
const project = assertProject(process.cwd())
const lockfile = await project.readLockfile()
expect(lockfile.packages).toHaveProperty(['/dep-of-pkg-with-1-dep/100.0.0'])
expect(lockfile.packages).not.toHaveProperty(['/dep-of-pkg-with-1-dep/100.1.0'])
// node_modules is not created
await project.hasNot('dep-of-pkg-with-1-dep')
await project.hasNot('pkg-with-1-dep')
})
test('import fails when no lockfiles are found', async () => {
prepare(undefined)
await expect(
importCommand.handler({
...DEFAULT_OPTS,
dir: process.cwd(),
}, [])
).rejects.toThrow(
new PnpmError('LOCKFILE_NOT_FOUND', 'No lockfile found')
)
})