all files / lib/ package.js

100% Statements 29/29
100% Branches 12/12
100% Functions 7/7
100% Lines 29/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                                                                                     
'use strict';
 
var File           = require('./file');
var ParseGitHubUrl = require('github-url-from-git');
 
/**
 * Get the package.json object located in the current directory.
 * @returns {Promise<Object>} package.json object
 */
exports.getUserPackage = function () {
  var userPackagePath = process.cwd() + '/package.json';
 
  return File.exists(userPackagePath)
  .then(function () {
    return require(userPackagePath);
  })
  .catch(function () {
    throw new Error('valid package.json not found');
  });
};
 
/**
 * Grabs the repository URL if it exists in the package.json.
 * @returns {Promise<String|Null>} the repository URL or null if it doesn't exist
 */
exports.extractRepoUrl = function () {
  return exports.getUserPackage()
  .then(function (userPackage) {
    var url = userPackage.repository && userPackage.repository.url;
 
    if (typeof url !== 'string') {
      return null;
    }
 
    if (url.indexOf('github') === -1) {
      return url;
    } else {
      return ParseGitHubUrl(url);
    }
  });
};
 
/**
 * Calculate the new semver version depending on the options.
 * @param {Object} options - calculation options
 * @param {Boolean} options.patch - whether it should be a patch version
 * @param {Boolean} options.minor - whether it should be a minor version
 * @param {Boolean} options.major - whether it should be a major version
 * @returns {Promise<String>} - new version
 */
exports.calculateNewVersion = function (options) {
  return exports.getUserPackage()
  .then(function (userPackage) {
    var split = userPackage.version.split('.');
 
    if (options.major) {
      split[0] = (parseInt(split[0]) + 1).toString();
      split[1] = '0';
      split[2] = '0';
    } else if (options.minor) {
      split[1] = (parseInt(split[1]) + 1).toString();
      split[2] = '0';
    } else if (options.patch) {
      split[2] = (parseInt(split[2]) + 1).toString();
    } else {
      throw new Error('patch, minor, or major needs to be set');
    }
 
    return split.join('.');
  });
};