Skip to content

Commit da45bf8

Browse files
authored
fix: consolidate completions (#38)
1 parent bdc86d1 commit da45bf8

File tree

3 files changed

+74
-73
lines changed

3 files changed

+74
-73
lines changed

src/completions.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import * as Config from '@oclif/config'
33
import * as path from 'path'
44

55
import deps from './deps'
6-
7-
export {AppCompletion, RemoteCompletion} from './flags/app'
6+
import {configRemote, getGitRemotes} from './git'
87

98
export const oneDay = 60 * 60 * 24
109

@@ -15,21 +14,11 @@ export const herokuGet = async (resource: string, ctx: { config: Config.IConfig
1514
return resources.map((a: any) => a.name).sort()
1615
}
1716

18-
export const BuildpackCompletion: flags.ICompletion = {
19-
skipCache: true,
20-
21-
options: async () => {
22-
return [
23-
'heroku/ruby',
24-
'heroku/nodejs',
25-
'heroku/clojure',
26-
'heroku/python',
27-
'heroku/java',
28-
'heroku/gradle',
29-
'heroku/scala',
30-
'heroku/php',
31-
'heroku/go',
32-
]
17+
export const AppCompletion: flags.ICompletion = {
18+
cacheDuration: oneDay,
19+
options: async ctx => {
20+
let apps = await herokuGet('apps', ctx)
21+
return apps
3322
},
3423
}
3524

@@ -55,6 +44,24 @@ export const AppDynoCompletion: flags.ICompletion = {
5544
},
5645
}
5746

47+
export const BuildpackCompletion: flags.ICompletion = {
48+
skipCache: true,
49+
50+
options: async () => {
51+
return [
52+
'heroku/ruby',
53+
'heroku/nodejs',
54+
'heroku/clojure',
55+
'heroku/python',
56+
'heroku/java',
57+
'heroku/gradle',
58+
'heroku/scala',
59+
'heroku/php',
60+
'heroku/go',
61+
]
62+
},
63+
}
64+
5865
export const DynoSizeCompletion: flags.ICompletion = {
5966
cacheDuration: oneDay * 90,
6067
options: async ctx => {
@@ -112,6 +119,15 @@ export const RegionCompletion: flags.ICompletion = {
112119
},
113120
}
114121

122+
export const RemoteCompletion: flags.ICompletion = {
123+
skipCache: true,
124+
125+
options: async () => {
126+
let remotes = getGitRemotes(configRemote())
127+
return remotes.map(r => r.remote)
128+
},
129+
}
130+
115131
export const RoleCompletion: flags.ICompletion = {
116132
skipCache: true,
117133

src/flags/app.ts

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import {flags} from '@oclif/command'
22
import {CLIError, error} from '@oclif/errors'
33

4-
import {herokuGet, oneDay} from '../completions'
5-
import {Git} from '../git'
6-
import {vars} from '../vars'
4+
import {AppCompletion, RemoteCompletion} from '../completions'
5+
import {configRemote, getGitRemotes, IGitRemotes} from '../git'
76

87
class MultipleRemotesError extends CLIError {
9-
constructor(gitRemotes: IGitRemote[]) {
8+
constructor(gitRemotes: IGitRemotes[]) {
109
super(`Multiple apps in git remotes
1110
Usage: --remote ${gitRemotes[1].remote}
1211
or: --app ${gitRemotes[1].app}
@@ -20,14 +19,6 @@ class MultipleRemotesError extends CLIError {
2019
}
2120
}
2221

23-
export const AppCompletion: flags.ICompletion = {
24-
cacheDuration: oneDay,
25-
options: async ctx => {
26-
let apps = await herokuGet('apps', ctx)
27-
return apps
28-
},
29-
}
30-
3122
export const app = flags.build({
3223
char: 'a',
3324
completion: AppCompletion,
@@ -47,52 +38,8 @@ export const app = flags.build({
4738
},
4839
})
4940

50-
export const RemoteCompletion: flags.ICompletion = {
51-
skipCache: true,
52-
53-
options: async () => {
54-
let remotes = getGitRemotes(configRemote())
55-
return remotes.map(r => r.remote)
56-
},
57-
}
58-
5941
export const remote = flags.build({
6042
char: 'r',
6143
completion: RemoteCompletion,
6244
description: 'git remote of app to use',
6345
})
64-
65-
function configRemote() {
66-
let git = new Git()
67-
try {
68-
return git.exec('config heroku.remote').trim()
69-
} catch {}
70-
}
71-
72-
interface IGitRemote {
73-
remote: string
74-
app: string
75-
}
76-
function getGitRemotes(onlyRemote: string | undefined): IGitRemote[] {
77-
let git = new Git()
78-
let appRemotes = []
79-
let remotes
80-
try {
81-
remotes = git.remotes
82-
} catch {
83-
return []
84-
}
85-
for (let remote of remotes) {
86-
if (onlyRemote && remote.name !== onlyRemote) continue
87-
for (let prefix of vars.gitPrefixes) {
88-
const suffix = '.git'
89-
let match = remote.url.match(`${prefix}(.*)${suffix}`)
90-
if (!match) continue
91-
appRemotes.push({
92-
app: match[1],
93-
remote: remote.name,
94-
})
95-
}
96-
}
97-
return appRemotes
98-
}

src/git.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {CLIError} from '@oclif/errors'
22

3+
import {vars} from './vars'
4+
35
export interface IGitRemote {
46
name: string
57
url: string
@@ -31,3 +33,39 @@ export class Git {
3133
}
3234
}
3335
}
36+
37+
export function configRemote() {
38+
let git = new Git()
39+
try {
40+
return git.exec('config heroku.remote').trim()
41+
} catch {}
42+
}
43+
44+
export interface IGitRemotes {
45+
remote: string
46+
app: string
47+
}
48+
49+
export function getGitRemotes(onlyRemote: string | undefined): IGitRemotes[] {
50+
let git = new Git()
51+
let appRemotes = []
52+
let remotes
53+
try {
54+
remotes = git.remotes
55+
} catch {
56+
return []
57+
}
58+
for (let remote of remotes) {
59+
if (onlyRemote && remote.name !== onlyRemote) continue
60+
for (let prefix of vars.gitPrefixes) {
61+
const suffix = '.git'
62+
let match = remote.url.match(`${prefix}(.*)${suffix}`)
63+
if (!match) continue
64+
appRemotes.push({
65+
app: match[1],
66+
remote: remote.name,
67+
})
68+
}
69+
}
70+
return appRemotes
71+
}

0 commit comments

Comments
 (0)