/
index.js
57 lines (49 loc) · 1.02 KB
/
index.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
const pacote = require('pacote')
const formatDiff = require('./lib/format-diff.js')
const getTarball = require('./lib/tarball.js')
const untar = require('./lib/untar.js')
const argsError = () =>
Object.assign(
new TypeError('libnpmdiff needs two arguments to compare'),
{ code: 'EDIFFARGS' }
)
const diff = async (specs, opts = {}) => {
if (specs.length !== 2)
throw argsError()
const [
aManifest,
bManifest,
] =
await Promise.all(specs.map(spec => pacote.manifest(spec, opts)))
const versions = {
a: aManifest.version,
b: bManifest.version,
}
// fetches tarball using pacote
const [a, b] = await Promise.all([
getTarball(aManifest, opts),
getTarball(bManifest, opts),
])
// read all files
// populates `files` and `refs`
const {
files,
refs,
} = await untar([
{
prefix: 'a/',
item: a,
},
{
prefix: 'b/',
item: b,
},
], opts)
return formatDiff({
files,
opts,
refs,
versions,
})
}
module.exports = diff