-
Notifications
You must be signed in to change notification settings - Fork 4
/
create-pull-request.js
executable file
·73 lines (63 loc) · 1.65 KB
/
create-pull-request.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
#!/usr/bin/env node
const gitRemoteOriginUrl = require("git-remote-origin-url");
const branchName = require("current-git-branch");
const open = require("open");
const chalk = require("chalk");
const warning = chalk.keyword("orange");
const error = chalk.keyword("red");
(async () => {
try {
let remoteOriginUrl = await gitRemoteOriginUrl();
if (remoteOriginUrl.startsWith("git@")) {
remoteOriginUrl = remoteOriginUrl
.replace(/^git@/, "https://")
.replace(/(com|org):/, "$1/");
}
remoteOriginUrl = remoteOriginUrl.replace(/\.git$/, "");
const pullRequestURL = getPullRequestUrl(remoteOriginUrl);
if (!pullRequestURL) {
return;
}
await open(pullRequestURL, { app: getBrowser() });
} catch (err) {
console.log(
error(err.message.charAt(0).toUpperCase() + err.message.slice(1))
);
}
})();
/**
* @param {string} repoUrl
* @returns {string}
*/
function getPullRequestUrl(repoUrl) {
const url = new URL(repoUrl);
if (url.host === "github.com") {
return `${url}/pull/new/${branchName()}`;
} else if (url.host === "bitbucket.org") {
return `${url}/pull-requests/new?source=${branchName()}&t=1#diff`;
}
return "";
}
/**
* @returns {string}
*/
function getBrowser() {
const browsers = {
chrome: "chrome",
firefox: "firefox",
edge: "msedge",
safari: "safari",
ie: "iexplore",
opera: "opera",
};
const [, , userInput] = process.argv;
const browser = browsers[userInput];
if (userInput && !browser) {
console.log(
warning(
`Browser "${userInput}" was not found, we will use your default browser.`
)
);
}
return browser || "";
}