-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathtarball.js
95 lines (83 loc) · 2.08 KB
/
tarball.js
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
const { resolve } = require('path')
const t = require('tap')
const tar = require('tar')
const pacote = require('pacote')
pacote.tarball = () => {
throw new Error('Failed to detect node_modules tarball')
}
const tarball = require('../lib/tarball.js')
const json = (obj) => `${JSON.stringify(obj, null, 2)}\n`
t.test('returns a tarball from node_modules', async t => {
t.plan(2)
const path = t.testdir({
node_modules: {
a: {
'package.json': json({
name: 'a',
version: '1.0.0',
bin: { a: 'index.js' },
}),
'index.js': '',
},
},
})
const _cwd = process.cwd()
process.chdir(path)
t.teardown(() => {
process.chdir(_cwd)
})
const res = await tarball(
{ bin: { a: 'index.js' }, _resolved: resolve(path, 'node_modules/a') },
{ where: path }
)
tar.list({
filter: p => {
t.match(
p,
/package.json|index.js/,
'should return tarball with expected files'
)
},
}).on('error', e => {
throw e
}).end(res)
})
t.test('node_modules folder within a linked dir', async t => {
const path = t.testdir({
node_modules: {
a: t.fixture('symlink', '../packages/a'),
},
packages: {
a: {
node_modules: {
b: {
'package.json': json({
name: 'a',
version: '1.0.0',
}),
},
},
},
},
})
const link = await tarball({ _resolved: resolve(path, 'node_modules/a/node_modules/b') }, {})
t.ok(link, 'should retrieve tarball from reading link')
const target = await tarball({ _resolved: resolve(path, 'packages/a/node_modules/b') }, {})
t.ok(target, 'should retrieve tarball from reading target')
})
t.test('pkg not in a node_modules folder', async t => {
const path = t.testdir({
packages: {
a: {
'package.json': json({
name: 'a',
version: '1.0.0',
}),
},
},
})
t.throws(
() => tarball({ _resolved: resolve(path, 'packages/a') }, {}),
'should call regular pacote.tarball method instead'
)
})