deno.land / std@0.91.0 / node / _tools / setup.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
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
import { gunzip } from "https://deno.land/x/compress@v0.3.6/gzip/gzip.ts";import { Untar } from "../../archive/tar.ts";import { walk } from "../../fs/walk.ts";import { basename, fromFileUrl, join, resolve } from "../../path/mod.ts";import { ensureFile } from "../../fs/ensure_file.ts";import { config, ignoreList } from "./common.ts";
/** * This script will download and extract the test files specified in the * configuration file * * It will delete any previous tests unless they are specified on the `ignore` * section of the configuration file * * Usage: `deno run --allow-read --allow-net --allow-write setup.ts` */
const NODE_URL = "https://nodejs.org/dist/vNODE_VERSION";const NODE_FILE = "node-vNODE_VERSION.tar.gz";
/** URL for the download */const url = `${NODE_URL}/${NODE_FILE}`.replaceAll( "NODE_VERSION", config.nodeVersion,);/** Local url location */const path = join( config.versionsFolder, NODE_FILE.replaceAll("NODE_VERSION", config.nodeVersion),);
/** * This will overwrite the file if found * */async function downloadFile(url: string, path: string) { console.log(`Downloading: ${url}...`); const fileContent = await fetch(url) .then((response) => { if (response.ok) { if (!response.body) { throw new Error( `The requested download url ${url} doesn't contain an archive to download`, ); } return response.body.getIterator(); } else if (response.status === 404) { throw new Error( `The requested version ${config.nodeVersion} could not be found for download`, ); } throw new Error(`Request failed with status ${response.status}`); });
const filePath = fromFileUrl(new URL(path, import.meta.url));
await ensureFile(filePath); const file = await Deno.open(filePath, { truncate: true, write: true, }); for await (const chunk of fileContent) { await Deno.write(file.rid, chunk); } file.close(); console.log(`Downloaded: ${url} into ${path}`);}
async function clearTests() { console.log("Cleaning up previous tests");
const files = walk( (fromFileUrl(new URL(config.suitesFolder, import.meta.url))), { includeDirs: false, skip: ignoreList, }, );
for await (const file of files) { await Deno.remove(file.path); }}
/** * This will iterate over the ignore and test lists defined in the * configuration file * * If it were to be found in the ignore list or not found in the test list, the * function will return undefined, meaning the file won't be regenerated */function getRequestedFileSuite(file: string): string | undefined { for (const regex of ignoreList) { if (regex.test(file)) { return; } }
for (const suite in config.tests) { for (const regex of config.tests[suite]) { if (new RegExp(regex).test(file)) { return suite; } } }}
async function decompressTests(filePath: string) { console.log(`Processing ${basename(filePath)}...`); const compressedFile = await Deno.open( new URL(filePath, import.meta.url), { read: true }, );
const buffer = new Deno.Buffer(gunzip(await Deno.readAll(compressedFile))); Deno.close(compressedFile.rid);
const tar = new Untar(buffer);
for await (const entry of tar) { if (entry.type !== "file") continue; const suite = getRequestedFileSuite(entry.fileName); if (!suite) continue; const path = resolve( fromFileUrl(new URL(config.suitesFolder, import.meta.url)), suite, basename(entry.fileName), ); await ensureFile(path); const file = await Deno.open(path, { create: true, truncate: true, write: true, }); // This will allow CI to pass without checking linting and formatting // on the test suite files, removing the need to mantain that as well await Deno.writeAll( file, new TextEncoder().encode( "// deno-fmt-ignore-file\n// deno-lint-ignore-file\n" + "\n// Copyright Joyent and Node contributors. All rights reserved. MIT license.\n" + `// Taken from Node ${config.nodeVersion}\n` + '// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually\n\n', ), ); await Deno.copy(entry, file); Deno.close(file.rid); }}
let shouldDownload = false;try { Deno.lstatSync(new URL(path, import.meta.url)).isFile; while (true) { const r = (prompt(`File "${path}" found, use file? Y/N:`) ?? "").trim() .toUpperCase(); if (r === "Y") { break; } else if (r === "N") { shouldDownload = true; break; } else { console.log(`Unexpected: "${r}"`); } }} catch (e) { if (!(e instanceof Deno.errors.NotFound)) { throw e; } shouldDownload = true;}
if (shouldDownload) { console.log(`Downloading ${url} in path "${path}" ...`); await downloadFile(url, path);}
await clearTests();
await decompressTests(path);
std

Version Info

Tagged at
3 years ago