All files / dockerfile-generator/lib dockerGenerator.js

100% Statements 47/47
100% Branches 7/7
100% Functions 2/2
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 481x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 2x 2x 30x 30x 30x 98x 98x 98x 30x 30x 30x 30x 1x 1x 1x 1x 1x 4x 4x 4x 20x 20x 20x 20x 20x 4x 4x 4x 1x 1x 1x 1x 1x  
const {Validator} = require('jsonschema');
const v = new Validator();
const dockerProcessor = require('./dockerProcessor');
const Schema = require('../schema.json');
 
/**
 * Generate Dockerfile from JSON.
 * @param {string} inputJSON - The JSON representation of the Dockerfile.
 * @return {string} The generated Dockerfile.
 */
function generateDockerFileFromJSON(inputJSON) {
  const validationResult = v.validate(inputJSON, Schema);
 
  if (validationResult.errors.length !== 0) {
    throw Error('Input Validation error');
  }
 
  let resp = '';
  Object.keys(inputJSON).forEach((key) => {
    const callableFunction = dockerProcessor.determineFunction(key);
    resp += callableFunction(inputJSON[key]);
    resp += '\n';
  });
 
  return resp;
}
/**
 * Generate Dockerfile from an Array.
 * @param {Array} inputArray - Array representation of the Dockerfile.
 * @return {string}The generated Dockerfile.
 */
function generateDockerFileFromArray(inputArray) {
  let resp = '';
  inputArray.forEach((item) => {
    Object.keys(item).forEach((key) => {
      const callableFunction = dockerProcessor.determineFunction(key);
      resp += callableFunction(item[key]);
      resp += '\n';
    });
  });
  return resp;
}
 
module.exports = {
  generateDockerFile: generateDockerFileFromJSON,
  generateDockerFileFromArray,
};