weixu365 / serverless-scriptable-plugin

Adding script support to Serverless 1.x which enables you to customize Serverless behavior without writing a plugin.
MIT License
111 stars 11 forks source link

[Question] serverless-scriptable vs serverless-offline #49

Closed kolya-ay closed 4 years ago

kolya-ay commented 4 years ago

I'm trying to execute a command after serverless-offline is started. I found a place with hooks in the source and try to do:

custom:
  scriptHooks:
    offline:start:end: echo "Hello world!"

Do I understand the logic correctly? Is serverless-scriptable supposed to work with other serverless plugins?

weixu365 commented 4 years ago

It works with serverless-offline plugin, but you need to think about which event do you want to hook to. Here are the settings I have tested:

custom: scriptHooks: before:offline:start:init: echo "Hello world!"

$ npx serverless offline start Running command: echo "Hello world!" Hello world! Serverless: Starting Offline: dev/ap-southeast-2.

Serverless: Routes for test: Serverless: POST /{apiVersion}/functions/demo-dev-test/invocations

Serverless: Offline [HTTP] listening on http://localhost:3000 Serverless: Enter "rp" to replay the last request


The above is the expected behavior.

- Hook to `offline:start:init: echo "Hello world!"` or `after:offline:start:init: echo "Hello world!"`
  then the script will be executed after you stop serverless-offline, in my case, I use `Ctrl + C` to stopped the app.

$ npx serverless offline start Serverless: Starting Offline: dev/ap-southeast-2.

Serverless: Routes for test: Serverless: POST /{apiVersion}/functions/demo-dev-test/invocations

Serverless: Offline [HTTP] listening on http://localhost:3000 Serverless: Enter "rp" to replay the last request ^CServerless: Got SIGINT signal. Offline Halting... Running command: echo "Hello world!" Hello world! Serverless: Halting offline server



I found the following events would work:

1. For command `npx serverless offline start`

    When you start the local server, the following hooks will be executed
      - before:offline:start:init

    When you stop the local server, the following hooks will be executed
    - after:offline:start:init
    - before:offline:start:end 
    - after:offline:start:end

    Reference:
    `start() method is the Entry point for the plugin (sls offline) when running 'sls offline start'`
    https://github.com/dherault/serverless-offline/blob/8187eeefd9512970806386e472fb51bc6b393e88/src/ServerlessOffline.js#L58

2. For command `npx serverless offline`

    When you start the local server, the following hooks will be executed
    - before:offline:start

    Reference:
    `_startWithExplicitEnd() is the Entry point for the plugin (sls offline) when running 'sls offline'`
    https://github.com/dherault/serverless-offline/blob/8187eeefd9512970806386e472fb51bc6b393e88/src/ServerlessOffline.js#L134
kolya-ay commented 4 years ago

It seems you did my dirty work for me;) Thank you for your support! I'm closing the issue. serverless-offline behavior doesn't look too consistent though..