Skip to content

Commit 7f72de7

Browse files
committed
👍 Avoid using dynamic imports
1 parent 58181b2 commit 7f72de7

File tree

8 files changed

+37
-31
lines changed

8 files changed

+37
-31
lines changed

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
}

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)