forked from revopush/code-push-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-utils.ts
More file actions
209 lines (169 loc) · 7.3 KB
/
Copy pathbinary-utils.ts
File metadata and controls
209 lines (169 loc) · 7.3 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import * as path from "path";
import * as fs from "fs";
import * as chalk from "chalk";
import { log } from "./command-executor";
import { hashFile } from "./hash-utils";
import * as os from "os";
import * as Q from "q";
import * as yazl from "yazl";
import { readFile } from "node:fs/promises";
import * as plist from "plist"
import * as bplist from "bplist-parser";
export async function extractMetadataFromAndroid(extractFolder, outputFolder) {
const assetsFolder = path.join(extractFolder, "assets");
if (!fs.existsSync(assetsFolder)) {
throw new Error("Invalid APK structure: assets folder not found.");
}
const codepushMetadata = path.join(assetsFolder, "CodePushMetadata");
let fileHashes: { [key: string]: string } = {};
if (fs.existsSync(codepushMetadata)) {
fileHashes = await takeHashesFromMetadata(codepushMetadata);
} else {
log(chalk.yellow(`\nWarning: CodepushMetadata file not found in APK. Check used version of SDK\n`));
}
// Get index.android.bundle from root of app folder
const mainJsBundlePath = path.join(assetsFolder, "index.android.bundle");
if (fs.existsSync(mainJsBundlePath)) {
// Copy bundle to output folder
const outputCodePushFolder = path.join(outputFolder, "CodePush");
fs.mkdirSync(outputCodePushFolder, { recursive: true });
const outputBundlePath = path.join(outputCodePushFolder, "index.android.bundle");
fs.copyFileSync(mainJsBundlePath, outputBundlePath);
} else {
throw new Error("index.android.bundle not found in APK root folder.");
}
// Save packageManifest.json
const manifestPath = path.join(outputFolder, "packageManifest.json");
fs.writeFileSync(manifestPath, JSON.stringify(fileHashes, null, 2));
log(chalk.cyan(`\nSaved packageManifest.json with ${Object.keys(fileHashes).length} entries.\n`));
// Create zip archive with packageManifest.json and bundle file
const zipPath = path.join(os.tmpdir(), `CodePushBinary-${Date.now()}.zip`);
await createZipArchive(outputFolder, zipPath, ["packageManifest.json", "CodePush/index.android.bundle"]);
return zipPath;
}
export async function extractMetadataFromIOS(extractFolder, outputFolder) {
const payloadFolder = path.join(extractFolder, "Payload");
if (!fs.existsSync(payloadFolder)) {
throw new Error("Invalid IPA structure: Payload folder not found.");
}
const appFolders = fs.readdirSync(payloadFolder).filter((item) => {
const itemPath = path.join(payloadFolder, item);
return fs.statSync(itemPath).isDirectory() && item.endsWith(".app");
});
if (appFolders.length === 0) {
throw new Error("Invalid IPA structure: No .app folder found in Payload.");
}
const appFolder = path.join(payloadFolder, appFolders[0]);
const codePushFolder = path.join(appFolder, "assets");
const fileHashes: { [key: string]: string } = {};
if (fs.existsSync(codePushFolder)) {
await calculateHashesForDirectory(codePushFolder, appFolder, fileHashes);
} else {
log(chalk.yellow(`\nWarning: CodePush folder not found in IPA.\n`));
}
const mainJsBundlePath = path.join(appFolder, "main.jsbundle");
if (fs.existsSync(mainJsBundlePath)) {
log(chalk.cyan(`\nFound main.jsbundle, calculating hash:\n`));
const bundleHash = await hashFile(mainJsBundlePath);
fileHashes["CodePush/main.jsbundle"] = bundleHash;
// Copy bundle to output folder
const outputCodePushFolder = path.join(outputFolder, "CodePush");
fs.mkdirSync(outputCodePushFolder, { recursive: true });
const outputBundlePath = path.join(outputCodePushFolder, "main.jsbundle");
fs.copyFileSync(mainJsBundlePath, outputBundlePath);
} else {
throw new Error("main.jsbundle not found in IPA root folder.");
}
// Save packageManifest.json
const manifestPath = path.join(outputFolder, "packageManifest.json");
fs.writeFileSync(manifestPath, JSON.stringify(fileHashes, null, 2));
log(chalk.cyan(`\nSaved packageManifest.json with ${Object.keys(fileHashes).length} entries.\n`));
// Create zip archive with packageManifest.json and bundle file
const zipPath = path.join(os.tmpdir(), `CodePushBinary-${Date.now()}.zip`);
await createZipArchive(outputFolder, zipPath, ["packageManifest.json", "CodePush/main.jsbundle"]);
return zipPath;
}
async function calculateHashesForDirectory(
directoryPath: string,
basePath: string,
fileHashes: { [key: string]: string }
) {
const items = fs.readdirSync(directoryPath);
for (const item of items) {
const itemPath = path.join(directoryPath, item);
const stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
await calculateHashesForDirectory(itemPath, basePath, fileHashes);
} else {
// Calculate relative path from basePath (app folder) to the file
const relativePath = path.relative(basePath, itemPath).replace(/\\/g, "/");
const hash = await hashFile(itemPath);
const hashKey = `CodePush/${relativePath}`
fileHashes[hashKey] = hash;
log(chalk.gray(` ${relativePath}:${hash.substring(0, 8)}...\n`));
}
}
}
type BinaryHashes = { [p: string]: string };
async function takeHashesFromMetadata(metadataPath: string): Promise<BinaryHashes> {
const content = await readFile(metadataPath, "utf-8");
const metadata = JSON.parse(content);
if (!metadata || !metadata.manifest) {
throw new Error("Failed to take manifest from metadata file of APK");
}
return Object.fromEntries(metadata.manifest.map((item) => item.split(":")));
}
function createZipArchive(sourceFolder: string, zipPath: string, filesToInclude: string[]): Q.Promise<void> {
return Q.Promise<void>((resolve, reject) => {
const zipFile = new yazl.ZipFile();
const writeStream = fs.createWriteStream(zipPath);
zipFile.outputStream
.pipe(writeStream)
.on("error", (error: Error) => {
reject(error);
})
.on("close", () => {
resolve();
});
for (const file of filesToInclude) {
const filePath = path.join(sourceFolder, file);
if (fs.existsSync(filePath)) {
zipFile.addFile(filePath, file);
}
}
zipFile.end();
});
}
function parseAnyPlistFile(plistPath: string): any {
const buf = fs.readFileSync(plistPath);
if (buf.slice(0, 6).toString("ascii") === "bplist") {
const arr = bplist.parseBuffer(buf);
if (!arr?.length) throw new Error("Empty binary plist");
return arr[0];
}
const xml = buf.toString("utf8");
return plist.parse(xml);
}
export async function getIosVersion(extractFolder: string) {
const payloadFolder = path.join(extractFolder, "Payload");
if (!fs.existsSync(payloadFolder)) {
throw new Error("Invalid IPA structure: Payload folder not found.");
}
const appFolders = fs.readdirSync(payloadFolder).filter((item) => {
const itemPath = path.join(payloadFolder, item);
return fs.statSync(itemPath).isDirectory() && item.endsWith(".app");
});
if (appFolders.length === 0) {
throw new Error("Invalid IPA structure: No .app folder found in Payload.");
}
const appFolder = path.join(payloadFolder, appFolders[0]);
const plistPath = path.join(appFolder, "Info.plist");
const data = parseAnyPlistFile(plistPath);
console.log('App Version (Short):', data.CFBundleShortVersionString);
console.log('Build Number:', data.CFBundleVersion);
console.log('Bundle ID:', data.CFBundleIdentifier);
return {
version: data.CFBundleShortVersionString,
build: data.CFBundleVersion
};
}