Skip to content

Commit

Permalink
Merge pull request #6 from fakoua/master
Browse files Browse the repository at this point in the history
Support multiple registry.
  • Loading branch information
AnandChowdhary authored Jun 26, 2020
2 parents 1acdd94 + d181e66 commit 7de5159
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 7 deletions.
10 changes: 8 additions & 2 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { dpx } from "./mod.ts";
import { parse } from "https://deno.land/std/flags/mod.ts";

const DENO_FLAGS = [
"-A",
Expand All @@ -11,7 +12,7 @@ const DENO_FLAGS = [
"--allow-run",
"--allow-write=",
"--reload",
"-R",
"-r",
"--lock=",
"--importmap=",
"--unstable",
Expand All @@ -25,6 +26,11 @@ if (import.meta.main) {
const flags: string[] = [];
const args: string[] = [];
let packageName = "";
let registry: string | undefined = undefined;

let argsv = parse(Deno.args);
registry = argsv.registry

Deno.args.forEach((arg, index) => {
if (index === 0) packageName = arg;
let isDenoFlag = false;
Expand All @@ -38,5 +44,5 @@ if (import.meta.main) {
if (isDenoFlag) flags.push(arg);
else args.push(arg);
});
dpx(packageName, flags, args);
dpx(packageName, flags, args, registry);
}
13 changes: 8 additions & 5 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { getRegistryUrl } from "./src/utils.ts"

/** Get the file URL to run */
export async function getEntryFile(packageName: string) {
const REPO_URL = `https://deno.land/x/${packageName}`;
export async function getEntryFile(packageName: string, registry?: string) {
let repo_url = getRegistryUrl(packageName, registry)
const potentialFiles = ["cli.ts", "mod.ts"];
let fileUrl = "";
for await (const file of potentialFiles) {
fileUrl = `${REPO_URL}/${file}`;
fileUrl = `${repo_url}/${file}`;
const fetchResult = await fetch(fileUrl);
if (fetchResult.ok) {
const text = await fetchResult.text();
Expand All @@ -28,9 +30,10 @@ export async function getEntryFile(packageName: string) {
export async function dpx(
packageName: string,
flags: string[],
args: string[]
args: string[],
registry?: string
) {
const filePath = await getEntryFile(packageName);
const filePath = await getEntryFile(packageName, registry);
return Deno.run({
cmd: ["deno", "run", ...flags, filePath, ...args],
stdout: "inherit",
Expand Down
4 changes: 4 additions & 0 deletions src/Repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type Repository = {
name: string,
url: string
}
57 changes: 57 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Repository } from "./Repository.ts"

const Repositories: Array<Repository> = [
{
name: 'deno',
url: 'https://deno.land/x'
},
{
name: 'nest',
url: 'https://x.nest.land'
}
];

/**
* Calculate the registry url for a package.
* @param packageName deno package.
* @param registry custom registry.
*/
export function getRegistryUrl(packageName: string, registry?: string): string {
// Custom Registry by url
if (registry !== undefined) {
let reg = trimChar(registry as string, "/");
return `${reg}/${packageName}`
}

//Custom Registry provided with package: repo/pack
if (packageName.indexOf('/') > 0) {
let sp = packageName.split('/')
let reg = sp[0]
packageName = sp[1]
reg = getRepositoryUrl(reg)
return `${reg}/${packageName}`
}

// Default deno.land registry
return `https://deno.land/x/${packageName}`;
}

/**
* Trim the input string with specified char.
* @param input input string.
* @param char char to trim.
*/
export function trimChar(input: string, char: string): string {
char = escapeRegExp(char);
var regEx = new RegExp("^[" + char + "]+|[" + char + "]+$", "g");
return input.replace(regEx, "");
}

function getRepositoryUrl(name: string): string {
let result = Repositories.find(r => r.name === name);
return result === undefined ? Repositories[0].url : result.url;
}

function escapeRegExp(input: string): string {
return input.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
52 changes: 52 additions & 0 deletions src/utils_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import * as utils from './utils.ts'
import { assertEquals } from "https://deno.land/std/testing/asserts.ts"

Deno.test(
"Utils - trimChar-1",
(): void => {
assertEquals(
utils.trimChar("https://www.google.com/", "/"),
"https://www.google.com"
);
}
);

Deno.test(
"Utils - trimChar-2",
(): void => {
assertEquals(
utils.trimChar("https://www.google.com//", "/"),
"https://www.google.com"
);
}
);

Deno.test(
"Utils - getRegistryUrl - Only Package Name",
(): void => {
assertEquals(
utils.getRegistryUrl("online"),
"https://deno.land/x/online"
);
}
);

Deno.test(
"Utils - getRegistryUrl - Registry URL",
(): void => {
assertEquals(
utils.getRegistryUrl("online", "https://www.myrepo.com/abc/"),
"https://www.myrepo.com/abc/online"
);
}
);

Deno.test(
"Utils - getRegistryUrl - Registry/Package",
(): void => {
assertEquals(
utils.getRegistryUrl("nest/[email protected]"),
"https://x.nest.land/[email protected]"
);
}
);

0 comments on commit 7de5159

Please sign in to comment.