• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2module.exports = installedShallow
3
4var npm = require('../../npm.js')
5var fs = require('graceful-fs')
6var path = require('path')
7var readJson = require('read-package-json')
8var asyncMap = require('slide').asyncMap
9
10function installedShallow (opts, filter, cb) {
11  if (typeof cb !== 'function') {
12    cb = filter
13    filter = null
14  }
15  var conf = opts.conf
16  var args = conf.argv.remain
17  if (args.length > 3) return cb()
18  var local
19  var global
20  var localDir = npm.dir
21  var globalDir = npm.globalDir
22  if (npm.config.get('global')) {
23    local = []
24    next()
25  } else {
26    fs.readdir(localDir, function (er, pkgs) {
27      local = (pkgs || []).filter(function (p) {
28        return p.charAt(0) !== '.'
29      })
30      next()
31    })
32  }
33
34  fs.readdir(globalDir, function (er, pkgs) {
35    global = (pkgs || []).filter(function (p) {
36      return p.charAt(0) !== '.'
37    })
38    next()
39  })
40  function next () {
41    if (!local || !global) return
42    filterInstalled(local, global, filter, cb)
43  }
44}
45
46function filterInstalled (local, global, filter, cb) {
47  var fl
48  var fg
49
50  if (!filter) {
51    fl = local
52    fg = global
53    return next()
54  }
55
56  asyncMap(local, function (p, cb) {
57    readJson(path.join(npm.dir, p, 'package.json'), function (er, d) {
58      if (!d || !filter(d)) return cb(null, [])
59      return cb(null, d.name)
60    })
61  }, function (er, local) {
62    fl = local || []
63    next()
64  })
65
66  var globalDir = npm.globalDir
67  asyncMap(global, function (p, cb) {
68    readJson(path.join(globalDir, p, 'package.json'), function (er, d) {
69      if (!d || !filter(d)) return cb(null, [])
70      return cb(null, d.name)
71    })
72  }, function (er, global) {
73    fg = global || []
74    next()
75  })
76
77  function next () {
78    if (!fg || !fl) return
79    if (!npm.config.get('global')) {
80      fg = fg.map(function (g) {
81        return [g, '-g']
82      })
83    }
84    console.error('filtered', fl, fg)
85    return cb(null, fl.concat(fg))
86  }
87}
88