Skip to content

Commit f341154

Browse files
committed
👍 Support aliases.json to define custom aliases
1 parent e41d3cd commit f341154

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

bin/browse.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
#!/usr/bin/env -S deno run --allow-run --allow-read
1+
#!/usr/bin/env -S deno run --allow-run --allow-read --allow-env
22
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
3+
import { join } from "https://deno.land/[email protected]/path/mod.ts";
34
import { ensure, is } from "https://deno.land/x/[email protected]/mod.ts";
45
import { systemopen } from "https://deno.land/x/[email protected]/mod.ts";
6+
import config_dir from "https://deno.land/x/[email protected]/config_dir/mod.ts";
57
import {
68
getCommitAbbrevRef,
79
getCommitSHA1,
@@ -27,6 +29,7 @@ export async function getURL(
2729
commitish: string,
2830
options: Options = {},
2931
): Promise<URL> {
32+
options.aliases = options.aliases ?? await readAliasesFile();
3033
if (options.home) {
3134
return getHomeURL(options);
3235
}
@@ -49,6 +52,23 @@ export async function getURL(
4952
return getObjectURL(commitish, options.path ?? ".", options);
5053
}
5154

55+
export async function readAliasesFile(): Promise<Record<string, string>> {
56+
const cdir = config_dir();
57+
if (!cdir) {
58+
return {};
59+
}
60+
try {
61+
return await import(join(cdir, "browse", "aliases.json"), {
62+
assert: { type: "json" },
63+
});
64+
} catch (err) {
65+
if (err instanceof TypeError) {
66+
return {};
67+
}
68+
throw err;
69+
}
70+
}
71+
5272
async function main(args: string[]): Promise<void> {
5373
const opts = parse(args, {
5474
boolean: [

bin/browse_usage.txt

+23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ OPTIONS
1212
--permalink Use a permalink instead of a regular URL.
1313
--remote=REMOTE Use a URL from the specified remote repository.
1414

15+
ALIASES
16+
To enable support for non-standard repository URLs (e.g., GitHub Enterprise), you can create aliases
17+
for the repository URL. To define these aliases, create a file named "aliases.json" within the
18+
specified configuration directory, as illustrated below:
19+
20+
{
21+
"my.github.com": "github.com",
22+
// Add more aliases as needed
23+
}
24+
25+
This file needs to be stored in the "browse" directory within the configuration directory, as
26+
indicated by the following paths based on your operating system:
27+
28+
┌─────────┬────────────────────────────────────────────┬──────────────────────────────────────────┐
29+
│ OS │ Value │ Example │
30+
├─────────┼────────────────────────────────────────────┼──────────────────────────────────────────┤
31+
│ Linux │ ($XDG_CONFIG_HOME or $HOME/.config)/browse │ /home/alisue/.config/browse │
32+
├─────────┼────────────────────────────────────────────┼──────────────────────────────────────────┤
33+
│ macOS │ $HOME/Library/Preferences/browse │ /Users/alisue/Library/Preferences/browse │
34+
├─────────┼────────────────────────────────────────────┼──────────────────────────────────────────┤
35+
│ Windows │ $APPDATA\browse │ C:\Users\alisue\AppData\Roaming\browse │
36+
└─────────┴────────────────────────────────────────────┴──────────────────────────────────────────┘
37+
1538
EXAMPLES
1639
$ browse
1740
#=> Opens a tree page of the current working directory in the HEAD commit of the current branch

0 commit comments

Comments
 (0)