2.0.2 • Published 5 years ago

refs-compiler v2.0.2

Weekly downloads
49
License
MIT
Repository
github
Last release
5 years ago

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