1.0.0 • Published 4 years ago

cloudbusting-handlebars v1.0.0

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
4 years ago

@cloudbusting/handlebars

What is this package for?

This package helps you transform a Cloudbusting infraplan to a set of files (e.g. Terraform or Cloudformation templates) using Handlebar templates.

How do I use it?

Use this package as the base for your cloudbuster.

First, create a new node.js project for your cloudbuster, and add this package to it:

npm install @cloudbusting/handlebars

Next, write code to read the infraplan file, and pass it to the Handlebars:

const fs = require('fs');
const handlebars = require('@cloudbusting/handlebars');

const infraplan = JSON.parse(fs.readFileSync('infraplan.js'));

handlebars.generate(infraplan, { 
  templateDir: './templates',
  outputDir: './output',
  extension: '.tf' 
});

How does it work?

The structure of the templates directory is important. It can contain the following:

.root directory

The .root subdirectory whose files will be copied to the root of the outputDir. This is useful for files that are always the same. When using Terraform, for example, it can contain Terraform files for the provider, variables, roles or resources that are not part of the infraplan's blueprint.

partials

The files under the partials subdirectory will be registered as Handlebars partials. They will be registered under their filename. Registration is recursive. So the file ./partials/tags/default.tf can be referenced in a Handlebars template file as {{> tags/default.tf}}.

templates files

Finally, the root of the templateDir should contain Handlebar template files for each resource type in your infraplan.

Running the package

When generating files from the infraplan, the package will first clear the outputDir by removing all files with the provided extension.

The infraplan will be normalized using the @cloudbuster/modelbuilder package. This way, you can easily reference an items parent, and all incoming and outgoing connections.

For each item in the infraplan it will look for a file that starts with the item's resourceType, use that as a template for Handlebars are process it using the item as input.

The result is written to the outputDir under the name ${resourceType}.${name}.${extension}.

Extra's

This package includes and initializes the handlebar-helpers package.

See the Handlebars website for information on using Handlebars templates.

How do I get set up if I want to fork or contribute?

  • Clone the repository
  • npm install

You can run npm run test to make test-runs. It executes the test.js file. This contains a very simple infraplan object and the required directory structure under the ./test subdirectory.