forked from github-tools/github-release-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-info.js
More file actions
102 lines (95 loc) · 2.21 KB
/
github-info.js
File metadata and controls
102 lines (95 loc) · 2.21 KB
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
'use strict';
var exec = require('child_process').exec;
var chalk = require('chalk');
var Promise = Promise || require('es6-promise').Promise;
/**
* Execute a command in the bash and run a callback
*
* @since 0.5.0
* @private
*
* @param {string} command The command to execute
* @param {Function} callback The callback which returns the stdout
*
* @return {Promise}
*/
function executeCommand(command, callback) {
return new Promise(function(resolve, reject) {
exec(command, function(err, stdout, stderr) {
if (err || stderr) {
reject(err || stderr);
} else {
resolve(stdout.replace('\n', ''));
}
});
})
.then(callback)
.catch(function(error) {
throw new Error(chalk.red(error) + chalk.yellow('Make sure you\'re running the command from the repo folder, or you using the --username and --repo flags.'));
});
}
/**
* Get user informations
*
* @since 0.5.0
* @public
*
* @param {Function} callback
*
* @return {Promise} The promise that resolves user informations ({ user: username})
*/
function user(callback) {
return executeCommand('git config user.name', function(user) {
return {
user: user
};
})
.then(callback);
}
/**
* Get repo informations
*
* @since 0.5.0
* @public
*
* @param {Function} callback
*
* @return {Promise} The promise that resolves repo informations ({user: user, name: name})
*/
function repo(callback) {
return executeCommand('git config remote.origin.url', function(repo) {
var repoPath = repo
.replace(/([^:]*:)|\.[^.]+$/g, '')
.split('/');
var user = repoPath[0];
var name = repoPath[1];
return {
username: user,
repo: name
};
})
.then(callback);
}
/**
* Get token informations
*
* @since 0.5.0
* @public
*
* @param {Function} callback
*
* @return {Promise} The promise that resolves token informations ({token: token})
*/
function token(callback) {
return executeCommand('echo $GREN_GITHUB_TOKEN', function(token) {
return {
token: token
};
})
.then(callback);
}
module.exports = {
user: user,
repo: repo,
token: token
};