diff --git a/index.ts b/index.ts index 6337f99..76ba532 100644 --- a/index.ts +++ b/index.ts @@ -9,7 +9,7 @@ import { } from 'list-github-dir-content'; import pMap from 'p-map'; import {downloadFile} from './download.js'; -import getRepositoryInfo from './repository-info.js'; +import {getRepositoryInfo, getRepositoryPreview} from './repository-info.js'; type ApiOptions = ListGithubDirectoryOptions & {getFullData: true}; @@ -87,15 +87,25 @@ async function init() { throw new Error('You are offline'); } - const parsedPath = await getRepositoryInfo(url); - - if ('error' in parsedPath) { - // eslint-disable-next-line unicorn/prefer-switch -- I hate how it looks - if (parsedPath.error === 'NOT_A_REPOSITORY') { + const repositoryPreview = getRepositoryPreview(url); + if ('error' in repositoryPreview) { + if (repositoryPreview.error === 'NOT_A_REPOSITORY') { updateStatus('⚠ Not a repository'); - } else if (parsedPath.error === 'NOT_A_DIRECTORY') { + } else if (repositoryPreview.error === 'NOT_A_DIRECTORY') { updateStatus('⚠ Not a directory'); - } else if (parsedPath.error === 'REPOSITORY_NOT_FOUND') { + } else { + updateStatus('⚠ Unknown error'); + } + + return; + } + + updateStatus(`Repo: ${repositoryPreview.user}/${repositoryPreview.repository}`); + + const parsedPath = await getRepositoryInfo(repositoryPreview); + + if ('error' in parsedPath) { + if (parsedPath.error === 'REPOSITORY_NOT_FOUND') { updateStatus('⚠ Repository not found. If it’s private, you should enter a token that can access it.'); } else { updateStatus('⚠ Unknown error'); @@ -105,7 +115,7 @@ async function init() { } const {user, repository, gitReference, directory, isPrivate} = parsedPath; - updateStatus(`Repo: ${user}/${repository}\nDirectory: /${directory}`, { + updateStatus(`Directory: /${directory}`, { source: { user, repository, diff --git a/repository-info.test.ts b/repository-info.test.ts index 83fd578..8ceec1a 100644 --- a/repository-info.test.ts +++ b/repository-info.test.ts @@ -1,29 +1,40 @@ import {test, expect} from 'vitest'; -import getRepositoryInfo from './repository-info.js'; +import {getRepositoryInfo, getRepositoryPreview} from './repository-info.js'; + +test('getRepositoryPreview', () => { + expect(getRepositoryPreview('https://github.com/user')).toEqual({ + error: 'NOT_A_REPOSITORY', + }); + expect(getRepositoryPreview('https://github.com/fregante/doma/blob/develop/readme.md')).toEqual({ + error: 'NOT_A_DIRECTORY', + }); + expect(getRepositoryPreview('https://github.com/wesbos/JavaScript30/tree/master/01%20-%20JavaScript%20Drum%20Kit/sounds')).toEqual({ + user: 'wesbos', + repository: 'JavaScript30', + parts: ['master', '01 - JavaScript Drum Kit', 'sounds'], + directory: '01 - JavaScript Drum Kit/sounds', + }); +}); test('getRepositoryInfo', async () => { - await expect(getRepositoryInfo('https://github.com/user')).resolves.toMatchInlineSnapshot(` - { - "error": "NOT_A_REPOSITORY", - } - `); - await expect(getRepositoryInfo('https://github.com/fregante/doma/blob/develop/readme.md')).resolves.toMatchInlineSnapshot(` - { - "error": "NOT_A_DIRECTORY", - } - `); - await expect(getRepositoryInfo('https://github.com/refined-github/sandbox/tree/durian/folder')).resolves.toMatchInlineSnapshot(` + const sandboxDurianFolder = getRepositoryPreview('https://github.com/refined-github/sandbox/tree/durian/folder'); + expect(sandboxDurianFolder).not.toHaveProperty('error'); + await expect(getRepositoryInfo(sandboxDurianFolder as {user: string; repository: string; parts: string[]})).resolves.toMatchInlineSnapshot(` { "error": "BRANCH_NOT_FOUND", } `); - await expect(getRepositoryInfo('https://github.com/refined-github/private/tree/durian/folder')).resolves.toMatchInlineSnapshot(` + const privateDurianFolder = getRepositoryPreview('https://github.com/refined-github/private/tree/durian/folder'); + expect(privateDurianFolder).not.toHaveProperty('error'); + await expect(getRepositoryInfo(privateDurianFolder as {user: string; repository: string; parts: string[]})).resolves.toMatchInlineSnapshot(` { "error": "REPOSITORY_NOT_FOUND", } `); // Simple branches are not verified at this point. Should they be? - await expect(getRepositoryInfo('https://github.com/refined-github/sandbox/tree/durian')).resolves.toMatchInlineSnapshot(` + const sandboxDurian = getRepositoryPreview('https://github.com/refined-github/sandbox/tree/durian'); + expect(sandboxDurian).not.toHaveProperty('error'); + await expect(getRepositoryInfo(sandboxDurian as {user: string; repository: string; parts: string[]})).resolves.toMatchInlineSnapshot(` { "directory": "", "downloadUrl": "https://api.github.com/repos/refined-github/sandbox/zipball/durian", @@ -33,7 +44,9 @@ test('getRepositoryInfo', async () => { "user": "refined-github", } `); - await expect(getRepositoryInfo('https://github.com/refined-github/sandbox/tree/branch/with/slashes')).resolves.toMatchInlineSnapshot(` + const sandboxSlashes = getRepositoryPreview('https://github.com/refined-github/sandbox/tree/branch/with/slashes'); + expect(sandboxSlashes).not.toHaveProperty('error'); + await expect(getRepositoryInfo(sandboxSlashes as {user: string; repository: string; parts: string[]})).resolves.toMatchInlineSnapshot(` { "directory": "", "gitReference": "branch/with/slashes", @@ -42,7 +55,9 @@ test('getRepositoryInfo', async () => { "user": "refined-github", } `); - await expect(getRepositoryInfo('https://github.com/refined-github/sandbox/tree/default-a/.github/workflows')).resolves.toMatchInlineSnapshot(` + const sandboxWorkflows = getRepositoryPreview('https://github.com/refined-github/sandbox/tree/default-a/.github/workflows'); + expect(sandboxWorkflows).not.toHaveProperty('error'); + await expect(getRepositoryInfo(sandboxWorkflows as {user: string; repository: string; parts: string[]})).resolves.toMatchInlineSnapshot(` { "directory": ".github/workflows", "gitReference": "default-a", @@ -51,7 +66,9 @@ test('getRepositoryInfo', async () => { "user": "refined-github", } `); - await expect(getRepositoryInfo('https://github.com/microsoft/typescript')).resolves.toMatchInlineSnapshot(` + const microsoftTypescript = getRepositoryPreview('https://github.com/microsoft/typescript'); + expect(microsoftTypescript).not.toHaveProperty('error'); + await expect(getRepositoryInfo(microsoftTypescript as {user: string; repository: string; parts: string[]})).resolves.toMatchInlineSnapshot(` { "directory": "", "downloadUrl": "https://api.github.com/repos/microsoft/typescript/zipball", @@ -60,7 +77,9 @@ test('getRepositoryInfo', async () => { "user": "microsoft", } `); - await expect(getRepositoryInfo('https://github.com/fregante/doma/tree/develop')).resolves.toMatchInlineSnapshot(` + const domaDevelop = getRepositoryPreview('https://github.com/fregante/doma/tree/develop'); + expect(domaDevelop).not.toHaveProperty('error'); + await expect(getRepositoryInfo(domaDevelop as {user: string; repository: string; parts: string[]})).resolves.toMatchInlineSnapshot(` { "directory": "", "downloadUrl": "https://api.github.com/repos/fregante/doma/zipball/develop", @@ -70,7 +89,9 @@ test('getRepositoryInfo', async () => { "user": "fregante", } `); - await expect(getRepositoryInfo('https://github.com/wesbos/JavaScript30/tree/master/01%20-%20JavaScript%20Drum%20Kit/sounds')).resolves.toMatchInlineSnapshot(` + const javascript30Sounds = getRepositoryPreview('https://github.com/wesbos/JavaScript30/tree/master/01%20-%20JavaScript%20Drum%20Kit/sounds'); + expect(javascript30Sounds).not.toHaveProperty('error'); + await expect(getRepositoryInfo(javascript30Sounds as {user: string; repository: string; parts: string[]})).resolves.toMatchInlineSnapshot(` { "directory": "01 - JavaScript Drum Kit/sounds", "gitReference": "master", diff --git a/repository-info.ts b/repository-info.ts index 525b974..e759aab 100644 --- a/repository-info.ts +++ b/repository-info.ts @@ -6,6 +6,33 @@ function cleanUrl(url: string) { .replace(/[/]$/, ''); // Drop trailing slash } +export function getRepositoryPreview(url: string): +| {error: 'NOT_A_REPOSITORY' | 'NOT_A_DIRECTORY'} +| {user: string; repository: string; parts: string[]; directory: string} { + const [, user, repository, ...restPathParts] = cleanUrl( + decodeURIComponent(new URL(url).pathname), + ).split('/'); + const type = restPathParts[0]; + + if (!user || !repository) { + return {error: 'NOT_A_REPOSITORY'}; + } + + if (type && type !== 'tree') { + return {error: 'NOT_A_DIRECTORY'}; + } + + const directoryParts = type === 'tree' ? restPathParts.slice(2) : []; + const parts = type === 'tree' ? restPathParts.slice(1) : []; + + return { + user, + repository, + parts, + directory: directoryParts.join('/'), + }; +} + async function parsePath( user: string, repo: string, @@ -23,8 +50,8 @@ async function parsePath( } } -export default async function getRepositoryInfo( - url: string, +export async function getRepositoryInfo( + repositoryInfo: {user: string; repository: string; parts: string[]}, ): Promise< | {error: string} | { @@ -43,17 +70,7 @@ export default async function getRepositoryInfo( isPrivate: boolean; } > { - const [, user, repository, type, ...parts] = cleanUrl( - decodeURIComponent(new URL(url).pathname), - ).split('/'); - - if (!user || !repository) { - return {error: 'NOT_A_REPOSITORY'}; - } - - if (type && type !== 'tree') { - return {error: 'NOT_A_DIRECTORY'}; - } + const {user, repository, parts} = repositoryInfo; const repoInfoResponse = await authenticatedFetch( `https://api.github.com/repos/${user}/${repository}`,