import * as ini from 'ini' ; import { findNode } from 'utils/general' ; const subModuleURLRegex = { HTTP : /^https?:\/\/.*?$/ , HTTPGit : /^https:.*?\.git$/ , git : /^git@.*?:(.*?)\/(.*?)\.git$/ , } ; function transformModuleGitURL ( node , URL ) { const matched = URL . match ( subModuleURLRegex . git ) ; if ( ! matched ) return ; const [ _ , userName , repoName ] = matched ; return appendCommitPath ( `https://${ window . location . host }/${ userName }/${ repoName }` , node ) ; } function cutDotGit ( URL ) { return URL . replace ( /\.git$/ , '' ) ; } function appendCommitPath ( URL , node ) { return URL . replace ( /\/?$/ , `/tree/${ node . sha }` ) ; } function transformModuleHTTPDotGitURL ( node , URL ) { return appendCommitPath ( cutDotGit ( URL ) , node ) ; } function transformModuleHTTPURL ( node , URL ) { return appendCommitPath ( URL , node ) ; } type ParsedINI = { [ key ] : ParsedModule | ParsedINI | undefined ; } ; type ParsedModule = { [ key ] : string | undefined ; } ; async function handleParsed ( root , parsed ) { for ( const value of Object . values ( parsed ) ) { if ( typeof value === 'string' ) return ; const url = value ?. url ; const path = value ?. path ; if ( typeof url === 'string' && typeof path === 'string' ) { const node = await findNode ( root , path ) ; if ( node ) { if ( subModuleURLRegex . HTTPGit . test ( url ) ) { node . url = transformModuleHTTPDotGitURL ( node , url ) ; } else if ( subModuleURLRegex . git . test ( url ) ) { node . url = transformModuleGitURL ( node , url ) ; } else if ( subModuleURLRegex . HTTP . test ( url ) ) { node . url = transformModuleHTTPURL ( node , url ) ; } else { node . accessDenied = true ; } } else { } } else { await handleParsed ( root , value as ParsedINI ) ; } } } export async function resolveGitModules ( root , content ) { try { if ( Array . isArray ( root . contents ) ) { const parsed = ini . parse ( content ) ; await handleParsed ( root , parsed ) ; } } catch ( err ) { throw new Error ( `Error resolving git modules` ) ; } }