npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serverless-core-hooks

v0.0.2-3

Published

A plugin which enables hooks in events of core serverless objects

Downloads

7

Readme

serverless-core-hooks npm npm

A plugin which enables hooks in events of core serverless objects.

First things first - serverless-core-hooks enables other plugins. It does not accomplish much by itself.

Having said that, it allows your own plugin to hook into (almost) any point of the the Serverless Framework. A good understanding of serverless and its flow might empower the use of serverless-core-hooks (i.e. YMMV).

What do I need this for?

Let's say you needed to audit a few steps which happen during initialization (serverless.init). For example, when and which plugins are loaded (pluginManager.addPlugin). There are no available hooks for that - they will be loaded in there.

Or imagine you want to add or change information to the configuration loaded from serverless.yml on runtime. Unless it's something as simple as an arg (opt) or environment variable (env), you have to switch configuration from YML to JS (e.g. serverless.js) to have a more dynamic capability.

The purpose of serverless-core-hooks is to provide means for your plugins to handle those types of requirement. The latter is actually what motivated its creation.

How does it work?

You can either inherit or use it directly from serverless configuration. Both ways are described below in more details.

Inheritance

  1. You create your own plugin, extending serverless-core-hooks:
const CoreHooks = require("serverless-core-hooks");
class MyPlugin extends CoreHooks {
  constructor(sls) {
    super(sls);
  }
...
  1. Then you override the configure method to specify which core object(s) you want to hook:
...
  configure() {
    super.configure();
    this.config.core.push("serverless", "cli"); // adds (!replace)
...
  1. Core hooks might also be added:
...
    Object.assign(this.hooks, { // adds/merges (!replace)
      "before:serverless:init": this.myHook.bind(this),
      "after:serverless:run": this.myHook.bind(this),
    });
  }
  myHook(event, trigger) {
    this.log("core hooking some methods");
  }
}
module.exports = MyPlugin;

Direct serverless configuration

  1. You specify which core object(s) you want to hook via configuration:
...
custom:
  coreHooks:
    objects:
      - serverless
      - pluginManager
...
  1. Then you setup and use core hooks in your plugin, on a similar fashion to regular ones:
class MyPlugin {
  constructor(sls) {
    this.sls = sls;
    this.hooks = {
      "after:serverless:run": this.myHook.bind(this)
    };
  }
  myHook(event, trigger) {
    this.sls.cli.log("Serverless run ended");
  }
}
module.exports = MyPlugin;
  1. Done! Actually, do not forget to add both plugins to your configuration:
...
plugins:
  - serverless-core-hooks
  - my-plugin
...

Which objects can I hook?

Currently, it is possible to hook the serverless instance and all its children - we call them core objects here. They are listed below:

  • cli
  • pluginManager
  • serverless
  • service
  • utils
  • variables
  • yamlParser

Examples

Author

Ricardo Aielo @aielo

License

serverless-core-hooks is licensed under the ISC License.