all files / lib/ writeDockerfile.js

46.67% Statements 14/30
25% Branches 3/12
11.11% Functions 1/9
44.83% Lines 13/29
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                                                                                                                                             
'use strict';
 
var _defineProperty2 = require('babel-runtime/helpers/defineProperty');
 
var _defineProperty3 = _interopRequireDefault(_defineProperty2);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
var fs = require('fs');
var Promise = require('bluebird');
var parser = require('./parsePackage.js');
 
// For the moment, stub out the package.json
var PKG_PATH = './test/fixtures/package-test.json';
 
// TODO: make this work from node_modules
var PATH_TO_DOCKER_FILES = 'docker/';
 
// Uncomment this when we're calling writeDockerfile from writeComposefile
// const whatImages = parser.matchDependencies(PKG_PATH);
 
var makeDockerFiles = {};
/**
* makeDockerFiles is the parent for our internal Dockerfile construction
* processes. Pretty much everything is private to the function itself.
* To call it, use something like:
* makeDockerFiles.makeDockers(whatImages).then(p => console.log(p));
* @private
**/
 
makeDockerFiles.formulateDocker = function formulateDocker(dependency) {
  /**
  * Makes the text string for a dockerfile
  * @param {string} The name of the dependency
  * @returns {Object} What should be created, form of {dependency: the contents for the Dockerfile}
  * @private
  **/
  return 'FROM ' + dependency + ': latestt\n  EXPOSE $' + dependency + '_PORT';
};
 
makeDockerFiles.saveDocker = function saveDocker(dependency, formulaObj) {
  /**
  * Saves a dockerfile
  * All dockerfiles will be saved in the /docker directory
  * @param {Object} Format: {dependency: the contents for the Dockerfile}
  * @returns {Function} Promise of filepath to saved dockerfile
  * @private
  **/
  if (formulaObj) {
    return new Promise(function (resolve, reject) {
      // Check for a /docker/ dir
      fs.stat(PATH_TO_DOCKER_FILES, function (err) {
        if (err) {
          // If not, returns an error value
          // So, make a directory
          fs.mkdir(PATH_TO_DOCKER_FILES, function (err) {
            if (err) {
              // Can't make a directory!
              // Might as well just plotz
              reject(err);
            }
          });
        }
        // Now we know we have a directory. Save the file.
        fs.writeFile(PATH_TO_DOCKER_FILES + dependency, formulaObj, function (err) {
          if (err) {
            // Can't write a file!
            // Always the shlmiel. Might as well make the app my shlimazel.
            reject(err);
          } else {
            // alert! [Below] is ES6 only
            resolve((0, _defineProperty3.default)({}, dependency, PATH_TO_DOCKER_FILES + dependency));
          }
        });
      });
    });
  } else {
    return null;
  }
};
 
makeDockerFiles.makeDockers = function makeDockers(imagesNeeded) {
  var _this = this;
 
  /**
  * Bring the other methods together, which we have to do because
  * There will be a lot of Promises here.
  * All dockerfiles will be saved in the /docker directory
  * @param {Function} a Promise of the images needed to be made
  * @returns {Function} a Promise of the dockerfiles and their paths. [{ dependency: path }]
  * @private
  **/
  // Map the promises of images needed to promises of images created
  return Promise.map(imagesNeeded, function (image) {
    //  Formulate a Docker, save it, and store the Promise resulting
    return _this.saveDocker(image, _this.formulateDocker(image));
  });
};
 
module.exports = makeDockerFiles;