-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (71 loc) · 2.87 KB
/
index.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
/* jshint node: true */
'use strict';
var DeployPluginBase = require('ember-cli-deploy-plugin');
var RSVP = require('rsvp');
var cp = require('child_process');
// Executes a command, returns the value wrapped in a promise.
// If error code is returned promise resolved okay with value null.
function execP(cmd) {
return new RSVP.Promise(function(resolve, reject) {
cp.exec(cmd, function(error, stdout, stderr) {
resolve(error ? null : stdout.trim());
});
});
}
module.exports = {
name: 'ember-cli-deploy-git-info',
createDeployPlugin: function(options) {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
branchEnvVars: ['TRAVIS_BRANCH', 'CIRCLE_BRANCH', 'CI_BRANCH']
},
requiredConfig: [],
prepare: function(context) {
this.log('Finding git info for commit');
return execP('git rev-parse --short HEAD').then(function(commit) {
return RSVP.hash({
commit: commit,
fullCommit: execP('git rev-parse HEAD'),
lastTag: this._lastTag(),
commitsSinceLastTag: this._commitsSinceLastTag(),
branch: this._getBranch(),
subject: execP('git show -s --format=%s ' + commit),
body: execP('git show -s --format=%b ' + commit),
rawBody: execP('git show -s --format=%B ' + commit),
committer: execP('git show -s --format=%cN ' + commit),
committerEmail: execP('git show -s --format=%cE ' + commit),
author: execP('git show -s --format=%aN ' + commit),
authorEmail: execP('git show -s --format=%aE ' + commit),
commitDate: execP('git show -s --format=%cd ' + commit)
});
}.bind(this)).then(function(hash) {
this.log('Git info avaiable in context["git-info"]')
return {'git-info': hash};
}.bind(this));
},
_getBranch: function() {
return execP('git symbolic-ref --short -q HEAD').then(function(branch) {
return branch || this._envBranch();
}.bind(this));
},
_envBranch: function() {
return this.readConfig('branchEnvVars').
map(function(branch) { return process.env[branch]; }).
filter(function(b) { return b; }).
shift();
},
_lastTag: function() {
return execP('git describe --tags').then(function(describe) {
return describe ? describe.split('-').slice(0, -2).join('-') : null;
});
},
_commitsSinceLastTag: function() {
return execP('git describe --tags').then(function(describe) {
return describe ? describe.split('-').reverse()[1] : null;
});
}
});
return new DeployPlugin();
}
};