|
| 1 | +import axios from 'axios'; |
| 2 | +import { Command, Option } from 'commander'; |
| 3 | +import FormData from 'form-data'; |
| 4 | +import fs from 'fs'; |
| 5 | + |
| 6 | +interface UploadSoFilesOptions { |
| 7 | + arch: 'x86' | 'x86_64' | 'arm64-v8a' | 'armeabi-v7a'; |
| 8 | + file: string; |
| 9 | + token: string; |
| 10 | + name: string; |
| 11 | + api_key: string; |
| 12 | +} |
| 13 | +/** |
| 14 | + * This script uploads .so files to the specified endpoint used in NDK crash reporting. |
| 15 | + * Usage: node upload-so-files.js --arch <arch> --file <path> --api_key <key> --token <token> --name <name> |
| 16 | + */ |
| 17 | + |
| 18 | +export const UploadSoFilesCommand = new Command(); |
| 19 | + |
| 20 | +UploadSoFilesCommand.name('upload-so-files') |
| 21 | + .addOption( |
| 22 | + new Option('-arch, --arch <value>', 'arch') |
| 23 | + .choices(['x86', 'x86_64', 'arm64-v8a', 'armeabi-v7a']) |
| 24 | + .makeOptionMandatory(), |
| 25 | + ) |
| 26 | + .addOption( |
| 27 | + new Option( |
| 28 | + '-f, --file <path>', |
| 29 | + 'The path of the symbol files in Zip format', |
| 30 | + ).makeOptionMandatory(), |
| 31 | + ) |
| 32 | + .addOption(new Option('--api_key <value>', 'Your App key').makeOptionMandatory()) |
| 33 | + .addOption( |
| 34 | + new Option('-t, --token <value>', 'Your App Token') |
| 35 | + .env('INSTABUG_APP_TOKEN') |
| 36 | + .makeOptionMandatory(), |
| 37 | + ) |
| 38 | + .addOption( |
| 39 | + new Option('-n, --name <value>', 'The app version name') |
| 40 | + .env('INSTABUG_APP_VERSION_NAME') |
| 41 | + .makeOptionMandatory(), |
| 42 | + ) |
| 43 | + .action(function (this: Command) { |
| 44 | + const options = this.opts<UploadSoFilesOptions>(); |
| 45 | + UploadSoFiles(options); |
| 46 | + }) |
| 47 | + .showHelpAfterError(); |
| 48 | + |
| 49 | +const UploadSoFiles = async (opts: UploadSoFilesOptions) => { |
| 50 | + const fileName = opts.file; |
| 51 | + if (fileName == null) { |
| 52 | + console.error('Failed to upload So Files: invalid file path'); |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | + |
| 56 | + if (fs.existsSync(fileName) === false) { |
| 57 | + console.error('Failed to upload So Files: File not found'); |
| 58 | + process.exit(1); |
| 59 | + } |
| 60 | + var fileExt = fileName.split('.').pop(); |
| 61 | + |
| 62 | + if (fileExt !== 'zip') { |
| 63 | + console.error('Failed to upload So Files: You can only upload ZIP files'); |
| 64 | + process.exit(1); |
| 65 | + } |
| 66 | + |
| 67 | + const fileBlob = fs.readFileSync(opts.file); |
| 68 | + |
| 69 | + const form = new FormData(); |
| 70 | + form.append('app_version', opts.name); |
| 71 | + form.append('so_file', fileBlob, fileName); |
| 72 | + form.append('application_token', opts.token); |
| 73 | + form.append('api_key', opts.api_key); |
| 74 | + form.append('arch', opts.arch); |
| 75 | + |
| 76 | + console.log('Uploading So files...'); |
| 77 | + const uploadEndpoint = 'https://api.instabug.com/api/web/public/so_files'; |
| 78 | + try { |
| 79 | + await axios.post(uploadEndpoint, form, { |
| 80 | + headers: form.getHeaders(), |
| 81 | + }); |
| 82 | + |
| 83 | + console.log(`Successfully uploaded So Files for version: ${opts.name} with arch ${opts.arch}`); |
| 84 | + } catch (err) { |
| 85 | + console.error('Failed to upload So Files:', axios.isAxiosError(err) ? err.response?.data : err); |
| 86 | + process.exit(1); |
| 87 | + } |
| 88 | +}; |
0 commit comments