forked from github-tools/github-release-notes
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGitHubInfo.js
More file actions
111 lines (100 loc) · 2.47 KB
/
GitHubInfo.js
File metadata and controls
111 lines (100 loc) · 2.47 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
103
104
105
106
107
108
109
110
111
import { exec } from "child_process";
import chalk from "chalk";
/** Class retrieving GitHub informations from the folder where .git is initialised. */
class GitHubInfo {
/**
* Getter for the options
*
* @return {Promise.all}
*/
get options() {
return Promise.all([this._repo(), this._token()]);
}
/**
* Getter for the token
*
* @return {Promise}
*/
get token() {
return this._token();
}
/**
* Getter for the repo
*
* @return {Promise}
*/
get repo() {
return this._repo();
}
/**
* 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}
*/
_executeCommand(command, callback) {
return new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if (err || stderr) {
reject(err || stderr);
} else {
resolve(stdout.replace("\n", ""));
}
});
})
.then(callback) // eslint-disable-line promise/no-callback-in-promise
.catch((error) => {
throw new Error(
chalk.red(error) +
chalk.yellow(
"\nMake sure you're running the command from the repo folder, or you using the --username and --repo flags.",
),
);
});
}
/**
* Get repo informations
*
* @since 0.5.0
* @public
*
* @param {Function} callback
*
* @return {Promise} The promise that resolves repo informations ({user: user, name: name})
*/
_repo(callback) {
return this._executeCommand("git config remote.origin.url", (repo) => {
const regex = /([\w-.]+)\/([\w-.]+?)(\.git)?$/g;
const matches = [...repo.matchAll(regex)];
if (!matches[0]) {
return Promise.reject(new Error("No repo found"));
}
const user = matches[0][1];
const name = matches[0][2];
return {
username: user,
repo: name,
};
}).then(callback); // eslint-disable-line promise/no-callback-in-promise
}
/**
* Get token informations
*
* @since 0.5.0
* @public
*
* @param {Function} callback
*
* @return {Promise} The promise that resolves token informations ({token: token})
*/
_token() {
const token = process.env.GREN_GITHUB_TOKEN;
return token ? Promise.resolve({ token }) : Promise.resolve(null);
}
}
export default GitHubInfo;