deno.land / x / trex@v1.13.1 / handlers / handle_cache.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152/** * Copyright (c) Crew Dev. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */
import { ResolveDenoPath } from "../commands/run.ts";import { ErrorInstalling } from "../utils/logs.ts";import { LoadingSpinner } from "../tools/logs.ts";import { createHash } from "../utils/storage.ts";import { exists } from "tools-fs";import { denoApidb } from "../utils/db.ts";import { needProxy, Proxy } from "proxy";import * as colors from "fmt/colors.ts";import { STD } from "../utils/info.ts";
const { red, yellow, green, bold } = colors;
// * create a simple delayexport const delay = (time: number) => new Promise((res) => setTimeout(res, time * 1000));
/** * get cache deps path * @param {string} protocol * @param hostname * @param {string} hash * @returns string */function getCachePath(protocol: string, hostname: string, hash: string) { const user = Deno.env.get("USERNAME")! || Deno.env.get("HOME")!; protocol = protocol.includes(":") ? protocol.replace(":", "") : protocol;
if (Deno.build.os === "windows") { return `C:\\Users\\${user}\\AppData\\Local\\deno\\deps\\${protocol}\\${hostname}\\${hash}`; } // * for any linux distro else if (Deno.build.os === "linux") { return `${user}/.cache/deno/deps/${protocol}/${hostname}/${hash}`; } // * for macOs deno cache deps path else { return `${user}/Library/Caches/deno/deps/${protocol}/${hostname}/${hash}`; }}
/** * detect if a package is installed * @param {string} packageUrl */export async function isCachePackage(packageUrl: string) { if (!(packageUrl.includes("http://") || packageUrl.includes("https://"))) { throw new Error( red( "this is not a valid package url, only http or https urls are allowed", ), ).message; } // * get file path else { const { hostname, protocol, pathname, search } = new URL(packageUrl); const toHash = await createHash( "SHA-256", `${pathname}${search ? `?${search}` : ""}`, ); const filePath = getCachePath(protocol, hostname, toHash);
return { exist: await (exists(filePath) || exists(`${filePath}.metadata.json`)), path: filePath, }; }}
/** * caches packages. * @param {string} pkgName - name the package. * @param {string} pkgUrl - package url. * @return void */export async function cached(pkgName: string, pkgUrl: string, show = true) { let process: Deno.Process;
const { hostname } = new URL(pkgUrl);
const loading = LoadingSpinner( green( ` Installing ${bold(yellow(pkgName))} from ${bold(yellow(hostname))}`, ), show, ); const CMD = [ResolveDenoPath(), "cache", "-q", "--unstable"];
const target = needProxy(pkgName) ? Proxy(pkgName) : `${ pkgUrl.startsWith("https://deno.land/std") ? `${pkgUrl}mod.ts` : pkgUrl }`;
if (STD.includes(pkgName) && (await denoApidb(pkgName)).length) { process = Deno.run({ cmd: [...CMD, target], stdout: "null", stdin: "null", });
if (!(await process.status()).success) { loading?.stop(); process.close(); ErrorInstalling(); return; }
process.close(); loading?.stop(); } // * install standard package by default use mod.ts else if (STD.includes(pkgName)) { process = Deno.run({ cmd: [...CMD, target], });
if (!(await process.status()).success) { loading?.stop(); process.close(); ErrorInstalling(); return; }
process.close(); loading?.stop(); } // * install third party package else if ((await denoApidb(pkgName)).length) { process = Deno.run({ cmd: [...CMD, pkgUrl], });
// * if cannot download package, throw error message if (!(await process.status()).success) { process.close(); ErrorInstalling(); }
process.close(); loading?.stop(); } // * log error if package is not found else if (!STD.includes(pkgName) && !(await denoApidb(pkgName)).length) { throw new Error(red("package not found.")).message; }
loading?.stop();}
export default cached;
Version Info