UNPKG

1.94 kBJavaScriptView Raw
1'use strict';
2
3var File = require('./file');
4var ParseGitHubUrl = require('github-url-from-git');
5
6/**
7 * Get the package.json object located in the current directory.
8 * @returns {Promise<Object>} package.json object
9 */
10exports.getUserPackage = function () {
11 var userPackagePath = process.cwd() + '/package.json';
12
13 return File.exists(userPackagePath)
14 .then(function () {
15 return require(userPackagePath);
16 })
17 .catch(function () {
18 throw new Error('valid package.json not found');
19 });
20};
21
22/**
23 * Grabs the repository URL if it exists in the package.json.
24 * @returns {Promise<String|Null>} the repository URL or null if it doesn't exist
25 */
26exports.extractRepoUrl = function () {
27 return exports.getUserPackage()
28 .then(function (userPackage) {
29 var url = userPackage.repository && userPackage.repository.url;
30
31 if (typeof url !== 'string') {
32 return null;
33 }
34
35 if (url.indexOf('github') === -1) {
36 return url;
37 } else {
38 return ParseGitHubUrl(url);
39 }
40 });
41};
42
43/**
44 * Calculate the new semver version depending on the options.
45 * @param {Object} options - calculation options
46 * @param {Boolean} options.patch - whether it should be a patch version
47 * @param {Boolean} options.minor - whether it should be a minor version
48 * @param {Boolean} options.major - whether it should be a major version
49 * @returns {Promise<String>} - new version
50 */
51exports.calculateNewVersion = function (options) {
52 return exports.getUserPackage()
53 .then(function (userPackage) {
54 var split = userPackage.version.split('.');
55
56 if (options.major) {
57 split[0] = (parseInt(split[0]) + 1).toString();
58 split[1] = '0';
59 split[2] = '0';
60 } else if (options.minor) {
61 split[1] = (parseInt(split[1]) + 1).toString();
62 split[2] = '0';
63 } else if (options.patch) {
64 split[2] = (parseInt(split[2]) + 1).toString();
65 }
66
67 return split.join('.');
68 });
69};