UNPKG

1.3 kBPlain TextView Raw
1import HostedGitInfo from 'hosted-git-info';
2import * as path from 'path';
3import { TaggedGitRepository } from '../types/tagged-git-repository';
4import { toForwardSlashes } from '../utils/to-forward-slashes';
5
6export type RepositoryFileURLProvider = ({
7 filename,
8 line,
9}: {
10 filename: string;
11 line?: number;
12}) => string | undefined;
13
14export function getRepositoryFileURLProvider({
15 repository,
16}: {
17 repository?: TaggedGitRepository;
18}): RepositoryFileURLProvider {
19 if (!repository) {
20 return () => undefined;
21 }
22
23 const { url, tag = '', dir = '' } = repository;
24 const info = HostedGitInfo.fromUrl(url, { noGitPlus: true });
25 if (!info) {
26 return () => undefined;
27 }
28
29 const linePrefix = getLinePrefix({ info });
30
31 return ({ filename, line }) => {
32 const filepath = toForwardSlashes(path.join(dir, filename));
33 const fileURL = info.browse(filepath, { committish: tag });
34 const lineFragment = line ? `${linePrefix}${line}` : '';
35 return `${fileURL}${lineFragment}`;
36 };
37}
38
39function getLinePrefix({ info }: { info: HostedGitInfo }): string {
40 const { type: provider } = info;
41 switch (provider) {
42 case 'bitbucket':
43 return '#lines-';
44 default:
45 return '#L';
46 }
47}