This repository was archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserverless.js
85 lines (59 loc) · 2.25 KB
/
serverless.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
78
79
80
81
82
83
84
85
const { Component } = require('@serverless/core')
const {
getTemplate,
resolveTemplate,
getAllComponents,
downloadComponents,
setDependencies,
createGraph,
executeGraph,
syncState,
getOutputs,
createCustomMethodHandler
} = require('./utils')
class Template extends Component {
constructor(id, context) {
const defaultFunction = super(id, context)
return new Proxy(defaultFunction, {
get: (obj, prop) => {
// This handles the weird case when `then` is called on the `defaultFunction`
if (prop === 'then') {
return obj[prop]
}
if (obj.hasOwnProperty(prop)) {
return obj[prop]
}
// Return a function that will invoke the custom method on requested components
return createCustomMethodHandler(obj, prop)
}
})
}
async default(inputs = {}) {
this.context.status('Deploying')
const template = await getTemplate(inputs)
this.context.debug(`Resolving the template's static variables.`)
const resolvedTemplate = resolveTemplate(template)
this.context.debug('Collecting components from the template.')
const allComponents = await getAllComponents(resolvedTemplate)
this.context.debug('Downloading any NPM components found in the template.')
const allComponentsDownloaded = await downloadComponents(allComponents)
this.context.debug(`Analyzing the template's components dependencies.`)
const allComponentsWithDependencies = setDependencies(allComponentsDownloaded)
this.context.debug(`Creating the template's components graph.`)
const graph = createGraph(allComponentsWithDependencies)
this.context.debug('Syncing template state.')
await syncState(allComponentsWithDependencies, this)
this.context.debug(`Executing the template's components graph.`)
const allComponentsWithOutputs = await executeGraph(allComponentsWithDependencies, graph, this)
const outputs = getOutputs(allComponentsWithOutputs)
return outputs
}
async remove() {
this.context.status('Removing')
this.context.debug('Flushing template state and removing all components.')
await syncState({}, this)
// todo should we return the removed components outputs here?!
return {}
}
}
module.exports = Template