deno.land / x / trex@v1.13.1 / handlers / handle_cache.ts

handle_cache.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/** * 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;
trex

Version Info

Tagged at
9 months ago