1
1
import spawnAsync from '@expo/spawn-async'
2
+ import { bold } from 'kleur'
3
+ import config from './config'
2
4
import logger from './logger'
3
5
4
6
interface Repo {
@@ -29,12 +31,35 @@ export const listRepos = async (repoPath: string): Promise<Repo[]> => {
29
31
30
32
// Check if that repo exists
31
33
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
+ }
33
43
34
44
// If there's a repo with the exact name, return it
35
- const exactRepo = repos . find (
45
+ let exactRepo = repos . find (
36
46
( repo ) => `${ repo . owner . login } /${ repo . name } ` === repoPath
37
47
)
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
+ }
38
63
return exactRepo ? [ exactRepo ] : repos
39
64
}
40
65
@@ -48,17 +73,33 @@ export const cloneRepo = async (repo: string, destination: string) => {
48
73
}
49
74
50
75
// 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 = [
53
81
'search' ,
54
82
'repos' ,
55
83
repoPath ,
56
84
'--json' ,
57
85
'owner,name,url,description' ,
58
- ] )
86
+ '--limit' ,
87
+ '10' ,
88
+ ]
59
89
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
+ }
62
103
logger . debug ( repos )
63
104
64
105
return repos
0 commit comments