Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Commit 4d52f4f

Browse files
authored
Merge pull request #9 from Pavel910/master
Fix programmatic method invocation.
2 parents 0b70bfb + af70e31 commit 4d52f4f

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,90 @@
11
# Template
2+
3+
This component is used behind the scenes to deploy your YAML templates. It is loaded by the serverless CLI and executed just as any other component.
4+
However, there's a lot you can do with this component programmatically. See the sections below for some examples.
5+
6+
### Programmatic usage and custom environments
7+
8+
To deploy to multiple environments you must utilize the programmatic API, via `serverless.js` file.
9+
10+
`serverless.js`
11+
12+
```js
13+
const { Component } = require('@serverless/core')
14+
15+
class Deploy extends Component {
16+
async default(inputs = {}) {
17+
const { env } = inputs
18+
19+
const template = await this.load('@serverless/template', env)
20+
return await template({ template: __dirname + '/serverless.yml' })
21+
}
22+
23+
async remove(inputs = {}) {
24+
const { env } = inputs
25+
26+
const template = await this.load('@serverless/template', env)
27+
await template.remove(inputs)
28+
}
29+
}
30+
31+
module.exports = Deploy
32+
```
33+
34+
`serverless.yml`
35+
36+
```yml
37+
name: test
38+
39+
lambda:
40+
component: '@serverless/function'
41+
inputs:
42+
name: my-function
43+
description: My Serverless Function
44+
memory: 128
45+
timeout: 20
46+
code: './code'
47+
hanlder": 'handler.handler'
48+
region: us-east-1
49+
runtime: nodejs10.x
50+
```
51+
52+
Invoking `sls --env=dev` will result in state files in `.serverless/` being prefixed with the value of your `env`:
53+
`Deploy.dev.json`, etc. That way you can deploy unlimited environments, add pre/post processing, load whatever `.env` you need, etc.
54+
55+
### Running custom methods on a template
56+
57+
A template itself does not contain any methods so custom methods are executed on the specific template aliases.
58+
59+
`sls install --component lambda` will load the `lambda` alias from your template, instantiate the corresponding component, and execute the `install` method passing in the inputs you specify via CLI parameters.
60+
Inputs from the template itself are not passed as they can not be interpreted/resolved without running the entire template.
61+
62+
You can pass as many `--component` parameters as you need.
63+
64+
### Running custom methods programmatically
65+
66+
`serverless.js`
67+
68+
```js
69+
const { Component } = require('@serverless/core')
70+
71+
class Deploy extends Component {
72+
/* ...skipped default method for brevity ... */
73+
74+
async install(inputs = {}) {
75+
const template = await this.load('@serverless/template')
76+
await template.install({ template: __dirname + '/serverless.yml', ...inputs })
77+
}
78+
}
79+
80+
module.exports = Deploy
81+
```
82+
83+
When invoking methods on a `template` instance, you always need to pass in a path to the template file or a template object.
84+
85+
Running `sls install --component lambda --debug` - this will load the template, find the `lambda` alias, and invoke the `install` method on the instance of the component.
86+
87+
### Couldn't find what you were looking for?
88+
89+
Visit our github and file an issue with as much info as possible about your problem.
90+
If you think you've found a bug - please do post a complete reproduction repo. It will help us and our contributors to help you much faster.

serverless.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class Template extends Component {
1818

1919
return new Proxy(defaultFunction, {
2020
get: (obj, prop) => {
21+
// This handles the weird case when `then` is called on the `defaultFunction`
22+
if (prop === 'then') {
23+
return obj[prop]
24+
}
25+
2126
if (obj.hasOwnProperty(prop)) {
2227
return obj[prop]
2328
}

utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,12 @@ const createCustomMethodHandler = (instance, method) => {
353353
instance.context.debug(
354354
`Attempting to run method "${method}" on template aliases: ${components.join(', ')}`
355355
)
356+
357+
// Make sure the template is an object
358+
const templateObject = await getTemplate({ template })
359+
356360
// Load template components
357-
const templateComponents = await getAllComponents(template)
361+
const templateComponents = await getAllComponents(templateObject)
358362

359363
// Get only the requested components ("component" input)
360364
const componentsToRun = Object.keys(templateComponents)

0 commit comments

Comments
 (0)