-
Notifications
You must be signed in to change notification settings - Fork 181
/
removeGraph.js
77 lines (70 loc) · 2.38 KB
/
removeGraph.js
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
import { contains, get, isEmpty } from '@serverless/utils'
import resolveComponentEvaluables from '../component/resolveComponentEvaluables'
import execGraph from './execGraph'
const validateNode = (node, context) => {
const { nextInstance, prevInstance, instanceId, operation } = node
if (contains(operation, ['remove', 'replace'])) {
if (!prevInstance && !nextInstance) {
throw new Error(`removeGraph expected prevInstance to be defined for ${operation} operation`)
}
if (!instanceId) {
throw new Error(`removeGraph expected instanceId to be defined for ${operation} operation`)
}
if (nextInstance && nextInstance.name === undefined) {
context.debug(`This instance has an undefined name`)
context.debug(nextInstance)
}
if (prevInstance && prevInstance.name === undefined) {
context.debug(`This instance has an undefined name`)
context.debug(prevInstance)
}
}
}
const removeInstance = async (instance, context) => {
context.debug(`removing component: ${instance.name} { instanceId: ${instance.instanceId} }`)
await instance.remove(context)
context.debug(
`component removal complete: ${instance.name} { instanceId: ${instance.instanceId} }`
)
}
const removeNode = async (node, context) => {
const { instanceId, operation } = node
let { nextInstance, prevInstance } = node
if (!isEmpty(nextInstance)) {
nextInstance = resolveComponentEvaluables(nextInstance)
}
if (!isEmpty(prevInstance)) {
prevInstance = resolveComponentEvaluables(prevInstance)
}
context.debug(
`checking if component should be removed - operation: ${operation} instanceId: ${instanceId} nextInstance: ${get(
'name',
nextInstance
)} prevInstance: ${get('name', prevInstance)}`
)
validateNode(node, context)
if (contains(operation, ['remove', 'replace'])) {
if (operation === 'replace') {
if (prevInstance) {
await removeInstance(prevInstance, context)
}
} else if (operation === 'remove') {
if (prevInstance) {
await removeInstance(prevInstance, context)
} else if (nextInstance) {
await removeInstance(nextInstance, context)
}
}
}
}
const nextNodeIds = (graph) => graph.sources()
const removeGraph = async (graph, context) =>
execGraph(
{
iteratee: removeNode,
next: nextNodeIds
},
graph,
context
)
export default removeGraph