-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Plugins): Support variables in configuration extensions (#11558)
- Loading branch information
Showing
7 changed files
with
237 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<!-- | ||
title: Serverless Framework - Plugins - Extending and overriding the configuration | ||
menuText: Extending and overriding configuration | ||
menuOrder: 6 | ||
description: How to extend and override configuration via a plugin | ||
layout: Doc | ||
--> | ||
|
||
<!-- DOCS-SITE-LINK:START automatically generated --> | ||
|
||
### [Read this on the main serverless docs site](https://www.serverless.com/framework/docs/guides/plugins/extending-configuration) | ||
|
||
<!-- DOCS-SITE-LINK:END --> | ||
|
||
# Extending and overriding configuration | ||
|
||
Plugins can extend and override the internal configuration. | ||
|
||
To do so, plugins may use the `serverless.extendConfiguration(...)` method. | ||
This is only allowed at pre-init stage of serverless. | ||
The method also takes care of resolving all variables in the given value. But it **does not validate you input** nor the target. Improper usage can cause serverless to fail. | ||
|
||
The `serverless.extendConfiguration(configurationPathKeys, value)` method takes two arguments. | ||
|
||
| Argument | Type | Description | | ||
| ----------------------- | ------------------------- | ------------------------------------------------------------------ | | ||
| `configurationPathKeys` | string[] | Path of the configuration property to set; must not be empty | | ||
| `value` | string \| object \| array | New value of the configuration property in `configurationPathKeys` | | ||
|
||
If configuration in `configurationPathKeys` **does exist** the value will be overwritten. | ||
If configuration in `configurationPathKeys` **does not exist** the whole path will be created. | ||
|
||
You can use it in plugin constructor, or if for some reason configuration extension is resolved asynchronously you may resort to `asyncInit()` method | ||
|
||
```js | ||
class MyPlugin { | ||
constructor(serverless) { | ||
this.serverless = serverless; | ||
|
||
const value = { | ||
myKey: 'myValue', | ||
}; | ||
this.serverless.extendConfiguration(['custom', 'myPlugin'], value); | ||
} | ||
} | ||
|
||
module.exports = MyPlugin; | ||
``` | ||
|
||
If your plugin needs merging you need to take care of it yourself. | ||
|
||
```js | ||
class MyPlugin { | ||
constructor(serverless) { | ||
this.serverless = serverless; | ||
|
||
const currentConfig = this.serverless.configurationInput.custom.myPlugin; | ||
const value = Object.assign(currentConfig, { | ||
myKey: 'myValue', | ||
}); | ||
this.serverless.extendConfiguration(['custom', 'myPlugin'], value); | ||
} | ||
} | ||
|
||
module.exports = MyPlugin; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
test/fixtures/programmatic/plugin/extend-config-plugin/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const pluginConfig = { | ||
targetValuePath: ['custom', 'extend', 'value'], | ||
overwriteValuePath: ['custom', 'extend', 'overwrite'], | ||
afterInitValuePath: ['custom', 'extend', 'afterInit'], | ||
refValuePath: ['custom', 'extend', 'ref'], | ||
}; | ||
|
||
module.exports = class TestPlugin { | ||
constructor(serverless, options, utils) { | ||
this.serverless = serverless; | ||
this.options = options; | ||
this.utils = utils; | ||
|
||
this.hooks = { | ||
initialize: () => this.extendAfterInit(), | ||
}; | ||
} | ||
|
||
async asyncInit() { | ||
const configExt = { | ||
var: 'value', | ||
}; | ||
this.serverless.extendConfiguration(pluginConfig.targetValuePath, configExt); | ||
this.serverless.extendConfiguration(pluginConfig.overwriteValuePath, configExt); | ||
this.serverless.extendConfiguration(pluginConfig.refValuePath, '${self:custom.extend.value}'); | ||
|
||
try { | ||
this.serverless.extendConfiguration([], { custom: {} }); | ||
} catch (error) { | ||
// ignore this | ||
} | ||
|
||
try { | ||
this.serverless.extendConfiguration('custom.target.invalid', {}); | ||
} catch (error) { | ||
// ignore this | ||
} | ||
} | ||
|
||
extendAfterInit() { | ||
try { | ||
this.serverless.extendConfiguration(pluginConfig.afterInitValuePath, 'value'); | ||
} catch (error) { | ||
// ignore this | ||
} | ||
} | ||
}; | ||
|
||
module.exports.pluginConfig = pluginConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters