-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(node/_tools): make setup.ts more efficient (#917)
- Loading branch information
Showing
1 changed file
with
100 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import { gunzip } from "https://deno.land/x/[email protected]/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 { | ||
basename, | ||
dirname, | ||
fromFileUrl, | ||
join, | ||
resolve, | ||
} from "../../path/mod.ts"; | ||
import { ensureFile } from "../../fs/ensure_file.ts"; | ||
import { config, ignoreList } from "./common.ts"; | ||
import { Buffer } from "../../io/buffer.ts"; | ||
|
@@ -18,15 +24,22 @@ import { readAll, writeAll } from "../../io/util.ts"; | |
*/ | ||
|
||
const NODE_URL = "https://nodejs.org/dist/vNODE_VERSION"; | ||
const NODE_FILE = "node-vNODE_VERSION.tar.gz"; | ||
const NODE_FILE = "node-vNODE_VERSION"; | ||
const NODE_ARCHIVE_FILE = `${NODE_FILE}.tar.gz`; | ||
const NATIVE_NODE_TESTS_FOLDER = "/test"; | ||
|
||
/** URL for the download */ | ||
const url = `${NODE_URL}/${NODE_FILE}`.replaceAll( | ||
const url = `${NODE_URL}/${NODE_ARCHIVE_FILE}`.replaceAll( | ||
"NODE_VERSION", | ||
config.nodeVersion, | ||
); | ||
/** Local url location */ | ||
const path = join( | ||
/** Local archive's url location */ | ||
const archivePath = join( | ||
config.versionsFolder, | ||
NODE_ARCHIVE_FILE.replaceAll("NODE_VERSION", config.nodeVersion), | ||
); | ||
/** Local decompressed source's location */ | ||
const decompressedSourcePath = join( | ||
config.versionsFolder, | ||
NODE_FILE.replaceAll("NODE_VERSION", config.nodeVersion), | ||
); | ||
|
@@ -106,54 +119,90 @@ function getRequestedFileSuite(file: string): string | undefined { | |
} | ||
} | ||
|
||
async function decompressTests(filePath: string) { | ||
console.log(`Processing ${basename(filePath)}...`); | ||
async function decompressTests(archivePath: string) { | ||
console.log(`Decompressing ${basename(archivePath)}...`); | ||
|
||
const compressedFile = await Deno.open( | ||
new URL(filePath, import.meta.url), | ||
new URL(archivePath, import.meta.url), | ||
{ read: true }, | ||
); | ||
|
||
const buffer = new Buffer(gunzip(await readAll(compressedFile))); | ||
Deno.close(compressedFile.rid); | ||
|
||
const tar = new Untar(buffer); | ||
const outFolder = dirname(fromFileUrl(new URL(archivePath, import.meta.url))); | ||
const testsFolder = `${NODE_FILE}${NATIVE_NODE_TESTS_FOLDER}`.replace( | ||
"NODE_VERSION", | ||
config.nodeVersion, | ||
); | ||
|
||
for await (const entry of tar) { | ||
if (entry.type !== "file") continue; | ||
const suite = getRequestedFileSuite(entry.fileName); | ||
if (!entry.fileName.startsWith(testsFolder)) continue; | ||
const path = join(outFolder, entry.fileName); | ||
await ensureFile(path); | ||
const file = await Deno.open(path, { | ||
create: true, | ||
truncate: true, | ||
write: true, | ||
}); | ||
await Deno.copy(entry, file); | ||
file.close(); | ||
} | ||
} | ||
|
||
async function copyTests(filePath: string): Promise<void> { | ||
console.log("Copying test files..."); | ||
const path = join( | ||
fromFileUrl(new URL(filePath, import.meta.url)), | ||
NATIVE_NODE_TESTS_FOLDER, | ||
); | ||
const suitesFolder = fromFileUrl( | ||
new URL(config.suitesFolder, import.meta.url), | ||
); | ||
for await (const entry of walk(path)) { | ||
const suite = getRequestedFileSuite(entry.name); | ||
if (!suite) continue; | ||
const path = resolve( | ||
fromFileUrl(new URL(config.suitesFolder, import.meta.url)), | ||
|
||
const destPath = resolve( | ||
suitesFolder, | ||
suite, | ||
basename(entry.fileName), | ||
basename(entry.name), | ||
); | ||
await ensureFile(path); | ||
const file = await Deno.open(path, { | ||
await ensureFile(destPath); | ||
const destFile = await Deno.open(destPath, { | ||
create: true, | ||
truncate: true, | ||
write: true, | ||
}); | ||
const srcFile = await Deno.open( | ||
join(path, suite, entry.name), | ||
{ read: 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 writeAll( | ||
file, | ||
destFile, | ||
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); | ||
await Deno.copy(srcFile, destFile); | ||
srcFile.close(); | ||
destFile.close(); | ||
} | ||
} | ||
|
||
let shouldDownload = false; | ||
try { | ||
Deno.lstatSync(new URL(path, import.meta.url)).isFile; | ||
Deno.lstatSync(new URL(archivePath, import.meta.url)); | ||
while (true) { | ||
const r = (prompt(`File "${path}" found, use file? Y/N:`) ?? "").trim() | ||
const r = (prompt(`File "${archivePath}" found, use file? Y/N:`) ?? "") | ||
.trim() | ||
.toUpperCase(); | ||
if (r === "Y") { | ||
break; | ||
|
@@ -172,10 +221,38 @@ try { | |
} | ||
|
||
if (shouldDownload) { | ||
console.log(`Downloading ${url} in path "${path}" ...`); | ||
await downloadFile(url, path); | ||
console.log(`Downloading ${url} in path "${archivePath}" ...`); | ||
await downloadFile(url, archivePath); | ||
} | ||
|
||
await clearTests(); | ||
let shouldDecompress = false; | ||
try { | ||
const p = new URL(decompressedSourcePath, import.meta.url); | ||
Deno.lstatSync(p); | ||
while (true) { | ||
const r = (prompt( | ||
`Decompressed file "${decompressedSourcePath}" found, use file? Y/N:`, | ||
) ?? "").trim() | ||
.toUpperCase(); | ||
if (r === "Y") { | ||
break; | ||
} else if (r === "N") { | ||
shouldDecompress = true; | ||
break; | ||
} else { | ||
console.log(`Unexpected: "${r}"`); | ||
} | ||
} | ||
} catch (e) { | ||
if (!(e instanceof Deno.errors.NotFound)) { | ||
throw e; | ||
} | ||
shouldDecompress = true; | ||
} | ||
|
||
await decompressTests(path); | ||
if (shouldDecompress) { | ||
await decompressTests(archivePath); | ||
} | ||
|
||
await clearTests(); | ||
await copyTests(decompressedSourcePath); |