-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
227 lines (214 loc) · 5.7 KB
/
mod.ts
File metadata and controls
227 lines (214 loc) · 5.7 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import {
cacheDir,
copy,
globToRegExp,
GzDecoder,
join,
readerFromStreamReader,
Transform,
Untar,
} from "./deps.ts";
import type {
ConfigEntry,
GithubPickOptions,
PathLike,
PickConfig,
ReadableEntry,
ReadPredicate,
ReadStrategy,
} from "./mod.d.ts";
export const CACHE_DIR = join((await cacheDir()) ?? ".cache", "__pick_cache0");
await Deno.mkdir(CACHE_DIR).catch((_) => {});
/**
* Read config and write files
*/
export async function write(entires: PickConfig) {
const _entries: ReadableEntry[] = entires.map(asReadable$);
for (const entry of _entries) {
const output = entry.output;
await Deno.mkdir(output, { recursive: true });
for await (const reader of entry.read()) {
const file = await Deno.open(
join(output, filename(reader.fileName ?? "")),
{ create: true, write: true },
);
await copy(reader, file);
file.close();
}
}
}
/**
* Decorator which makes config entries readable
*/
function asReadable$(entry: ConfigEntry): ReadableEntry {
// convert globs to regex if required
const pick = entry.pick.map((p) => {
if (typeof p === "string") {
return globToRegExp(p);
}
return p;
});
// if read is a function, use it
let read: ReadableEntry["read"];
if (typeof entry.read === "function") {
read = () => (entry.read as ReadPredicate)(entry);
} else {
// otherwise, use a specific strategy
const strategy = entry.strategy ?? autoStrategy$(entry.source);
switch (strategy) {
case "tar": {
read = () => tarPickFiles(entry.source, pick);
break;
}
case "targz": {
read = () => tarGzPickFiles(entry.source, pick);
break;
}
case "github": {
const [repo, ...version] = entry.source.split("@");
if (!version) {
throw new Error(
`Invalid source format: ${entry.source}
Expected: <repo>@<version>`,
);
}
read = () => githubPick({ repo, version: version.join("@"), pick });
break;
}
default: {
throw new Error(`Unknown strategy: ${entry.strategy}`);
}
}
}
return {
...entry,
read,
};
}
function autoStrategy$(source: string): ReadStrategy {
if (source.endsWith(".tar")) {
return "tar";
} else if (source.endsWith(".tar.gz")) {
return "targz";
} else if (source.includes("@")) {
return "github";
} else {
throw new Error(`Unknown strategy for source: ${source}`);
}
}
function filename(path: string) {
return path.split("/").pop() ?? "";
}
/**
* Pick files from a github repo.
* @param {GithubPickOptions} opts - { repo: "denoland/deno", version: "v1.0.0", pick: [/\.ts$/] }
*/
export async function* githubPick(
{ repo, version, pick }: GithubPickOptions,
): ReturnType<ReadableEntry["read"]> {
await Deno.mkdir(join(CACHE_DIR, repo.split("/").shift() ?? "")).catch(
(_) => {},
);
const _cached = await getFetchCache(`${repo}@${version}`);
if (_cached) {
const reader = await Deno.open(_cached);
yield* tarGzPickFiles(reader, pick);
reader.close();
} else {
yield* githubPickFiles({ repo, version, pick });
}
}
/**
* Reads *.tar.gz file from a version tag and returns generator of the files that match the pick regex.
*
* @param {GithubPickOptions} opts - { repo: "denoland/deno", version: "v1.0.0", pick: [/\.ts$/] }
*/
export async function* githubPickFiles(
{ repo, version, pick }: GithubPickOptions,
): ReturnType<ReadableEntry["read"]> {
const targz = await fetch(
`https://github.com/${repo}/archive/refs/tags/${version}.tar.gz`,
);
const name = `${repo}@${version}`;
await putFetchCache(targz.body, name);
const file = await Deno.open(join(CACHE_DIR, name));
yield* tarGzPickFiles(file, pick);
file.close();
}
/**
* Pick files from a tar.gz archive.
*
* @param {Deno.Reader} targz - Deno.open('archive.tar.gz')
* @param {RegExp[]} pick - [ /\.ts$/ ]
*/
export async function* tarGzPickFiles(
targz: Deno.Reader | Deno.FsFile | PathLike,
pick: RegExp[],
): ReturnType<ReadableEntry["read"]> {
if (typeof targz === "string" || targz instanceof URL) {
targz = await Deno.open(targz);
}
const untar = new Untar(
Transform.newReader(
targz,
new GzDecoder(),
),
);
for await (const entry of untar) {
if (entry.type === "file" && pick.some((re) => re.test(entry.fileName))) {
yield entry;
}
}
}
/**
* Pick files from a tar.gz archive.
*
* @param {Deno.Reader} tar - Deno.open('archive.tar')
* @param {RegExp[]} pick - [ /\.ts$/ ]
*/
export async function* tarPickFiles(
tar: Deno.Reader | Deno.FsFile | PathLike,
pick: RegExp[],
): ReturnType<ReadableEntry["read"]> {
if (typeof tar === "string" || tar instanceof URL) {
tar = await Deno.open(tar);
}
const untar = new Untar(tar);
for await (const entry of untar) {
if (entry.type === "file" && pick.some((re) => re.test(entry.fileName))) {
yield entry;
}
}
}
/**
* Writes fetch body to a cache file.
*/
export async function putFetchCache(body: Response["body"], name: string) {
const reader = readerFromStreamReader(body!.getReader());
const _file = join(CACHE_DIR, name);
await Deno.remove(_file).catch((_) => {});
const writer = await Deno.open(_file, {
write: true,
create: true,
});
await copy(reader, writer);
writer.close();
}
/**
* Get either the path to the cache file or null if it doesn't exist.
*/
export async function getFetchCache(name: string) {
try {
const _file = join(CACHE_DIR, name);
await Deno.lstat(_file);
return _file;
} catch (_) {
return null;
}
}
/**
* Clean the fetch cache.
*/
export async function cleanFetchCache() {
return await Deno.remove(CACHE_DIR, { recursive: true });
}