• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const localeCompare = require('@isaacs/string-locale-compare')('en')
2const Arborist = require('../')
3const log = require('./lib/logging.js')
4
5module.exports = (options, time) => {
6  const query = options._.shift()
7  const a = new Arborist(options)
8  return a
9    .loadVirtual()
10    .then(tree => {
11      // only load the actual tree if the virtual one doesn't have modern metadata
12      if (!tree.meta || !(tree.meta.originalLockfileVersion >= 2)) {
13        throw 'load actual'
14      } else {
15        return tree
16      }
17    }).catch((er) => {
18      log.error('loading actual tree', er)
19      return a.loadActual()
20    })
21    .then(time)
22    .then(({ result: tree }) => {
23      const output = []
24      if (!query) {
25        const set = []
26        for (const license of tree.inventory.query('license')) {
27          set.push([tree.inventory.query('license', license).size, license])
28        }
29
30        for (const [count, license] of set.sort((a, b) =>
31          a[1] && b[1] ? b[0] - a[0] || localeCompare(a[1], b[1])
32          : a[1] ? -1
33          : b[1] ? 1
34          : 0)) {
35          output.push(`${count} ${license}`)
36          log.info(count, license)
37        }
38      } else {
39        for (const node of tree.inventory.query('license', query === 'undefined' ? undefined : query)) {
40          const msg = `${node.name} ${node.location} ${node.package.description || ''}`
41          output.push(msg)
42          log.info(msg)
43        }
44      }
45
46      return output.join('\n')
47    })
48}
49