|
| 1 | +import axios from 'axios'; |
| 2 | +import { Command, Option } from 'commander'; |
| 3 | +import FormData from 'form-data'; |
| 4 | +import fs from 'fs'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +interface UploadSourcemapsOptions { |
| 8 | + platform: 'android' | 'ios'; |
| 9 | + dir: string; |
| 10 | + token: string; |
| 11 | + name: string; |
| 12 | + code: string; |
| 13 | + label?: string; |
| 14 | +} |
| 15 | + |
| 16 | +export const uploadSourcemapsCommand = new Command(); |
| 17 | + |
| 18 | +uploadSourcemapsCommand |
| 19 | + .name('upload-sourcemaps') |
| 20 | + .addOption( |
| 21 | + new Option('-p, --platform <value>', 'Platform') |
| 22 | + .choices(['ios', 'android']) |
| 23 | + .makeOptionMandatory(), |
| 24 | + ) |
| 25 | + .addOption( |
| 26 | + new Option( |
| 27 | + '-d, --dir <value>', |
| 28 | + 'The path of the directory including the source map file', |
| 29 | + ).makeOptionMandatory(), |
| 30 | + ) |
| 31 | + .addOption( |
| 32 | + new Option('-t, --token <value>', 'Your App Token') |
| 33 | + .env('INSTABUG_APP_TOKEN') |
| 34 | + .makeOptionMandatory(), |
| 35 | + ) |
| 36 | + .addOption( |
| 37 | + new Option('-n, --name <value>', 'The app version name') |
| 38 | + .env('INSTABUG_APP_VERSION_NAME') |
| 39 | + .makeOptionMandatory(), |
| 40 | + ) |
| 41 | + .addOption( |
| 42 | + new Option('-c, --code <value>', 'The app version code') |
| 43 | + .env('INSTABUG_APP_VERSION_CODE') |
| 44 | + .makeOptionMandatory(), |
| 45 | + ) |
| 46 | + .addOption( |
| 47 | + new Option('-l, --label <value>', "The CodePush label if it's a CodePush release").env( |
| 48 | + 'INSTABUG_APP_VERSION_LABEL', |
| 49 | + ), |
| 50 | + ) |
| 51 | + .action(function (this: Command) { |
| 52 | + const options = this.opts<UploadSourcemapsOptions>(); |
| 53 | + uploadSourcemaps(options); |
| 54 | + }) |
| 55 | + .showHelpAfterError(); |
| 56 | + |
| 57 | +const uploadSourcemaps = async (opts: UploadSourcemapsOptions) => { |
| 58 | + const fileName = `${opts.platform}-sourcemap.json`; |
| 59 | + const filePath = path.join(opts.dir, fileName); |
| 60 | + const fileBlob = fs.readFileSync(filePath); |
| 61 | + |
| 62 | + const version = { |
| 63 | + code: opts.code, |
| 64 | + name: opts.name, |
| 65 | + codePush: opts.label, |
| 66 | + }; |
| 67 | + |
| 68 | + const form = new FormData(); |
| 69 | + form.append('app_version', JSON.stringify(version)); |
| 70 | + form.append('symbols_file', fileBlob, fileName); |
| 71 | + form.append('application_token', opts.token); |
| 72 | + form.append('platform', 'react_native'); |
| 73 | + form.append('os', opts.platform); |
| 74 | + |
| 75 | + console.log('Uploading Source map file...'); |
| 76 | + |
| 77 | + try { |
| 78 | + const response = await axios.post('https://api.instabug.com/api/sdk/v3/symbols_files', form, { |
| 79 | + headers: form.getHeaders(), |
| 80 | + }); |
| 81 | + |
| 82 | + const appVersion = version.codePush |
| 83 | + ? `${version.name} (${version.code})+codepush:${version.codePush}` |
| 84 | + : `${version.name} (${version.code})`; |
| 85 | + |
| 86 | + console.log(`Successfully uploaded Source maps for version: ${appVersion}`); |
| 87 | + console.log(response.data); |
| 88 | + } catch (err) { |
| 89 | + console.error( |
| 90 | + 'Failed to upload source maps:', |
| 91 | + axios.isAxiosError(err) ? err.response?.data : err, |
| 92 | + ); |
| 93 | + } |
| 94 | +}; |
0 commit comments