Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

perf 🚀: (cli) Some improvements #96

Merged
merged 1 commit into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
import path from "node:path";
/* import { clear } from "./clear.js";
import install from "./install.js";
import { benchmark } from "./benchmark.js";
import upgrade from "./upgrade.js";
import list from "./list.js";
import run from "./run.js";
import { update } from "../utils/readConfig.js";
import create from "./create.js";
import remove from "./remove.js";
import test from "./test.js";
import init from "./init.js";
import continuousInstall from "./ci.js"; */
import readPackage from "../utils/readPackage.js";

const comms = [
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { installLocalDep } from "../utils/installLocalDep.js";
import getParamsDeps from "../utils/parseDepsParams.js";
import parseTime from "../utils/parseTime.js";
import { spinnerGradient } from "../utils/spinnerGradient.js";
import { installPkg } from "../utils/installPkg.js";
import manifestFetcher from "../utils/manifestFetcher.js";
import readPackage from "../utils/readPackage.js";
import basePostInstall from "../utils/basePostInstall.js";
Expand Down Expand Up @@ -192,9 +191,11 @@ export async function install(opts: string[]) {
const __install = spinnerGradient(chalk.green("Installing packages..."));
const __install_start = performance.now();

const { installPkg } = await import("../utils/installPkg.js");

await Promise.all(
pkgs.map(async (pkg) => {
return await installPkg(pkg, pkg.parent, __install);
return installPkg(pkg, pkg.parent, __install);
})
);

Expand Down
104 changes: 57 additions & 47 deletions packages/cli/src/utils/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
existsSync,
readFileSync,
writeFileSync,
unlinkSync,
} from "node:fs";
import axios from "axios";
import { createHash } from "node:crypto";
Expand All @@ -22,7 +23,7 @@ export async function ultraExtract(
target: string,
tarball: string,
sha: string
) {
): Promise<void | { res: string }> {
if (!tarball) {
throw new Error("No tarball provided");
}
Expand All @@ -49,58 +50,67 @@ export async function ultraExtract(
mkdirSync(cacheBasePath);
}

if (!existsSync(file)) {
const writer = createWriteStream(file);
const response = await axios({
url: tarball,
method: "GET",
responseType: "stream",
});
try {
if (!existsSync(file)) {
const writer = createWriteStream(file);
const response = await axios({
url: tarball,
method: "GET",
responseType: "stream",
});

response.data.pipe(writer);
response.data.pipe(writer);

await new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
await new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});

const fileBuffer = readFileSync(file);
const hashSum = createHash("sha512");
const fileBuffer = readFileSync(file);
const hashSum = createHash("sha512");

hashSum.update(fileBuffer);
const hash = "sha512-" + hashSum.digest("base64");
hashSum.update(fileBuffer);
const hash = "sha512-" + hashSum.digest("base64");

if (hash !== sha) {
ora().fail(chalk.red(`SHA512 mismatch for ${tarball}`));
ora().fail(chalk.red(`Expected ${sha} but got ${hash}`));
if (hash !== sha) {
ora().fail(chalk.red(`SHA512 mismatch for ${tarball}`));
ora().fail(chalk.red(`Expected ${sha} but got ${hash}`));
}

__VERIFIED.push(tarball);
}

__VERIFIED.push(tarball);
}
// Extract "package" directory from tarball to "target" directory
mkdirSync(target, { recursive: true });

await tar
.extract({
file,
cwd: target,
strip: 1,
})
.catch((err) => {
ora(
chalk.red(
`Error extracting ${file} to ${target}: ${err.message || err}`
)
).fail();
});

// Create .ultra file
writeFileSync(ultraFile, "{}");

__DOWNLOADING.splice(__DOWNLOADING.indexOf(tarball), 1);

// Extract "package" directory from tarball to "target" directory
mkdirSync(target, { recursive: true });

await tar
.extract({
file,
cwd: target,
strip: 1,
})
.catch((err) => {
ora(
chalk.red(
`Error extracting ${file} to ${target}: ${err.message || err}`
)
).fail();
});

// Create .ultra file
writeFileSync(ultraFile, "{}");

__DOWNLOADING.splice(__DOWNLOADING.indexOf(tarball), 1);

return {
res: "extracted",
};
return {
res: "extracted",
};
} catch (err) {
// Try again but remove the file
if (existsSync(file)) {
unlinkSync(file);
}

return ultraExtract(target, tarball, sha);
}
}
12 changes: 11 additions & 1 deletion packages/cli/src/utils/installPkg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ export async function installPkg(
readFileSync(`${cacheFolder}/${downloadFile}`, "utf-8")
);

const deps = getDeps(pkgJson, {
dev: true,
});

// Symlink bin files
await binLinks({
path: pkgProjectDir,
Expand All @@ -190,6 +194,12 @@ export async function installPkg(
);
}

for (const dep of deps) {
if (!cachedDeps[dep.name]) {
await installPkg(dep, pkgProjectDir, spinner);
}
}

__INSTALLED.push({
name: manifest.name,
version: manifest.version,
Expand Down Expand Up @@ -265,7 +275,7 @@ export async function installPkg(
pkg.dist.integrity
);

if (status.res === "skipped") {
if (status && status.res === "skipped") {
return null;
}

Expand Down
9 changes: 7 additions & 2 deletions packages/cli/src/utils/manifestFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ const token = readConfig().token;
const specialChars = ["^", "~", ">", "<", "=", "|", "&", "*"];

export default async function manifestFetcher(spec: string, props: any) {
// Remove spaces from spec
const sanitizedSpec = spec.replace(/\s/g, "");
// Remove spaces "|", ">" and "<" from the spec
const sanitizedSpec = spec
.replace(/ /g, "")
.replace(/\|/g, "")
.replace(/>/g, "")
.replace(/</g, "");

const cacheFile = path.join(cacheFolder, `${sanitizedSpec}.json`);
const now = Date.now();
try {
Expand Down