Skip to content

Commit

Permalink
chore(node/_tools): make setup.ts more efficient (#917)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored May 16, 2021
1 parent b20aee7 commit eab3a72
Showing 1 changed file with 100 additions and 23 deletions.
123 changes: 100 additions & 23 deletions node/_tools/setup.ts
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";
Expand All @@ -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),
);
Expand Down Expand Up @@ -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;
Expand All @@ -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);

0 comments on commit eab3a72

Please sign in to comment.