Skip to content

Commit

Permalink
[feat] support custom cache dir
Browse files Browse the repository at this point in the history
  • Loading branch information
myr authored and myr committed Oct 13, 2020
1 parent 5a53073 commit d83399c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Expand Up @@ -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?

Expand All @@ -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";
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions 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;

Expand All @@ -14,22 +14,26 @@ export interface PrepareOptions {
name: string;
printLog?: boolean;
checkCache?: boolean;
cacheDir?: string;
urls: {
darwin?: string;
linux?: string;
windows?: string;
};
}

const defaultCacheDir = Deno.env.get("DENO_PLUGIN_DIR") ?? ".deno_plugins";

export async function download(options: PrepareOptions): Promise<string> {
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) {
Expand All @@ -38,7 +42,7 @@ export async function download(options: PrepareOptions): Promise<string> {
);
}

await Deno.mkdir(".deno_plugins", { recursive: true });
await Deno.mkdir(cacheDir, { recursive: true });

if (remoteUrl.startsWith("file://")) {
const fromPath = path.resolve(remoteUrl.slice(7));
Expand Down
6 changes: 5 additions & 1 deletion 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";

Expand Down Expand Up @@ -28,6 +28,7 @@ Deno.test("testPrepareLocal", async () => {
"run",
"--allow-read",
"--allow-write",
"--allow-env",
"--allow-plugin",
"--unstable",
"./tests/local.ts",
Expand All @@ -45,6 +46,7 @@ Deno.test("testPrepareLocal", async () => {
"run",
"--allow-read",
"--allow-write",
"--allow-env",
"--allow-plugin",
"--unstable",
"./tests/local.ts",
Expand Down Expand Up @@ -84,6 +86,7 @@ Deno.test("testPrepareRemote", async () => {
"deno",
"run",
"--allow-read",
"--allow-env",
"--allow-write",
"--allow-plugin",
"--allow-net",
Expand All @@ -103,6 +106,7 @@ Deno.test("testPrepareRemote", async () => {
"deno",
"run",
"--allow-read",
"--allow-env",
"--allow-write",
"--allow-plugin",
"--allow-net",
Expand Down

0 comments on commit d83399c

Please sign in to comment.