Awesome
Serverless YAML extension parser
This tiny library will parse YAML extensions used in serverless framework.
Usage
npm install sls-yaml
import yaml from "sls-yaml";
const compiledYamlAsJsonObject = yaml(YAML_FILE_PATH_OR_BUFFER);
Supported sls extensions
- Include external file
${file(path/to/file.yml)}
- Inject environment variable
${env:NODE_ENV}
- Inject global variables
${global:path.to.variable}
- Inject local variables
${self:path.to.variable}
New extensions*
- Inject current git branch
${git:branch}
- Inject last git commit hash
${git:sha1}
- String replace
${replace(str, searchPattern, replaceValue )}
* - New extension not present in serverless yaml
Custom extensions
const context = {
custom: ([arg]: string[]) => {
return `${arg}-beta`;
}
};
const result = yaml(content, null, context);
name: service
version: v1.0.2
subset: service@${custom(${self:version})}
- output
name: service
version: v1.0.2
subset: service@v1.0.2-beta
Include external file
This extension will include content of external yaml files.
- config.yml
version: 1
env: dev
config: ${file(./common.yml)}
- common.yml
endpoint: http://service-url
- Generated final yaml
version: 1
env: dev
config:
endpoint: http://service-url
Inject environment variable
This extension will inject envronment values
- config.yml
export NODE_ENV = development
version: 1
env: ${env:NODE_ENV}
- Generated final yaml
version: 1
env: development
Inject global variables
This extension will inject variable from global scope.
- config.yml
version: 1
env: stage
config: ${file(./common.yml)}
- common.yml
endpoint: http://service-${global:env}
- Generated final yaml
version: 1
env: stage
config:
endpoint: http://service-stage
Inject local variables
This extension will inject variable from local scope.
- config.yml
version: 1
env: stage
config: ${file(./common.yml)}
- common.yml
port: 8080
endpoint: http://service:${self:port}
- Generated final yaml
version: 1
env: stage
config:
port: 8080
endpoint: http://service:8080
Inject current git branch
This extension will inject current git branch name
- config.yml
branch: ${git:branch}
Inject last git commit hash
This extension will inject last git commit hash
- config.yml
image.tag: ${git:sha1}
String replace
replace(str:string, searchPattern:RegExp|string, replaceValue:string )
This extension will returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced.
- config.yml
version: v1.0.0
name: ServiceName@${replace(${ self : version }, /\\./gi, - )}
- output
version: v1.0.0
name: ServiceName@v1-0-0