node.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict'
  2. var defaultTemplate = {
  3. package: {
  4. version: '',
  5. dependencies: {},
  6. devDependencies: {},
  7. optionalDependencies: {}
  8. },
  9. loaded: false,
  10. children: [],
  11. requiredBy: [],
  12. requires: [],
  13. missingDeps: {},
  14. missingDevDeps: {},
  15. phantomChildren: {},
  16. path: null,
  17. realpath: null,
  18. location: null,
  19. userRequired: false,
  20. save: false,
  21. saveSpec: null,
  22. isTop: false,
  23. fromBundle: false
  24. }
  25. function isLink (node) {
  26. return node && node.isLink
  27. }
  28. function isInLink (node) {
  29. return node && (node.isInLink || node.isLink)
  30. }
  31. var create = exports.create = function (node, template, isNotTop) {
  32. if (!template) template = defaultTemplate
  33. Object.keys(template).forEach(function (key) {
  34. if (template[key] != null && typeof template[key] === 'object' && !(template[key] instanceof Array)) {
  35. if (!node[key]) node[key] = {}
  36. return create(node[key], template[key], true)
  37. }
  38. if (node[key] != null) return
  39. node[key] = template[key]
  40. })
  41. if (!isNotTop) {
  42. // isLink is true for the symlink and everything inside it.
  43. // by contrast, isInLink is true for only the things inside a link
  44. if (node.isLink == null) node.isLink = isLink(node.parent)
  45. if (node.isInLink == null) node.isInLink = isInLink(node.parent)
  46. if (node.fromBundle == null) {
  47. node.fromBundle = false
  48. }
  49. }
  50. return node
  51. }
  52. exports.reset = function (node) {
  53. reset(node, new Set())
  54. }
  55. function reset (node, seen) {
  56. if (seen.has(node)) return
  57. seen.add(node)
  58. var child = create(node)
  59. // FIXME: cleaning up after read-package-json's mess =(
  60. if (child.package._id === '@') delete child.package._id
  61. child.isTop = false
  62. child.requiredBy = []
  63. child.requires = []
  64. child.missingDeps = {}
  65. child.missingDevDeps = {}
  66. child.phantomChildren = {}
  67. child.location = null
  68. child.children.forEach(function (child) { reset(child, seen) })
  69. }