-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·324 lines (277 loc) · 8.96 KB
/
Copy pathcli.js
File metadata and controls
executable file
·324 lines (277 loc) · 8.96 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env node
'use strict';
// ----------------------
// TODO - List of tasks in order of priority
// ----------------------
// 1. Create Homebrew tap -> https://docs.brew.sh/How-to-Create-and-Maintain-a-Tap.html
// 2. Rewrite docs for easier setup
// 3. Add more parameters and help
// 4. Cleanup fallback issues
// 5. Put variables into stored config file
// require and string constants
const meow = require('meow'),
opn = require('opn'),
execa = require('execa'),
chalk = require('chalk'),
bbPrefix = ' 🌀 ',
Bitbucket = 'bitbucket',
Github = 'github',
Git = 'git',
Hg = 'hg';
// cli constants
const cli = meow(`
Usage: bb [command] [options]
Options:
--help, -h
--v, -v
Commands:
branch [branch] go to branch
commits [branch] go to commits on repo
compare [branch] go to compare in browser
info get branch and repo information
issues go to issues in browser
pipelines go to pipelines page
pr go to pr page for branch
repo go to repo homepage
`, { alias: { h: 'help', v: 'version'}}),
command = cli.input[0],
option1 = cli.input[1],
option2 = cli.input[2];
// vars
var repo = '',
repoPrefix = '',
repoType = '',
repoUrl = '',
branchType = '',
branch = '',
branchToLog = '',
commandName = '',
url = '',
preventOpenUrl = false,
isValidCommand = false,
isValidRepo = false;
let runBB = () => {
try {
tryGit();
} catch(error) {
tryHg();
// TODO error catching nested?
}
// cli steps to get repo/paths/urls
determineRepoPrefix();
cleanupRepoString();
setRepoDomain(); // validate this works in all cases
// run through cli commands
if (command) {
// TODO: clean up alternate way to check if valid
if ( command == 'branch' || command == 'b'
|| command == 'commits' || command == 'comm'
|| command == 'compare' || command == 'comp'
|| command == 'issues' || command == 'is'
|| command == 'pr' || command == 'p'
|| command == 'repo' || command == 'r'
|| command == 'info' || command == 'in' ) {
isValidCommand = true;
}
// checks list of commands to set url
if (command == 'branch' || command == 'b') {
commandName = 'branch';
setUrlForBranch();
} else if (command == 'commits' || command == 'comm') {
commandName = 'commits';
setUrlForCommits();
} else if (command == 'compare' || command == 'comp') {
commandName = 'compare';
setUrlForCompare();
} else if (command == 'issues' || command == 'is') {
commandName = 'issues';
setUrlForIssues();
} else if (command == 'pipelines' || command == 'pipeline' || command == 'pipe' || command == 'p') {
commandName = 'pipelines';
setUrlForPipeline();
} else if (command == 'pr') {
commandName = 'pull request';
setUrlForPR();
} else if (command == 'repo' || command == 'r') {
commandName = 'repository'
url = `${repoUrl}${repo}`;
} else if (command == 'info' || command == 'in') {
commandName = 'information';
url = `${repoUrl}${repo}`;
preventOpenUrl = true;
} else {
// help fallback
cli.showHelp([2]);
}
openRepoInBrowserIfValid();
} else {
// defaults to `bb repo`
commandName = 'repository';
url = `${repoUrl}${repo}`;
isValidCommand = true;
openRepoInBrowserIfValid();
}
logInfo();
}
//-------------
// methods
//-------------
let tryGit = () => {
// try git
const gitRepo = execa.sync(Git, ['config', '--get', 'remote.origin.url']).stdout;
// strip repo path
repo = gitRepo.toLowerCase();
repo = repo.replace('.git','');
repoType = Git;
}
let tryHg = () => {
// try hg
const hgRepo = execa.sync(Hg, ['paths']).stdout;
// strip repo path
repo = hgRepo.toLowerCase();
repoType = Hg;
}
let openRepoInBrowserIfValid = () => {
isValidRepo = checkIsValidRepo();
if (isValidRepo && isValidCommand && !preventOpenUrl) {
// delay open browser 0.3sec to see output before browser takes over
setTimeout(function(){
opn(url, {wait: false});
},300);
}
}
let determineRepoPrefix = () => {
if (repo.includes(Bitbucket + '.org')) {
repo = repo.replace(Bitbucket,'');
repoPrefix = Bitbucket;
} else if (repo.includes(Github) + '.com') {
repo = repo.replace(Github,'');
repoPrefix = Github;
}
}
let cleanupRepoString = () => {
// remove common strings to get repo
repo = repo.substring(repo.indexOf("@") + 1); //rm anything before @
repo = repo.replace('ssh://','');
repo = repo.replace('https://','');
repo = repo.replace('.org/','');
repo = repo.replace('.org:','');
repo = repo.replace('.com/','');
repo = repo.replace('.com:','');
}
let logInfo = () => {
if (command && branchToLog) {
console.log('\n' + chalk.yellow.bold('[Bash Bucket]') + ' ' + chalk.yellow.bgBlue.bold(` ${commandName} > `) + chalk.blue.bgYellow.bold(` ${branchToLog} `) + '\n');
} else if (command) {
console.log('\n' + chalk.yellow.bold('[Bash Bucket]') + ' ' + chalk.yellow.bgBlue.bold(` ${commandName} `) + '\n');
} else {
console.log('\n' + chalk.yellow.bold('[Bash Bucket]') + '\n');
}
if (isValidRepo && isValidCommand) {
console.log(bbPrefix + 'repo: ' + chalk.blue.bold.underline(repo));
console.log(bbPrefix + 'repo type: ' + chalk.blue.bold.underline(repoType));
console.log(bbPrefix + 'repo prefix: ' + chalk.blue.bold.underline(repoPrefix));
if (branch) {
console.log(bbPrefix + 'branch: ' + chalk.blue.bold.underline(branch));
}
if (preventOpenUrl) {
console.log(bbPrefix + 'repo home -> ' + chalk.blue.bold.underline(url));
} else {
console.log(bbPrefix + 'opening url -> ' + chalk.blue.bold.underline(url));
}
} else if (!isValidCommand) {
console.log(bbPrefix + 'Not a valid command, see `bb --help` for options.');
} else {
console.log(bbPrefix + 'No repository found for this directory -> ', chalk.blue.bold.underline(process.cwd()));
}
console.log();
}
//-------------
// getters
//-------------
let getBranch = () => {
// get branch
if (repoType == Git) {
const gitBranch = execa.sync('git', ['rev-parse', '--abbrev-ref', 'HEAD']).stdout;
branch = gitBranch;
} else if (repoType == Hg) {
const hgBranch = execa.sync('hg', ['branch']).stdout;
branch = hgBranch;
}
// get branch type
if (repoPrefix == Github) {
branchType = 'tree';
} else if (repoPrefix == Bitbucket) {
branchType = 'branch';
}
branchToLog = branch;
return branch;
}
let checkIsValidRepo = () => {
if (repo) {
return true;
}
}
//-------------
// setters
//-------------
let setRepoDomain = () => {
if (repoPrefix == Github) {
repoUrl = ('https://' + Github + '.com/');
} else if (repoPrefix == Bitbucket) {
repoUrl = ('https://' + Bitbucket + '.org/');
}
}
let setUrlForCompare = () => {
branch = getBranch();
if (option1) {
if (repoPrefix == Github) {
url = `${repoUrl}${repo}/compare/${branch}...${option1}`;
} else if (repoPrefix == Bitbucket) {
url = `${repoUrl}${repo}/${branchType}/${branch}?dest=${option1}`;
}
} else {
if (repoPrefix == Github) {
url = `${repoUrl}${repo}/compare/${branch}...develop`;
} else if (repoPrefix == Bitbucket) {
url = `${repoUrl}${repo}/${branchType}/${branch}?dest=develop`;
}
}
}
let setUrlForCommits = () => {
branch = getBranch();
var commitBranch = branch;
if (option1) {
commitBranch = option1;
}
if (repoPrefix == Github) {
url = `${repoUrl}${repo}/commits/${commitBranch}`;
} else if (repoPrefix == Bitbucket) {
url = `${repoUrl}${repo}/commits/${branchType}/${commitBranch}`;
}
}
let setUrlForBranch = () => {
branch = getBranch();
url = `${repoUrl}${repo}/${branchType}/`;
if (option1) {
branchToLog = option1;
url += `${option1}`;
} else {
url += `${branch}`;
}
}
let setUrlForPR = () => {
if (repoPrefix == Github) {
url = `${repoUrl}${repo}/pulls`;
} else if (repoPrefix == Bitbucket) {
branch = getBranch();
url = `${repoUrl}${repo}/pull-requests/new?source=${branch}&t=1`;
}
}
let setUrlForIssues = () => {
url = `${repoUrl}${repo}/issues`;
}
let setUrlForPipeline = () => {
url = `${repoUrl}${repo}/addon/pipelines/`;
}
runBB(); // runs bash bucket cli