forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcode-push.config.example.firebase.ts
More file actions
191 lines (157 loc) · 5.64 KB
/
Copy pathcode-push.config.example.firebase.ts
File metadata and controls
191 lines (157 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import {
type BundleDownloadInfo,
type BundleUploadArtifact,
CliConfigInterface,
ReleaseHistoryInterface,
} from "@bravemobile/react-native-code-push";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import dotenv from "dotenv"; // install as devDependency
import * as admin from "firebase-admin"; // install as devDependency
dotenv.config();
const FIREBASE_SERVICE_ACCOUNT_PATH = process.env.FIREBASE_SERVICE_ACCOUNT_PATH!;
const FIREBASE_STORAGE_BUCKET = process.env.FIREBASE_STORAGE_BUCKET!;
if (!admin.apps.length) {
const serviceAccount = JSON.parse(
fs.readFileSync(FIREBASE_SERVICE_ACCOUNT_PATH, "utf8"),
);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
storageBucket: FIREBASE_STORAGE_BUCKET,
});
}
const storage = admin.storage().bucket();
function historyJsonFileRemotePath(
platform: "ios" | "android",
identifier: string,
binaryVersion: string,
) {
return `histories/${platform}/${identifier}/${binaryVersion}.json`;
}
function bundleFileRemotePath(
platform: "ios" | "android",
identifier: string,
artifact: BundleUploadArtifact,
) {
if (artifact.type === "full-bundle") {
return fullBundleRemotePath(platform, identifier, artifact.packageHash);
}
const artifactPath = artifact.type === "asset-diff"
? `asset-diff/${artifact.targetBinaryVersion}/${artifact.packageHash}/${artifact.basePackageHash}`
: `binary-patch/${artifact.targetBinaryVersion}/${artifact.packageHash}`;
return `bundles/${platform}/${identifier}/${artifactPath}`;
}
function fullBundleRemotePath(
platform: "ios" | "android",
identifier: string,
packageHash: string,
) {
return `bundles/${platform}/${identifier}/full-bundle/${packageHash}`;
}
const Config: CliConfigInterface = {
bundleUploader: async (
source: string,
platform: "ios" | "android",
identifier = "staging",
artifact,
): Promise<{downloadUrl: string}> => {
try {
if (artifact === undefined) {
throw new Error("The release command did not provide bundle artifact metadata.");
}
const remotePath = bundleFileRemotePath(platform, identifier, artifact);
const file = storage.file(remotePath);
await file.save(fs.readFileSync(source), {
metadata: {
contentType: "application/zip",
},
});
await file.makePublic();
const downloadUrl = `https://storage.googleapis.com/${FIREBASE_STORAGE_BUCKET}/${remotePath}`;
console.log("Bundle File uploaded:", downloadUrl);
return {
downloadUrl,
};
} catch (error) {
console.error("Error uploading bundle:", (error as Error).message);
throw error;
}
},
getReleaseHistory: async (
targetBinaryVersion: string,
platform: "ios" | "android",
identifier = "staging",
): Promise<ReleaseHistoryInterface> => {
const remoteJsonPath = historyJsonFileRemotePath(
platform,
identifier,
targetBinaryVersion,
);
const file = storage.file(remoteJsonPath);
try {
const [exists] = await file.exists();
if (!exists) {
throw new Error(`Release history file not found: ${remoteJsonPath}`);
}
const [contents] = await file.download();
return JSON.parse(contents.toString()) as ReleaseHistoryInterface;
} catch (error) {
console.error("Error getting release history:", (error as Error).message);
throw error;
}
},
setReleaseHistory: async (
targetBinaryVersion: string,
jsonFilePath: string,
releaseInfo: ReleaseHistoryInterface,
platform: "ios" | "android",
identifier = "staging",
): Promise<void> => {
// upload JSON file or call API using `releaseInfo` metadata.
try {
const fileContent = fs.readFileSync(jsonFilePath, "utf8");
const remoteJsonPath = historyJsonFileRemotePath(
platform,
identifier,
targetBinaryVersion,
);
const file = storage.file(remoteJsonPath);
await file.save(fileContent, {
metadata: {
contentType: "application/json",
cacheControl: "no-cache",
},
});
await file.makePublic();
const downloadUrl = `https://storage.googleapis.com/${FIREBASE_STORAGE_BUCKET}/${remoteJsonPath}`;
console.log(
"Release history File uploaded:",
downloadUrl,
);
} catch (error) {
console.error("Error setting release history:", (error as Error).message);
throw error;
}
},
bundleDownloader: async (
archive: BundleDownloadInfo,
platform: "ios" | "android",
identifier = "staging",
): Promise<{downloadedFilePath: string}> => {
const remotePath = fullBundleRemotePath(
platform,
identifier,
archive.packageHash,
);
const downloadedFilePath = path.join(
os.tmpdir(),
`codepush-${archive.releaseVersion}-${archive.packageHash}`,
);
await storage.file(remotePath).download({destination: downloadedFilePath});
return {downloadedFilePath};
},
};
module.exports = Config;