Skip to content
This repository was archived by the owner on Nov 6, 2021. It is now read-only.

Conversation

@franciscocpg
Copy link

Fixes #22

@azurelogic
Copy link
Contributor

Can you confirm that this works when there are multiple functions and the target function isn't the first one?

@franciscocpg
Copy link
Author

Yes, I'll do it tomorrow.

Also I'm going to try with serverless-offline plugin.

@franciscocpg
Copy link
Author

@azurelogic
You were totally right about multiple functions.

So the best approach I found was to inject process.env._HANDLER just like AWS is doing on its environments.

Maybe @HyperBrain could give a quick look here too?

plugin/index.js Outdated

// process.env.IS_LOCAL === 'true' is set when called by 'sls invoke local'
if (process.env._HANDLER === undefined && process.env.IS_LOCAL === 'true') {
const invokedFunction = functions[this.serverless.processedInput.options.function]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this.options already the processed option object given to the plugin? If that's the case using this.options would decouple the implementation from Serverless internals.

Copy link
Author

@franciscocpg franciscocpg Dec 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap.

But there is a little difference.
If I call sls invoke local --function some-function:

this.options {
  "function": "some-function"
}
this.serverless.processedInput.options {
  "commands": [
    "invoke",
    "local"
  ],
  "options": {
    "function": "some-function"
  }
}

And If I call sls invoke local -f some-function:

this.options {
  "f": "some-function"
}
this.serverless.processedInput.options {
  "commands": [
    "invoke",
    "local"
  ],
  "options": {
    "f": "some-function",
    "function": "some-function"
  }
}

So using this.serverless.processedInput.options we always have the function key.

In any case I could also do something like this:

const invokedFunction = functions[this.options.f || this.options.function]

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... That's strange also serverless-webpack uses this.options.function throughout the whole implementation. I was quite sure that the pluginManager would populate the long version in any case (regardless if you specify -f or --function). At least that was the handling for a long time.
However, the const invokedFunction = ... looks good otherwise.

Copy link
Author

@franciscocpg franciscocpg Dec 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HyperBrain
You're right.

I was calling JSON.stringify inside the plugin constructor.

But if I call it later then the results are:

"options": {
    "f": "some-function",
    "function": "some-function"
  }

It looks like serverless normalize it somehow, but I didn't take a deep look why this is happening.

So it's safe to use this.options.function as long as you are not using this in the plugin constructor.

Copy link
Contributor

@HyperBrain HyperBrain Dec 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why it is not yet normalized in the constructor is, that Serverless first loads the plugins and then starts the lifecycles of the invoked command. One of Serverless' lifecycle steps is the normalization (most likely in validate), but that can only be run as soon as Serverless knows the command (and thus the supported options, declared in the command structure).
So the same is true for any plugin - resolved and initialized properties (like options) are only available within the hooks, but not the constructor.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see.

Thanks for the explanation @HyperBrain

@HyperBrain
Copy link
Contributor

In general, I think for the invoke local case, this would work (although it should be tested thoroughly first).

@franciscocpg
Copy link
Author

@azurelogic
I think the PR is ready for review now.

@franciscocpg
Copy link
Author

Any news here?

@ghost
Copy link

ghost commented Jun 18, 2018

Waiting for this PR to be merged..

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants