Skip to content

Commit

Permalink
feat: UrlOptions for name-less operation which actually works :P
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen committed Jan 26, 2022
1 parent a4698df commit e7763a1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { extname, parse } from "https://deno.land/std@0.122.0/path/mod.ts";
export { extname } from "https://deno.land/std@0.122.0/path/mod.ts";
export { green } from "https://deno.land/std@0.122.0/fmt/colors.ts";

export * as Cache from "https://deno.land/x/cache@0.2.13/mod.ts";
22 changes: 13 additions & 9 deletions plug.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cache, extname, green, parse } from "./deps.ts";
import { Cache, extname, green } from "./deps.ts";

export interface CrossOptions {
name: string;
Expand All @@ -8,6 +8,13 @@ export interface CrossOptions {
log?: boolean;
}

export interface UrlOptions {
url: string;
policy?: CachePolicy;
cache?: string;
log?: boolean;
}

export interface SingleOptions {
name: string;
url: string;
Expand All @@ -16,7 +23,7 @@ export interface SingleOptions {
log?: boolean;
}

export type Options = string | CrossOptions | SingleOptions;
export type Options = string | UrlOptions | CrossOptions | SingleOptions;

export type CachePolicy = "NONE" | "STORE";
export const CachePolicy = {
Expand Down Expand Up @@ -56,10 +63,7 @@ export class PlugImportError extends Error {

export async function download(options: Options): Promise<string> {
if (typeof options === "string") {
options = {
name: parse(options).name,
url: options,
};
options = { url: options };
}

const directory = options.cache ?? Cache.options.directory;
Expand All @@ -81,9 +85,9 @@ export async function download(options: Options): Promise<string> {
const pref = prefixes[os];
const ext = extensions[os];

url = Object.values(extensions).includes(extname(url))
? url
: `${url}${(url.endsWith("/") ? "" : "/")}${pref}${options.name}${ext}`;
if ("name" in options && !Object.values(extensions).includes(extname(url))) {
url = `${url}${(url.endsWith("/") ? "" : "/")}${pref}${options.name}${ext}`;
}

const plug = Cache.namespace("plug");
if ((options.log ?? true) && !(await plug.exists(url))) {
Expand Down

0 comments on commit e7763a1

Please sign in to comment.