Skip to content

Commit 0b4b0e7

Browse files
committed
feat: Add repo cloning from many owners
1 parent 09cf006 commit 0b4b0e7

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

src/commands/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class Goto extends Command {
133133
return
134134
}
135135

136-
logger.info(`No paths found for ${bold(path)}`)
136+
logger.info(`No paths found for ${bold(path)}, checking GitHub next`)
137137

138138
// Check for repo on github via gh cli
139139
const repos = await getRepos(path)
@@ -150,8 +150,6 @@ export class Goto extends Command {
150150
path
151151
)}. Please specify the repo name.`
152152
)
153-
this.exit(1)
154-
return
155153
}
156154

157155
// Base case for single repo

src/lib/gh.ts

+48-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import spawnAsync from '@expo/spawn-async'
2+
import { bold } from 'kleur'
3+
import config from './config'
24
import logger from './logger'
35

46
interface Repo {
@@ -29,12 +31,35 @@ export const listRepos = async (repoPath: string): Promise<Repo[]> => {
2931

3032
// Check if that repo exists
3133
export const getRepos = async (repoPath: string): Promise<Repo[]> => {
32-
const repos = await listRepos(repoPath)
34+
const owners = config.get('owners', []) as string[]
35+
36+
const repos = await searchRepos(repoPath)
37+
38+
// Fetch all repos
39+
for (const owner of owners) {
40+
const ownerRepos = await searchRepos(repoPath, owner)
41+
repos.push(...ownerRepos)
42+
}
3343

3444
// If there's a repo with the exact name, return it
35-
const exactRepo = repos.find(
45+
let exactRepo = repos.find(
3646
(repo) => `${repo.owner.login}/${repo.name}` === repoPath
3747
)
48+
if (!exactRepo) {
49+
for (const owner of owners) {
50+
exactRepo = repos.find(
51+
(repo) => `${repo.owner.login}/${repo.name}` === `${owner}/${repoPath}`
52+
)
53+
if (exactRepo) {
54+
logger.info(
55+
`Found repo using owner ${bold(owner)}: ${bold(
56+
`${owner}/${repoPath}`
57+
)}`
58+
)
59+
break
60+
}
61+
}
62+
}
3863
return exactRepo ? [exactRepo] : repos
3964
}
4065

@@ -48,17 +73,33 @@ export const cloneRepo = async (repo: string, destination: string) => {
4873
}
4974

5075
// Search for a repo by it's path
51-
export const searchRepos = async (repoPath: string): Promise<Repo> => {
52-
let resultPromise = spawnAsync('gh', [
76+
export const searchRepos = async (
77+
repoPath: string,
78+
owner?: string
79+
): Promise<Repo[]> => {
80+
const args = [
5381
'search',
5482
'repos',
5583
repoPath,
5684
'--json',
5785
'owner,name,url,description',
58-
])
86+
'--limit',
87+
'10',
88+
]
5989

60-
let { stdout } = await resultPromise
61-
const repos = JSON.parse(stdout)
90+
if (owner) {
91+
args.push('--owner', owner)
92+
}
93+
94+
let repos: Repo[] = []
95+
try {
96+
let resultPromise = spawnAsync('gh', args)
97+
98+
let { stdout } = await resultPromise
99+
repos = JSON.parse(stdout)
100+
} catch (e) {
101+
logger.debug(`Error searching for repo ${repoPath}`)
102+
}
62103
logger.debug(repos)
63104

64105
return repos

src/user/owners.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import logger from '../lib/logger'
55

66
// User prompt for settings dev directory
77
export const updateGitHubOwners = async (update: boolean = false) => {
8-
const owners = config.get('owners')
8+
const owners = config.get('owners', []) as string[]
99
if (owners) {
1010
logger.info(
1111
`Current github orgs enabled: ${

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"skipLibCheck": true,
1212
"esModuleInterop": true,
1313
"allowSyntheticDefaultImports": true,
14+
"inlineSourceMap": true
1415
},
1516
"include": ["src/**/*"]
1617
}

0 commit comments

Comments
 (0)