diff --git a/cli.ts b/cli.ts index 2b03f19..70cd9c4 100644 --- a/cli.ts +++ b/cli.ts @@ -1,4 +1,5 @@ import { dpx } from "./mod.ts"; +import { parse } from "https://deno.land/std/flags/mod.ts"; const DENO_FLAGS = [ "-A", @@ -11,7 +12,7 @@ const DENO_FLAGS = [ "--allow-run", "--allow-write=", "--reload", - "-R", + "-r", "--lock=", "--importmap=", "--unstable", @@ -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; @@ -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); } diff --git a/mod.ts b/mod.ts index 5494b97..c143ed8 100644 --- a/mod.ts +++ b/mod.ts @@ -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(); @@ -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", diff --git a/src/Repository.ts b/src/Repository.ts new file mode 100644 index 0000000..4d3e511 --- /dev/null +++ b/src/Repository.ts @@ -0,0 +1,4 @@ +export type Repository = { + name: string, + url: string +} \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..9e2c257 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,57 @@ +import { Repository } from "./Repository.ts" + +const Repositories: Array = [ + { + 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, "\\$&"); +} \ No newline at end of file diff --git a/src/utils_test.ts b/src/utils_test.ts new file mode 100644 index 0000000..5e32a83 --- /dev/null +++ b/src/utils_test.ts @@ -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/deno_gui@2.0.5"), + "https://x.nest.land/deno_gui@2.0.5" + ); + } + ); \ No newline at end of file