Miss any of our Open RFC calls?Watch the recordings here! »

refs-compiler

2.0.2 • Public • Published

Coverage Status Build Status

References Compiler

Compiler for YAML, JSON and INI files using path references

Install

# npm 
# global 
$ npm install -g refs-compiler
 
# dev dependency 
$ npm install refs-compiler --save-dev
 
# Yarn 
# global 
$ yarn global add refs-compiler
 
# dev dependency 
$ yarn add refs-compiler -D

Example: YAML

Template(s)

AWSTemplateFormatVersion: '2010-09-09'
 
Resources:
  $ref: ./relative/path/to/file.yaml
RolePolicies:
  $ref: ./resources/role-policies.yaml
Type: 'AWS::IAM::Policy'
Properties:
  PolicyName: custom-role
  Roles:
    - custom-role
  PolicyDocument:
    Version: '2012-10-17'
    Statement:
      -
        Sid: PassRole
        Effect: Allow
        Resource:
          -
            'Fn::Join':
              - ""
              -
                - 'arn:aws:iam::'
                -
                  Ref: 'AWS::AccountId'
                - ':role/*'
        Action:
          - 'iam:PassRole'

Code

// CommonJS
const path = require('path');
const { default: compiler } = require('refs-compiler');
// ES6
import path from 'path';
import compiler from 'refs-compiler';
 
const inputTemplate = path.resolve('/path/to/template.yaml');
const outputFile = path.resolve(`/path/to/output.yaml`);
 
try {
  compiler(inputTemplate, outputFile)
      .then(results => {
          console.log(`file created in ${results.outputFile}`);
      })
      .catch(error => {
          console.log(`An error occurred while writing the file: ${error.message}`);
          console.log(error.stack);
      });
} catch (e) {
  console.error(e.message);
  console.error(e.stack);
}

CLI

$ refs-compiler -o ./build/output.yaml ./templates/template.yaml

Output

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  RolePolicies:
      Type: 'AWS::IAM::Policy'
      Properties:
        PolicyName: custom-role
        Roles:
          - custom-role
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
            Sid: PassRole
              Effect: Allow
              Resource:
                'Fn::Join':
                    - ''
                    - - 'arn:aws:iam::'
                      Ref: 'AWS::AccountId'
                      - ':role/*'
              Action:
                - 'iam:PassRole'

Acknowledgement

The base code was borrowed from doublenot/refs and then refactored / rewritten to support NodeJS 8+ using native Promises + async/await

Install

npm i refs-compiler

DownloadsWeekly Downloads

2

Version

2.0.2

License

MIT

Unpacked Size

48.7 kB

Total Files

64

Last publish

Collaborators

  • avatar