|
| 1 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 2 | +// @ts-nocheck |
| 3 | + |
| 4 | +import { |
| 5 | + CliConfigInterface, |
| 6 | + ReleaseHistoryInterface, |
| 7 | +} from "@bravemobile/react-native-code-push"; |
| 8 | +import * as fs from "fs"; |
| 9 | +import dotenv from "dotenv"; // install as devDependency |
| 10 | +import * as admin from "firebase-admin"; // install as devDependency |
| 11 | + |
| 12 | +dotenv.config(); |
| 13 | + |
| 14 | +const FIREBASE_SERVICE_ACCOUNT_PATH = process.env.FIREBASE_SERVICE_ACCOUNT_PATH!; |
| 15 | +const FIREBASE_STORAGE_BUCKET = process.env.FIREBASE_STORAGE_BUCKET!; |
| 16 | + |
| 17 | +if (!admin.apps.length) { |
| 18 | + const serviceAccount = JSON.parse( |
| 19 | + fs.readFileSync(FIREBASE_SERVICE_ACCOUNT_PATH, "utf8"), |
| 20 | + ); |
| 21 | + |
| 22 | + admin.initializeApp({ |
| 23 | + credential: admin.credential.cert(serviceAccount), |
| 24 | + storageBucket: FIREBASE_STORAGE_BUCKET, |
| 25 | + }); |
| 26 | +} |
| 27 | + |
| 28 | +const storage = admin.storage().bucket(); |
| 29 | + |
| 30 | +function historyJsonFileRemotePath( |
| 31 | + platform: "ios" | "android", |
| 32 | + identifier: string, |
| 33 | + binaryVersion: string, |
| 34 | +) { |
| 35 | + return `histories/${platform}/${identifier}/${binaryVersion}.json`; |
| 36 | +} |
| 37 | + |
| 38 | +function bundleFileRemotePath( |
| 39 | + platform: "ios" | "android", |
| 40 | + identifier: string, |
| 41 | + fileName: string, |
| 42 | +) { |
| 43 | + return `bundles/${platform}/${identifier}/${fileName}`; |
| 44 | +} |
| 45 | + |
| 46 | +const Config: CliConfigInterface = { |
| 47 | + bundleUploader: async ( |
| 48 | + source: string, |
| 49 | + platform: "ios" | "android", |
| 50 | + identifier = "staging", |
| 51 | + ): Promise<{downloadUrl: string}> => { |
| 52 | + try { |
| 53 | + const fileName = source.split("/").pop(); |
| 54 | + const remotePath = bundleFileRemotePath(platform, identifier, fileName!); |
| 55 | + |
| 56 | + const file = storage.file(remotePath); |
| 57 | + |
| 58 | + await file.save(fs.readFileSync(source), { |
| 59 | + metadata: { |
| 60 | + contentType: "application/zip", |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + await file.makePublic(); |
| 65 | + |
| 66 | + const downloadUrl = `https://storage.googleapis.com/${FIREBASE_STORAGE_BUCKET}/${remotePath}`; |
| 67 | + |
| 68 | + console.log("Bundle File uploaded:", downloadUrl); |
| 69 | + |
| 70 | + return { |
| 71 | + downloadUrl, |
| 72 | + }; |
| 73 | + } catch (error) { |
| 74 | + console.error("Error uploading bundle:", (error as Error).message); |
| 75 | + throw error; |
| 76 | + } |
| 77 | + }, |
| 78 | + |
| 79 | + getReleaseHistory: async ( |
| 80 | + targetBinaryVersion: string, |
| 81 | + platform: "ios" | "android", |
| 82 | + identifier = "staging", |
| 83 | + ): Promise<ReleaseHistoryInterface> => { |
| 84 | + const remoteJsonPath = historyJsonFileRemotePath( |
| 85 | + platform, |
| 86 | + identifier, |
| 87 | + targetBinaryVersion, |
| 88 | + ); |
| 89 | + |
| 90 | + const file = storage.file(remoteJsonPath); |
| 91 | + |
| 92 | + try { |
| 93 | + const [exists] = await file.exists(); |
| 94 | + if (!exists) { |
| 95 | + throw new Error(`Release history file not found: ${remoteJsonPath}`); |
| 96 | + } |
| 97 | + |
| 98 | + const [contents] = await file.download(); |
| 99 | + return JSON.parse(contents.toString()) as ReleaseHistoryInterface; |
| 100 | + } catch (error) { |
| 101 | + console.error("Error getting release history:", (error as Error).message); |
| 102 | + throw error; |
| 103 | + } |
| 104 | + }, |
| 105 | + |
| 106 | + setReleaseHistory: async ( |
| 107 | + targetBinaryVersion: string, |
| 108 | + jsonFilePath: string, |
| 109 | + releaseInfo: ReleaseHistoryInterface, |
| 110 | + platform: "ios" | "android", |
| 111 | + identifier = "staging", |
| 112 | + ): Promise<void> => { |
| 113 | + // upload JSON file or call API using `releaseInfo` metadata. |
| 114 | + |
| 115 | + try { |
| 116 | + const fileContent = fs.readFileSync(jsonFilePath, "utf8"); |
| 117 | + const remoteJsonPath = historyJsonFileRemotePath( |
| 118 | + platform, |
| 119 | + identifier, |
| 120 | + targetBinaryVersion, |
| 121 | + ); |
| 122 | + |
| 123 | + const file = storage.file(remoteJsonPath); |
| 124 | + |
| 125 | + await file.save(fileContent, { |
| 126 | + metadata: { |
| 127 | + contentType: "application/json", |
| 128 | + cacheControl: "no-cache", |
| 129 | + }, |
| 130 | + }); |
| 131 | + |
| 132 | + await file.makePublic(); |
| 133 | + |
| 134 | + const downloadUrl = `https://storage.googleapis.com/${FIREBASE_STORAGE_BUCKET}/${remoteJsonPath}`; |
| 135 | + |
| 136 | + console.log( |
| 137 | + "Release history File uploaded:", |
| 138 | + downloadUrl, |
| 139 | + ); |
| 140 | + } catch (error) { |
| 141 | + console.error("Error setting release history:", (error as Error).message); |
| 142 | + throw error; |
| 143 | + } |
| 144 | + }, |
| 145 | +}; |
| 146 | + |
| 147 | +module.exports = Config; |
0 commit comments