-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
version.js
144 lines (126 loc) · 4.05 KB
/
version.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var semver = require('semver');
var which = require('which');
var fs = require('../util/fs');
var path = require('path');
var Q = require('q');
var execFile = require('child_process').execFile;
var Project = require('../core/Project');
var defaultConfig = require('../config');
var createError = require('../util/createError');
function version(logger, versionArg, options, config) {
var project;
options = options || {};
config = defaultConfig(config);
project = new Project(config, logger);
return bump(project, versionArg, options.message);
}
function bump(project, versionArg, message) {
var cwd = project._config.cwd || process.cwd();
var newVersion;
return checkGit(cwd)
.then(function (hasGit) {
if (!hasGit) {
throw createError('This directory does not appear to be a git repository', 'ENOTGITREPOSITORY');
}
})
.then(project.getJson.bind(project))
.then(function (/*json (ignored)*/) {
return currentGitVersion(cwd);
})
.then(function (currentVersion) {
newVersion = getNewVersion(currentVersion, versionArg);
return gitCommitAndTag(cwd, newVersion, message);
})
.then(function () {
console.log('v' + newVersion);
return newVersion;
});
}
function getNewVersion(currentVersion, versionArg) {
var newVersion = semver.valid(versionArg);
if (!newVersion) {
newVersion = semver.inc(currentVersion, versionArg);
}
if (!newVersion) {
throw createError('Invalid version argument: `' + versionArg + '`. Usage: `bower version [<newversion> | major | minor | patch]`', 'EINVALIDVERSION');
}
if (currentVersion === newVersion) {
throw createError('Version not changed', 'EVERSIONNOTCHANGED');
}
return newVersion;
}
function checkGit(cwd) {
var gitDir = path.join(cwd, '.git');
return Q.nfcall(fs.stat, gitDir)
.then(function (stat) {
if (stat.isDirectory()) {
return checkGitStatus(cwd);
}
return false;
}, function () {
//Ignore not found .git directory
return false;
});
}
function checkGitStatus(cwd) {
return Q.nfcall(which, 'git')
.fail(function (err) {
err.code = 'ENOGIT';
throw err;
})
.then(function () {
return Q.nfcall(execFile, 'git', ['status', '--porcelain'], {env: process.env, cwd: cwd});
})
.then(function (value) {
var stdout = value[0];
var lines = filterModifiedStatusLines(stdout);
if (lines.length) {
throw createError('Git working directory not clean.\n' + lines.join('\n'), 'EWORKINGDIRECTORYDIRTY');
}
return true;
});
}
function filterModifiedStatusLines(stdout) {
return stdout.trim().split('\n')
.filter(function (line) {
return line.trim() && !line.match(/^\?\? /);
}).map(function (line) {
return line.trim();
});
}
function gitCommitAndTag(cwd, newVersion, message) {
var tag = 'v' + newVersion;
message = message || tag;
message = message.replace(/%s/g, newVersion);
return Q.nfcall(execFile, 'git', ['commit', '-m', message, '--allow-empty'], {env: process.env, cwd: cwd})
.then(function () {
Q.nfcall(execFile, 'git', ['tag', tag, '-am', message], {env: process.env, cwd: cwd});
});
}
function currentGitVersion(cwd) {
return Q.nfcall(execFile, 'git', ['tag'], {env: process.env, cwd: cwd})
.then(function (res) {
var versions = res[0]
.split(/\r?\n/)
.map(stripV)
.filter(semver.valid)
.sort(semver.compare);
return versions[versions.length - 1] || '0.0.0';
});
}
function stripV(s) {
if (s[0] === 'v') {
return s.slice(1);
} else {
return s;
}
}
// -------------------
version.readOptions = function (argv) {
var cli = require('../util/cli');
var options = cli.readOptions({
'message': { type: String, shorthand: 'm'}
}, argv);
return [options.argv.remain[1], options];
};
module.exports = version;