• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, opts)))
26
27  const versions = {
28    a: aManifest.version,
29    b: bManifest.version,
30  }
31
32  // fetches tarball using pacote
33  const [a, b] = await Promise.all([
34    getTarball(aManifest, opts),
35    getTarball(bManifest, opts),
36  ])
37
38  // read all files
39  // populates `files` and `refs`
40  const {
41    files,
42    refs,
43  } = await untar([
44    {
45      prefix: 'a/',
46      item: a,
47    },
48    {
49      prefix: 'b/',
50      item: b,
51    },
52  ], opts)
53
54  return formatDiff({
55    files,
56    opts,
57    refs,
58    versions,
59  })
60}
61
62module.exports = diff
63