diff --git a/README.md b/README.md index f73a3d4..7d14e94 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A library for managing deno native plugin dependencies [![tag](https://img.shields.io/github/tag/manyuanrong/deno-plugin-prepare.svg)](https://github.com/manyuanrong/deno-plugin-prepare) [![Build Status](https://github.com/manyuanrong/deno-plugin-prepare/workflows/ci/badge.svg?branch=master)](https://github.com/manyuanrong/deno-plugin-prepare/actions) [![license](https://img.shields.io/github/license/manyuanrong/deno-plugin-prepare.svg)](https://github.com/manyuanrong/deno-plugin-prepare) -[![tag](https://img.shields.io/badge/deno-v1.0.0-green.svg)](https://github.com/denoland/deno) +[![tag](https://img.shields.io/badge/deno-v1.4.0-green.svg)](https://github.com/denoland/deno) ### Why do you need this module? @@ -24,8 +24,13 @@ The API needs to provide some plug-in information, including the name of the plu ```ts import { prepare, +<<<<<<< HEAD PrepareOptions, } from "https://deno.land/x/plugin_prepare@v0.6.0/mod.ts"; +======= + PerpareOptions, +} from "https://deno.land/x/plugin_prepare@v0.8.0/mod.ts"; +>>>>>>> [feat] support custom cache dir const releaseUrl = "https://github.com/manyuanrong/deno-plugin-prepare/releases/download/plugin_bins"; @@ -59,6 +64,10 @@ console.log(response); Deno.close(rid); ``` +### Custom cache path + +By default, binary files will be cached in the `.deno_plugins` directory. In some cases, we need to store in other locations, you can set the `DENO_PLUGIN_DIR` environment variable + ### TODOs - [x] Caching binary files with URL hash (multi-version coexistence) diff --git a/mod.ts b/mod.ts index e48d5b5..e140621 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,4 @@ -import { exists, createHash, log, path } from "./deps.ts"; +import { createHash, exists, log, path } from "./deps.ts"; const os = Deno.build.os; @@ -14,6 +14,7 @@ export interface PrepareOptions { name: string; printLog?: boolean; checkCache?: boolean; + cacheDir?: string; urls: { darwin?: string; linux?: string; @@ -21,15 +22,18 @@ export interface PrepareOptions { }; } +const defaultCacheDir = Deno.env.get("DENO_PLUGIN_DIR") ?? ".deno_plugins"; + export async function download(options: PrepareOptions): Promise { const { name, urls, checkCache = true } = options; + const cacheDir = options.cacheDir ?? defaultCacheDir; const remoteUrl = urls[os]; const md5 = createHash("md5"); md5.update(remoteUrl + pluginSuffix); const remoteHash = md5.toString("hex"); const cacheFileName = `${name}_${remoteHash}${pluginSuffix}`; - const localPath = path.resolve(".deno_plugins", cacheFileName); + const localPath = path.resolve(cacheDir, cacheFileName); if (!(await exists(localPath)) || !checkCache) { if (!remoteUrl) { @@ -38,7 +42,7 @@ export async function download(options: PrepareOptions): Promise { ); } - await Deno.mkdir(".deno_plugins", { recursive: true }); + await Deno.mkdir(cacheDir, { recursive: true }); if (remoteUrl.startsWith("file://")) { const fromPath = path.resolve(remoteUrl.slice(7)); diff --git a/test.ts b/test.ts index d2b3c5b..50e433f 100644 --- a/test.ts +++ b/test.ts @@ -1,5 +1,5 @@ import { exists } from "./deps.ts"; -import { assert, serve, serveFile, resolve } from "./test_deps.ts"; +import { assert, resolve, serve, serveFile } from "./test_deps.ts"; const pluginDir = ".deno_plugins"; @@ -28,6 +28,7 @@ Deno.test("testPrepareLocal", async () => { "run", "--allow-read", "--allow-write", + "--allow-env", "--allow-plugin", "--unstable", "./tests/local.ts", @@ -45,6 +46,7 @@ Deno.test("testPrepareLocal", async () => { "run", "--allow-read", "--allow-write", + "--allow-env", "--allow-plugin", "--unstable", "./tests/local.ts", @@ -84,6 +86,7 @@ Deno.test("testPrepareRemote", async () => { "deno", "run", "--allow-read", + "--allow-env", "--allow-write", "--allow-plugin", "--allow-net", @@ -103,6 +106,7 @@ Deno.test("testPrepareRemote", async () => { "deno", "run", "--allow-read", + "--allow-env", "--allow-write", "--allow-plugin", "--allow-net",