Skip to content

Commit e23aa3c

Browse files
authored
Merge pull request #17 from lambdalisue/support-jsr
Support jsr
2 parents 58181b2 + b8c07ca commit e23aa3c

File tree

12 files changed

+72
-32
lines changed

12 files changed

+72
-32
lines changed

.github/workflows/jsr.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: jsr
2+
3+
env:
4+
DENO_VERSION: 1.x
5+
6+
on:
7+
push:
8+
tags:
9+
- "v*"
10+
11+
permissions:
12+
contents: read
13+
id-token: write
14+
15+
jobs:
16+
publish:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
- uses: denoland/setup-deno@v1
23+
with:
24+
deno-version: ${{ env.DENO_VERSION }}
25+
- name: Publish
26+
run: |
27+
deno run -A jsr:@david/[email protected]

.github/workflows/test.yml

+3
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ jobs:
4040
run: |
4141
deno task test
4242
timeout-minutes: 5
43+
- name: JSR publish (dry-run)
44+
run: |
45+
deno publish --dry-run

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# git-browse
22

3-
[![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno)](https://deno.land/x/git_browse)
3+
[![jsr](https://img.shields.io/jsr/v/%40lambdalisue/git-browse?logo=javascript&logoColor=white)](https://jsr.io/@lambdalisue/git-browse)
4+
[![denoland](https://img.shields.io/github/v/release/lambdalisue/deno-git-browse?logo=deno&label=denoland)](https://github.com/lambdalisue/deno-git-browse/releases)
45
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/git_browse/mod.ts)
56
[![Test](https://github.com/lambdalisue/deno-git-browse/workflows/Test/badge.svg)](https://github.com/lambdalisue/deno-git-browse/actions?query=workflow%3ATest)
67

bin/browse.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ export async function readAliasesFile(): Promise<Record<string, string>> {
5858
return {};
5959
}
6060
try {
61-
return await import(join(cdir, "browse", "aliases.json"), {
62-
with: { type: "json" },
63-
});
61+
return JSON.parse(
62+
await Deno.readTextFile(join(cdir, "browse", "aliases.json")),
63+
);
6464
} catch (err) {
65-
if (err instanceof TypeError) {
65+
if (err instanceof Deno.errors.NotFound) {
6666
return {};
6767
}
6868
throw err;

commit_url.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ export async function getCommitURL(
1818
if (!fetchURL) {
1919
throw new Error(`No remote '${remote}' found`);
2020
}
21-
const hostingService = await getHostingService(fetchURL, options);
21+
const hostingService = getHostingService(fetchURL, options);
2222
return hostingService.getCommitURL(fetchURL, commitish);
2323
}

deno.jsonc

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"lock": false,
3+
"name": "@lambdalisue/git-browse",
4+
"version": "0.0.0",
5+
"exports": "./mod.ts",
36
"tasks": {
47
"test": "deno test -A --parallel --doc",
58
"check": "deno check **/*.ts",

home_url.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ export async function getHomeURL(
1717
if (!fetchURL) {
1818
throw new Error(`No remote '${remote}' found or failed to get fetch URL.`);
1919
}
20-
const hostingService = await getHostingService(fetchURL, options);
20+
const hostingService = getHostingService(fetchURL, options);
2121
return hostingService.getHomeURL(fetchURL);
2222
}

hosting_service/mod.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
import type { ExecuteOptions } from "../process.ts";
2+
import { service as bitbucket_org } from "./services/bitbucket_org.ts";
3+
import { service as github_com } from "./services/github_com.ts";
4+
import { service as gitlab_com } from "./services/gitlab_com.ts";
5+
6+
const serviceMap = {
7+
bitbucket_org,
8+
github_com,
9+
gitlab_com,
10+
};
211

312
export type Range = number | [number, number];
413

@@ -39,24 +48,18 @@ export type HostingService = {
3948
/**
4049
* Get git hosting service from URL
4150
*/
42-
export async function getHostingService(
51+
export function getHostingService(
4352
fetchURL: URL,
4453
{ aliases }: { aliases?: Record<string, string> } = {},
45-
): Promise<HostingService> {
54+
): HostingService {
4655
const hostname = aliases?.[fetchURL.hostname] ?? fetchURL.hostname;
4756
const svcName = hostname.replace(/\W/g, "_");
48-
try {
49-
const svc = await import(
50-
new URL(`./services/${svcName}.ts`, import.meta.url).href
51-
);
52-
return svc.service;
53-
} catch (err: unknown) {
54-
if (err instanceof TypeError) {
55-
// TypeError: Module not found "...".
56-
throw new UnsupportedHostingServiceError(hostname, svcName);
57-
}
58-
throw err;
57+
// deno-lint-ignore no-explicit-any
58+
const svc = (serviceMap as any)[svcName];
59+
if (!svc) {
60+
throw new UnsupportedHostingServiceError(hostname, svcName);
5961
}
62+
return svc;
6063
}
6164

6265
export class UnsupportedHostingServiceError extends Error {

hosting_service/mod_test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { assertRejects } from "https://deno.land/[email protected]/assert/mod.ts";
1+
import { assertThrows } from "https://deno.land/[email protected]/assert/mod.ts";
22
import { assertSnapshot } from "https://deno.land/[email protected]/testing/snapshot.ts";
33
import { getHostingService, UnsupportedHostingServiceError } from "./mod.ts";
44

55
Deno.test("getHostingService", async (t) => {
6-
await t.step("throws error for unsupported hosting service", async () => {
6+
await t.step("throws error for unsupported hosting service", () => {
77
const url = new URL("https://example.com/lambdalisue/deno-git-browse");
8-
await assertRejects(
8+
assertThrows(
99
() => {
1010
return getHostingService(url);
1111
},
@@ -22,7 +22,7 @@ Deno.test("getHostingService", async (t) => {
2222
new URL("https://bitbucket.org/lambdalisue/deno-git-browse"),
2323
];
2424
for (const url of urls) {
25-
const svc = await getHostingService(url);
25+
const svc = getHostingService(url);
2626

2727
await t.step(`getHomeURL for ${url}`, async () => {
2828
const result = await svc.getHomeURL(url);
@@ -134,7 +134,7 @@ Deno.test("getHostingService with alias", async (t) => {
134134
};
135135

136136
for (const url of urls) {
137-
const svc = await getHostingService(url, { aliases });
137+
const svc = getHostingService(url, { aliases });
138138

139139
await t.step(`getHomeURL for ${url}`, async () => {
140140
const result = await svc.getHomeURL(url);

object_url.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { execute, ExecuteOptions } from "./process.ts";
2-
import { getHostingService, Range } from "./hosting_service/mod.ts";
1+
import { execute, type ExecuteOptions } from "./process.ts";
2+
import { getHostingService, type Range } from "./hosting_service/mod.ts";
33
import { getRemoteContains, getRemoteFetchURL } from "./util.ts";
44

55
type Options = ExecuteOptions & {
@@ -20,7 +20,7 @@ export async function getObjectURL(
2020
if (!fetchURL) {
2121
throw new Error(`No remote '${remote}' found`);
2222
}
23-
const hostingService = await getHostingService(fetchURL, options);
23+
const hostingService = getHostingService(fetchURL, options);
2424
const [normPath, range] = parsePath(path);
2525
const objectType = await getObjectType(commitish, normPath, options);
2626
if (objectType === "tree") {

pull_request_url.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { execute, ExecuteOptions } from "./process.ts";
2-
import { getHostingService, HostingService } from "./hosting_service/mod.ts";
1+
import { execute, type ExecuteOptions } from "./process.ts";
2+
import {
3+
getHostingService,
4+
type HostingService,
5+
} from "./hosting_service/mod.ts";
36
import {
47
__throw,
58
getCommitSHA1,
@@ -23,7 +26,7 @@ export async function getPullRequestURL(
2326
if (!fetchURL) {
2427
throw new Error(`No remote '${remote}' found`);
2528
}
26-
const hostingService = await getHostingService(fetchURL, options);
29+
const hostingService = getHostingService(fetchURL, options);
2730
if (!hostingService.getPullRequestURL) {
2831
throw new Error(
2932
`Hosting service of ${fetchURL} has no pull request URL`,

util.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { execute, ExecuteError, ExecuteOptions } from "./process.ts";
1+
import { execute, ExecuteError, type ExecuteOptions } from "./process.ts";
22

33
/**
44
* A helper function to throw error

0 commit comments

Comments
 (0)