1+ #!/usr/bin/env node
2+
3+ const fs = require ( 'fs' ) ;
4+ const path = require ( 'path' ) ;
5+ const https = require ( 'https' ) ;
6+ const { execSync } = require ( 'child_process' ) ;
7+ const tar = require ( 'tar' ) ;
8+
9+ const PACKAGE_JSON = require ( '../package.json' ) ;
10+ const VERSION = PACKAGE_JSON . version ;
11+ const REPO = 'unomed-dev/react-native-matrix-sdk' ;
12+
13+ async function downloadFile ( url , dest ) {
14+ return new Promise ( ( resolve , reject ) => {
15+ const file = fs . createWriteStream ( dest ) ;
16+ https . get ( url , ( response ) => {
17+ if ( response . statusCode === 302 || response . statusCode === 301 ) {
18+ // Follow redirect
19+ https . get ( response . headers . location , ( redirectResponse ) => {
20+ redirectResponse . pipe ( file ) ;
21+ file . on ( 'finish' , ( ) => {
22+ file . close ( resolve ) ;
23+ } ) ;
24+ } ) . on ( 'error' , reject ) ;
25+ } else if ( response . statusCode === 200 ) {
26+ response . pipe ( file ) ;
27+ file . on ( 'finish' , ( ) => {
28+ file . close ( resolve ) ;
29+ } ) ;
30+ } else {
31+ reject ( new Error ( `HTTP ${ response . statusCode } ` ) ) ;
32+ }
33+ } ) . on ( 'error' , reject ) ;
34+ } ) ;
35+ }
36+
37+ async function downloadBinaries ( ) {
38+ const buildDir = path . join ( __dirname , '..' , 'build' ) ;
39+
40+ // Check if binaries already exist
41+ if ( fs . existsSync ( path . join ( buildDir , 'RnMatrixRustSdk.xcframework' ) ) ) {
42+ console . log ( 'Binaries already exist, skipping download.' ) ;
43+ return ;
44+ }
45+
46+ console . log ( `Downloading binaries for v${ VERSION } ...` ) ;
47+
48+ // Create build directory
49+ if ( ! fs . existsSync ( buildDir ) ) {
50+ fs . mkdirSync ( buildDir , { recursive : true } ) ;
51+ }
52+
53+ const releaseUrl = `https://github.com/${ REPO } /releases/download/v${ VERSION } /binaries.tar.gz` ;
54+ const tempFile = path . join ( buildDir , 'binaries.tar.gz' ) ;
55+
56+ try {
57+ // Download the binary archive
58+ console . log ( `Downloading from ${ releaseUrl } ...` ) ;
59+ await downloadFile ( releaseUrl , tempFile ) ;
60+
61+ // Extract the archive
62+ console . log ( 'Extracting binaries...' ) ;
63+ await tar . x ( {
64+ file : tempFile ,
65+ cwd : buildDir ,
66+ } ) ;
67+
68+ // Clean up
69+ fs . unlinkSync ( tempFile ) ;
70+
71+ console . log ( 'Binaries downloaded successfully!' ) ;
72+ } catch ( error ) {
73+ console . error ( 'Failed to download binaries:' , error . message ) ;
74+ console . error ( 'You may need to build the binaries locally using:' ) ;
75+ console . error ( ' yarn generate:release' ) ;
76+ // Exit gracefully if binaries do not exist
77+ process . exit ( 0 ) ;
78+ }
79+ }
80+
81+ if ( require . main === module ) {
82+ downloadBinaries ( ) . catch ( console . error ) ;
83+ }
84+
85+ module . exports = { downloadBinaries } ;
0 commit comments