Skip to content

Commit e492c5e

Browse files
committed
Refactor app-info-parser: migrate ApkParser and AppParser to TypeScript, remove deprecated apk.js and app.js files, and update import statements for consistency.
1 parent 00e5446 commit e492c5e

File tree

9 files changed

+108
-286
lines changed

9 files changed

+108
-286
lines changed

src/package.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import type { Platform } from './types';
77
import { getAabInfo, getApkInfo, getAppInfo, getIpaInfo } from './utils';
88
import { depVersions } from './utils/dep-versions';
99
import { getCommitInfo } from './utils/git';
10-
import AabParser from './utils/app-info-parser/aab';
10+
import { AabParser } from './utils/app-info-parser/aab';
1111
import { t } from './utils/i18n';
1212
import path from 'path';
1313

1414
export async function listPackage(appId: string) {
15-
const allPkgs = await getAllPackages(appId) || [];
15+
const allPkgs = (await getAllPackages(appId)) || [];
1616

1717
const header = [
1818
{ value: t('nativePackageId') },
@@ -261,7 +261,12 @@ export const packageCommands = {
261261
options,
262262
}: {
263263
args: string[];
264-
options: { appId?: string; packageId?: string; packageVersion?: string; platform?: Platform };
264+
options: {
265+
appId?: string;
266+
packageId?: string;
267+
packageVersion?: string;
268+
platform?: Platform;
269+
};
265270
}) => {
266271
let { appId, packageId, packageVersion } = options;
267272

src/utils/app-info-parser/aab.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import fs from 'fs-extra';
22
import path from 'path';
33
import os from 'os';
44
import { open as openZipFile } from 'yauzl';
5-
import Zip from './zip';
5+
import { Zip } from './zip';
66
import { t } from '../i18n';
77

8-
class AabParser extends Zip {
8+
export class AabParser extends Zip {
99
file: string | File;
1010

1111
constructor(file: string | File) {
@@ -169,5 +169,3 @@ class AabParser extends Zip {
169169

170170
const escapeRegExp = (value: string) =>
171171
value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
172-
173-
export = AabParser;

src/utils/app-info-parser/apk.js

Lines changed: 0 additions & 246 deletions
This file was deleted.

src/utils/app-info-parser/apk.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { findApkIconPath, getBase64FromBuffer, mapInfoResource } from './utils';
2+
import { Zip } from './zip';
3+
4+
const ManifestName = /^androidmanifest\.xml$/;
5+
const ResourceName = /^resources\.arsc$/;
6+
7+
const ManifestXmlParser = require('./xml-parser/manifest');
8+
const ResourceFinder = require('./resource-finder');
9+
10+
export class ApkParser extends Zip {
11+
parse(): Promise<any> {
12+
return new Promise((resolve, reject) => {
13+
this.getEntries([ManifestName, ResourceName])
14+
.then((buffers: any) => {
15+
const manifestBuffer = buffers[ManifestName];
16+
if (!manifestBuffer) {
17+
throw new Error("AndroidManifest.xml can't be found.");
18+
}
19+
let apkInfo: any;
20+
let resourceMap: any;
21+
22+
apkInfo = this._parseManifest(manifestBuffer as Buffer);
23+
24+
if (!buffers[ResourceName]) {
25+
resolve(apkInfo);
26+
} else {
27+
resourceMap = this._parseResourceMap(
28+
buffers[ResourceName] as Buffer,
29+
);
30+
apkInfo = mapInfoResource(apkInfo, resourceMap);
31+
32+
const iconPath = findApkIconPath(apkInfo);
33+
if (iconPath) {
34+
this.getEntry(iconPath)
35+
.then((iconBuffer: Buffer | null) => {
36+
apkInfo.icon = iconBuffer
37+
? getBase64FromBuffer(iconBuffer)
38+
: null;
39+
resolve(apkInfo);
40+
})
41+
.catch((e: any) => {
42+
apkInfo.icon = null;
43+
resolve(apkInfo);
44+
console.warn('[Warning] failed to parse icon: ', e);
45+
});
46+
} else {
47+
apkInfo.icon = null;
48+
resolve(apkInfo);
49+
}
50+
}
51+
})
52+
.catch((e: any) => {
53+
reject(e);
54+
});
55+
});
56+
}
57+
58+
/**
59+
* Parse manifest
60+
* @param {Buffer} buffer // manifest file's buffer
61+
*/
62+
private _parseManifest(buffer: Buffer) {
63+
try {
64+
const parser = new ManifestXmlParser(buffer, {
65+
ignore: [
66+
'application.activity',
67+
'application.service',
68+
'application.receiver',
69+
'application.provider',
70+
'permission-group',
71+
],
72+
});
73+
return parser.parse();
74+
} catch (e: any) {
75+
throw new Error(`Parse AndroidManifest.xml error: ${e.message || e}`);
76+
}
77+
}
78+
79+
/**
80+
* Parse resourceMap
81+
* @param {Buffer} buffer // resourceMap file's buffer
82+
*/
83+
private _parseResourceMap(buffer: Buffer) {
84+
try {
85+
return new ResourceFinder().processResourceTable(buffer);
86+
} catch (e: any) {
87+
throw new Error(`Parser resources.arsc error: ${e}`);
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)