rebuild.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. module.exports = rebuild
  2. var readInstalled = require('read-installed')
  3. var semver = require('semver')
  4. var log = require('npmlog')
  5. var npm = require('./npm.js')
  6. var npa = require('npm-package-arg')
  7. var usage = require('./utils/usage')
  8. var output = require('./utils/output.js')
  9. rebuild.usage = usage(
  10. 'rebuild',
  11. 'npm rebuild [[<@scope>/<name>]...]'
  12. )
  13. rebuild.completion = require('./utils/completion/installed-deep.js')
  14. function rebuild (args, cb) {
  15. var opt = { depth: npm.config.get('depth'), dev: true }
  16. readInstalled(npm.prefix, opt, function (er, data) {
  17. log.info('readInstalled', typeof data)
  18. if (er) return cb(er)
  19. var set = filter(data, args)
  20. var folders = Object.keys(set).filter(function (f) {
  21. return f !== npm.prefix
  22. })
  23. if (!folders.length) return cb()
  24. log.silly('rebuild set', folders)
  25. cleanBuild(folders, set, cb)
  26. })
  27. }
  28. function cleanBuild (folders, set, cb) {
  29. npm.commands.build(folders, function (er) {
  30. if (er) return cb(er)
  31. output(folders.map(function (f) {
  32. return set[f] + ' ' + f
  33. }).join('\n'))
  34. cb()
  35. })
  36. }
  37. function filter (data, args, set, seen) {
  38. if (!set) set = {}
  39. if (!seen) seen = new Set()
  40. if (set.hasOwnProperty(data.path)) return set
  41. if (seen.has(data)) return set
  42. seen.add(data)
  43. var pass
  44. if (!args.length) pass = true // rebuild everything
  45. else if (data.name && data._id) {
  46. for (var i = 0, l = args.length; i < l; i++) {
  47. var arg = args[i]
  48. var nv = npa(arg)
  49. var n = nv.name
  50. var v = nv.rawSpec
  51. if (n !== data.name) continue
  52. if (!semver.satisfies(data.version, v, true)) continue
  53. pass = true
  54. break
  55. }
  56. }
  57. if (pass && data._id) {
  58. log.verbose('rebuild', 'path, id', [data.path, data._id])
  59. set[data.path] = data._id
  60. }
  61. // need to also dive through kids, always.
  62. // since this isn't an install these won't get auto-built unless
  63. // they're not dependencies.
  64. Object.keys(data.dependencies || {}).forEach(function (d) {
  65. // return
  66. var dep = data.dependencies[d]
  67. if (typeof dep === 'string') return
  68. filter(dep, args, set, seen)
  69. })
  70. return set
  71. }