Skip to content

Commit 7de5159

Browse files
Merge pull request #6 from fakoua/master
Support multiple registry.
2 parents 1acdd94 + d181e66 commit 7de5159

File tree

5 files changed

+129
-7
lines changed

5 files changed

+129
-7
lines changed

cli.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { dpx } from "./mod.ts";
2+
import { parse } from "https://deno.land/std/flags/mod.ts";
23

34
const DENO_FLAGS = [
45
"-A",
@@ -11,7 +12,7 @@ const DENO_FLAGS = [
1112
"--allow-run",
1213
"--allow-write=",
1314
"--reload",
14-
"-R",
15+
"-r",
1516
"--lock=",
1617
"--importmap=",
1718
"--unstable",
@@ -25,6 +26,11 @@ if (import.meta.main) {
2526
const flags: string[] = [];
2627
const args: string[] = [];
2728
let packageName = "";
29+
let registry: string | undefined = undefined;
30+
31+
let argsv = parse(Deno.args);
32+
registry = argsv.registry
33+
2834
Deno.args.forEach((arg, index) => {
2935
if (index === 0) packageName = arg;
3036
let isDenoFlag = false;
@@ -38,5 +44,5 @@ if (import.meta.main) {
3844
if (isDenoFlag) flags.push(arg);
3945
else args.push(arg);
4046
});
41-
dpx(packageName, flags, args);
47+
dpx(packageName, flags, args, registry);
4248
}

mod.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { getRegistryUrl } from "./src/utils.ts"
2+
13
/** Get the file URL to run */
2-
export async function getEntryFile(packageName: string) {
3-
const REPO_URL = `https://deno.land/x/${packageName}`;
4+
export async function getEntryFile(packageName: string, registry?: string) {
5+
let repo_url = getRegistryUrl(packageName, registry)
46
const potentialFiles = ["cli.ts", "mod.ts"];
57
let fileUrl = "";
68
for await (const file of potentialFiles) {
7-
fileUrl = `${REPO_URL}/${file}`;
9+
fileUrl = `${repo_url}/${file}`;
810
const fetchResult = await fetch(fileUrl);
911
if (fetchResult.ok) {
1012
const text = await fetchResult.text();
@@ -28,9 +30,10 @@ export async function getEntryFile(packageName: string) {
2830
export async function dpx(
2931
packageName: string,
3032
flags: string[],
31-
args: string[]
33+
args: string[],
34+
registry?: string
3235
) {
33-
const filePath = await getEntryFile(packageName);
36+
const filePath = await getEntryFile(packageName, registry);
3437
return Deno.run({
3538
cmd: ["deno", "run", ...flags, filePath, ...args],
3639
stdout: "inherit",

src/Repository.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type Repository = {
2+
name: string,
3+
url: string
4+
}

src/utils.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Repository } from "./Repository.ts"
2+
3+
const Repositories: Array<Repository> = [
4+
{
5+
name: 'deno',
6+
url: 'https://deno.land/x'
7+
},
8+
{
9+
name: 'nest',
10+
url: 'https://x.nest.land'
11+
}
12+
];
13+
14+
/**
15+
* Calculate the registry url for a package.
16+
* @param packageName deno package.
17+
* @param registry custom registry.
18+
*/
19+
export function getRegistryUrl(packageName: string, registry?: string): string {
20+
// Custom Registry by url
21+
if (registry !== undefined) {
22+
let reg = trimChar(registry as string, "/");
23+
return `${reg}/${packageName}`
24+
}
25+
26+
//Custom Registry provided with package: repo/pack
27+
if (packageName.indexOf('/') > 0) {
28+
let sp = packageName.split('/')
29+
let reg = sp[0]
30+
packageName = sp[1]
31+
reg = getRepositoryUrl(reg)
32+
return `${reg}/${packageName}`
33+
}
34+
35+
// Default deno.land registry
36+
return `https://deno.land/x/${packageName}`;
37+
}
38+
39+
/**
40+
* Trim the input string with specified char.
41+
* @param input input string.
42+
* @param char char to trim.
43+
*/
44+
export function trimChar(input: string, char: string): string {
45+
char = escapeRegExp(char);
46+
var regEx = new RegExp("^[" + char + "]+|[" + char + "]+$", "g");
47+
return input.replace(regEx, "");
48+
}
49+
50+
function getRepositoryUrl(name: string): string {
51+
let result = Repositories.find(r => r.name === name);
52+
return result === undefined ? Repositories[0].url : result.url;
53+
}
54+
55+
function escapeRegExp(input: string): string {
56+
return input.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
57+
}

src/utils_test.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as utils from './utils.ts'
2+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"
3+
4+
Deno.test(
5+
"Utils - trimChar-1",
6+
(): void => {
7+
assertEquals(
8+
utils.trimChar("https://www.google.com/", "/"),
9+
"https://www.google.com"
10+
);
11+
}
12+
);
13+
14+
Deno.test(
15+
"Utils - trimChar-2",
16+
(): void => {
17+
assertEquals(
18+
utils.trimChar("https://www.google.com//", "/"),
19+
"https://www.google.com"
20+
);
21+
}
22+
);
23+
24+
Deno.test(
25+
"Utils - getRegistryUrl - Only Package Name",
26+
(): void => {
27+
assertEquals(
28+
utils.getRegistryUrl("online"),
29+
"https://deno.land/x/online"
30+
);
31+
}
32+
);
33+
34+
Deno.test(
35+
"Utils - getRegistryUrl - Registry URL",
36+
(): void => {
37+
assertEquals(
38+
utils.getRegistryUrl("online", "https://www.myrepo.com/abc/"),
39+
"https://www.myrepo.com/abc/online"
40+
);
41+
}
42+
);
43+
44+
Deno.test(
45+
"Utils - getRegistryUrl - Registry/Package",
46+
(): void => {
47+
assertEquals(
48+
utils.getRegistryUrl("nest/deno_gui@2.0.5"),
49+
"https://x.nest.land/deno_gui@2.0.5"
50+
);
51+
}
52+
);

0 commit comments

Comments
 (0)