codekingpro/portable-devtools
115k
1const pacote = require('pacote')
2
3const formatDiff = require('./format-diff.js')
4const getTarball = require('./tarball.js')
5const untar = require('./untar.js')
6
7// TODO: we test this condition in the diff command
8// so this error probably doesnt need to be here. Or
9// if it does we should figure out a standard code
10// so we can catch it in the cli and display it consistently
11const argsError = () =>
12 Object.assign(
13 new TypeError('libnpmdiff needs two arguments to compare'),
14 { code: 'EDIFFARGS' }
15 )
16const diff = async (specs, opts = {}) => {
17 if (specs.length !== 2) {
18 throw argsError()
19 }
20
21 const [
22 aManifest,
23 bManifest,
24 ] =
25 await Promise.all(specs.map(spec => pacote.manifest(spec, {
26 ...opts,
27 _isRoot: true,
28 })))
29
30 const versions = {
31 a: aManifest.version,
32 b: bManifest.version,
33 }
34
35 // fetches tarball using pacote
36 const [a, b] = await Promise.all([
37 getTarball(aManifest, opts),
38 getTarball(bManifest, opts),
39 ])
40
41 // read all files
42 // populates `files` and `refs`
43 const {
44 files,
45 refs,
46 } = await untar([
47 {
48 prefix: 'a/',
49 item: a,
50 },
51 {
52 prefix: 'b/',
53 item: b,
54 },
55 ], opts)
56
57 return formatDiff({
58 files,
59 opts,
60 refs,
61 versions,
62 })
63}
64
65module.exports = diff
66 