# serverless-core-hooks

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

Latest version **0.0.2-3** (published 2020-03-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install serverless-core-hooks
pnpm add serverless-core-hooks
yarn add serverless-core-hooks
bun add serverless-core-hooks
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2-3 |
| Published | 2020-03-14 |
| First published | 2020-03-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ricardo Aielo |
| Maintainers | aielo |
| Keywords | serverless core hooks, serverless core, serverless hooks, serverless plugin, serverless, core, hooks, plugin |

## Links

- npm: https://www.npmjs.com/package/serverless-core-hooks
- Repository: https://github.com/aielo/serverless-core-hooks
- Homepage: https://github.com/aielo/serverless-core-hooks#readme
- Issues: https://github.com/aielo/serverless-core-hooks/issues
- npm.io page: https://npm.io/package/serverless-core-hooks

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 0.0.2-3 (latest) — 2020-03-14
- 0.0.2-2 — 2020-03-14
- 0.0.2-1 — 2020-03-10
- 0.0.2-0 — 2020-03-09

## README

# serverless-core-hooks ![npm](https://img.shields.io/npm/v/serverless-core-hooks) ![npm](https://img.shields.io/npm/l/serverless-core-hooks)


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](http://www.serverless.com). 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);
  }
...
```
2. 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)
...
```
3. **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
...
```
2. 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;
```
3. 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
- [Hello World](https://github.com/aielo/serverless-core-hooks/tree/master/examples/helloworld)
- [Demo](https://github.com/aielo/serverless-core-hooks/tree/master/examples/demo)
- [Inheritance](https://github.com/aielo/serverless-core-hooks/tree/master/examples/inheritance)

## Author
Ricardo Aielo [@aielo](https://github.com/aielo/)

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

---
_Source: https://npm.io/package/serverless-core-hooks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
