generated from denorg/starter
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from fakoua/master
Support multiple registry.
- Loading branch information
Showing
5 changed files
with
129 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Repository = { | ||
name: string, | ||
url: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "\\$&"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
); | ||
} | ||
); |