@@ -8,24 +8,54 @@ export interface UploadSoFilesOptions {
8
8
token : string ;
9
9
name : string ;
10
10
api_key : string ;
11
+
12
+ /**
13
+ * Disables logging to the console and prevents process exit on error.
14
+ *
15
+ * @default false
16
+ * */
17
+ silent ?: boolean ;
11
18
}
12
19
13
- export const uploadSoFiles = async ( opts : UploadSoFilesOptions ) => {
20
+ /**
21
+ * Uploads NDK `.so` files to Instabug.
22
+ *
23
+ * @param opts Options for the `.so` files upload process.
24
+ * @returns A promise that resolves to a boolean indicating whether the upload was successful.
25
+ */
26
+ export const uploadSoFiles = async ( opts : UploadSoFilesOptions ) : Promise < boolean > => {
14
27
const fileName = opts . file ;
15
- if ( fileName == null ) {
16
- console . error ( 'Failed to upload So Files: invalid file path' ) ;
17
- process . exit ( 1 ) ;
28
+
29
+ const validFilePath = assert (
30
+ fileName != null ,
31
+ 'Failed to upload So Files: invalid file path' ,
32
+ opts . silent ,
33
+ ) ;
34
+
35
+ if ( ! validFilePath ) {
36
+ return false ;
18
37
}
19
38
20
- if ( fs . existsSync ( fileName ) === false ) {
21
- console . error ( 'Failed to upload So Files: File not found' ) ;
22
- process . exit ( 1 ) ;
39
+ const fileExists = assert (
40
+ fs . existsSync ( fileName ) ,
41
+ 'Failed to upload So Files: File not found' ,
42
+ opts . silent ,
43
+ ) ;
44
+
45
+ if ( ! fileExists ) {
46
+ return false ;
23
47
}
24
- var fileExt = fileName . split ( '.' ) . pop ( ) ;
25
48
26
- if ( fileExt !== 'zip' ) {
27
- console . error ( 'Failed to upload So Files: You can only upload ZIP files' ) ;
28
- process . exit ( 1 ) ;
49
+ const fileExt = fileName . split ( '.' ) . pop ( ) ;
50
+
51
+ const isZipFile = assert (
52
+ fileExt === 'zip' ,
53
+ 'Failed to upload So Files: You can only upload ZIP files' ,
54
+ opts . silent ,
55
+ ) ;
56
+
57
+ if ( ! isZipFile ) {
58
+ return false ;
29
59
}
30
60
31
61
const fileBlob = fs . readFileSync ( opts . file ) ;
@@ -37,16 +67,46 @@ export const uploadSoFiles = async (opts: UploadSoFilesOptions) => {
37
67
form . append ( 'api_key' , opts . api_key ) ;
38
68
form . append ( 'arch' , opts . arch ) ;
39
69
40
- console . log ( 'Uploading So files...' ) ;
70
+ if ( ! opts . silent ) {
71
+ console . log ( 'Uploading So files...' ) ;
72
+ }
73
+
41
74
const uploadEndpoint = 'https://api.instabug.com/api/web/public/so_files' ;
42
75
try {
43
76
await axios . post ( uploadEndpoint , form , {
44
77
headers : form . getHeaders ( ) ,
45
78
} ) ;
46
79
47
- console . log ( `Successfully uploaded So Files for version: ${ opts . name } with arch ${ opts . arch } ` ) ;
80
+ if ( ! opts . silent ) {
81
+ console . log (
82
+ `Successfully uploaded So Files for version: ${ opts . name } with arch ${ opts . arch } ` ,
83
+ ) ;
84
+ }
85
+
86
+ return true ;
48
87
} catch ( err ) {
49
- console . error ( 'Failed to upload So Files:' , axios . isAxiosError ( err ) ? err . response ?. data : err ) ;
88
+ if ( ! opts . silent ) {
89
+ console . error (
90
+ 'Failed to upload So Files:' ,
91
+ axios . isAxiosError ( err ) ? err . response ?. data : err ,
92
+ ) ;
93
+
94
+ process . exit ( 1 ) ;
95
+ }
96
+
97
+ return false ;
98
+ }
99
+ } ;
100
+
101
+ export const assert = ( condition : unknown , message : string , silent ?: boolean ) : boolean => {
102
+ if ( silent ) {
103
+ return Boolean ( condition ) ;
104
+ }
105
+
106
+ if ( ! condition ) {
107
+ console . error ( message ) ;
50
108
process . exit ( 1 ) ;
51
109
}
110
+
111
+ return true ;
52
112
} ;
0 commit comments