1- import { type BuildConfig , ensureDir } from './util' ;
21import spawn from 'cross-spawn' ;
3- import { join } from 'node:path' ;
4- import semver from 'semver' ;
2+ import { copyFile , writeFile } from 'fs/promises' ;
53import { existsSync } from 'node:fs' ;
6- import { copyFile , readdir , writeFile } from 'fs/promises' ;
4+ import { join } from 'node:path' ;
5+ import { ensureDir , type BuildConfig } from './util' ;
76
87export async function buildPlatformBinding ( config : BuildConfig ) {
98 await new Promise ( ( resolve , reject ) => {
@@ -55,33 +54,78 @@ export async function copyPlatformBindingWasm(config: BuildConfig) {
5554 ensureDir ( config . distBindingsDir ) ;
5655 const cacheDir = join ( config . tmpDir , `cached-bindings` ) ;
5756
58- let buildVersion = '0.0.0' ;
59- try {
60- const releaseDataUrl = `https://data.jsdelivr.com/v1/package/npm/@builder.io/qwik` ;
61- const releaseRsp = await fetch ( releaseDataUrl ) ;
62- const releases = ( await releaseRsp . json ( ) ) as any ;
63- buildVersion = releases . tags . latest ;
64- Object . values ( releases . tags ) . forEach ( ( version : any ) => {
65- if ( semver . gt ( version , buildVersion ) ) {
66- buildVersion = version ;
67- }
68- } ) ;
69- } catch ( e ) {
70- const cachedDirs = await readdir ( cacheDir ) ;
71- for ( const cachedVersion of cachedDirs ) {
72- if ( semver . gt ( cachedVersion , buildVersion ) ) {
73- buildVersion = cachedVersion ;
57+ let version = config . distVersion ;
58+ const isDev = version . includes ( '-dev' ) ;
59+ let cdnUrl = 'https://cdn.jsdelivr.net/npm/' ;
60+ let packageName : string ;
61+ if ( isDev ) {
62+ cdnUrl = `https://pkg.pr.new/QwikDev/qwik/` ;
63+ version = version . split ( '-dev' ) [ 0 ] ;
64+ }
65+ if ( version . startsWith ( '2' ) ) {
66+ // 6903 is the PR that builds v2
67+ packageName = `@qwik.dev/core@${ isDev ? '6903' : version } ` ;
68+ } else {
69+ packageName = `@builder.io/qwik@${ isDev ? 'main' : version } ` ;
70+ }
71+
72+ let cacheVersionDir : string ;
73+ if ( isDev ) {
74+ // We fetch from pkg.pr.new which is a CDN for the CI builds
75+ // It redirects to the latest version
76+ cdnUrl = `${ cdnUrl } ${ packageName } ` ;
77+ // First request the URL, this will redirect to the latest version
78+ const rsp = await fetch ( cdnUrl ) ;
79+ if ( ! rsp . ok ) {
80+ throw new Error ( `Unable to find Qwik package from ${ cdnUrl } ` ) ;
81+ }
82+ const url = rsp . url ;
83+ // get the package name from the url
84+ const realPackageName = url . split ( '/' ) . pop ( ) ! ;
85+ // now check if we already have this package in the cache
86+ const cachedPath = join ( cacheDir , realPackageName ) ;
87+ if ( ! existsSync ( cachedPath ) ) {
88+ // download the package
89+ console . log ( `🦉 downloading CI build from ${ url } ` ) ;
90+ const pkgRsp = await fetch ( url ) ;
91+ if ( ! pkgRsp . ok ) {
92+ console . error ( pkgRsp ) ;
93+ throw new Error ( `Unable to fetch Qwik package from ${ pkgRsp . url } ` ) ;
7494 }
95+ await writeFile ( cachedPath , pkgRsp . body as any ) ;
7596 }
76- }
97+ // now unpack the package using tar, into the cache directory
98+ const unpackedPath = join ( cacheDir , `${ realPackageName } -unpacked` ) ;
99+ ensureDir ( unpackedPath ) ;
100+ await new Promise ( ( resolve , reject ) => {
101+ const child = spawn ( 'tar' , [ '-xvf' , cachedPath , '-C' , unpackedPath ] ) ;
102+ child . on ( 'error' , ( e ) => {
103+ console . error ( e ) ;
104+ reject ( e ) ;
105+ } ) ;
106+ child . on ( 'close' , ( code ) => {
107+ if ( code === 0 ) {
108+ resolve ( child . stdout ) ;
109+ } else {
110+ console . error ( child . stdout ) ;
111+ reject ( `tar exited with code ${ code } ` ) ;
112+ }
113+ } ) ;
114+ } ) ;
77115
78- try {
79- const cacheVersionDir = join ( cacheDir , buildVersion ) ;
116+ // now we need to find the bindings in the package
117+ cacheVersionDir = join ( unpackedPath , 'package' , 'bindings' ) ;
118+ } else {
119+ cdnUrl = `${ cdnUrl } ${ packageName } /bindings/` ;
120+ cacheVersionDir = join ( cacheDir , version ) ;
80121 ensureDir ( cacheVersionDir ) ;
122+ }
81123
124+ try {
82125 const bindingFilenames = [
83126 'qwik.darwin-arm64.node' ,
84127 'qwik.darwin-x64.node' ,
128+ 'qwik.linux-x64-gnu.node' ,
85129 'qwik.wasm.cjs' ,
86130 'qwik.wasm.mjs' ,
87131 'qwik.win32-x64-msvc.node' ,
@@ -94,16 +138,23 @@ export async function copyPlatformBindingWasm(config: BuildConfig) {
94138 const distPath = join ( config . distBindingsDir , bindingFilename ) ;
95139
96140 if ( ! existsSync ( cachedPath ) ) {
97- const cdnUrl = `https://cdn.jsdelivr.net/npm/@builder.io/qwik@${ buildVersion } /bindings/${ bindingFilename } ` ;
98- const rsp = ( await fetch ( cdnUrl ) ) as any ;
141+ if ( isDev ) {
142+ throw new Error ( `Unable to find Qwik binding from ${ cachedPath } ` ) ;
143+ }
144+ const url = `${ cdnUrl } ${ bindingFilename } ` ;
145+ console . log ( `🦉 native binding / wasm (downloading from ${ url } )` ) ;
146+ const rsp = ( await fetch ( url ) ) as any ;
147+ if ( ! rsp . ok ) {
148+ throw new Error ( `Unable to fetch Qwik binding from ${ rsp . url } ` ) ;
149+ }
99150 await writeFile ( cachedPath , rsp . body ) ;
100151 }
101152
102153 await copyFile ( cachedPath , distPath ) ;
103154 } )
104155 ) ;
105156
106- console . log ( `🦉 native binding / wasm (copied from npm v${ buildVersion } )` ) ;
157+ console . log ( `🦉 native binding / wasm (copied from npm v${ version } )` ) ;
107158 } catch ( e ) {
108159 console . warn ( `😱 ${ e } ` ) ;
109160 }
0 commit comments