Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dream & taucorder] add npm support #187

Merged
merged 4 commits into from
Jun 9, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
pkg/vm-orbit/tests/e2e/go/fixtures/*.wasm
pkg/vm-orbit/tests/e2e/go/fixtures/testPlugin*

npm/bin

./services/hoarder/testGIT
./services/monkey/build/
./services/auth/build/
Expand Down
1 change: 1 addition & 0 deletions tools/dream/npm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`dream` is a local development tool that lets you run a full-fledged Taubyte-based Cloud on your machine. For documentation, visit [https://tau.how](https://tau.how).
130 changes: 130 additions & 0 deletions tools/dream/npm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/env node

const fs = require("fs");
const path = require("path");
const axios = require("axios");
const ProgressBar = require("progress");
const { spawn } = require("child_process");
const tar = require("tar");
const packageJson = require("./package.json");

const binaryDir = path.join(__dirname, "bin");
const binaryPath = path.join(binaryDir, "dream");
const versionFilePath = path.join(binaryDir, "version.txt");
const packageVersion = packageJson.dream;

function binaryExists() {
return fs.existsSync(binaryPath);
}

function versionMatches() {
if (!fs.existsSync(versionFilePath)) {
return false;
}
const installedVersion = fs.readFileSync(versionFilePath, "utf-8").trim();
return installedVersion === packageVersion;
}

function parseAssetName() {
let os, arch;

if (process.platform === "darwin") {
os = "darwin";
} else if (process.platform === "linux") {
os = "linux";
} else if (process.platform === "win32") {
os = "windows";
} else {
os = null;
}

if (process.arch === "x64") {
arch = "amd64";
} else if (process.arch === "arm64") {
arch = "arm64";
} else {
arch = null;
}

return { os, arch };
}

async function downloadAndExtractBinary() {
if (binaryExists() && versionMatches()) {
return;
}

let version = packageVersion;

const { os: currentOs, arch: currentArch } = parseAssetName();
const assetName = `dream_${version}_${currentOs}_${currentArch}.tar.gz`;
const assetUrl = `https://github.com/taubyte/tau/releases/download/v${version}/${assetName}`;

console.log(`Downloading dream v${version}...`);
const { data, headers } = await axios({
url: assetUrl,
method: "GET",
responseType: "stream",
});

const totalLength = headers["content-length"];
const progressBar = new ProgressBar("-> downloading [:bar] :percent :etas", {
width: 40,
complete: "=",
incomplete: " ",
renderThrottle: 1,
total: parseInt(totalLength),
});

if (!fs.existsSync(binaryDir)) {
fs.mkdirSync(binaryDir);
}

const tarPath = path.join(binaryDir, assetName);
const writer = fs.createWriteStream(tarPath);
data.on("data", (chunk) => progressBar.tick(chunk.length));
data.pipe(writer);

return new Promise((resolve, reject) => {
writer.on("finish", async () => {
console.log(`Extracting dream v${version}...`);
await tar.x({
file: tarPath,
C: binaryDir,
});
fs.unlinkSync(tarPath); // Remove the tarball after extraction
fs.writeFileSync(versionFilePath, version); // Save the version to a file
resolve();
});
writer.on("error", reject);
});
}

function executeBinary() {
if (!binaryExists()) {
console.error("Binary not found. Please run the install script.");
return;
}

// Capture arguments passed to the script, excluding the first two elements
const args = process.argv.slice(2);

const child = spawn(binaryPath, args, {
stdio: "inherit",
});

child.on("error", (err) => {
console.error("Failed to start binary:", err);
});
}

async function main() {
try {
await downloadAndExtractBinary();
executeBinary();
} catch (err) {
console.error(err.message);
}
}

main();
226 changes: 226 additions & 0 deletions tools/dream/npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading