Skip to content

Commit

Permalink
feat: add parse function for extract repo name
Browse files Browse the repository at this point in the history
  • Loading branch information
aster-mnch committed Nov 14, 2024
1 parent 32df2ce commit faffb3e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/octoget.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { mkdir, writeFile } from './fs';
import { Client, getEntries } from './gh';
import type { DownloadOption, DownloadResult } from './types';
import { parseGitHubURI } from './utils';

export async function download(
path: string,
options?: DownloadOption,
): Promise<DownloadResult> {
const source = extractSource(path);
const source = parseGitHubURI(path);
const dir = resolveDir(options?.dir);

const client = new Client();
Expand All @@ -31,13 +32,6 @@ export async function download(
};
}

function extractSource(path: string): string {
// TODO extract repository source string from path
// `aster-mnch/octoget` -> `aster-mnch/octoget`
// `https://github.com/aster-mnch/octoget` -> `aster-mnch/octoget`
return path;
}

function resolveDir(dir: string | undefined): string {
// TODO resolve path to download directory
return dir ?? '.';
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function parseGitHubURI(path: string): string {
const pattern =
/^(?<domain>https:\/\/github.com\/)?(?<repo>[\w.-]+\/[\w.-]+)(?<subdir>[^#]+)?(?<ref>#[\w.\/@-]+)?/;
const g = pattern.exec(path)?.groups;
if (g?.repo == null) {
throw new Error('Invalid format');
}
return g.repo;
}
16 changes: 16 additions & 0 deletions test/unit/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from 'vitest';
import { parseGitHubURI } from '../../src/utils';

describe('parseGitHubURI', () => {
it('returns as is when given `org/repo` format', () => {
expect(parseGitHubURI('aster-mnch/octoget')).toBe('aster-mnch/octoget');
});

it('returns `org/repo` when given a full URI', () => {
expect(parseGitHubURI('https://github.com/aster-mnch/octoget')).toBe('aster-mnch/octoget');
});

it('throws "Invalid format" error when given invalid syntax', () => {
expect(() => parseGitHubURI('invalid')).toThrow('Invalid format');
});
});

0 comments on commit faffb3e

Please sign in to comment.