All files / dockerfile-generator/lib jsonGenerator.js

96.25% Statements 77/80
92.3% Branches 12/13
100% Functions 1/1
96.25% Lines 77/80

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 811x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 3x 3x 3x 3x 3x 3x 26x 26x 1x 1x 1x 1x 26x 26x 1x 1x 26x 26x 1x 1x 26x 26x 26x 26x 26x 26x 22x 22x 26x       4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 1x 1x  
const readline = require('readline');
 
const jsonProcessor = require('./jsonProcessor');
 
/**
 * Generate JSON object from a dockerfile.
 * @param {*} dockerfileStream - Dockerfile as Stream.
 * @return {object}- Generated JSON object.
 */
function generateJSON(dockerfileStream) {
  return new Promise((resolve) => {
    const lineReader = readline.createInterface({
      input: dockerfileStream,
    });
 
    let resp = {};
 
    lineReader.on('line', (line) => {
      // determine functionality
      const callableFunction = jsonProcessor.determineFunction(line);
 
      // call the function
      let lineResult = callableFunction(line);
 
      // merge the lineResult
      let processedKeyword = Object.getOwnPropertyNames(lineResult)[0];
      let processedValue = lineResult[processedKeyword];
 
      // small transforms, it would be great to refactor this later.
      if (processedKeyword === 'arg' || processedKeyword === 'volume') {
        processedKeyword += 's';
 
        const tmpVariable = processedValue;
        processedValue = [];
        processedValue.push(tmpVariable);
      }
 
      if (processedKeyword === 'expose') {
        const tmpVariable = processedValue;
        processedValue = [];
        processedValue.push(tmpVariable);
      }
 
      if (processedKeyword === 'label') {
        processedKeyword += 's';
      }
 
      if (processedKeyword === 'workdir') {
        processedKeyword = 'working_dir';
      }
 
      const tmpObject = {};
      tmpObject[processedKeyword] = processedValue;
      lineResult = tmpObject;
 
      if (!resp[processedKeyword]) {
        // not exists in the response object yet.
        resp = Object.assign(resp, lineResult);
      } else if (typeof processedValue === 'string') {
        // process single element and exists in the response object already
        // TODO handling keyword can't have multiple value (FROM)
        resp = Object.assign(resp, lineResult);
      } else if (Array.isArray(processedValue)) {
        // process array and exists in the response object already
        resp[processedKeyword] = resp[processedKeyword].concat(processedValue);
      } else {
        // process key-value pairs
        resp[processedKeyword] = Object.assign(
            resp[processedKeyword],
            processedValue,
        );
      }
    });
    lineReader.on('close', () => {
      resolve(resp);
    });
  });
}
 
module.exports.generateJSON = generateJSON;