import {
type GitConfig,
} from './configTypes.ts'
import {type BranchInfo} from './dataTypes.ts'
import repoOperations from './vcses/operations.ts'
import { getLocation} from './helpers.ts'
import repoHelpers from './vcses/helpers.ts'
type BranchInfoTuple = [string, BranchInfo]
type BranchObject = {
[key: string]: BranchInfo
}
type RepoObjectTuple = [string, BranchObject]
type RepoObject = {
[key: string]: {
branches: BranchObject,
cloneUrl: string,
}
}
const getBranchNames = (repoConfig: GitConfig): Array<string> => {
return repoConfig.branchesToPull
}
let cachedRepos = null
const repos: (reposConfig: any) => Promise<RepoObject> = async (reposConfig) => {
if (cachedRepos !== null) { return cachedRepos }
const repoNames = Object.keys(reposConfig.repos)
const reposTuples: RepoObjectTuple[] = await Promise.all(repoNames.map(async (repoName): Promise<RepoObjectTuple> => {
const vcs = reposConfig.repos[repoName]._type
const branchNames = getBranchNames(reposConfig.repos[repoName])
const branchTuples: BranchInfoTuple[] = await Promise.all(branchNames.map(async (branchName): Promise<BranchInfoTuple> => {
const repoLocation = getLocation(reposConfig, branchName, repoName)
const files = await repoOperations[vcs].getFileList(repoName, branchName, repoLocation)
const patches = await repoOperations[vcs].getBranchInfo(repoName, branchName, repoLocation)
return [branchName, {
files,
patches,
}]
}))
const branchesObject: BranchObject = {}
for (let branchTuple of branchTuples) {
branchesObject[branchTuple[0]] = branchTuple[1]
}
return [repoName, branchesObject]
}))
const reposObject: RepoObject = {}
for (let repoTuple of reposTuples) {
const repoName = repoTuple[0]
const repoType = reposConfig.repos[repoName]._type
reposObject[repoName] = {
branches: repoTuple[1],
cloneUrl: repoHelpers[repoType].cloneUrl(reposConfig.baseUrl + (reposConfig.path || ""), repoName)
}
}
cachedRepos = reposObject
return reposObject
}
export default repos