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

🌱 initial collect-assets script #106

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ node_modules/
out/
build/
dist/
downloaded_assets/

# TypeScript build info
**/tsconfig.tsbuildinfo
Expand Down
117 changes: 117 additions & 0 deletions collect-assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import {
savitharaghunathan marked this conversation as resolved.
Show resolved Hide resolved
existsSync,
mkdirSync,
createWriteStream,
createReadStream,
writeFileSync,
readdirSync,
renameSync,
} from "fs";
import { resolve as _resolve, join, dirname } from "path";
import { fileURLToPath } from "url";
import fetch from "node-fetch";
savitharaghunathan marked this conversation as resolved.
Show resolved Hide resolved
import { Extract } from "unzipper";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const GITHUB_API = "https://api.github.com";
const REPOSITORY = "konveyor/kai";
const RELEASE_TAG = "v0.0.1";
const DOWNLOAD_DIR = _resolve(__dirname, "downloaded_assets");
const META_FILE = _resolve(DOWNLOAD_DIR, "collect.json");

const ASSETS_TO_DOWNLOAD = [
{ name: "kai-rpc-server.linux-x86_64.zip", folder: "linux", platform: "linux" },
{ name: "kai-rpc-server.macos-arm64.zip", folder: "macos-arm", platform: "macos-arm" },
{ name: "kai-rpc-server.macos-x86_64.zip", folder: "macos-x86", platform: "macos-x86" },
{ name: "kai-rpc-server.windows-x86_64.zip", folder: "windows", platform: "windows" },
];

if (!existsSync(DOWNLOAD_DIR)) {
mkdirSync(DOWNLOAD_DIR, { recursive: true });
}

async function getReleaseMetadata() {
const url = `${GITHUB_API}/repos/${REPOSITORY}/releases/tags/${RELEASE_TAG}`;
console.log(`Fetching release metadata for tag: ${RELEASE_TAG}`);

const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch release metadata: ${response.statusText}`);
}

return await response.json();
}

async function downloadAndExtractAsset(asset, folder, platform) {
const platformDir = join(DOWNLOAD_DIR, folder);
const assetPath = join(platformDir, asset.name);

if (!existsSync(platformDir)) {
mkdirSync(platformDir, { recursive: true });
}

console.log(`Downloading asset: ${asset.name}`);
const response = await fetch(asset.browser_download_url);
if (!response.ok) {
throw new Error(`Failed to download ${asset.name}: ${response.statusText}`);
}

const fileStream = createWriteStream(assetPath);
await new Promise((resolve, reject) => {
response.body.pipe(fileStream);
response.body.on("error", reject);
fileStream.on("finish", resolve);
savitharaghunathan marked this conversation as resolved.
Show resolved Hide resolved
});

console.log(`Extracting asset: ${asset.name} to ${platformDir}`);
await createReadStream(assetPath)
.pipe(Extract({ path: platformDir }))
.promise();

const extractedFiles = readdirSync(platformDir);
extractedFiles.forEach((file) => {
const oldPath = join(platformDir, file);
const newPath = join(platformDir, `${platform}-${file}`);
renameSync(oldPath, newPath);
});

console.log(`Extracted files for: ${asset.name}`);
}

(async function () {
try {
const releaseData = await getReleaseMetadata();
const commitId = releaseData.target_commitish;
const assets = releaseData.assets;

const metadata = {
releaseTag: RELEASE_TAG,
commitId,
collectedAt: new Date().toISOString(),
assets: [],
};

for (const { name, folder, platform } of ASSETS_TO_DOWNLOAD) {
const asset = assets.find((a) => a.name === name);
if (asset) {
await downloadAndExtractAsset(asset, folder, platform);
metadata.assets.push({
name: asset.name,
updatedAt: asset.updated_at,
folder: folder,
platform: platform,
});
} else {
console.warn(`Asset not found: ${name}`);
}
}

writeFileSync(META_FILE, JSON.stringify(metadata, null, 2));
console.log(`Metadata written to ${META_FILE}`);
console.log(`All assets downloaded to: ${DOWNLOAD_DIR}`);
} catch (error) {
console.error("Error:", error.message);
}
})();
148 changes: 140 additions & 8 deletions package-lock.json

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

Loading
Loading