Welcome to mirror list, hosted at ThFree Co, Russian Federation.

edge.js « lib « arborist « @npmcli « node_modules - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a0bb7a432feefdbabb8f4c3c144ef0caba252ea2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// An edge in the dependency graph
// Represents a dependency relationship of some kind

const npa = require('npm-package-arg')
const depValid = require('./dep-valid.js')
const _from = Symbol('_from')
const _to = Symbol('_to')
const _type = Symbol('_type')
const _spec = Symbol('_spec')
const _accept = Symbol('_accept')
const _name = Symbol('_name')
const _error = Symbol('_error')
const _loadError = Symbol('_loadError')
const _setFrom = Symbol('_setFrom')

const types = new Set([
  'prod',
  'dev',
  'optional',
  'peer',
  'peerOptional',
  'workspace'
])

class Edge {
  constructor (options) {
    const { type, name, spec, accept, from } = options

    if (typeof spec !== 'string')
      throw new TypeError('must provide string spec')

    if (type === 'workspace' && npa(spec).type !== 'directory')
      throw new TypeError('workspace edges must be a symlink')

    this[_spec] = spec

    if (accept !== undefined) {
      if (typeof accept !== 'string')
        throw new TypeError('accept field must be a string if provided')
      this[_accept] = accept || '*'
    }

    if (typeof name !== 'string')
      throw new TypeError('must provide dependency name')
    this[_name] = name

    if (!types.has(type)) {
      throw new TypeError(
        `invalid type: ${type}\n` +
        `(valid types are: ${Edge.types.join(', ')})`)
    }
    this[_type] = type
    if (!from)
      throw new TypeError('must provide "from" node')
    this[_setFrom](from)
    this[_error] = this[_loadError]()
  }

  satisfiedBy (node) {
    return depValid(node, this.spec, this.accept, this.from)
  }

  get dev () {
    return this[_type] === 'dev'
  }

  get optional () {
    return this[_type] === 'optional' || this[_type] === 'peerOptional'
  }

  get peer () {
    return this[_type] === 'peer' || this[_type] === 'peerOptional'
  }

  get type () {
    return this[_type]
  }

  get name () {
    return this[_name]
  }

  get spec () {
    return this[_spec]
  }

  get accept () {
    return this[_accept]
  }

  get valid () {
    return !this.error
  }

  get error () {
    this[_error] = this[_error] || this[_loadError]()
    return this[_error] === 'OK' ? null : this[_error]
  }

  [_loadError] () {
    return !this[_to] ? (this.optional ? null : 'MISSING')
      : this.peer &&
          this.from === this.to.parent &&
          !this.from.isTop
        ? 'PEER LOCAL'
      : !depValid(this.to, this.spec, this.accept, this.from)
        ? 'INVALID'
      : 'OK'
  }

  reload (hard = false) {
    const newTo = this[_from].resolve(this.name)
    if (newTo !== this[_to]) {
      if (this[_to])
        this[_to].edgesIn.delete(this)
      this[_to] = newTo
      this[_error] = this[_loadError]()
      if (this[_to])
        this[_to].addEdgeIn(this)
    } else if (hard)
      this[_error] = this[_loadError]()
  }

  detach () {
    if (this[_to])
      this[_to].edgesIn.delete(this)
    this[_from].edgesOut.delete(this.name)
    this[_to] = null
    this[_error] = 'DETACHED'
    this[_from] = null
  }

  [_setFrom] (node) {
    this[_from] = node
    if (node.edgesOut.has(this.name))
      node.edgesOut.get(this.name).detach()
    node.addEdgeOut(this)
    this.reload()
  }

  get from () {
    return this[_from]
  }

  get to () {
    return this[_to]
  }
}

Edge.types = [...types]
Edge.errors = [
  'DETACHED',
  'MISSING',
  'PEER LOCAL',
  'INVALID',
]

module.exports = Edge