-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (130 loc) · 4.88 KB
/
index.js
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
const child_process = require( 'child_process' );
const os = require( 'os' );
const path = require( 'path' );
const fs = require( 'fs' );
const extract = require( 'extract-zip' );
const fetch = require( 'node-fetch' );
const tempy = require( 'tempy' );
const parser = require( 'fast-xml-parser' );
// https://developer.android.com/studio/command-line/aapt2
const VERSION_URL = 'https://dl.google.com/dl/android/maven2/com/android/tools/build/group-index.xml';
const getLatestVersion = async function getLatestVersion(){
const response = await fetch( VERSION_URL )
.then( ( response ) => {
return response.text();
} );
const JSONData = parser.parse( response, {
ignoreAttributes: false,
} );
const versions = JSONData[ 'com.android.tools.build' ].aapt2[ '@_versions' ].split( ',' ).sort();
let currentVersion = false;
for ( const versionString of versions ) {
const regex = new RegExp( );
if ( !versionString.match( /\d+\.\d+\.\d+-\d+/ ) ) {
continue;
}
currentVersion = versionString;
}
return currentVersion;
}
const getBinaries = async function getBinaries(){
const version = await getLatestVersion();
if ( fs.existsSync( path.join( __dirname, 'binaries', `${ version }-${ getGooglePlatform() }`, 'aapt2' ) ) ) {
return version;
}
await getBinary( version );
return version;
};
const getDownloadURL = function getDownloadURL( version ) {
return `https://dl.google.com/dl/android/maven2/com/android/tools/build/aapt2/${ version }/aapt2-${ version }-${ getGooglePlatform() }.jar`;
};
const downloadFile = function downloadFile( url, version ) {
return new Promise( async ( resolve, reject ) => {
const response = await fetch( url );
const tempPath = tempy.file( {
extension: 'jar',
} );
const destination = fs.createWriteStream( tempPath );
response.body.pipe( destination );
destination.on( 'finish', () => {
extract( tempPath, {
dir: path.join( __dirname, 'binaries', `${ version }-${ getGooglePlatform() }` ),
}, ( extractError ) => {
if ( extractError ) {
return reject( extractError );
}
fs.chmod( path.join( __dirname, 'binaries', `${ version }-${ getGooglePlatform() }`, 'aapt2' ), 0o755, ( chmodError ) => {
if ( chmodError ) {
return reject( chmodError );
}
resolve();
} );
} );
} );
} );
};
const getGooglePlatform = function getGooglePlatform() {
switch ( process.platform ) {
case 'darwin':
return 'osx';
case 'win32':
return 'windows';
default:
// falback to 'linux
return 'linux';
}
};
const getBinary = async function getBinary( version ){
await downloadFile( getDownloadURL( version ), version );
}
const parse = async function parse ( apkPath ) {
const version = await getBinaries();
let resources = [];
const command = `${ path.join( __dirname, 'binaries', `${ version }-${ getGooglePlatform() }`, 'aapt2' ) } dump resources ${ apkPath }`;
console.log( command );
return new Promise ( ( resolve, reject ) => {
child_process.exec( command, ( execError, stdout, stderr ) => {
if ( execError ) {
return reject( execError );
}
if ( stderr ) {
return reject( stderr );
}
const lines = stdout.split( os.EOL );
let currentResource = false;
for ( const line of lines ) {
const matches = line.match( /resource\s(?<resourceID>0x[0-9a-f]+)\s(?<resourceName>.+?)$/um );
if ( matches ) {
if ( currentResource ) {
resources.push( currentResource );
}
currentResource = {
id: matches.groups.resourceID,
name: matches.groups.resourceName,
values: [],
};
continue;
}
if ( currentResource ) {
currentResource.values.push( line.trim() );
}
}
resolve( resources );
} );
} );
};
const getResourceByID = function getResourceByID( resources, resourceID ) {
return resources.find( ( resource ) => {
return resource.id === resourceID;
} );
};
const getResourceByName = function getResourceByName( resources, resourceName ) {
return resources.find( ( resource ) => {
return resource.name === resourceName;
} );
};
module.exports = {
parse: parse,
getResourceByID: getResourceByID,
getResourceByName: getResourceByName,
};