-
Notifications
You must be signed in to change notification settings - Fork 365
/
get-repo-data.ts
79 lines (67 loc) · 2.44 KB
/
get-repo-data.ts
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
import { dirname } from 'path'
import util from 'util'
import { findUp } from 'find-up'
import gitRepoInfo from 'git-repo-info'
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'gitc... Remove this comment to see the full error message
import gitconfiglocal from 'gitconfiglocal'
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'pars... Remove this comment to see the full error message
import parseGitRemote from 'parse-github-url'
import { log } from './command-helpers.js'
/**
*
* @param {object} config
* @param {string} [config.remoteName]
* @param {string} config.workingDir
* @returns
*/
const getRepoData = async function ({ remoteName, workingDir }: { remoteName?: string; workingDir: string }) {
try {
const [gitConfig, gitDirectory] = await Promise.all([
util.promisify(gitconfiglocal)(workingDir),
findUp('.git', { cwd: workingDir, type: 'directory' }),
])
if (!gitDirectory || !gitConfig || !gitConfig.remote || Object.keys(gitConfig.remote).length === 0) {
throw new Error('No Git remote found')
}
const baseGitPath = dirname(gitDirectory)
if (workingDir !== baseGitPath) {
log(`Git directory located in ${baseGitPath}`)
}
if (!remoteName) {
const remotes = Object.keys(gitConfig.remote)
remoteName = remotes.find((remote) => remote === 'origin') || remotes[0]
}
if (
!Object.prototype.hasOwnProperty.call(gitConfig.remote, remoteName) ||
!gitConfig.remote[remoteName] ||
Object.keys(gitConfig.remote[remoteName]).length === 0
) {
throw new Error(
`The specified remote "${remoteName}" is not defined in Git repo. Please use --git-remote-name flag to specify a remote.`,
)
}
const { url } = gitConfig.remote[remoteName]
const { host, name, owner, repo } = parseGitRemote(url)
const { branch } = gitRepoInfo()
return {
name,
owner,
repo,
url,
branch,
// @ts-expect-error TS(7053) FIXME: Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
provider: PROVIDERS[host] || host,
httpsUrl: `https://${host}/${repo}`,
}
} catch (error) {
return {
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
error: error.message,
}
}
}
const PROVIDERS = {
'github.com': 'github',
'gitlab.com': 'gitlab',
}
export default getRepoData