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

fix(core dev): copy optimizer from CI build if dev #7283

Merged
merged 2 commits into from
Jan 30, 2025
Merged
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
103 changes: 77 additions & 26 deletions scripts/binding-platform.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { type BuildConfig, ensureDir } from './util';
import spawn from 'cross-spawn';
import { join } from 'node:path';
import semver from 'semver';
import { copyFile, writeFile } from 'fs/promises';
import { existsSync } from 'node:fs';
import { copyFile, readdir, writeFile } from 'fs/promises';
import { join } from 'node:path';
import { ensureDir, type BuildConfig } from './util';

export async function buildPlatformBinding(config: BuildConfig) {
await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -55,33 +54,78 @@ export async function copyPlatformBindingWasm(config: BuildConfig) {
ensureDir(config.distBindingsDir);
const cacheDir = join(config.tmpDir, `cached-bindings`);

let buildVersion = '0.0.0';
try {
const releaseDataUrl = `https://data.jsdelivr.com/v1/package/npm/@builder.io/qwik`;
const releaseRsp = await fetch(releaseDataUrl);
const releases = (await releaseRsp.json()) as any;
buildVersion = releases.tags.latest;
Object.values(releases.tags).forEach((version: any) => {
if (semver.gt(version, buildVersion)) {
buildVersion = version;
}
});
} catch (e) {
const cachedDirs = await readdir(cacheDir);
for (const cachedVersion of cachedDirs) {
if (semver.gt(cachedVersion, buildVersion)) {
buildVersion = cachedVersion;
let version = config.distVersion;
const isDev = version.includes('-dev');
let cdnUrl = 'https://cdn.jsdelivr.net/npm/';
let packageName: string;
if (isDev) {
cdnUrl = `https://pkg.pr.new/QwikDev/qwik/`;
version = version.split('-dev')[0];
}
if (version.startsWith('2')) {
// 6903 is the PR that builds v2
packageName = `@qwik.dev/core@${isDev ? '6903' : version}`;
} else {
packageName = `@builder.io/qwik@${isDev ? 'main' : version}`;
}

let cacheVersionDir: string;
if (isDev) {
// We fetch from pkg.pr.new which is a CDN for the CI builds
// It redirects to the latest version
cdnUrl = `${cdnUrl}${packageName}`;
// First request the URL, this will redirect to the latest version
const rsp = await fetch(cdnUrl);
if (!rsp.ok) {
throw new Error(`Unable to find Qwik package from ${cdnUrl}`);
}
const url = rsp.url;
// get the package name from the url
const realPackageName = url.split('/').pop()!;
// now check if we already have this package in the cache
const cachedPath = join(cacheDir, realPackageName);
if (!existsSync(cachedPath)) {
// download the package
console.log(`🦉 downloading CI build from ${url}`);
const pkgRsp = await fetch(url);
if (!pkgRsp.ok) {
console.error(pkgRsp);
throw new Error(`Unable to fetch Qwik package from ${pkgRsp.url}`);
}
await writeFile(cachedPath, pkgRsp.body as any);
}
}
// now unpack the package using tar, into the cache directory
const unpackedPath = join(cacheDir, `${realPackageName}-unpacked`);
ensureDir(unpackedPath);
await new Promise((resolve, reject) => {
const child = spawn('tar', ['-xvf', cachedPath, '-C', unpackedPath]);
child.on('error', (e) => {
console.error(e);
reject(e);
});
child.on('close', (code) => {
if (code === 0) {
resolve(child.stdout);
} else {
console.error(child.stdout);
reject(`tar exited with code ${code}`);
}
});
});

try {
const cacheVersionDir = join(cacheDir, buildVersion);
// now we need to find the bindings in the package
cacheVersionDir = join(unpackedPath, 'package', 'bindings');
} else {
cdnUrl = `${cdnUrl}${packageName}/bindings/`;
cacheVersionDir = join(cacheDir, version);
ensureDir(cacheVersionDir);
}

try {
const bindingFilenames = [
'qwik.darwin-arm64.node',
'qwik.darwin-x64.node',
'qwik.linux-x64-gnu.node',
'qwik.wasm.cjs',
'qwik.wasm.mjs',
'qwik.win32-x64-msvc.node',
Expand All @@ -94,16 +138,23 @@ export async function copyPlatformBindingWasm(config: BuildConfig) {
const distPath = join(config.distBindingsDir, bindingFilename);

if (!existsSync(cachedPath)) {
const cdnUrl = `https://cdn.jsdelivr.net/npm/@builder.io/qwik@${buildVersion}/bindings/${bindingFilename}`;
const rsp = (await fetch(cdnUrl)) as any;
if (isDev) {
throw new Error(`Unable to find Qwik binding from ${cachedPath}`);
}
const url = `${cdnUrl}${bindingFilename}`;
console.log(`🦉 native binding / wasm (downloading from ${url})`);
const rsp = (await fetch(url)) as any;
if (!rsp.ok) {
throw new Error(`Unable to fetch Qwik binding from ${rsp.url}`);
}
await writeFile(cachedPath, rsp.body);
}

await copyFile(cachedPath, distPath);
})
);

console.log(`🦉 native binding / wasm (copied from npm v${buildVersion})`);
console.log(`🦉 native binding / wasm (copied from npm v${version})`);
} catch (e) {
console.warn(`😱 ${e}`);
}
Expand Down