Skip to content

Commit

Permalink
Merge pull request #28 from gitops-toolbox/feature/add-meta-to-templa…
Browse files Browse the repository at this point in the history
…teContext

Add templateData to rendered mapping
  • Loading branch information
LucaLanziani authored Oct 10, 2021
2 parents 00dea1f + 628f735 commit 0cf349f
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 118 deletions.
141 changes: 84 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ npm i
```
> ./bin/cli.js -b examples list templates # listTemplates
[
"context.js",
"context.njk"
]
```
Expand Down Expand Up @@ -128,12 +129,18 @@ prd:
"tags": {
"type": "application"
},
"templateContext": {
"name": "templator"
"templateData": {
"context": {
"name": "templator"
},
"meta": {
"__mapping": "mappings/nested/example.njk",
"__template": "templates/context.njk"
}
}
},
{
"template": "context.njk",
"template": "context.js",
"contextSelector": "components.database",
"destination": {
"type": "echo",
Expand All @@ -145,8 +152,14 @@ prd:
"tags": {
"type": "database"
},
"templateContext": {
"name": "Database"
"templateData": {
"context": {
"name": "Database"
},
"meta": {
"__mapping": "mappings/nested/example.njk",
"__template": "templates/context.js"
}
}
}
]
Expand All @@ -165,50 +178,52 @@ prd:
}
```

## Render in json format
## Render in yaml format

```
> ./bin/cli.js -b examples generate -o yaml nested/example.njk dev # renderTemplate
locations:
- template: context.njk
contextSelector: components.application
destination:
type: echo
params:
repo: myorg/development
filepath: application.json
tags:
type: application
templateData:
context:
name: templator
meta:
__mapping: mappings/nested/example.njk
__template: templates/context.njk
renderedTemplate: |
# Template file templates/context.njk
# Mapping file mappings/nested/example.njk
{"name":"templator"}
- template: context.js
contextSelector: components.database
destination:
type: echo
params:
repo: myorg/development
filepath: database.json
tags:
type: database
templateData:
context:
name: Database
meta:
__mapping: mappings/nested/example.njk
__template: templates/context.js
renderedTemplate: |
# Template file templates/context.js
# Mapping file mappings/nested/example.njk
{"name":"Database"}
```
> ./bin/cli.js -b examples generate -o json nested/example.njk dev # renderTemplate
{
"locations": [
{
"template": "context.njk",
"contextSelector": "components.application",
"destination": {
"type": "echo",
"params": {
"repo": "myorg/development",
"filepath": "application.json"
}
},
"tags": {
"type": "application"
},
"templateContext": {
"name": "templator"
},
"renderedTemplate": "# Template file templates/context.njk\n# Mapping file mappings/nested/example.njk\n\n{\"name\":\"templator\"}\n"
},
{
"template": "context.njk",
"contextSelector": "components.database",
"destination": {
"type": "echo",
"params": {
"repo": "myorg/development",
"filepath": "database.json"
}
},
"tags": {
"type": "database"
},
"templateContext": {
"name": "Database"
},
"renderedTemplate": "# Template file templates/context.njk\n# Mapping file mappings/nested/example.njk\n\n{\"name\":\"Database\"}\n"
}
]
}
```

## Render in human readable format
Expand All @@ -226,7 +241,7 @@ prd:
---
{"destination":{"type":"echo","params":{"repo":"myorg/development","filepath":"database.json"}},"tags":{"type":"database"}}
---
# Template file templates/context.njk
# Template file templates/context.js
# Mapping file mappings/nested/example.njk
{"name":"Database"}
Expand All @@ -240,7 +255,7 @@ prd:
---
{"destination":{"type":"echo","params":{"repo":"myorg/development","filepath":"database.json"}},"tags":{"type":"database"}}
---
# Template file templates/context.njk
# Template file templates/context.js
# Mapping file mappings/nested/example.njk
{"name":"Database"}
Expand All @@ -251,7 +266,7 @@ prd:

```
> ./bin/cli.js -b examples generate nested/example.njk dev -h --limit-to '{"type": "database"}' --hide-headers # renderFileContent
# Template file templates/context.njk
# Template file templates/context.js
# Mapping file mappings/nested/example.njk
{"name":"Database"}
Expand All @@ -278,13 +293,19 @@ prd:
"tags": {
"type": "application"
},
"templateContext": {
"name": "templator"
"templateData": {
"context": {
"name": "templator"
},
"meta": {
"__mapping": "mappings/nested/example.njk",
"__template": "templates/context.njk"
}
},
"renderedTemplate": "# Template file templates/context.njk\n# Mapping file mappings/nested/example.njk\n\n{\"name\":\"templator\"}\n"
},
{
"template": "context.njk",
"template": "context.js",
"contextSelector": "components.database",
"destination": {
"type": "echo",
Expand All @@ -296,10 +317,16 @@ prd:
"tags": {
"type": "database"
},
"templateContext": {
"name": "Database"
"templateData": {
"context": {
"name": "Database"
},
"meta": {
"__mapping": "mappings/nested/example.njk",
"__template": "templates/context.js"
}
},
"renderedTemplate": "# Template file templates/context.njk\n# Mapping file mappings/nested/example.njk\n\n{\"name\":\"Database\"}\n"
"renderedTemplate": "# Template file templates/context.js\n# Mapping file mappings/nested/example.njk\n\n{\"name\":\"Database\"}\n"
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion cmds/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ exports.handler = async (args) => {
args.mapping,
args.contextSelector
);
await templator.expandTemplatesContext(mapping, context);

await templator.expandTemplatesContext(mapping, context, args.mapping);

output({ mapping, context }, args.output || 'json');
return;
Expand Down
3 changes: 2 additions & 1 deletion examples/mappings/nested/example.njk
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{# You can access the context with 'this' #}
{# the output should be a valid json #}
{% set comma = joiner() %}
{% set templates = ['njk', 'js'] %}
{
"locations": [
{%- for component, data in context.components %}
{{comma()}}{
"template": "context.njk",
"template": "context.{{templates[loop.index-1]}}",
"contextSelector": "components.{{ component }}",
"destination": {
"type": "echo",
Expand Down
7 changes: 7 additions & 0 deletions examples/templates/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.render = function (context, meta) {
return `# Template file ${meta.__template}
# Mapping file ${meta.__mapping}
${JSON.stringify(context)}
`;
};
4 changes: 0 additions & 4 deletions lib/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ class Mappings extends Templates {
}
}

list() {
return utils.listFiles(this.absTemplatesPath);
}

async render(template, context) {
const result = await super.render(template, context);

Expand Down
38 changes: 14 additions & 24 deletions lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ class Templates {
this.base = base;

log('options %o', options);
this.templatesDir = options.templatesDir || 'templates';

this.absTemplatesPath = path.resolve(
base,
options.templatesDir || 'templates'
);
this.absTemplatesPath = path.resolve(base, this.templatesDir);

nunjucks.installJinjaCompat();
this.njk = nunjucks.configure(this.absTemplatesPath, {
Expand All @@ -27,15 +25,19 @@ class Templates {
});
}

getAbsTemplatePath(template) {
absPath(template) {
return path.resolve(this.absTemplatesPath, template);
}

relPath(template) {
return path.join(this.templatesDir, template);
}

list() {
return utils.listFiles(this.absTemplatesPath);
}

async _renderJs(templatePath, context, mappingFilepath) {
async _renderJs(templatePath, context, meta) {
const jsTemplate = require(templatePath);

if (jsTemplate.render == undefined) {
Expand All @@ -44,34 +46,22 @@ class Templates {
);
}

return jsTemplate.render({
context,
meta: {
__template: path.relative(this.base, templatePath),
__mapping: mappingFilepath,
},
});
return jsTemplate.render(context, meta);
}

async render(template, context, mappingFilepath = undefined) {
const templatePath = this.getAbsTemplatePath(template);
async render(template, context, meta) {
const templatePath = this.absPath(template);
const deepClonedContext = _.cloneDeep(context);
log('Template path %o, context %o', templatePath, deepClonedContext);
if (mappingFilepath !== undefined) {
mappingFilepath = path.relative(this.base, mappingFilepath);
}

if (templatePath.endsWith('.js')) {
return this._renderJs(templatePath, deepClonedContext, mappingFilepath);
return this._renderJs(templatePath, deepClonedContext, meta);
}

return this.njk
.addGlobal('context', deepClonedContext)
.addGlobal('meta', {
__mapping: mappingFilepath,
__template: path.relative(this.base, templatePath),
})
.render(templatePath, deepClonedContext);
.addGlobal('meta', meta)
.render(templatePath, context);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/templates/mapping.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// You can access the context with 'data.context'
// the output should be an object

exports.render = (data) => {
exports.render = (context) => {
return {
locations: [
{
template: `${data.context.TEMPLATE}`,
template: `${context.TEMPLATE}`,
contextSelector: 'PATH.TO.CONTEXT',
destination: {
type: 'echo',
Expand Down
Loading

0 comments on commit 0cf349f

Please sign in to comment.