mirror of
https://gitcode.com/github-mirrors/react-native-update-cli.git
synced 2025-09-17 18:06:10 +08:00
Compare commits
141 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
91fc0caa07 | ||
![]() |
36e1be084d | ||
![]() |
e099002629 | ||
![]() |
5dd3b64720 | ||
![]() |
31ec3edc5a | ||
![]() |
4b772194d1 | ||
![]() |
792418c964 | ||
![]() |
d5f468aa44 | ||
![]() |
04f75cd51b | ||
![]() |
5a05679e2b | ||
![]() |
69f4372b63 | ||
![]() |
dbfdce6412 | ||
![]() |
1372a30ea3 | ||
![]() |
d51ef37a62 | ||
![]() |
90a59cfe7b | ||
![]() |
ffcc6c9c96 | ||
![]() |
b00b3144a3 | ||
![]() |
d3ee908227 | ||
![]() |
5302c8968f | ||
![]() |
5740a3315c | ||
![]() |
4a11a4118f | ||
![]() |
feb1128748 | ||
![]() |
8fd74d76b1 | ||
![]() |
209a989a48 | ||
![]() |
aab9ea6b98 | ||
![]() |
5f45a66aad | ||
![]() |
67f6aeef02 | ||
![]() |
5a28847e46 | ||
![]() |
54f7358357 | ||
![]() |
d2ec856756 | ||
![]() |
84e5e34820 | ||
![]() |
006951ae88 | ||
![]() |
b1fda5ab9c | ||
![]() |
385b974aff | ||
![]() |
2a02deb015 | ||
![]() |
8b7cb809f6 | ||
![]() |
070991d08b | ||
![]() |
0d03e18c58 | ||
![]() |
a0dfcb5c4b | ||
![]() |
587da8aaf9 | ||
![]() |
724088a810 | ||
![]() |
7c20b30c85 | ||
![]() |
6a053c6428 | ||
![]() |
f202fc590d | ||
![]() |
0f8cf3c399 | ||
![]() |
6a0d82c7a5 | ||
![]() |
eed19992d8 | ||
![]() |
1b5078831c | ||
![]() |
bc9aff343a | ||
![]() |
7aaa32a5a2 | ||
![]() |
ab7920fe38 | ||
![]() |
d912ace4a7 | ||
![]() |
8af4d314ce | ||
![]() |
f2d5269512 | ||
![]() |
fe24c4ca36 | ||
![]() |
cf61c297a6 | ||
![]() |
f9adc700ed | ||
![]() |
dcff16cbb5 | ||
![]() |
2bb8e83010 | ||
![]() |
0cfc6e4f0d | ||
![]() |
da7bdbfdd2 | ||
![]() |
07ee28ba3b | ||
![]() |
dae3e4024f | ||
![]() |
d673b5736a | ||
![]() |
732845faad | ||
![]() |
bcfdd67ea8 | ||
![]() |
27ea54c1ec | ||
![]() |
ba0fa836d1 | ||
![]() |
bde76094fc | ||
![]() |
f1d6c3744e | ||
![]() |
768484d7b5 | ||
![]() |
d6632ffcc6 | ||
![]() |
24f5b316a9 | ||
![]() |
03a4108a08 | ||
![]() |
32d7ed9b00 | ||
![]() |
6f3d45c3f2 | ||
![]() |
25cb724921 | ||
![]() |
a7b79a30e8 | ||
![]() |
11799dd0c1 | ||
![]() |
2ab0cad7e5 | ||
![]() |
ec8d6a767b | ||
![]() |
fa8290fcbf | ||
![]() |
46d308c7c2 | ||
![]() |
9e721e1858 | ||
![]() |
c30454976c | ||
![]() |
3d73b95140 | ||
![]() |
32cafd92bf | ||
![]() |
ea10cd7828 | ||
![]() |
574a52b126 | ||
![]() |
c00a7a72fe | ||
![]() |
0e8e3aa420 | ||
![]() |
7426e93647 | ||
![]() |
b6fb2e7d2a | ||
![]() |
7f1dcbb571 | ||
![]() |
59fad93832 | ||
![]() |
3db4f803e2 | ||
![]() |
56d44b8c85 | ||
![]() |
ecd951265f | ||
![]() |
62d455ac07 | ||
![]() |
acfc2dd65b | ||
![]() |
7f062f681a | ||
![]() |
7f5b9fd7fd | ||
![]() |
13b21c49c1 | ||
![]() |
85db61704c | ||
![]() |
57ddfc5d7d | ||
![]() |
6cb38eeec1 | ||
![]() |
8a7c21fb50 | ||
![]() |
7223fa1389 | ||
![]() |
2d5c01e990 | ||
![]() |
16b1cc3ed4 | ||
![]() |
5e69793925 | ||
![]() |
b3c8e1f8e3 | ||
![]() |
27ed4bd8d1 | ||
![]() |
f0ad8bdcde | ||
![]() |
62a37a955b | ||
![]() |
233e661f17 | ||
![]() |
3a9bf67703 | ||
![]() |
aa5fd4b4b1 | ||
![]() |
6ed6420dd1 | ||
![]() |
92f7d7efc4 | ||
![]() |
de68470060 | ||
![]() |
dfee2dd762 | ||
![]() |
d726987b2c | ||
![]() |
01f11822e1 | ||
![]() |
37353222ef | ||
![]() |
b76a2eb989 | ||
![]() |
05b97d1e1e | ||
![]() |
ba4969fe68 | ||
![]() |
dad63743c1 | ||
![]() |
924c58d254 | ||
![]() |
b950d3dbe8 | ||
![]() |
1206cd4461 | ||
![]() |
0147582561 | ||
![]() |
a343999cb6 | ||
![]() |
ef28133cfc | ||
![]() |
d16292a7a6 | ||
![]() |
2146940d01 | ||
![]() |
755485b4cb | ||
![]() |
6bc7ee7091 | ||
![]() |
9d00325519 | ||
![]() |
84facadf3d |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -104,3 +104,4 @@ dist
|
|||||||
.tern-port
|
.tern-port
|
||||||
|
|
||||||
lib/
|
lib/
|
||||||
|
.DS_Store
|
||||||
|
15
.swcrc
Normal file
15
.swcrc
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"jsc": {
|
||||||
|
"loose": true,
|
||||||
|
"target": "es2018",
|
||||||
|
"parser": {
|
||||||
|
"syntax": "typescript",
|
||||||
|
"tsx": false,
|
||||||
|
"decorators": false,
|
||||||
|
"dynamicImport": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"module": {
|
||||||
|
"type": "commonjs"
|
||||||
|
}
|
||||||
|
}
|
29
LICENSE
29
LICENSE
@@ -1,29 +0,0 @@
|
|||||||
BSD 3-Clause License
|
|
||||||
|
|
||||||
Copyright (c) 2020, reactnativecn
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
3. Neither the name of the copyright holder nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
103
cli.json
103
cli.json
@@ -2,15 +2,11 @@
|
|||||||
"useCommand": true,
|
"useCommand": true,
|
||||||
"defaultCommand": "help",
|
"defaultCommand": "help",
|
||||||
"commands": {
|
"commands": {
|
||||||
"help": {
|
"help": {},
|
||||||
},
|
|
||||||
|
|
||||||
"login":{
|
"login": {},
|
||||||
},
|
"logout": {},
|
||||||
"logout": {
|
"me": {},
|
||||||
},
|
|
||||||
"me": {
|
|
||||||
},
|
|
||||||
|
|
||||||
"createApp": {
|
"createApp": {
|
||||||
"options": {
|
"options": {
|
||||||
@@ -29,8 +25,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"deleteApp": {
|
"deleteApp": {},
|
||||||
},
|
|
||||||
"selectApp": {
|
"selectApp": {
|
||||||
"options": {
|
"options": {
|
||||||
"platform": {
|
"platform": {
|
||||||
@@ -39,10 +34,10 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
"uploadIpa": {
|
"uploadIpa": {},
|
||||||
},
|
"uploadApk": {},
|
||||||
"uploadApk": {
|
"parseIpa": {},
|
||||||
},
|
"parseApk": {},
|
||||||
"packages": {
|
"packages": {
|
||||||
"options": {
|
"options": {
|
||||||
"platform": {
|
"platform": {
|
||||||
@@ -85,6 +80,41 @@
|
|||||||
},
|
},
|
||||||
"packageId": {
|
"packageId": {
|
||||||
"hasValue": true
|
"hasValue": true
|
||||||
|
},
|
||||||
|
"packageVersion": {
|
||||||
|
"hasValue": true
|
||||||
|
},
|
||||||
|
"minPackageVersion": {
|
||||||
|
"hasValue": true
|
||||||
|
},
|
||||||
|
"maxPackageVersion": {
|
||||||
|
"hasValue": true
|
||||||
|
},
|
||||||
|
"rollout": {
|
||||||
|
"hasValue": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
"updateVersionInfo": {
|
||||||
|
"options": {
|
||||||
|
"platform": {
|
||||||
|
"hasValue": true
|
||||||
|
},
|
||||||
|
"versionId": {
|
||||||
|
"hasValue": true
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"default": false,
|
||||||
|
"hasValue": true
|
||||||
|
},
|
||||||
|
"description": {
|
||||||
|
"default": false,
|
||||||
|
"hasValue": true
|
||||||
|
},
|
||||||
|
"metaInfo": {
|
||||||
|
"default": false,
|
||||||
|
"hasValue": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -102,7 +132,7 @@
|
|||||||
"platform": {
|
"platform": {
|
||||||
"hasValue": true
|
"hasValue": true
|
||||||
},
|
},
|
||||||
"bundleName":{
|
"bundleName": {
|
||||||
"default": "index.bundlejs",
|
"default": "index.bundlejs",
|
||||||
"hasValue": true
|
"hasValue": true
|
||||||
},
|
},
|
||||||
@@ -111,15 +141,15 @@
|
|||||||
"hasValue": true
|
"hasValue": true
|
||||||
},
|
},
|
||||||
"intermediaDir": {
|
"intermediaDir": {
|
||||||
"default": "build/intermedia/${platform}",
|
"default": ".pushy/intermedia/${platform}",
|
||||||
"hasValue": true
|
"hasValue": true
|
||||||
},
|
},
|
||||||
"output": {
|
"output": {
|
||||||
"default": "build/output/${platform}.${time}.ppk",
|
"default": ".pushy/output/${platform}.${time}.ppk",
|
||||||
"hasValue": true
|
"hasValue": true
|
||||||
},
|
},
|
||||||
"verbose": {
|
"sourcemap": {
|
||||||
|
"default": false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -130,7 +160,7 @@
|
|||||||
"description": "Create diff patch",
|
"description": "Create diff patch",
|
||||||
"options": {
|
"options": {
|
||||||
"output": {
|
"output": {
|
||||||
"default": "build/output/diff",
|
"default": ".pushy/output/diff",
|
||||||
"hasValue": true
|
"hasValue": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,7 +169,7 @@
|
|||||||
"description": "Create diff patch from a Android package(.apk)",
|
"description": "Create diff patch from a Android package(.apk)",
|
||||||
"options": {
|
"options": {
|
||||||
"output": {
|
"output": {
|
||||||
"default": "build/output/diff-${time}.apk-patch",
|
"default": ".pushy/output/diff-${time}.apk-patch",
|
||||||
"hasValue": true
|
"hasValue": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -148,13 +178,40 @@
|
|||||||
"description": "Create diff patch from a iOS package(.ipa)",
|
"description": "Create diff patch from a iOS package(.ipa)",
|
||||||
"options": {
|
"options": {
|
||||||
"output": {
|
"output": {
|
||||||
"default": "build/output/diff-${time}.ipa-patch",
|
"default": ".pushy/output/diff-${time}.ipa-patch",
|
||||||
|
"hasValue": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"hdiff": {
|
||||||
|
"description": "Create hdiff patch",
|
||||||
|
"options": {
|
||||||
|
"output": {
|
||||||
|
"default": ".pushy/output/hdiff",
|
||||||
|
"hasValue": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"hdiffFromApk": {
|
||||||
|
"description": "Create hdiff patch from a Android package(.apk)",
|
||||||
|
"options": {
|
||||||
|
"output": {
|
||||||
|
"default": ".pushy/output/hdiff-${time}.apk-patch",
|
||||||
|
"hasValue": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"hdiffFromIpa": {
|
||||||
|
"description": "Create hdiff patch from a iOS package(.ipa)",
|
||||||
|
"options": {
|
||||||
|
"output": {
|
||||||
|
"default": ".pushy/output/hdiff-${time}.ipa-patch",
|
||||||
"hasValue": true
|
"hasValue": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"globalOptions":{
|
"globalOptions": {
|
||||||
"no-interactive": {
|
"no-interactive": {
|
||||||
"default": false
|
"default": false
|
||||||
}
|
}
|
||||||
|
5
index.js
5
index.js
@@ -1,4 +1 @@
|
|||||||
/**
|
module.exports = require('./lib');
|
||||||
* Created by tdzl2003 on 2/22/16.
|
|
||||||
*/
|
|
||||||
module.exports = require('./lib');
|
|
||||||
|
55
package.json
55
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "react-native-update-cli",
|
"name": "react-native-update-cli",
|
||||||
"version": "1.1.0",
|
"version": "1.32.3",
|
||||||
"description": "Command tools for javaScript updater with `pushy` service for react native apps.",
|
"description": "Command tools for javaScript updater with `pushy` service for react native apps.",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
@@ -12,8 +12,8 @@
|
|||||||
"cli.json"
|
"cli.json"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"build": "swc src -d lib --strip-leading-paths",
|
||||||
"prepublish": "babel src --out-dir lib"
|
"prepare": "npm run build && chmod +x lib/index.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -32,29 +32,38 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/reactnativecn/react-native-pushy/tree/master/react-native-pushy-cli",
|
"homepage": "https://github.com/reactnativecn/react-native-pushy/tree/master/react-native-pushy-cli",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"app-info-parser": "^0.3.9",
|
"bplist-parser": "^0.3.2",
|
||||||
|
"bytebuffer": "^5.0.1",
|
||||||
|
"cgbi-to-png": "^1.0.7",
|
||||||
"cli-arguments": "^0.2.1",
|
"cli-arguments": "^0.2.1",
|
||||||
"fs-extra": "^8.1.0",
|
"commander": "^12.1.0",
|
||||||
"gradle-to-js": "^2.0.0",
|
"compare-versions": "^6.1.1",
|
||||||
"node-fetch": "^2.6.0",
|
"filesize-parser": "^1.5.1",
|
||||||
|
"form-data": "^4.0.1",
|
||||||
|
"fs-extra": "8",
|
||||||
|
"gradle-to-js": "^2.0.1",
|
||||||
|
"isomorphic-unzip": "^1.1.5",
|
||||||
|
"node-fetch": "^2.6.1",
|
||||||
|
"plist": "^3.1.0",
|
||||||
"progress": "^2.0.3",
|
"progress": "^2.0.3",
|
||||||
"read": "^1.0.7",
|
"properties": "^1.2.1",
|
||||||
"request": "^2.88.2",
|
"read": "^4.0.0",
|
||||||
|
"semver": "^7.6.3",
|
||||||
"tcp-ping": "^0.1.1",
|
"tcp-ping": "^0.1.1",
|
||||||
"tty-table": "2.8",
|
"tty-table": "4.2",
|
||||||
"update-notifier": "^4.1.0",
|
"update-notifier": "^5.1.0",
|
||||||
"yauzl": "^2.10.0",
|
"yauzl": "^3.2.0",
|
||||||
"yazl": "2.5.1"
|
"yazl": "3.3.0"
|
||||||
},
|
|
||||||
"devDependencies": {
|
|
||||||
"babel-cli": "^6.26.0",
|
|
||||||
"babel-eslint": "^10.1.0",
|
|
||||||
"babel-plugin-syntax-object-rest-spread": "^6.13.0",
|
|
||||||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
|
|
||||||
"babel-plugin-transform-es2015-spread": "^6.22.0",
|
|
||||||
"babel-plugin-transform-object-rest-spread": "^6.26.0"
|
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 8"
|
"node": ">= 10"
|
||||||
}
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@swc/cli": "^0.5.0",
|
||||||
|
"@swc/core": "^1.9.2",
|
||||||
|
"@types/node": "^22.9.0",
|
||||||
|
"oxlint": "^0.11.1",
|
||||||
|
"typescript": "^5.6.3"
|
||||||
|
},
|
||||||
|
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
|
||||||
}
|
}
|
||||||
|
165
src/api.js
165
src/api.js
@@ -1,165 +0,0 @@
|
|||||||
/**
|
|
||||||
* Created by tdzl2003 on 2/13/16.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const fetch = require('node-fetch');
|
|
||||||
let host = process.env.PUSHY_REGISTRY || 'https://update.reactnative.cn/api';
|
|
||||||
const fs = require('fs-extra');
|
|
||||||
import request from 'request';
|
|
||||||
import ProgressBar from 'progress';
|
|
||||||
const packageJson = require('../package.json');
|
|
||||||
const tcpp = require('tcp-ping');
|
|
||||||
const util = require('util');
|
|
||||||
|
|
||||||
const tcpPing = util.promisify(tcpp.ping);
|
|
||||||
|
|
||||||
let session = undefined;
|
|
||||||
let savedSession = undefined;
|
|
||||||
|
|
||||||
const userAgent = `react-native-update-cli/${packageJson.version}`;
|
|
||||||
|
|
||||||
exports.loadSession = async function() {
|
|
||||||
if (fs.existsSync('.update')) {
|
|
||||||
try {
|
|
||||||
exports.replaceSession(JSON.parse(fs.readFileSync('.update', 'utf8')));
|
|
||||||
savedSession = session;
|
|
||||||
} catch (e) {
|
|
||||||
console.error(
|
|
||||||
'Failed to parse file `.update`. Try to remove it manually.',
|
|
||||||
);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.getSession = function() {
|
|
||||||
return session;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.replaceSession = function(newSession) {
|
|
||||||
session = newSession;
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.saveSession = function() {
|
|
||||||
// Only save on change.
|
|
||||||
if (session !== savedSession) {
|
|
||||||
const current = session;
|
|
||||||
const data = JSON.stringify(current, null, 4);
|
|
||||||
fs.writeFileSync('.update', data, 'utf8');
|
|
||||||
savedSession = current;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.closeSession = function() {
|
|
||||||
if (fs.existsSync('.update')) {
|
|
||||||
fs.unlinkSync('.update');
|
|
||||||
savedSession = undefined;
|
|
||||||
}
|
|
||||||
session = undefined;
|
|
||||||
host = process.env.PUSHY_REGISTRY || 'https://update.reactnative.cn';
|
|
||||||
};
|
|
||||||
|
|
||||||
async function query(url, options) {
|
|
||||||
const resp = await fetch(url, options);
|
|
||||||
const json = await resp.json();
|
|
||||||
if (resp.status !== 200) {
|
|
||||||
throw Object.assign(new Error(json.message || json.error), {
|
|
||||||
status: resp.status,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
|
|
||||||
function queryWithoutBody(method) {
|
|
||||||
return function(api) {
|
|
||||||
return query(host + api, {
|
|
||||||
method,
|
|
||||||
headers: {
|
|
||||||
'User-Agent': userAgent,
|
|
||||||
'X-AccessToken': session ? session.token : '',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function queryWithBody(method) {
|
|
||||||
return function(api, body) {
|
|
||||||
return query(host + api, {
|
|
||||||
method,
|
|
||||||
headers: {
|
|
||||||
'User-Agent': userAgent,
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'X-AccessToken': session ? session.token : '',
|
|
||||||
},
|
|
||||||
body: JSON.stringify(body),
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.get = queryWithoutBody('GET');
|
|
||||||
exports.post = queryWithBody('POST');
|
|
||||||
exports.put = queryWithBody('PUT');
|
|
||||||
exports.doDelete = queryWithBody('DELETE');
|
|
||||||
|
|
||||||
async function uploadFile(fn, key) {
|
|
||||||
const { url, backupUrl, formData } = await exports.post('/upload', {});
|
|
||||||
let realUrl = url;
|
|
||||||
|
|
||||||
if (backupUrl) {
|
|
||||||
const pingResult = await tcpPing({
|
|
||||||
address: url.replace('https://', ''),
|
|
||||||
attempts: 4,
|
|
||||||
timeout: 1000,
|
|
||||||
});
|
|
||||||
// console.log({pingResult});
|
|
||||||
if (isNaN(pingResult.avg) || pingResult.avg > 150) {
|
|
||||||
realUrl = backupUrl;
|
|
||||||
}
|
|
||||||
// console.log({realUrl});
|
|
||||||
}
|
|
||||||
|
|
||||||
const fileSize = fs.statSync(fn).size;
|
|
||||||
|
|
||||||
const bar = new ProgressBar(' Uploading [:bar] :percent :etas', {
|
|
||||||
complete: '=',
|
|
||||||
incomplete: ' ',
|
|
||||||
total: fileSize,
|
|
||||||
});
|
|
||||||
|
|
||||||
const info = await new Promise((resolve, reject) => {
|
|
||||||
if (key) {
|
|
||||||
formData.key = key;
|
|
||||||
}
|
|
||||||
formData.file = fs.createReadStream(fn);
|
|
||||||
|
|
||||||
formData.file.on('data', function(data) {
|
|
||||||
bar.tick(data.length);
|
|
||||||
});
|
|
||||||
request.post(
|
|
||||||
realUrl,
|
|
||||||
{
|
|
||||||
formData,
|
|
||||||
},
|
|
||||||
(err, resp, body) => {
|
|
||||||
if (err) {
|
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
if (resp.statusCode > 299) {
|
|
||||||
return reject(
|
|
||||||
Object.assign(new Error(body), { status: resp.statusCode }),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
resolve(
|
|
||||||
body
|
|
||||||
? // qiniu
|
|
||||||
JSON.parse(body)
|
|
||||||
: // aliyun oss
|
|
||||||
{ hash: formData.key },
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.uploadFile = uploadFile;
|
|
178
src/api.ts
Normal file
178
src/api.ts
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
import fetch from 'node-fetch';
|
||||||
|
import fs from 'node:fs';
|
||||||
|
import util from 'node:util';
|
||||||
|
import path from 'node:path';
|
||||||
|
import ProgressBar from 'progress';
|
||||||
|
import packageJson from '../package.json';
|
||||||
|
import tcpp from 'tcp-ping';
|
||||||
|
import filesizeParser from 'filesize-parser';
|
||||||
|
import { pricingPageUrl } from './utils';
|
||||||
|
import { Session } from 'types';
|
||||||
|
import FormData from 'form-data';
|
||||||
|
|
||||||
|
const tcpPing = util.promisify(tcpp.ping);
|
||||||
|
|
||||||
|
let session: Session | undefined;
|
||||||
|
let savedSession: Session | undefined;
|
||||||
|
|
||||||
|
const defaultEndpoint = 'https://update.reactnative.cn/api';
|
||||||
|
let host = process.env.PUSHY_REGISTRY || defaultEndpoint;
|
||||||
|
|
||||||
|
const userAgent = `react-native-update-cli/${packageJson.version}`;
|
||||||
|
|
||||||
|
export const getSession = function () {
|
||||||
|
return session;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const replaceSession = function (newSession: { token: string }) {
|
||||||
|
session = newSession;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const loadSession = async function () {
|
||||||
|
if (fs.existsSync('.update')) {
|
||||||
|
try {
|
||||||
|
replaceSession(JSON.parse(fs.readFileSync('.update', 'utf8')));
|
||||||
|
savedSession = session;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(
|
||||||
|
'Failed to parse file `.update`. Try to remove it manually.',
|
||||||
|
);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const saveSession = function () {
|
||||||
|
// Only save on change.
|
||||||
|
if (session !== savedSession) {
|
||||||
|
const current = session;
|
||||||
|
const data = JSON.stringify(current, null, 4);
|
||||||
|
fs.writeFileSync('.update', data, 'utf8');
|
||||||
|
savedSession = current;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const closeSession = function () {
|
||||||
|
if (fs.existsSync('.update')) {
|
||||||
|
fs.unlinkSync('.update');
|
||||||
|
savedSession = undefined;
|
||||||
|
}
|
||||||
|
session = undefined;
|
||||||
|
host = process.env.PUSHY_REGISTRY || defaultEndpoint;
|
||||||
|
};
|
||||||
|
|
||||||
|
async function query(url: string, options: fetch.RequestInit) {
|
||||||
|
const resp = await fetch(url, options);
|
||||||
|
const text = await resp.text();
|
||||||
|
let json;
|
||||||
|
try {
|
||||||
|
json = JSON.parse(text);
|
||||||
|
} catch (e) {
|
||||||
|
if (resp.statusText.includes('Unauthorized')) {
|
||||||
|
throw new Error('登录信息已过期,请使用 pushy login 命令重新登录');
|
||||||
|
} else {
|
||||||
|
throw new Error(`Server error: ${resp.statusText}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resp.status !== 200) {
|
||||||
|
throw new Error(`${resp.status}: ${resp.statusText}`);
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
function queryWithoutBody(method: string) {
|
||||||
|
return function (api: string) {
|
||||||
|
return query(host + api, {
|
||||||
|
method,
|
||||||
|
headers: {
|
||||||
|
'User-Agent': userAgent,
|
||||||
|
'X-AccessToken': session ? session.token : '',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function queryWithBody(method: string) {
|
||||||
|
return function (api: string, body: Record<string, any>) {
|
||||||
|
return query(host + api, {
|
||||||
|
method,
|
||||||
|
headers: {
|
||||||
|
'User-Agent': userAgent,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-AccessToken': session ? session.token : '',
|
||||||
|
},
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const get = queryWithoutBody('GET');
|
||||||
|
export const post = queryWithBody('POST');
|
||||||
|
export const put = queryWithBody('PUT');
|
||||||
|
export const doDelete = queryWithBody('DELETE');
|
||||||
|
|
||||||
|
export async function uploadFile(fn: string, key?: string) {
|
||||||
|
const { url, backupUrl, formData, maxSize } = await post('/upload', {
|
||||||
|
ext: path.extname(fn),
|
||||||
|
});
|
||||||
|
let realUrl = url;
|
||||||
|
if (backupUrl) {
|
||||||
|
if (global.USE_ACC_OSS) {
|
||||||
|
realUrl = backupUrl;
|
||||||
|
} else {
|
||||||
|
const pingResult = await tcpPing({
|
||||||
|
address: url.replace('https://', ''),
|
||||||
|
attempts: 4,
|
||||||
|
timeout: 1000,
|
||||||
|
});
|
||||||
|
// console.log({pingResult});
|
||||||
|
if (isNaN(pingResult.avg) || pingResult.avg > 150) {
|
||||||
|
realUrl = backupUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// console.log({realUrl});
|
||||||
|
}
|
||||||
|
|
||||||
|
const fileSize = fs.statSync(fn).size;
|
||||||
|
if (maxSize && fileSize > filesizeParser(maxSize)) {
|
||||||
|
throw new Error(
|
||||||
|
`此文件大小 ${(fileSize / 1048576).toFixed(
|
||||||
|
1,
|
||||||
|
)}m , 超出当前额度 ${maxSize} 。您可以考虑升级付费业务以提升此额度。详情请访问: ${pricingPageUrl}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const bar = new ProgressBar(' 上传中 [:bar] :percent :etas', {
|
||||||
|
complete: '=',
|
||||||
|
incomplete: ' ',
|
||||||
|
total: fileSize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const form = new FormData();
|
||||||
|
|
||||||
|
Object.entries(formData).forEach(([k, v]) => {
|
||||||
|
form.append(k, v);
|
||||||
|
});
|
||||||
|
const fileStream = fs.createReadStream(fn);
|
||||||
|
fileStream.on('data', function (data) {
|
||||||
|
bar.tick(data.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (key) {
|
||||||
|
form.append('key', key);
|
||||||
|
}
|
||||||
|
form.append('file', fileStream);
|
||||||
|
|
||||||
|
const res = await fetch(realUrl, {
|
||||||
|
method: 'POST',
|
||||||
|
body: form,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.status > 299) {
|
||||||
|
throw new Error(`${res.status}: ${res.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// const body = await response.json();
|
||||||
|
return { hash: key || formData.key };
|
||||||
|
}
|
105
src/app.js
105
src/app.js
@@ -1,15 +1,8 @@
|
|||||||
/**
|
import { question } from './utils';
|
||||||
* Created by tdzl2003 on 2/13/16.
|
import fs from 'fs';
|
||||||
*/
|
import Table from 'tty-table';
|
||||||
|
|
||||||
import {question} from './utils';
|
import { post, get, doDelete } from './api';
|
||||||
import * as fs from 'fs-extra';
|
|
||||||
|
|
||||||
const {
|
|
||||||
post,
|
|
||||||
get,
|
|
||||||
doDelete,
|
|
||||||
} = require('./api');
|
|
||||||
|
|
||||||
const validPlatforms = {
|
const validPlatforms = {
|
||||||
ios: 1,
|
ios: 1,
|
||||||
@@ -18,34 +11,48 @@ const validPlatforms = {
|
|||||||
|
|
||||||
export function checkPlatform(platform) {
|
export function checkPlatform(platform) {
|
||||||
if (!validPlatforms[platform]) {
|
if (!validPlatforms[platform]) {
|
||||||
throw new Error(`Invalid platform '${platform}'`);
|
throw new Error(`无法识别的平台 '${platform}'`);
|
||||||
}
|
}
|
||||||
return platform
|
return platform;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getSelectedApp(platform) {
|
export function getSelectedApp(platform) {
|
||||||
checkPlatform(platform);
|
checkPlatform(platform);
|
||||||
|
|
||||||
if (!fs.existsSync('update.json')){
|
if (!fs.existsSync('update.json')) {
|
||||||
throw new Error(`App not selected. run 'pushy selectApp --platform ${platform}' first!`);
|
throw new Error(
|
||||||
|
`App not selected. run 'pushy selectApp --platform ${platform}' first!`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
|
const updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
|
||||||
if (!updateInfo[platform]) {
|
if (!updateInfo[platform]) {
|
||||||
throw new Error(`App not selected. run 'pushy selectApp --platform ${platform}' first!`);
|
throw new Error(
|
||||||
|
`App not selected. run 'pushy selectApp --platform ${platform}' first!`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return updateInfo[platform];
|
return updateInfo[platform];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function listApp(platform){
|
export async function listApp(platform) {
|
||||||
const {data} = await get('/app/list');
|
const { data } = await get('/app/list');
|
||||||
const list = platform?data.filter(v=>v.platform===platform):data;
|
const list = platform ? data.filter((v) => v.platform === platform) : data;
|
||||||
|
|
||||||
|
const header = [
|
||||||
|
{ value: '应用 id' },
|
||||||
|
{ value: '应用名称' },
|
||||||
|
{ value: '平台' },
|
||||||
|
];
|
||||||
|
const rows = [];
|
||||||
for (const app of list) {
|
for (const app of list) {
|
||||||
console.log(`${app.id}) ${app.name}(${app.platform})`);
|
rows.push([app.id, app.name, app.platform]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(Table(header, rows).render());
|
||||||
|
|
||||||
if (platform) {
|
if (platform) {
|
||||||
console.log(`\nTotal ${list.length} ${platform} apps`);
|
console.log(`\共 ${list.length} ${platform} 个应用`);
|
||||||
} else {
|
} else {
|
||||||
console.log(`\nTotal ${list.length} apps`);
|
console.log(`\共 ${list.length} 个应用`);
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
@@ -54,8 +61,8 @@ export async function chooseApp(platform) {
|
|||||||
const list = await listApp(platform);
|
const list = await listApp(platform);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const id = await question('Enter appId:');
|
const id = await question('输入应用 id:');
|
||||||
const app = list.find(v=>v.id === (id|0));
|
const app = list.find((v) => v.id === (id | 0));
|
||||||
if (app) {
|
if (app) {
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
@@ -63,48 +70,58 @@ export async function chooseApp(platform) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const commands = {
|
export const commands = {
|
||||||
createApp: async function ({options}) {
|
createApp: async function ({ options }) {
|
||||||
const name = options.name || await question('App Name:');
|
const name = options.name || (await question('应用名称:'));
|
||||||
const {downloadUrl} = options;
|
const { downloadUrl } = options;
|
||||||
const platform = checkPlatform(options.platform || await question('Platform(ios/android):'));
|
const platform = checkPlatform(
|
||||||
const {id} = await post('/app/create', {name, platform});
|
options.platform || (await question('平台(ios/android):')),
|
||||||
console.log(`Created app ${id}`);
|
);
|
||||||
|
const { id } = await post('/app/create', { name, platform });
|
||||||
|
console.log(`已成功创建应用(id: ${id})`);
|
||||||
await this.selectApp({
|
await this.selectApp({
|
||||||
args: [id],
|
args: [id],
|
||||||
options: {platform, downloadUrl},
|
options: { platform, downloadUrl },
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
deleteApp: async function ({args, options}) {
|
deleteApp: async function ({ args, options }) {
|
||||||
const {platform} = options;
|
const { platform } = options;
|
||||||
const id = args[0] || chooseApp(platform);
|
const id = args[0] || chooseApp(platform);
|
||||||
if (!id) {
|
if (!id) {
|
||||||
console.log('Canceled');
|
console.log('已取消');
|
||||||
}
|
}
|
||||||
await doDelete(`/app/${id}`);
|
await doDelete(`/app/${id}`);
|
||||||
console.log('Ok.');
|
console.log('操作成功');
|
||||||
},
|
},
|
||||||
apps: async function ({options}){
|
apps: async function ({ options }) {
|
||||||
const {platform} = options;
|
const { platform } = options;
|
||||||
listApp(platform);
|
listApp(platform);
|
||||||
},
|
},
|
||||||
selectApp: async function({args, options}) {
|
selectApp: async function ({ args, options }) {
|
||||||
const platform = checkPlatform(options.platform || await question('Platform(ios/android):'));
|
const platform = checkPlatform(
|
||||||
const id = args[0] || (await chooseApp(platform)).id;
|
options.platform || (await question('平台(ios/android):')),
|
||||||
|
);
|
||||||
|
const id = args[0] ? parseInt(args[0]) : (await chooseApp(platform)).id;
|
||||||
|
|
||||||
let updateInfo = {};
|
let updateInfo = {};
|
||||||
if (fs.existsSync('update.json')) {
|
if (fs.existsSync('update.json')) {
|
||||||
try {
|
try {
|
||||||
updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
|
updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to parse file `update.json`. Try to remove it manually.');
|
console.error(
|
||||||
|
'Failed to parse file `update.json`. Try to remove it manually.',
|
||||||
|
);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const {appKey} = await get(`/app/${id}`);
|
const { appKey } = await get(`/app/${id}`);
|
||||||
updateInfo[platform] = {
|
updateInfo[platform] = {
|
||||||
appId: id,
|
appId: id,
|
||||||
appKey,
|
appKey,
|
||||||
};
|
};
|
||||||
fs.writeFileSync('update.json', JSON.stringify(updateInfo, null, 4), 'utf8');
|
fs.writeFileSync(
|
||||||
|
'update.json',
|
||||||
|
JSON.stringify(updateInfo, null, 4),
|
||||||
|
'utf8',
|
||||||
|
);
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
|
430
src/bundle.js
430
src/bundle.js
@@ -1,40 +1,23 @@
|
|||||||
/**
|
import path from 'node:path';
|
||||||
* Created by tdzl2003 on 2/22/16.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const path = require('path');
|
|
||||||
import { getRNVersion, translateOptions } from './utils';
|
import { getRNVersion, translateOptions } from './utils';
|
||||||
import * as fs from 'fs-extra';
|
import * as fs from 'fs-extra';
|
||||||
import { ZipFile } from 'yazl';
|
import { ZipFile } from 'yazl';
|
||||||
import { open as openZipFile } from 'yauzl';
|
import { open as openZipFile } from 'yauzl';
|
||||||
import { question } from './utils';
|
import { question, printVersionCommand } from './utils';
|
||||||
import { checkPlatform } from './app';
|
import { checkPlatform } from './app';
|
||||||
const { spawn, spawnSync, execSync } = require('child_process');
|
import { spawn, spawnSync } from 'node:child_process';
|
||||||
const g2js = require('gradle-to-js/lib/parser');
|
const g2js = require('gradle-to-js/lib/parser');
|
||||||
const os = require('os');
|
import os from 'os';
|
||||||
|
const properties = require('properties');
|
||||||
|
|
||||||
var diff;
|
let bsdiff, hdiff, diff;
|
||||||
try {
|
try {
|
||||||
var bsdiff = require('node-bsdiff');
|
bsdiff = require('node-bsdiff').diff;
|
||||||
diff = typeof bsdiff != 'function' ? bsdiff.diff : bsdiff;
|
} catch (e) {}
|
||||||
} catch (e) {
|
|
||||||
diff = function() {
|
|
||||||
console.warn(
|
|
||||||
'This function needs "node-bsdiff". Please run "npm i node-bsdiff" from your project directory first!',
|
|
||||||
);
|
|
||||||
throw new Error('This function needs module "node-bsdiff". Please install it first.');
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function exec(command) {
|
try {
|
||||||
const commandResult = spawnSync(command, {
|
hdiff = require('node-hdiffpatch').diff;
|
||||||
shell: true,
|
} catch (e) {}
|
||||||
stdio: 'inherit',
|
|
||||||
});
|
|
||||||
if (commandResult.error) {
|
|
||||||
throw commandResult.error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function runReactNativeBundleCommand(
|
async function runReactNativeBundleCommand(
|
||||||
bundleName,
|
bundleName,
|
||||||
@@ -45,19 +28,61 @@ async function runReactNativeBundleCommand(
|
|||||||
sourcemapOutput,
|
sourcemapOutput,
|
||||||
config,
|
config,
|
||||||
) {
|
) {
|
||||||
|
let gradleConfig = {};
|
||||||
|
if (platform === 'android') {
|
||||||
|
gradleConfig = await checkGradleConfig();
|
||||||
|
if (gradleConfig.crunchPngs !== false) {
|
||||||
|
console.warn(
|
||||||
|
'android 的 crunchPngs 选项似乎尚未禁用(如已禁用则请忽略此提示),这可能导致热更包体积异常增大,具体请参考 https://pushy.reactnative.cn/docs/getting-started.html#%E7%A6%81%E7%94%A8-android-%E7%9A%84-crunch-%E4%BC%98%E5%8C%96 \n',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let reactNativeBundleArgs = [];
|
let reactNativeBundleArgs = [];
|
||||||
|
|
||||||
let envArgs = process.env.PUSHY_ENV_ARGS;
|
let envArgs = process.env.PUSHY_ENV_ARGS;
|
||||||
|
|
||||||
if (envArgs) {
|
if (envArgs) {
|
||||||
Array.prototype.push.apply(reactNativeBundleArgs, envArgs.trim().split(/\s+/));
|
Array.prototype.push.apply(
|
||||||
|
reactNativeBundleArgs,
|
||||||
|
envArgs.trim().split(/\s+/),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.emptyDirSync(outputFolder);
|
fs.emptyDirSync(outputFolder);
|
||||||
|
|
||||||
|
let cliPath;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// rn >= 0.75
|
||||||
|
cliPath = require.resolve('@react-native-community/cli/build/bin.js', {
|
||||||
|
paths: [process.cwd()],
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
// rn < 0.75
|
||||||
|
cliPath = require.resolve('react-native/local-cli/cli.js', {
|
||||||
|
paths: [process.cwd()],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let usingExpo = false;
|
||||||
|
try {
|
||||||
|
require.resolve('expo-router', {
|
||||||
|
paths: [process.cwd()],
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`expo-router detected, will use @expo/cli to bundle.\n`);
|
||||||
|
// if using expo-router, use expo-cli
|
||||||
|
cliPath = require.resolve('@expo/cli', {
|
||||||
|
paths: [process.cwd()],
|
||||||
|
});
|
||||||
|
usingExpo = true;
|
||||||
|
} catch (e) {}
|
||||||
|
const bundleCommand = usingExpo ? 'export:embed' : 'bundle';
|
||||||
|
|
||||||
Array.prototype.push.apply(reactNativeBundleArgs, [
|
Array.prototype.push.apply(reactNativeBundleArgs, [
|
||||||
path.join("node_modules", "react-native", "local-cli", "cli.js"),
|
cliPath,
|
||||||
"bundle",
|
bundleCommand,
|
||||||
'--assets-dest',
|
'--assets-dest',
|
||||||
outputFolder,
|
outputFolder,
|
||||||
'--bundle-output',
|
'--bundle-output',
|
||||||
@@ -80,23 +105,60 @@ async function runReactNativeBundleCommand(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const reactNativeBundleProcess = spawn('node', reactNativeBundleArgs);
|
const reactNativeBundleProcess = spawn('node', reactNativeBundleArgs);
|
||||||
console.log(`Running bundle command: node ${reactNativeBundleArgs.join(' ')}`);
|
console.log(
|
||||||
|
`Running bundle command: node ${reactNativeBundleArgs.join(' ')}`,
|
||||||
|
);
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
reactNativeBundleProcess.stdout.on('data', data => {
|
reactNativeBundleProcess.stdout.on('data', (data) => {
|
||||||
console.log(data.toString().trim());
|
console.log(data.toString().trim());
|
||||||
});
|
});
|
||||||
|
|
||||||
reactNativeBundleProcess.stderr.on('data', data => {
|
reactNativeBundleProcess.stderr.on('data', (data) => {
|
||||||
console.error(data.toString().trim());
|
console.error(data.toString().trim());
|
||||||
});
|
});
|
||||||
|
|
||||||
reactNativeBundleProcess.on('close', async exitCode => {
|
reactNativeBundleProcess.on('close', async (exitCode) => {
|
||||||
if (exitCode) {
|
if (exitCode) {
|
||||||
reject(new Error(`"react-native bundle" command exited with code ${exitCode}.`));
|
reject(
|
||||||
|
new Error(
|
||||||
|
`"react-native bundle" command exited with code ${exitCode}.`,
|
||||||
|
),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
|
let hermesEnabled = false;
|
||||||
|
|
||||||
if (platform === 'android') {
|
if (platform === 'android') {
|
||||||
await compileHermesByteCode(bundleName, outputFolder);
|
const gradlePropeties = await new Promise((resolve) => {
|
||||||
|
properties.parse(
|
||||||
|
'./android/gradle.properties',
|
||||||
|
{ path: true },
|
||||||
|
function (error, props) {
|
||||||
|
if (error) {
|
||||||
|
console.error(error);
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(props);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
hermesEnabled = gradlePropeties.hermesEnabled;
|
||||||
|
|
||||||
|
if (typeof hermesEnabled !== 'boolean')
|
||||||
|
hermesEnabled = gradleConfig.enableHermes;
|
||||||
|
} else if (
|
||||||
|
platform === 'ios' &&
|
||||||
|
fs.existsSync('ios/Pods/hermes-engine')
|
||||||
|
) {
|
||||||
|
hermesEnabled = true;
|
||||||
|
}
|
||||||
|
if (hermesEnabled) {
|
||||||
|
await compileHermesByteCode(
|
||||||
|
bundleName,
|
||||||
|
outputFolder,
|
||||||
|
sourcemapOutput,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
resolve(null);
|
resolve(null);
|
||||||
}
|
}
|
||||||
@@ -110,35 +172,75 @@ function getHermesOSBin() {
|
|||||||
if (os.platform() === 'linux') return 'linux64-bin';
|
if (os.platform() === 'linux') return 'linux64-bin';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function compileHermesByteCode(bundleName, outputFolder) {
|
async function checkGradleConfig() {
|
||||||
let enableHermes = false;
|
let enableHermes = false;
|
||||||
|
let crunchPngs;
|
||||||
try {
|
try {
|
||||||
const gradleConfig = await g2js.parseFile('android/app/build.gradle');
|
const gradleConfig = await g2js.parseFile('android/app/build.gradle');
|
||||||
|
crunchPngs = gradleConfig.android.buildTypes.release.crunchPngs;
|
||||||
const projectConfig = gradleConfig['project.ext.react'];
|
const projectConfig = gradleConfig['project.ext.react'];
|
||||||
for (const packagerConfig of projectConfig) {
|
if (projectConfig) {
|
||||||
if (packagerConfig.includes('enableHermes') && packagerConfig.includes('true')) {
|
for (const packagerConfig of projectConfig) {
|
||||||
enableHermes = true;
|
if (
|
||||||
break;
|
packagerConfig.includes('enableHermes') &&
|
||||||
|
packagerConfig.includes('true')
|
||||||
|
) {
|
||||||
|
enableHermes = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
if (enableHermes) {
|
return {
|
||||||
console.log(`Hermes enabled, now compiling to hermes bytecode:\n`);
|
enableHermes,
|
||||||
const hermesPath = fs.existsSync('node_modules/hermes-engine')
|
crunchPngs,
|
||||||
? 'node_modules/hermes-engine'
|
};
|
||||||
: 'node_modules/hermesvm';
|
}
|
||||||
execSync(
|
|
||||||
`${hermesPath}/${getHermesOSBin()}/hermes -emit-binary -out ${outputFolder}/${bundleName} ${outputFolder}/${bundleName} -O`,
|
async function compileHermesByteCode(
|
||||||
{ stdio: 'ignore' },
|
bundleName,
|
||||||
);
|
outputFolder,
|
||||||
|
sourcemapOutput,
|
||||||
|
) {
|
||||||
|
console.log(`Hermes enabled, now compiling to hermes bytecode:\n`);
|
||||||
|
// >= rn 0.69
|
||||||
|
const rnDir = path.dirname(
|
||||||
|
require.resolve('react-native', {
|
||||||
|
paths: [process.cwd()],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
let hermesPath = path.join(rnDir, `/sdks/hermesc/${getHermesOSBin()}`);
|
||||||
|
|
||||||
|
// < rn 0.69
|
||||||
|
if (!fs.existsSync(hermesPath)) {
|
||||||
|
hermesPath = `node_modules/hermes-engine/${getHermesOSBin()}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const hermesCommand = `${hermesPath}/hermesc`;
|
||||||
|
|
||||||
|
const args = [
|
||||||
|
'-emit-binary',
|
||||||
|
'-out',
|
||||||
|
path.join(outputFolder, bundleName),
|
||||||
|
path.join(outputFolder, bundleName),
|
||||||
|
'-O',
|
||||||
|
];
|
||||||
|
if (sourcemapOutput) {
|
||||||
|
args.push('-output-source-map');
|
||||||
|
}
|
||||||
|
console.log(
|
||||||
|
'Running hermesc: ' + hermesCommand + ' ' + args.join(' ') + '\n',
|
||||||
|
);
|
||||||
|
spawnSync(hermesCommand, args, {
|
||||||
|
stdio: 'ignore',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function pack(dir, output) {
|
async function pack(dir, output) {
|
||||||
console.log('Packing');
|
console.log('Packing');
|
||||||
fs.ensureDirSync(path.dirname(output));
|
fs.ensureDirSync(path.dirname(output));
|
||||||
await new Promise((resolve, reject) => {
|
await new Promise((resolve, reject) => {
|
||||||
var zipfile = new ZipFile();
|
const zipfile = new ZipFile();
|
||||||
|
|
||||||
function addDirectory(root, rel) {
|
function addDirectory(root, rel) {
|
||||||
if (rel) {
|
if (rel) {
|
||||||
@@ -146,7 +248,7 @@ async function pack(dir, output) {
|
|||||||
}
|
}
|
||||||
const childs = fs.readdirSync(root);
|
const childs = fs.readdirSync(root);
|
||||||
for (const name of childs) {
|
for (const name of childs) {
|
||||||
if (name === '.' || name === '..') {
|
if (name === '.' || name === '..' || name === 'index.bundlejs.map') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const fullPath = path.join(root, name);
|
const fullPath = path.join(root, name);
|
||||||
@@ -163,13 +265,15 @@ async function pack(dir, output) {
|
|||||||
|
|
||||||
addDirectory(dir, '');
|
addDirectory(dir, '');
|
||||||
|
|
||||||
zipfile.outputStream.on('error', err => reject(err));
|
zipfile.outputStream.on('error', (err) => reject(err));
|
||||||
zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', function() {
|
zipfile.outputStream
|
||||||
resolve();
|
.pipe(fs.createWriteStream(output))
|
||||||
});
|
.on('close', function () {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
zipfile.end();
|
zipfile.end();
|
||||||
});
|
});
|
||||||
console.log('Bundled saved to: ' + output);
|
console.log('ppk热更包已生成并保存到: ' + output);
|
||||||
}
|
}
|
||||||
|
|
||||||
function readEntire(entry, zipFile) {
|
function readEntire(entry, zipFile) {
|
||||||
@@ -213,26 +317,30 @@ async function diffFromPPK(origin, next, output) {
|
|||||||
|
|
||||||
if (entry.fileName === 'index.bundlejs') {
|
if (entry.fileName === 'index.bundlejs') {
|
||||||
// This is source.
|
// This is source.
|
||||||
return readEntire(entry, zipFile).then(v => (originSource = v));
|
return readEntire(entry, zipFile).then((v) => (originSource = v));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!originSource) {
|
if (!originSource) {
|
||||||
throw new Error(`Bundle file not found! Please use default bundle file name and path.`);
|
throw new Error(
|
||||||
|
`Bundle file not found! Please use default bundle file name and path.`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const copies = {};
|
const copies = {};
|
||||||
|
|
||||||
var zipfile = new ZipFile();
|
const zipfile = new ZipFile();
|
||||||
|
|
||||||
const writePromise = new Promise((resolve, reject) => {
|
const writePromise = new Promise((resolve, reject) => {
|
||||||
zipfile.outputStream.on('error', err => {
|
zipfile.outputStream.on('error', (err) => {
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', function() {
|
zipfile.outputStream
|
||||||
resolve();
|
.pipe(fs.createWriteStream(output))
|
||||||
});
|
.on('close', function () {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const addedEntry = {};
|
const addedEntry = {};
|
||||||
@@ -261,9 +369,12 @@ async function diffFromPPK(origin, next, output) {
|
|||||||
}
|
}
|
||||||
} else if (entry.fileName === 'index.bundlejs') {
|
} else if (entry.fileName === 'index.bundlejs') {
|
||||||
//console.log('Found bundle');
|
//console.log('Found bundle');
|
||||||
return readEntire(entry, nextZipfile).then(newSource => {
|
return readEntire(entry, nextZipfile).then((newSource) => {
|
||||||
//console.log('Begin diff');
|
//console.log('Begin diff');
|
||||||
zipfile.addBuffer(diff(originSource, newSource), 'index.bundlejs.patch');
|
zipfile.addBuffer(
|
||||||
|
diff(originSource, newSource),
|
||||||
|
'index.bundlejs.patch',
|
||||||
|
);
|
||||||
//console.log('End diff');
|
//console.log('End diff');
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -288,7 +399,7 @@ async function diffFromPPK(origin, next, output) {
|
|||||||
addEntry(basename(entry.fileName));
|
addEntry(basename(entry.fileName));
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
nextZipfile.openReadStream(entry, function(err, readStream) {
|
nextZipfile.openReadStream(entry, function (err, readStream) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
@@ -304,7 +415,7 @@ async function diffFromPPK(origin, next, output) {
|
|||||||
|
|
||||||
const deletes = {};
|
const deletes = {};
|
||||||
|
|
||||||
for (var k in originEntries) {
|
for (let k in originEntries) {
|
||||||
if (!newEntries[k]) {
|
if (!newEntries[k]) {
|
||||||
console.log('Delete ' + k);
|
console.log('Delete ' + k);
|
||||||
deletes[k] = 1;
|
deletes[k] = 1;
|
||||||
@@ -312,12 +423,21 @@ async function diffFromPPK(origin, next, output) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//console.log({copies, deletes});
|
//console.log({copies, deletes});
|
||||||
zipfile.addBuffer(Buffer.from(JSON.stringify({ copies, deletes })), '__diff.json');
|
zipfile.addBuffer(
|
||||||
|
Buffer.from(JSON.stringify({ copies, deletes })),
|
||||||
|
'__diff.json',
|
||||||
|
);
|
||||||
zipfile.end();
|
zipfile.end();
|
||||||
await writePromise;
|
await writePromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function diffFromPackage(origin, next, output, originBundleName, transformPackagePath = v => v) {
|
async function diffFromPackage(
|
||||||
|
origin,
|
||||||
|
next,
|
||||||
|
output,
|
||||||
|
originBundleName,
|
||||||
|
transformPackagePath = (v) => v,
|
||||||
|
) {
|
||||||
fs.ensureDirSync(path.dirname(output));
|
fs.ensureDirSync(path.dirname(output));
|
||||||
|
|
||||||
const originEntries = {};
|
const originEntries = {};
|
||||||
@@ -339,26 +459,30 @@ async function diffFromPackage(origin, next, output, originBundleName, transform
|
|||||||
|
|
||||||
if (fn === originBundleName) {
|
if (fn === originBundleName) {
|
||||||
// This is source.
|
// This is source.
|
||||||
return readEntire(entry, zipFile).then(v => (originSource = v));
|
return readEntire(entry, zipFile).then((v) => (originSource = v));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!originSource) {
|
if (!originSource) {
|
||||||
throw new Error(`Bundle file not found! Please use default bundle file name and path.`);
|
throw new Error(
|
||||||
|
`Bundle file not found! Please use default bundle file name and path.`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const copies = {};
|
const copies = {};
|
||||||
|
|
||||||
var zipfile = new ZipFile();
|
const zipfile = new ZipFile();
|
||||||
|
|
||||||
const writePromise = new Promise((resolve, reject) => {
|
const writePromise = new Promise((resolve, reject) => {
|
||||||
zipfile.outputStream.on('error', err => {
|
zipfile.outputStream.on('error', (err) => {
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
zipfile.outputStream.pipe(fs.createWriteStream(output)).on('close', function() {
|
zipfile.outputStream
|
||||||
resolve();
|
.pipe(fs.createWriteStream(output))
|
||||||
});
|
.on('close', function () {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await enumZipEntries(next, (entry, nextZipfile) => {
|
await enumZipEntries(next, (entry, nextZipfile) => {
|
||||||
@@ -367,9 +491,12 @@ async function diffFromPackage(origin, next, output, originBundleName, transform
|
|||||||
zipfile.addEmptyDirectory(entry.fileName);
|
zipfile.addEmptyDirectory(entry.fileName);
|
||||||
} else if (entry.fileName === 'index.bundlejs') {
|
} else if (entry.fileName === 'index.bundlejs') {
|
||||||
//console.log('Found bundle');
|
//console.log('Found bundle');
|
||||||
return readEntire(entry, nextZipfile).then(newSource => {
|
return readEntire(entry, nextZipfile).then((newSource) => {
|
||||||
//console.log('Begin diff');
|
//console.log('Begin diff');
|
||||||
zipfile.addBuffer(diff(originSource, newSource), 'index.bundlejs.patch');
|
zipfile.addBuffer(
|
||||||
|
diff(originSource, newSource),
|
||||||
|
'index.bundlejs.patch',
|
||||||
|
);
|
||||||
//console.log('End diff');
|
//console.log('End diff');
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -385,7 +512,7 @@ async function diffFromPackage(origin, next, output, originBundleName, transform
|
|||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
nextZipfile.openReadStream(entry, function(err, readStream) {
|
nextZipfile.openReadStream(entry, function (err, readStream) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err);
|
return reject(err);
|
||||||
}
|
}
|
||||||
@@ -412,7 +539,7 @@ function enumZipEntries(zipFn, callback) {
|
|||||||
}
|
}
|
||||||
zipfile.on('end', resolve);
|
zipfile.on('end', resolve);
|
||||||
zipfile.on('error', reject);
|
zipfile.on('error', reject);
|
||||||
zipfile.on('entry', entry => {
|
zipfile.on('entry', (entry) => {
|
||||||
const result = callback(entry, zipfile);
|
const result = callback(entry, zipfile);
|
||||||
if (result && typeof result.then === 'function') {
|
if (result && typeof result.then === 'function') {
|
||||||
result.then(() => zipfile.readEntry());
|
result.then(() => zipfile.readEntry());
|
||||||
@@ -425,16 +552,55 @@ function enumZipEntries(zipFn, callback) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function diffArgsCheck(args, options, diffFn) {
|
||||||
|
const [origin, next] = args;
|
||||||
|
|
||||||
|
if (!origin || !next) {
|
||||||
|
console.error(`Usage: pushy ${diffFn} <origin> <next>`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (diffFn.startsWith('hdiff')) {
|
||||||
|
if (!hdiff) {
|
||||||
|
console.error(
|
||||||
|
`This function needs "node-hdiffpatch".
|
||||||
|
Please run "npm i node-hdiffpatch" to install`,
|
||||||
|
);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
diff = hdiff;
|
||||||
|
} else {
|
||||||
|
if (!bsdiff) {
|
||||||
|
console.error(
|
||||||
|
`This function needs "node-bsdiff".
|
||||||
|
Please run "npm i node-bsdiff" to install`,
|
||||||
|
);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
diff = bsdiff;
|
||||||
|
}
|
||||||
|
const { output } = options;
|
||||||
|
|
||||||
|
return {
|
||||||
|
origin,
|
||||||
|
next,
|
||||||
|
realOutput: output.replace(/\$\{time\}/g, '' + Date.now()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export const commands = {
|
export const commands = {
|
||||||
bundle: async function({ options }) {
|
bundle: async function ({ options }) {
|
||||||
const platform = checkPlatform(options.platform || (await question('Platform(ios/android):')));
|
const platform = checkPlatform(
|
||||||
|
options.platform || (await question('平台(ios/android):')),
|
||||||
|
);
|
||||||
|
|
||||||
let { bundleName, entryFile, intermediaDir, output, dev, verbose } = translateOptions({
|
let { bundleName, entryFile, intermediaDir, output, dev, sourcemap } =
|
||||||
...options,
|
translateOptions({
|
||||||
platform,
|
...options,
|
||||||
});
|
platform,
|
||||||
|
});
|
||||||
|
|
||||||
// const sourcemapOutput = path.join(intermediaDir, bundleName + ".map");
|
const sourcemapOutput = path.join(intermediaDir, bundleName + '.map');
|
||||||
|
|
||||||
const realOutput = output.replace(/\$\{time\}/g, '' + Date.now());
|
const realOutput = output.replace(/\$\{time\}/g, '' + Date.now());
|
||||||
|
|
||||||
@@ -444,13 +610,20 @@ export const commands = {
|
|||||||
|
|
||||||
const { version, major, minor } = getRNVersion();
|
const { version, major, minor } = getRNVersion();
|
||||||
|
|
||||||
console.log('Bundling with React Native version: ', version);
|
console.log('Bundling with react-native: ', version);
|
||||||
|
|
||||||
await runReactNativeBundleCommand(bundleName, dev, entryFile, intermediaDir, platform);
|
await runReactNativeBundleCommand(
|
||||||
|
bundleName,
|
||||||
|
dev,
|
||||||
|
entryFile,
|
||||||
|
intermediaDir,
|
||||||
|
platform,
|
||||||
|
sourcemap ? sourcemapOutput : '',
|
||||||
|
);
|
||||||
|
|
||||||
await pack(path.resolve(intermediaDir), realOutput);
|
await pack(path.resolve(intermediaDir), realOutput);
|
||||||
|
|
||||||
const v = await question('Would you like to publish it?(Y/N)');
|
const v = await question('是否现在上传此热更包?(Y/N)');
|
||||||
if (v.toLowerCase() === 'y') {
|
if (v.toLowerCase() === 'y') {
|
||||||
await this.publish({
|
await this.publish({
|
||||||
args: [realOutput],
|
args: [realOutput],
|
||||||
@@ -462,47 +635,74 @@ export const commands = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async diff({ args, options }) {
|
async diff({ args, options }) {
|
||||||
const [origin, next] = args;
|
const { origin, next, realOutput } = diffArgsCheck(args, options, 'diff');
|
||||||
const { output } = options;
|
|
||||||
|
|
||||||
const realOutput = output.replace(/\$\{time\}/g, '' + Date.now());
|
await diffFromPPK(origin, next, realOutput, 'index.bundlejs');
|
||||||
|
console.log(`${realOutput} generated.`);
|
||||||
|
},
|
||||||
|
|
||||||
if (!origin || !next) {
|
async hdiff({ args, options }) {
|
||||||
console.error('pushy diff <origin> <next>');
|
const { origin, next, realOutput } = diffArgsCheck(args, options, 'hdiff');
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
await diffFromPPK(origin, next, realOutput, 'index.bundlejs');
|
await diffFromPPK(origin, next, realOutput, 'index.bundlejs');
|
||||||
console.log(`${realOutput} generated.`);
|
console.log(`${realOutput} generated.`);
|
||||||
},
|
},
|
||||||
|
|
||||||
async diffFromApk({ args, options }) {
|
async diffFromApk({ args, options }) {
|
||||||
const [origin, next] = args;
|
const { origin, next, realOutput } = diffArgsCheck(
|
||||||
const { output } = options;
|
args,
|
||||||
|
options,
|
||||||
|
'diffFromApk',
|
||||||
|
);
|
||||||
|
|
||||||
const realOutput = output.replace(/\$\{time\}/g, '' + Date.now());
|
await diffFromPackage(
|
||||||
|
origin,
|
||||||
|
next,
|
||||||
|
realOutput,
|
||||||
|
'assets/index.android.bundle',
|
||||||
|
);
|
||||||
|
console.log(`${realOutput} generated.`);
|
||||||
|
},
|
||||||
|
|
||||||
if (!origin || !next) {
|
async hdiffFromApk({ args, options }) {
|
||||||
console.error('pushy diffFromApk <origin> <next>');
|
const { origin, next, realOutput } = diffArgsCheck(
|
||||||
process.exit(1);
|
args,
|
||||||
}
|
options,
|
||||||
|
'hdiffFromApk',
|
||||||
|
);
|
||||||
|
|
||||||
await diffFromPackage(origin, next, realOutput, 'assets/index.android.bundle');
|
await diffFromPackage(
|
||||||
|
origin,
|
||||||
|
next,
|
||||||
|
realOutput,
|
||||||
|
'assets/index.android.bundle',
|
||||||
|
);
|
||||||
console.log(`${realOutput} generated.`);
|
console.log(`${realOutput} generated.`);
|
||||||
},
|
},
|
||||||
|
|
||||||
async diffFromIpa({ args, options }) {
|
async diffFromIpa({ args, options }) {
|
||||||
const [origin, next] = args;
|
const { origin, next, realOutput } = diffArgsCheck(
|
||||||
const { output } = options;
|
args,
|
||||||
|
options,
|
||||||
|
'diffFromIpa',
|
||||||
|
);
|
||||||
|
|
||||||
const realOutput = output.replace(/\$\{time\}/g, '' + Date.now());
|
await diffFromPackage(origin, next, realOutput, 'main.jsbundle', (v) => {
|
||||||
|
const m = /^Payload\/[^/]+\/(.+)$/.exec(v);
|
||||||
|
return m && m[1];
|
||||||
|
});
|
||||||
|
|
||||||
if (!origin || !next) {
|
console.log(`${realOutput} generated.`);
|
||||||
console.error('pushy diffFromIpa <origin> <next>');
|
},
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
await diffFromPackage(origin, next, realOutput, 'main.jsbundle', v => {
|
async hdiffFromIpa({ args, options }) {
|
||||||
|
const { origin, next, realOutput } = diffArgsCheck(
|
||||||
|
args,
|
||||||
|
options,
|
||||||
|
'hdiffFromIpa',
|
||||||
|
);
|
||||||
|
|
||||||
|
await diffFromPackage(origin, next, realOutput, 'main.jsbundle', (v) => {
|
||||||
const m = /^Payload\/[^/]+\/(.+)$/.exec(v);
|
const m = /^Payload\/[^/]+\/(.+)$/.exec(v);
|
||||||
return m && m[1];
|
return m && m[1];
|
||||||
});
|
});
|
||||||
|
67
src/index.js
67
src/index.js
@@ -1,67 +0,0 @@
|
|||||||
#!/usr/bin/env node
|
|
||||||
/**
|
|
||||||
* Created by tdzl2003 on 2/13/16.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const {loadSession} = require('./api');
|
|
||||||
const updateNotifier = require('update-notifier');
|
|
||||||
const pkg = require('../package.json');
|
|
||||||
|
|
||||||
updateNotifier({pkg}).notify();
|
|
||||||
|
|
||||||
function printUsage({args}) {
|
|
||||||
// const commandName = args[0];
|
|
||||||
// TODO: print usage of commandName, or print global usage.
|
|
||||||
|
|
||||||
console.log('Usage is under development now.')
|
|
||||||
console.log('Visit `https://github.com/reactnativecn/react-native-pushy` for early document.');
|
|
||||||
process.exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function printVersionCommand() {
|
|
||||||
if (process.argv.indexOf('-v') >= 0 || process.argv[2] === 'version') {
|
|
||||||
console.log('react-native-update-cli: ' + pkg.version);
|
|
||||||
try {
|
|
||||||
const PACKAGE_JSON_PATH = path.resolve(
|
|
||||||
process.cwd(),
|
|
||||||
'node_modules',
|
|
||||||
'react-native-update',
|
|
||||||
'package.json'
|
|
||||||
);
|
|
||||||
console.log('react-native-update: ' + require(PACKAGE_JSON_PATH).version);
|
|
||||||
} catch (e) {
|
|
||||||
console.log('react-native-update: n/a - not inside a React Native project directory')
|
|
||||||
}
|
|
||||||
process.exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const commands = {
|
|
||||||
...require('./user').commands,
|
|
||||||
...require('./bundle').commands,
|
|
||||||
...require('./app').commands,
|
|
||||||
...require('./package').commands,
|
|
||||||
...require('./versions').commands,
|
|
||||||
help: printUsage,
|
|
||||||
};
|
|
||||||
|
|
||||||
function run() {
|
|
||||||
printVersionCommand();
|
|
||||||
|
|
||||||
const argv = require('cli-arguments').parse(require('../cli.json'));
|
|
||||||
global.NO_INTERACTIVE = argv.options['no-interactive'];
|
|
||||||
|
|
||||||
loadSession()
|
|
||||||
.then(()=>commands[argv.command](argv))
|
|
||||||
.catch(err=>{
|
|
||||||
if (err.status === 401) {
|
|
||||||
console.log('Not loggined.\nRun `pushy login` at your project directory to login.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
console.error(err.stack);
|
|
||||||
process.exit(-1);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
run();
|
|
52
src/index.ts
Normal file
52
src/index.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
import { loadSession } from './api';
|
||||||
|
import updateNotifier from 'update-notifier';
|
||||||
|
import { printVersionCommand } from './utils/index.js';
|
||||||
|
import pkg from '../package.json';
|
||||||
|
|
||||||
|
updateNotifier({ pkg }).notify({ isGlobal: true });
|
||||||
|
|
||||||
|
function printUsage({ args }) {
|
||||||
|
// const commandName = args[0];
|
||||||
|
// TODO: print usage of commandName, or print global usage.
|
||||||
|
|
||||||
|
console.log('Usage is under development now.');
|
||||||
|
console.log(
|
||||||
|
'Visit `https://github.com/reactnativecn/react-native-pushy` for early document.',
|
||||||
|
);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const commands = {
|
||||||
|
...require('./user').commands,
|
||||||
|
...require('./bundle').commands,
|
||||||
|
...require('./app').commands,
|
||||||
|
...require('./package').commands,
|
||||||
|
...require('./versions').commands,
|
||||||
|
help: printUsage,
|
||||||
|
};
|
||||||
|
|
||||||
|
function run() {
|
||||||
|
printVersionCommand();
|
||||||
|
if (process.argv.indexOf('-v') >= 0 || process.argv[2] === 'version') {
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
const argv = require('cli-arguments').parse(require('../cli.json'));
|
||||||
|
global.NO_INTERACTIVE = argv.options['no-interactive'];
|
||||||
|
global.USE_ACC_OSS = argv.options['acc'];
|
||||||
|
|
||||||
|
loadSession()
|
||||||
|
.then(() => commands[argv.command](argv))
|
||||||
|
.catch((err) => {
|
||||||
|
if (err.status === 401) {
|
||||||
|
console.log('尚未登录。\n请在项目目录中运行`pushy login`命令来登录');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.error(err.stack);
|
||||||
|
process.exit(-1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
run();
|
109
src/package.js
109
src/package.js
@@ -1,34 +1,37 @@
|
|||||||
/**
|
import { get, post, uploadFile } from './api';
|
||||||
* Created by tdzl2003 on 4/2/16.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const { get, post, uploadFile } = require('./api');
|
|
||||||
import { question, saveToLocal } from './utils';
|
import { question, saveToLocal } from './utils';
|
||||||
|
|
||||||
import { checkPlatform, getSelectedApp } from './app';
|
import { checkPlatform, getSelectedApp } from './app';
|
||||||
|
|
||||||
import { getApkInfo, getIpaInfo } from './utils';
|
import { getApkInfo, getIpaInfo } from './utils';
|
||||||
const Table = require('tty-table');
|
import Table from 'tty-table';
|
||||||
|
|
||||||
export async function listPackage(appId) {
|
export async function listPackage(appId) {
|
||||||
const { data } = await get(`/app/${appId}/package/list?limit=1000`);
|
const { data } = await get(`/app/${appId}/package/list?limit=1000`);
|
||||||
|
|
||||||
const header = [{ value: 'Package Id' }, { value: 'Version' }];
|
const header = [{ value: '原生包 Id' }, { value: '原生版本' }];
|
||||||
const rows = [];
|
const rows = [];
|
||||||
for (const pkg of data) {
|
for (const pkg of data) {
|
||||||
const { version } = pkg;
|
const { version } = pkg;
|
||||||
let versionInfo = '';
|
let versionInfo = '';
|
||||||
if (version) {
|
if (version) {
|
||||||
versionInfo = ` - ${version.id} ${version.hash.slice(0, 8)} ${version.name}`;
|
versionInfo = `, 已绑定:${version.name} (${version.id})`;
|
||||||
} else {
|
} else {
|
||||||
versionInfo = ' (newest)';
|
// versionInfo = ' (newest)';
|
||||||
}
|
}
|
||||||
|
let output = pkg.name;
|
||||||
rows.push([pkg.id, `${pkg.name}(${pkg.status})${versionInfo}`]);
|
if (pkg.status === 'paused') {
|
||||||
|
output += '(已暂停)';
|
||||||
|
}
|
||||||
|
if (pkg.status === 'expired') {
|
||||||
|
output += '(已过期)';
|
||||||
|
}
|
||||||
|
output += versionInfo;
|
||||||
|
rows.push([pkg.id, output]);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(Table(header, rows).render());
|
console.log(Table(header, rows).render());
|
||||||
console.log(`\nTotal ${data.length} package(s).`);
|
console.log(`\n共 ${data.length} 个包`);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,8 +39,8 @@ export async function choosePackage(appId) {
|
|||||||
const list = await listPackage(appId);
|
const list = await listPackage(appId);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const id = await question('Enter Package Id:');
|
const id = await question('输入原生包 id:');
|
||||||
const app = list.find(v => v.id === (id | 0));
|
const app = list.find((v) => v.id === (id | 0));
|
||||||
if (app) {
|
if (app) {
|
||||||
return app;
|
return app;
|
||||||
}
|
}
|
||||||
@@ -45,13 +48,30 @@ export async function choosePackage(appId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const commands = {
|
export const commands = {
|
||||||
uploadIpa: async function({ args }) {
|
uploadIpa: async function ({ args }) {
|
||||||
const fn = args[0];
|
const fn = args[0];
|
||||||
if (!fn || !fn.endsWith('.ipa')) {
|
if (!fn || !fn.endsWith('.ipa')) {
|
||||||
throw new Error('Usage: pushy uploadIpa <ipaFile>');
|
throw new Error('使用方法: pushy uploadIpa ipa后缀文件');
|
||||||
|
}
|
||||||
|
const {
|
||||||
|
versionName,
|
||||||
|
buildTime,
|
||||||
|
appId: appIdInPkg,
|
||||||
|
appKey: appKeyInPkg,
|
||||||
|
} = await getIpaInfo(fn);
|
||||||
|
const { appId, appKey } = await getSelectedApp('ios');
|
||||||
|
|
||||||
|
if (appIdInPkg && appIdInPkg != appId) {
|
||||||
|
throw new Error(
|
||||||
|
`appId不匹配!当前ipa: ${appIdInPkg}, 当前update.json: ${appId}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (appKeyInPkg && appKeyInPkg !== appKey) {
|
||||||
|
throw new Error(
|
||||||
|
`appKey不匹配!当前ipa: ${appKeyInPkg}, 当前update.json: ${appKey}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const { versionName, buildTime } = await getIpaInfo(fn);
|
|
||||||
const { appId } = await getSelectedApp('ios');
|
|
||||||
|
|
||||||
const { hash } = await uploadFile(fn);
|
const { hash } = await uploadFile(fn);
|
||||||
|
|
||||||
@@ -61,15 +81,34 @@ export const commands = {
|
|||||||
buildTime,
|
buildTime,
|
||||||
});
|
});
|
||||||
saveToLocal(fn, `${appId}/package/${id}.ipa`);
|
saveToLocal(fn, `${appId}/package/${id}.ipa`);
|
||||||
console.log(`Ipa uploaded: ${id}`);
|
console.log(
|
||||||
|
`已成功上传ipa原生包(id: ${id}, version: ${versionName}, buildTime: ${buildTime})`,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
uploadApk: async function({ args }) {
|
uploadApk: async function ({ args }) {
|
||||||
const fn = args[0];
|
const fn = args[0];
|
||||||
if (!fn || !fn.endsWith('.apk')) {
|
if (!fn || !fn.endsWith('.apk')) {
|
||||||
throw new Error('Usage: pushy uploadApk <apkFile>');
|
throw new Error('使用方法: pushy uploadApk apk后缀文件');
|
||||||
|
}
|
||||||
|
const {
|
||||||
|
versionName,
|
||||||
|
buildTime,
|
||||||
|
appId: appIdInPkg,
|
||||||
|
appKey: appKeyInPkg,
|
||||||
|
} = await getApkInfo(fn);
|
||||||
|
const { appId, appKey } = await getSelectedApp('android');
|
||||||
|
|
||||||
|
if (appIdInPkg && appIdInPkg != appId) {
|
||||||
|
throw new Error(
|
||||||
|
`appId不匹配!当前apk: ${appIdInPkg}, 当前update.json: ${appId}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (appKeyInPkg && appKeyInPkg !== appKey) {
|
||||||
|
throw new Error(
|
||||||
|
`appKey不匹配!当前apk: ${appKeyInPkg}, 当前update.json: ${appKey}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const { versionName, buildTime } = await getApkInfo(fn);
|
|
||||||
const { appId } = await getSelectedApp('android');
|
|
||||||
|
|
||||||
const { hash } = await uploadFile(fn);
|
const { hash } = await uploadFile(fn);
|
||||||
|
|
||||||
@@ -79,10 +118,28 @@ export const commands = {
|
|||||||
buildTime,
|
buildTime,
|
||||||
});
|
});
|
||||||
saveToLocal(fn, `${appId}/package/${id}.apk`);
|
saveToLocal(fn, `${appId}/package/${id}.apk`);
|
||||||
console.log(`Apk uploaded: ${id}`);
|
console.log(
|
||||||
|
`已成功上传apk原生包(id: ${id}, version: ${versionName}, buildTime: ${buildTime})`,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
packages: async function({ options }) {
|
parseIpa: async function ({ args }) {
|
||||||
const platform = checkPlatform(options.platform || (await question('Platform(ios/android):')));
|
const fn = args[0];
|
||||||
|
if (!fn || !fn.endsWith('.ipa')) {
|
||||||
|
throw new Error('使用方法: pushy parseIpa ipa后缀文件');
|
||||||
|
}
|
||||||
|
console.log(await getIpaInfo(fn));
|
||||||
|
},
|
||||||
|
parseApk: async function ({ args }) {
|
||||||
|
const fn = args[0];
|
||||||
|
if (!fn || !fn.endsWith('.apk')) {
|
||||||
|
throw new Error('使用方法: pushy parseApk apk后缀文件');
|
||||||
|
}
|
||||||
|
console.log(await getApkInfo(fn));
|
||||||
|
},
|
||||||
|
packages: async function ({ options }) {
|
||||||
|
const platform = checkPlatform(
|
||||||
|
options.platform || (await question('平台(ios/android):')),
|
||||||
|
);
|
||||||
const { appId } = await getSelectedApp(platform);
|
const { appId } = await getSelectedApp(platform);
|
||||||
await listPackage(appId);
|
await listPackage(appId);
|
||||||
},
|
},
|
||||||
|
8
src/types.ts
Normal file
8
src/types.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
declare global {
|
||||||
|
var NO_INTERACTIVE: boolean;
|
||||||
|
var USE_ACC_OSS: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Session {
|
||||||
|
token: string;
|
||||||
|
}
|
38
src/user.js
38
src/user.js
@@ -1,38 +1,28 @@
|
|||||||
/**
|
import { question } from './utils';
|
||||||
* Created by tdzl2003 on 2/13/16.
|
import { post, get, replaceSession, saveSession, closeSession } from './api';
|
||||||
*/
|
import crypto from 'crypto';
|
||||||
|
|
||||||
import {question} from './utils';
|
|
||||||
const {
|
|
||||||
post,
|
|
||||||
get,
|
|
||||||
replaceSession,
|
|
||||||
saveSession,
|
|
||||||
closeSession,
|
|
||||||
} = require('./api');
|
|
||||||
const crypto = require('crypto');
|
|
||||||
|
|
||||||
function md5(str) {
|
function md5(str) {
|
||||||
return crypto.createHash('md5').update(str).digest('hex');
|
return crypto.createHash('md5').update(str).digest('hex');
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.commands = {
|
export const commands = {
|
||||||
login: async function ({args}){
|
login: async function ({ args }) {
|
||||||
const email = args[0] || await question('email:');
|
const email = args[0] || (await question('email:'));
|
||||||
const pwd = args[1] || await question('password:', true);
|
const pwd = args[1] || (await question('password:', true));
|
||||||
const {token, info} = await post('/user/login', {
|
const { token, info } = await post('/user/login', {
|
||||||
email,
|
email,
|
||||||
pwd: md5(pwd),
|
pwd: md5(pwd),
|
||||||
});
|
});
|
||||||
replaceSession({token});
|
replaceSession({ token });
|
||||||
await saveSession();
|
await saveSession();
|
||||||
console.log(`Welcome, ${info.name}.`);
|
console.log(`欢迎使用 pushy 热更新服务, ${info.name}.`);
|
||||||
},
|
},
|
||||||
logout: async function (){
|
logout: async function () {
|
||||||
await closeSession();
|
await closeSession();
|
||||||
console.log('Logged out.');
|
console.log('已退出登录');
|
||||||
},
|
},
|
||||||
me: async function (){
|
me: async function () {
|
||||||
const me = await get('/user/me');
|
const me = await get('/user/me');
|
||||||
for (const k in me) {
|
for (const k in me) {
|
||||||
if (k !== 'ok') {
|
if (k !== 'ok') {
|
||||||
@@ -40,4 +30,4 @@ exports.commands = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
};
|
||||||
|
90
src/utils/app-info-parser/apk.js
Normal file
90
src/utils/app-info-parser/apk.js
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
const Zip = require('./zip')
|
||||||
|
const { mapInfoResource, findApkIconPath, getBase64FromBuffer } = require('./utils')
|
||||||
|
const ManifestName = /^androidmanifest\.xml$/
|
||||||
|
const ResourceName = /^resources\.arsc$/
|
||||||
|
|
||||||
|
const ManifestXmlParser = require('./xml-parser/manifest')
|
||||||
|
const ResourceFinder = require('./resource-finder')
|
||||||
|
|
||||||
|
class ApkParser extends Zip {
|
||||||
|
/**
|
||||||
|
* parser for parsing .apk file
|
||||||
|
* @param {String | File | Blob} file // file's path in Node, instance of File or Blob in Browser
|
||||||
|
*/
|
||||||
|
constructor (file) {
|
||||||
|
super(file)
|
||||||
|
if (!(this instanceof ApkParser)) {
|
||||||
|
return new ApkParser(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parse () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.getEntries([ManifestName, ResourceName]).then(buffers => {
|
||||||
|
if (!buffers[ManifestName]) {
|
||||||
|
throw new Error('AndroidManifest.xml can\'t be found.')
|
||||||
|
}
|
||||||
|
let apkInfo = this._parseManifest(buffers[ManifestName])
|
||||||
|
let resourceMap
|
||||||
|
if (!buffers[ResourceName]) {
|
||||||
|
resolve(apkInfo)
|
||||||
|
} else {
|
||||||
|
// parse resourceMap
|
||||||
|
resourceMap = this._parseResourceMap(buffers[ResourceName])
|
||||||
|
// update apkInfo with resourceMap
|
||||||
|
apkInfo = mapInfoResource(apkInfo, resourceMap)
|
||||||
|
|
||||||
|
// find icon path and parse icon
|
||||||
|
const iconPath = findApkIconPath(apkInfo)
|
||||||
|
if (iconPath) {
|
||||||
|
this.getEntry(iconPath).then(iconBuffer => {
|
||||||
|
apkInfo.icon = iconBuffer ? getBase64FromBuffer(iconBuffer) : null
|
||||||
|
resolve(apkInfo)
|
||||||
|
}).catch(e => {
|
||||||
|
apkInfo.icon = null
|
||||||
|
resolve(apkInfo)
|
||||||
|
console.warn('[Warning] failed to parse icon: ', e)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
apkInfo.icon = null
|
||||||
|
resolve(apkInfo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).catch(e => {
|
||||||
|
reject(e)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Parse manifest
|
||||||
|
* @param {Buffer} buffer // manifest file's buffer
|
||||||
|
*/
|
||||||
|
_parseManifest (buffer) {
|
||||||
|
try {
|
||||||
|
const parser = new ManifestXmlParser(buffer, {
|
||||||
|
ignore: [
|
||||||
|
'application.activity',
|
||||||
|
'application.service',
|
||||||
|
'application.receiver',
|
||||||
|
'application.provider',
|
||||||
|
'permission-group'
|
||||||
|
]
|
||||||
|
})
|
||||||
|
return parser.parse()
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error('Parse AndroidManifest.xml error: ', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Parse resourceMap
|
||||||
|
* @param {Buffer} buffer // resourceMap file's buffer
|
||||||
|
*/
|
||||||
|
_parseResourceMap (buffer) {
|
||||||
|
try {
|
||||||
|
return new ResourceFinder().processResourceTable(buffer)
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error('Parser resources.arsc error: ' + e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ApkParser
|
35
src/utils/app-info-parser/index.js
Normal file
35
src/utils/app-info-parser/index.js
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
const ApkParser = require('./apk')
|
||||||
|
const IpaParser = require('./ipa')
|
||||||
|
const supportFileTypes = ['ipa', 'apk']
|
||||||
|
|
||||||
|
class AppInfoParser {
|
||||||
|
/**
|
||||||
|
* parser for parsing .ipa or .apk file
|
||||||
|
* @param {String | File | Blob} file // file's path in Node, instance of File or Blob in Browser
|
||||||
|
*/
|
||||||
|
constructor (file) {
|
||||||
|
if (!file) {
|
||||||
|
throw new Error('Param miss: file(file\'s path in Node, instance of File or Blob in browser).')
|
||||||
|
}
|
||||||
|
const splits = (file.name || file).split('.')
|
||||||
|
const fileType = splits[splits.length - 1].toLowerCase()
|
||||||
|
if (!supportFileTypes.includes(fileType)) {
|
||||||
|
throw new Error('Unsupported file type, only support .ipa or .apk file.')
|
||||||
|
}
|
||||||
|
this.file = file
|
||||||
|
|
||||||
|
switch (fileType) {
|
||||||
|
case 'ipa':
|
||||||
|
this.parser = new IpaParser(this.file)
|
||||||
|
break
|
||||||
|
case 'apk':
|
||||||
|
this.parser = new ApkParser(this.file)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parse () {
|
||||||
|
return this.parser.parse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = AppInfoParser
|
92
src/utils/app-info-parser/ipa.js
Normal file
92
src/utils/app-info-parser/ipa.js
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
const parsePlist = require('plist').parse
|
||||||
|
const parseBplist = require('bplist-parser').parseBuffer
|
||||||
|
const cgbiToPng = require('cgbi-to-png')
|
||||||
|
|
||||||
|
const Zip = require('./zip')
|
||||||
|
const { findIpaIconPath, getBase64FromBuffer, isBrowser } = require('./utils')
|
||||||
|
|
||||||
|
const PlistName = new RegExp('payload/[^/]+?.app/info.plist$', 'i')
|
||||||
|
const ProvisionName = /payload\/.+?\.app\/embedded.mobileprovision/
|
||||||
|
|
||||||
|
class IpaParser extends Zip {
|
||||||
|
/**
|
||||||
|
* parser for parsing .ipa file
|
||||||
|
* @param {String | File | Blob} file // file's path in Node, instance of File or Blob in Browser
|
||||||
|
*/
|
||||||
|
constructor (file) {
|
||||||
|
super(file)
|
||||||
|
if (!(this instanceof IpaParser)) {
|
||||||
|
return new IpaParser(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parse () {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.getEntries([PlistName, ProvisionName]).then(buffers => {
|
||||||
|
if (!buffers[PlistName]) {
|
||||||
|
throw new Error('Info.plist can\'t be found.')
|
||||||
|
}
|
||||||
|
const plistInfo = this._parsePlist(buffers[PlistName])
|
||||||
|
// parse mobile provision
|
||||||
|
const provisionInfo = this._parseProvision(buffers[ProvisionName])
|
||||||
|
plistInfo.mobileProvision = provisionInfo
|
||||||
|
|
||||||
|
// find icon path and parse icon
|
||||||
|
const iconRegex = new RegExp(findIpaIconPath(plistInfo).toLowerCase())
|
||||||
|
this.getEntry(iconRegex).then(iconBuffer => {
|
||||||
|
try {
|
||||||
|
// In general, the ipa file's icon has been specially processed, should be converted
|
||||||
|
plistInfo.icon = iconBuffer ? getBase64FromBuffer(cgbiToPng.revert(iconBuffer)) : null
|
||||||
|
} catch (err) {
|
||||||
|
if (isBrowser()) {
|
||||||
|
// Normal conversion in other cases
|
||||||
|
plistInfo.icon = iconBuffer ? getBase64FromBuffer(window.btoa(String.fromCharCode(...iconBuffer))) : null
|
||||||
|
} else {
|
||||||
|
plistInfo.icon = null
|
||||||
|
console.warn('[Warning] failed to parse icon: ', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resolve(plistInfo)
|
||||||
|
}).catch(e => {
|
||||||
|
reject(e)
|
||||||
|
})
|
||||||
|
}).catch(e => {
|
||||||
|
reject(e)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Parse plist
|
||||||
|
* @param {Buffer} buffer // plist file's buffer
|
||||||
|
*/
|
||||||
|
_parsePlist (buffer) {
|
||||||
|
let result
|
||||||
|
const bufferType = buffer[0]
|
||||||
|
if (bufferType === 60 || bufferType === '<' || bufferType === 239) {
|
||||||
|
result = parsePlist(buffer.toString())
|
||||||
|
} else if (bufferType === 98) {
|
||||||
|
result = parseBplist(buffer)[0]
|
||||||
|
} else {
|
||||||
|
throw new Error('Unknown plist buffer type.')
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* parse provision
|
||||||
|
* @param {Buffer} buffer // provision file's buffer
|
||||||
|
*/
|
||||||
|
_parseProvision (buffer) {
|
||||||
|
let info = {}
|
||||||
|
if (buffer) {
|
||||||
|
let content = buffer.toString('utf-8')
|
||||||
|
const firstIndex = content.indexOf('<?xml')
|
||||||
|
const endIndex = content.indexOf('</plist>')
|
||||||
|
content = content.slice(firstIndex, endIndex + 8)
|
||||||
|
if (content) {
|
||||||
|
info = parsePlist(content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return info
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = IpaParser
|
499
src/utils/app-info-parser/resource-finder.js
Normal file
499
src/utils/app-info-parser/resource-finder.js
Normal file
@@ -0,0 +1,499 @@
|
|||||||
|
/**
|
||||||
|
* Code translated from a C# project https://github.com/hylander0/Iteedee.ApkReader/blob/master/Iteedee.ApkReader/ApkResourceFinder.cs
|
||||||
|
*
|
||||||
|
* Decode binary file `resources.arsc` from a .apk file to a JavaScript Object.
|
||||||
|
*/
|
||||||
|
|
||||||
|
var ByteBuffer = require("bytebuffer");
|
||||||
|
|
||||||
|
var DEBUG = false;
|
||||||
|
|
||||||
|
var RES_STRING_POOL_TYPE = 0x0001;
|
||||||
|
var RES_TABLE_TYPE = 0x0002;
|
||||||
|
var RES_TABLE_PACKAGE_TYPE = 0x0200;
|
||||||
|
var RES_TABLE_TYPE_TYPE = 0x0201;
|
||||||
|
var RES_TABLE_TYPE_SPEC_TYPE = 0x0202;
|
||||||
|
|
||||||
|
// The 'data' holds a ResTable_ref, a reference to another resource
|
||||||
|
// table entry.
|
||||||
|
var TYPE_REFERENCE = 0x01;
|
||||||
|
// The 'data' holds an index into the containing resource table's
|
||||||
|
// global value string pool.
|
||||||
|
var TYPE_STRING = 0x03;
|
||||||
|
|
||||||
|
function ResourceFinder() {
|
||||||
|
this.valueStringPool = null;
|
||||||
|
this.typeStringPool = null;
|
||||||
|
this.keyStringPool = null;
|
||||||
|
|
||||||
|
this.package_id = 0;
|
||||||
|
|
||||||
|
this.responseMap = {};
|
||||||
|
this.entryMap = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same to C# BinaryReader.readBytes
|
||||||
|
*
|
||||||
|
* @param bb ByteBuffer
|
||||||
|
* @param len length
|
||||||
|
* @returns {Buffer}
|
||||||
|
*/
|
||||||
|
ResourceFinder.readBytes = function(bb, len) {
|
||||||
|
var uint8Array = new Uint8Array(len);
|
||||||
|
for (var i = 0; i < len; i++) {
|
||||||
|
uint8Array[i] = bb.readUint8();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ByteBuffer.wrap(uint8Array, "binary", true);
|
||||||
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {ByteBuffer} bb
|
||||||
|
* @return {Map<String, Set<String>>}
|
||||||
|
*/
|
||||||
|
ResourceFinder.prototype.processResourceTable = function(resourceBuffer) {
|
||||||
|
const bb = ByteBuffer.wrap(resourceBuffer, "binary", true);
|
||||||
|
|
||||||
|
// Resource table structure
|
||||||
|
var type = bb.readShort(),
|
||||||
|
headerSize = bb.readShort(),
|
||||||
|
size = bb.readInt(),
|
||||||
|
packageCount = bb.readInt(),
|
||||||
|
buffer,
|
||||||
|
bb2;
|
||||||
|
if (type != RES_TABLE_TYPE) {
|
||||||
|
throw new Error("No RES_TABLE_TYPE found!");
|
||||||
|
}
|
||||||
|
if (size != bb.limit) {
|
||||||
|
throw new Error("The buffer size not matches to the resource table size.");
|
||||||
|
}
|
||||||
|
bb.offset = headerSize;
|
||||||
|
|
||||||
|
var realStringPoolCount = 0,
|
||||||
|
realPackageCount = 0;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
var pos, t, hs, s;
|
||||||
|
try {
|
||||||
|
pos = bb.offset;
|
||||||
|
t = bb.readShort();
|
||||||
|
hs = bb.readShort();
|
||||||
|
s = bb.readInt();
|
||||||
|
} catch (e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (t == RES_STRING_POOL_TYPE) {
|
||||||
|
// Process the string pool
|
||||||
|
if (realStringPoolCount == 0) {
|
||||||
|
// Only the first string pool is processed.
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log("Processing the string pool ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = new ByteBuffer(s);
|
||||||
|
bb.offset = pos;
|
||||||
|
bb.prependTo(buffer);
|
||||||
|
|
||||||
|
bb2 = ByteBuffer.wrap(buffer, "binary", true);
|
||||||
|
|
||||||
|
bb2.LE();
|
||||||
|
this.valueStringPool = this.processStringPool(bb2);
|
||||||
|
}
|
||||||
|
realStringPoolCount++;
|
||||||
|
} else if (t == RES_TABLE_PACKAGE_TYPE) {
|
||||||
|
// Process the package
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log("Processing the package " + realPackageCount + " ...");
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = new ByteBuffer(s);
|
||||||
|
bb.offset = pos;
|
||||||
|
bb.prependTo(buffer);
|
||||||
|
|
||||||
|
bb2 = ByteBuffer.wrap(buffer, "binary", true);
|
||||||
|
bb2.LE();
|
||||||
|
this.processPackage(bb2);
|
||||||
|
|
||||||
|
realPackageCount++;
|
||||||
|
} else {
|
||||||
|
throw new Error("Unsupported type");
|
||||||
|
}
|
||||||
|
bb.offset = pos + s;
|
||||||
|
if (!bb.remaining()) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (realStringPoolCount != 1) {
|
||||||
|
throw new Error("More than 1 string pool found!");
|
||||||
|
}
|
||||||
|
if (realPackageCount != packageCount) {
|
||||||
|
throw new Error("Real package count not equals the declared count.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.responseMap;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {ByteBuffer} bb
|
||||||
|
*/
|
||||||
|
ResourceFinder.prototype.processPackage = function(bb) {
|
||||||
|
// Package structure
|
||||||
|
var type = bb.readShort(),
|
||||||
|
headerSize = bb.readShort(),
|
||||||
|
size = bb.readInt(),
|
||||||
|
id = bb.readInt();
|
||||||
|
|
||||||
|
this.package_id = id;
|
||||||
|
|
||||||
|
for (var i = 0; i < 256; ++i) {
|
||||||
|
bb.readUint8();
|
||||||
|
}
|
||||||
|
|
||||||
|
var typeStrings = bb.readInt(),
|
||||||
|
lastPublicType = bb.readInt(),
|
||||||
|
keyStrings = bb.readInt(),
|
||||||
|
lastPublicKey = bb.readInt();
|
||||||
|
|
||||||
|
if (typeStrings != headerSize) {
|
||||||
|
throw new Error(
|
||||||
|
"TypeStrings must immediately following the package structure header."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log("Type strings:");
|
||||||
|
}
|
||||||
|
|
||||||
|
var lastPosition = bb.offset;
|
||||||
|
bb.offset = typeStrings;
|
||||||
|
var bbTypeStrings = ResourceFinder.readBytes(bb, bb.limit - bb.offset);
|
||||||
|
bb.offset = lastPosition;
|
||||||
|
this.typeStringPool = this.processStringPool(bbTypeStrings);
|
||||||
|
|
||||||
|
// Key strings
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log("Key strings:");
|
||||||
|
}
|
||||||
|
|
||||||
|
bb.offset = keyStrings;
|
||||||
|
var key_type = bb.readShort(),
|
||||||
|
key_headerSize = bb.readShort(),
|
||||||
|
key_size = bb.readInt();
|
||||||
|
|
||||||
|
lastPosition = bb.offset;
|
||||||
|
bb.offset = keyStrings;
|
||||||
|
var bbKeyStrings = ResourceFinder.readBytes(bb, bb.limit - bb.offset);
|
||||||
|
bb.offset = lastPosition;
|
||||||
|
this.keyStringPool = this.processStringPool(bbKeyStrings);
|
||||||
|
|
||||||
|
// Iterate through all chunks
|
||||||
|
var typeSpecCount = 0;
|
||||||
|
var typeCount = 0;
|
||||||
|
|
||||||
|
bb.offset = keyStrings + key_size;
|
||||||
|
|
||||||
|
var bb2;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
var pos = bb.offset;
|
||||||
|
try {
|
||||||
|
var t = bb.readShort();
|
||||||
|
var hs = bb.readShort();
|
||||||
|
var s = bb.readInt();
|
||||||
|
} catch (e) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t == RES_TABLE_TYPE_SPEC_TYPE) {
|
||||||
|
bb.offset = pos;
|
||||||
|
bb2 = ResourceFinder.readBytes(bb, s);
|
||||||
|
this.processTypeSpec(bb2);
|
||||||
|
|
||||||
|
typeSpecCount++;
|
||||||
|
} else if (t == RES_TABLE_TYPE_TYPE) {
|
||||||
|
bb.offset = pos;
|
||||||
|
bb2 = ResourceFinder.readBytes(bb, s);
|
||||||
|
this.processType(bb2);
|
||||||
|
|
||||||
|
typeCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
bb.offset = pos + s;
|
||||||
|
|
||||||
|
if (!bb.remaining()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {ByteBuffer} bb
|
||||||
|
*/
|
||||||
|
ResourceFinder.prototype.processType = function(bb) {
|
||||||
|
var type = bb.readShort(),
|
||||||
|
headerSize = bb.readShort(),
|
||||||
|
size = bb.readInt(),
|
||||||
|
id = bb.readByte(),
|
||||||
|
res0 = bb.readByte(),
|
||||||
|
res1 = bb.readShort(),
|
||||||
|
entryCount = bb.readInt(),
|
||||||
|
entriesStart = bb.readInt();
|
||||||
|
|
||||||
|
var refKeys = {};
|
||||||
|
|
||||||
|
var config_size = bb.readInt();
|
||||||
|
|
||||||
|
// Skip the config data
|
||||||
|
bb.offset = headerSize;
|
||||||
|
|
||||||
|
if (headerSize + entryCount * 4 != entriesStart) {
|
||||||
|
throw new Error("HeaderSize, entryCount and entriesStart are not valid.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start to get entry indices
|
||||||
|
var entryIndices = new Array(entryCount);
|
||||||
|
for (var i = 0; i < entryCount; ++i) {
|
||||||
|
entryIndices[i] = bb.readInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get entries
|
||||||
|
for (var i = 0; i < entryCount; ++i) {
|
||||||
|
if (entryIndices[i] == -1) continue;
|
||||||
|
|
||||||
|
var resource_id = (this.package_id << 24) | (id << 16) | i;
|
||||||
|
|
||||||
|
var pos = bb.offset,
|
||||||
|
entry_size,
|
||||||
|
entry_flag,
|
||||||
|
entry_key,
|
||||||
|
value_size,
|
||||||
|
value_res0,
|
||||||
|
value_dataType,
|
||||||
|
value_data;
|
||||||
|
try {
|
||||||
|
entry_size = bb.readShort()
|
||||||
|
entry_flag = bb.readShort()
|
||||||
|
entry_key = bb.readInt()
|
||||||
|
} catch (e) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the value (simple) or map (complex)
|
||||||
|
|
||||||
|
var FLAG_COMPLEX = 0x0001;
|
||||||
|
if ((entry_flag & FLAG_COMPLEX) == 0) {
|
||||||
|
// Simple case
|
||||||
|
value_size = bb.readShort();
|
||||||
|
value_res0 = bb.readByte();
|
||||||
|
value_dataType = bb.readByte();
|
||||||
|
value_data = bb.readInt();
|
||||||
|
|
||||||
|
var idStr = Number(resource_id).toString(16);
|
||||||
|
var keyStr = this.keyStringPool[entry_key];
|
||||||
|
|
||||||
|
var data = null;
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log(
|
||||||
|
"Entry 0x" + idStr + ", key: " + keyStr + ", simple value type: "
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
var key = parseInt(idStr, 16);
|
||||||
|
|
||||||
|
var entryArr = this.entryMap[key];
|
||||||
|
if (entryArr == null) {
|
||||||
|
entryArr = [];
|
||||||
|
}
|
||||||
|
entryArr.push(keyStr);
|
||||||
|
|
||||||
|
this.entryMap[key] = entryArr;
|
||||||
|
|
||||||
|
if (value_dataType == TYPE_STRING) {
|
||||||
|
data = this.valueStringPool[value_data];
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log(", data: " + this.valueStringPool[value_data] + "");
|
||||||
|
}
|
||||||
|
} else if (value_dataType == TYPE_REFERENCE) {
|
||||||
|
var hexIndex = Number(value_data).toString(16);
|
||||||
|
|
||||||
|
refKeys[idStr] = value_data;
|
||||||
|
} else {
|
||||||
|
data = "" + value_data;
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log(", data: " + value_data + "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.putIntoMap("@" + idStr, data);
|
||||||
|
} else {
|
||||||
|
// Complex case
|
||||||
|
var entry_parent = bb.readInt();
|
||||||
|
var entry_count = bb.readInt();
|
||||||
|
|
||||||
|
for (var j = 0; j < entry_count; ++j) {
|
||||||
|
var ref_name = bb.readInt();
|
||||||
|
value_size = bb.readShort();
|
||||||
|
value_res0 = bb.readByte();
|
||||||
|
value_dataType = bb.readByte();
|
||||||
|
value_data = bb.readInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log(
|
||||||
|
"Entry 0x" +
|
||||||
|
Number(resource_id).toString(16) +
|
||||||
|
", key: " +
|
||||||
|
this.keyStringPool[entry_key] +
|
||||||
|
", complex value, not printed."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var refK in refKeys) {
|
||||||
|
var values = this.responseMap[
|
||||||
|
"@" +
|
||||||
|
Number(refKeys[refK])
|
||||||
|
.toString(16)
|
||||||
|
.toUpperCase()
|
||||||
|
];
|
||||||
|
if (values != null && Object.keys(values).length < 1000) {
|
||||||
|
for (var value in values) {
|
||||||
|
this.putIntoMap("@" + refK, values[value]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {ByteBuffer} bb
|
||||||
|
* @return {Array}
|
||||||
|
*/
|
||||||
|
ResourceFinder.prototype.processStringPool = function(bb) {
|
||||||
|
// String pool structure
|
||||||
|
//
|
||||||
|
var type = bb.readShort(),
|
||||||
|
headerSize = bb.readShort(),
|
||||||
|
size = bb.readInt(),
|
||||||
|
stringCount = bb.readInt(),
|
||||||
|
styleCount = bb.readInt(),
|
||||||
|
flags = bb.readInt(),
|
||||||
|
stringsStart = bb.readInt(),
|
||||||
|
stylesStart = bb.readInt(),
|
||||||
|
u16len,
|
||||||
|
buffer;
|
||||||
|
|
||||||
|
var isUTF_8 = (flags & 256) != 0;
|
||||||
|
|
||||||
|
var offsets = new Array(stringCount);
|
||||||
|
for (var i = 0; i < stringCount; ++i) {
|
||||||
|
offsets[i] = bb.readInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
var strings = new Array(stringCount);
|
||||||
|
|
||||||
|
for (var i = 0; i < stringCount; ++i) {
|
||||||
|
var pos = stringsStart + offsets[i];
|
||||||
|
bb.offset = pos;
|
||||||
|
|
||||||
|
strings[i] = "";
|
||||||
|
|
||||||
|
if (isUTF_8) {
|
||||||
|
u16len = bb.readUint8();
|
||||||
|
|
||||||
|
if ((u16len & 0x80) != 0) {
|
||||||
|
u16len = ((u16len & 0x7f) << 8) + bb.readUint8();
|
||||||
|
}
|
||||||
|
|
||||||
|
var u8len = bb.readUint8();
|
||||||
|
if ((u8len & 0x80) != 0) {
|
||||||
|
u8len = ((u8len & 0x7f) << 8) + bb.readUint8();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u8len > 0) {
|
||||||
|
buffer = ResourceFinder.readBytes(bb, u8len);
|
||||||
|
try {
|
||||||
|
strings[i] = ByteBuffer.wrap(buffer, "utf8", true).toString("utf8");
|
||||||
|
} catch (e) {
|
||||||
|
if (DEBUG) {
|
||||||
|
console.error(e);
|
||||||
|
console.log("Error when turning buffer to utf-8 string.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
strings[i] = "";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
u16len = bb.readUint16();
|
||||||
|
if ((u16len & 0x8000) != 0) {
|
||||||
|
// larger than 32768
|
||||||
|
u16len = ((u16len & 0x7fff) << 16) + bb.readUint16();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (u16len > 0) {
|
||||||
|
var len = u16len * 2;
|
||||||
|
buffer = ResourceFinder.readBytes(bb, len);
|
||||||
|
try {
|
||||||
|
strings[i] = ByteBuffer.wrap(buffer, "utf8", true).toString("utf8");
|
||||||
|
} catch (e) {
|
||||||
|
if (DEBUG) {
|
||||||
|
console.error(e);
|
||||||
|
console.log("Error when turning buffer to utf-8 string.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log("Parsed value: {0}", strings[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {ByteBuffer} bb
|
||||||
|
*/
|
||||||
|
ResourceFinder.prototype.processTypeSpec = function(bb) {
|
||||||
|
var type = bb.readShort(),
|
||||||
|
headerSize = bb.readShort(),
|
||||||
|
size = bb.readInt(),
|
||||||
|
id = bb.readByte(),
|
||||||
|
res0 = bb.readByte(),
|
||||||
|
res1 = bb.readShort(),
|
||||||
|
entryCount = bb.readInt();
|
||||||
|
|
||||||
|
if (DEBUG) {
|
||||||
|
console.log("Processing type spec " + this.typeStringPool[id - 1] + "...");
|
||||||
|
}
|
||||||
|
|
||||||
|
var flags = new Array(entryCount);
|
||||||
|
|
||||||
|
for (var i = 0; i < entryCount; ++i) {
|
||||||
|
flags[i] = bb.readInt();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ResourceFinder.prototype.putIntoMap = function(resId, value) {
|
||||||
|
if (this.responseMap[resId.toUpperCase()] == null) {
|
||||||
|
this.responseMap[resId.toUpperCase()] = []
|
||||||
|
}
|
||||||
|
if(value){
|
||||||
|
this.responseMap[resId.toUpperCase()].push(value)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = ResourceFinder;
|
167
src/utils/app-info-parser/utils.js
Normal file
167
src/utils/app-info-parser/utils.js
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
function objectType (o) {
|
||||||
|
return Object.prototype.toString.call(o).slice(8, -1).toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
|
function isArray (o) {
|
||||||
|
return objectType(o) === 'array'
|
||||||
|
}
|
||||||
|
|
||||||
|
function isObject (o) {
|
||||||
|
return objectType(o) === 'object'
|
||||||
|
}
|
||||||
|
|
||||||
|
function isPrimitive (o) {
|
||||||
|
return o === null || ['boolean', 'number', 'string', 'undefined'].includes(objectType(o))
|
||||||
|
}
|
||||||
|
|
||||||
|
function isBrowser () {
|
||||||
|
return (
|
||||||
|
typeof process === 'undefined' ||
|
||||||
|
Object.prototype.toString.call(process) !== '[object process]'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* map file place with resourceMap
|
||||||
|
* @param {Object} apkInfo // json info parsed from .apk file
|
||||||
|
* @param {Object} resourceMap // resourceMap
|
||||||
|
*/
|
||||||
|
function mapInfoResource (apkInfo, resourceMap) {
|
||||||
|
iteratorObj(apkInfo)
|
||||||
|
return apkInfo
|
||||||
|
function iteratorObj (obj) {
|
||||||
|
for (var i in obj) {
|
||||||
|
if (isArray(obj[i])) {
|
||||||
|
iteratorArray(obj[i])
|
||||||
|
} else if (isObject(obj[i])) {
|
||||||
|
iteratorObj(obj[i])
|
||||||
|
} else if (isPrimitive(obj[i])) {
|
||||||
|
if (isResources(obj[i])) {
|
||||||
|
obj[i] = resourceMap[transKeyToMatchResourceMap(obj[i])]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function iteratorArray (array) {
|
||||||
|
const l = array.length
|
||||||
|
for (let i = 0; i < l; i++) {
|
||||||
|
if (isArray(array[i])) {
|
||||||
|
iteratorArray(array[i])
|
||||||
|
} else if (isObject(array[i])) {
|
||||||
|
iteratorObj(array[i])
|
||||||
|
} else if (isPrimitive(array[i])) {
|
||||||
|
if (isResources(array[i])) {
|
||||||
|
array[i] = resourceMap[transKeyToMatchResourceMap(array[i])]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isResources (attrValue) {
|
||||||
|
if (!attrValue) return false
|
||||||
|
if (typeof attrValue !== 'string') {
|
||||||
|
attrValue = attrValue.toString()
|
||||||
|
}
|
||||||
|
return attrValue.indexOf('resourceId:') === 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function transKeyToMatchResourceMap (resourceId) {
|
||||||
|
return '@' + resourceId.replace('resourceId:0x', '').toUpperCase()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* find .apk file's icon path from json info
|
||||||
|
* @param info // json info parsed from .apk file
|
||||||
|
*/
|
||||||
|
function findApkIconPath (info) {
|
||||||
|
if (!info.application.icon || !info.application.icon.splice) {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
const rulesMap = {
|
||||||
|
mdpi: 48,
|
||||||
|
hdpi: 72,
|
||||||
|
xhdpi: 96,
|
||||||
|
xxdpi: 144,
|
||||||
|
xxxhdpi: 192
|
||||||
|
}
|
||||||
|
const resultMap = {}
|
||||||
|
const maxDpiIcon = { dpi: 120, icon: '' }
|
||||||
|
|
||||||
|
for (const i in rulesMap) {
|
||||||
|
info.application.icon.some((icon) => {
|
||||||
|
if (icon && icon.indexOf(i) !== -1) {
|
||||||
|
resultMap['application-icon-' + rulesMap[i]] = icon
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// get the maximal size icon
|
||||||
|
if (
|
||||||
|
resultMap['application-icon-' + rulesMap[i]] &&
|
||||||
|
rulesMap[i] >= maxDpiIcon.dpi
|
||||||
|
) {
|
||||||
|
maxDpiIcon.dpi = rulesMap[i]
|
||||||
|
maxDpiIcon.icon = resultMap['application-icon-' + rulesMap[i]]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(resultMap).length === 0 || !maxDpiIcon.icon) {
|
||||||
|
maxDpiIcon.dpi = 120
|
||||||
|
maxDpiIcon.icon = info.application.icon[0] || ''
|
||||||
|
resultMap['applicataion-icon-120'] = maxDpiIcon.icon
|
||||||
|
}
|
||||||
|
return maxDpiIcon.icon
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* find .ipa file's icon path from json info
|
||||||
|
* @param info // json info parsed from .ipa file
|
||||||
|
*/
|
||||||
|
function findIpaIconPath (info) {
|
||||||
|
if (
|
||||||
|
info.CFBundleIcons &&
|
||||||
|
info.CFBundleIcons.CFBundlePrimaryIcon &&
|
||||||
|
info.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles &&
|
||||||
|
info.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles.length
|
||||||
|
) {
|
||||||
|
return info.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles[info.CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles.length - 1]
|
||||||
|
} else if (info.CFBundleIconFiles && info.CFBundleIconFiles.length) {
|
||||||
|
return info.CFBundleIconFiles[info.CFBundleIconFiles.length - 1]
|
||||||
|
} else {
|
||||||
|
return '.app/Icon.png'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* transform buffer to base64
|
||||||
|
* @param {Buffer} buffer
|
||||||
|
*/
|
||||||
|
function getBase64FromBuffer (buffer) {
|
||||||
|
return 'data:image/png;base64,' + buffer.toString('base64')
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 去除unicode空字符
|
||||||
|
* @param {String} str
|
||||||
|
*/
|
||||||
|
function decodeNullUnicode (str) {
|
||||||
|
if (typeof str === 'string') {
|
||||||
|
// eslint-disable-next-line
|
||||||
|
str = str.replace(/\u0000/g, '')
|
||||||
|
}
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
isArray,
|
||||||
|
isObject,
|
||||||
|
isPrimitive,
|
||||||
|
isBrowser,
|
||||||
|
mapInfoResource,
|
||||||
|
findApkIconPath,
|
||||||
|
findIpaIconPath,
|
||||||
|
getBase64FromBuffer,
|
||||||
|
decodeNullUnicode
|
||||||
|
}
|
674
src/utils/app-info-parser/xml-parser/binary.js
Normal file
674
src/utils/app-info-parser/xml-parser/binary.js
Normal file
@@ -0,0 +1,674 @@
|
|||||||
|
// From https://github.com/openstf/adbkit-apkreader
|
||||||
|
const NodeType = {
|
||||||
|
ELEMENT_NODE: 1,
|
||||||
|
ATTRIBUTE_NODE: 2,
|
||||||
|
CDATA_SECTION_NODE: 4
|
||||||
|
}
|
||||||
|
|
||||||
|
const ChunkType = {
|
||||||
|
NULL: 0x0000,
|
||||||
|
STRING_POOL: 0x0001,
|
||||||
|
TABLE: 0x0002,
|
||||||
|
XML: 0x0003,
|
||||||
|
XML_FIRST_CHUNK: 0x0100,
|
||||||
|
XML_START_NAMESPACE: 0x0100,
|
||||||
|
XML_END_NAMESPACE: 0x0101,
|
||||||
|
XML_START_ELEMENT: 0x0102,
|
||||||
|
XML_END_ELEMENT: 0x0103,
|
||||||
|
XML_CDATA: 0x0104,
|
||||||
|
XML_LAST_CHUNK: 0x017f,
|
||||||
|
XML_RESOURCE_MAP: 0x0180,
|
||||||
|
TABLE_PACKAGE: 0x0200,
|
||||||
|
TABLE_TYPE: 0x0201,
|
||||||
|
TABLE_TYPE_SPEC: 0x0202
|
||||||
|
}
|
||||||
|
|
||||||
|
const StringFlags = {
|
||||||
|
SORTED: 1 << 0,
|
||||||
|
UTF8: 1 << 8
|
||||||
|
}
|
||||||
|
|
||||||
|
// Taken from android.util.TypedValue
|
||||||
|
const TypedValue = {
|
||||||
|
COMPLEX_MANTISSA_MASK: 0x00ffffff,
|
||||||
|
COMPLEX_MANTISSA_SHIFT: 0x00000008,
|
||||||
|
COMPLEX_RADIX_0p23: 0x00000003,
|
||||||
|
COMPLEX_RADIX_16p7: 0x00000001,
|
||||||
|
COMPLEX_RADIX_23p0: 0x00000000,
|
||||||
|
COMPLEX_RADIX_8p15: 0x00000002,
|
||||||
|
COMPLEX_RADIX_MASK: 0x00000003,
|
||||||
|
COMPLEX_RADIX_SHIFT: 0x00000004,
|
||||||
|
COMPLEX_UNIT_DIP: 0x00000001,
|
||||||
|
COMPLEX_UNIT_FRACTION: 0x00000000,
|
||||||
|
COMPLEX_UNIT_FRACTION_PARENT: 0x00000001,
|
||||||
|
COMPLEX_UNIT_IN: 0x00000004,
|
||||||
|
COMPLEX_UNIT_MASK: 0x0000000f,
|
||||||
|
COMPLEX_UNIT_MM: 0x00000005,
|
||||||
|
COMPLEX_UNIT_PT: 0x00000003,
|
||||||
|
COMPLEX_UNIT_PX: 0x00000000,
|
||||||
|
COMPLEX_UNIT_SHIFT: 0x00000000,
|
||||||
|
COMPLEX_UNIT_SP: 0x00000002,
|
||||||
|
DENSITY_DEFAULT: 0x00000000,
|
||||||
|
DENSITY_NONE: 0x0000ffff,
|
||||||
|
TYPE_ATTRIBUTE: 0x00000002,
|
||||||
|
TYPE_DIMENSION: 0x00000005,
|
||||||
|
TYPE_FIRST_COLOR_INT: 0x0000001c,
|
||||||
|
TYPE_FIRST_INT: 0x00000010,
|
||||||
|
TYPE_FLOAT: 0x00000004,
|
||||||
|
TYPE_FRACTION: 0x00000006,
|
||||||
|
TYPE_INT_BOOLEAN: 0x00000012,
|
||||||
|
TYPE_INT_COLOR_ARGB4: 0x0000001e,
|
||||||
|
TYPE_INT_COLOR_ARGB8: 0x0000001c,
|
||||||
|
TYPE_INT_COLOR_RGB4: 0x0000001f,
|
||||||
|
TYPE_INT_COLOR_RGB8: 0x0000001d,
|
||||||
|
TYPE_INT_DEC: 0x00000010,
|
||||||
|
TYPE_INT_HEX: 0x00000011,
|
||||||
|
TYPE_LAST_COLOR_INT: 0x0000001f,
|
||||||
|
TYPE_LAST_INT: 0x0000001f,
|
||||||
|
TYPE_NULL: 0x00000000,
|
||||||
|
TYPE_REFERENCE: 0x00000001,
|
||||||
|
TYPE_STRING: 0x00000003
|
||||||
|
}
|
||||||
|
|
||||||
|
class BinaryXmlParser {
|
||||||
|
constructor (buffer, options = {}) {
|
||||||
|
this.buffer = buffer
|
||||||
|
this.cursor = 0
|
||||||
|
this.strings = []
|
||||||
|
this.resources = []
|
||||||
|
this.document = null
|
||||||
|
this.parent = null
|
||||||
|
this.stack = []
|
||||||
|
this.debug = options.debug || false
|
||||||
|
}
|
||||||
|
|
||||||
|
readU8 () {
|
||||||
|
this.debug && console.group('readU8')
|
||||||
|
this.debug && console.debug('cursor:', this.cursor)
|
||||||
|
const val = this.buffer[this.cursor]
|
||||||
|
this.debug && console.debug('value:', val)
|
||||||
|
this.cursor += 1
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
readU16 () {
|
||||||
|
this.debug && console.group('readU16')
|
||||||
|
this.debug && console.debug('cursor:', this.cursor)
|
||||||
|
const val = this.buffer.readUInt16LE(this.cursor)
|
||||||
|
this.debug && console.debug('value:', val)
|
||||||
|
this.cursor += 2
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
readS32 () {
|
||||||
|
this.debug && console.group('readS32')
|
||||||
|
this.debug && console.debug('cursor:', this.cursor)
|
||||||
|
const val = this.buffer.readInt32LE(this.cursor)
|
||||||
|
this.debug && console.debug('value:', val)
|
||||||
|
this.cursor += 4
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
readU32 () {
|
||||||
|
this.debug && console.group('readU32')
|
||||||
|
this.debug && console.debug('cursor:', this.cursor)
|
||||||
|
const val = this.buffer.readUInt32LE(this.cursor)
|
||||||
|
this.debug && console.debug('value:', val)
|
||||||
|
this.cursor += 4
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
readLength8 () {
|
||||||
|
this.debug && console.group('readLength8')
|
||||||
|
let len = this.readU8()
|
||||||
|
if (len & 0x80) {
|
||||||
|
len = (len & 0x7f) << 8
|
||||||
|
len += this.readU8()
|
||||||
|
}
|
||||||
|
this.debug && console.debug('length:', len)
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return len
|
||||||
|
}
|
||||||
|
|
||||||
|
readLength16 () {
|
||||||
|
this.debug && console.group('readLength16')
|
||||||
|
let len = this.readU16()
|
||||||
|
if (len & 0x8000) {
|
||||||
|
len = (len & 0x7fff) << 16
|
||||||
|
len += this.readU16()
|
||||||
|
}
|
||||||
|
this.debug && console.debug('length:', len)
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return len
|
||||||
|
}
|
||||||
|
|
||||||
|
readDimension () {
|
||||||
|
this.debug && console.group('readDimension')
|
||||||
|
|
||||||
|
const dimension = {
|
||||||
|
value: null,
|
||||||
|
unit: null,
|
||||||
|
rawUnit: null
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = this.readU32()
|
||||||
|
const unit = dimension.value & 0xff
|
||||||
|
|
||||||
|
dimension.value = value >> 8
|
||||||
|
dimension.rawUnit = unit
|
||||||
|
|
||||||
|
switch (unit) {
|
||||||
|
case TypedValue.COMPLEX_UNIT_MM:
|
||||||
|
dimension.unit = 'mm'
|
||||||
|
break
|
||||||
|
case TypedValue.COMPLEX_UNIT_PX:
|
||||||
|
dimension.unit = 'px'
|
||||||
|
break
|
||||||
|
case TypedValue.COMPLEX_UNIT_DIP:
|
||||||
|
dimension.unit = 'dp'
|
||||||
|
break
|
||||||
|
case TypedValue.COMPLEX_UNIT_SP:
|
||||||
|
dimension.unit = 'sp'
|
||||||
|
break
|
||||||
|
case TypedValue.COMPLEX_UNIT_PT:
|
||||||
|
dimension.unit = 'pt'
|
||||||
|
break
|
||||||
|
case TypedValue.COMPLEX_UNIT_IN:
|
||||||
|
dimension.unit = 'in'
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return dimension
|
||||||
|
}
|
||||||
|
|
||||||
|
readFraction () {
|
||||||
|
this.debug && console.group('readFraction')
|
||||||
|
|
||||||
|
const fraction = {
|
||||||
|
value: null,
|
||||||
|
type: null,
|
||||||
|
rawType: null
|
||||||
|
}
|
||||||
|
|
||||||
|
const value = this.readU32()
|
||||||
|
const type = value & 0xf
|
||||||
|
|
||||||
|
fraction.value = this.convertIntToFloat(value >> 4)
|
||||||
|
fraction.rawType = type
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case TypedValue.COMPLEX_UNIT_FRACTION:
|
||||||
|
fraction.type = '%'
|
||||||
|
break
|
||||||
|
case TypedValue.COMPLEX_UNIT_FRACTION_PARENT:
|
||||||
|
fraction.type = '%p'
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return fraction
|
||||||
|
}
|
||||||
|
|
||||||
|
readHex24 () {
|
||||||
|
this.debug && console.group('readHex24')
|
||||||
|
var val = (this.readU32() & 0xffffff).toString(16)
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
readHex32 () {
|
||||||
|
this.debug && console.group('readHex32')
|
||||||
|
var val = this.readU32().toString(16)
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
readTypedValue () {
|
||||||
|
this.debug && console.group('readTypedValue')
|
||||||
|
|
||||||
|
const typedValue = {
|
||||||
|
value: null,
|
||||||
|
type: null,
|
||||||
|
rawType: null
|
||||||
|
}
|
||||||
|
|
||||||
|
const start = this.cursor
|
||||||
|
|
||||||
|
let size = this.readU16()
|
||||||
|
/* const zero = */ this.readU8()
|
||||||
|
const dataType = this.readU8()
|
||||||
|
|
||||||
|
// Yes, there has been a real world APK where the size is malformed.
|
||||||
|
if (size === 0) {
|
||||||
|
size = 8
|
||||||
|
}
|
||||||
|
|
||||||
|
typedValue.rawType = dataType
|
||||||
|
|
||||||
|
switch (dataType) {
|
||||||
|
case TypedValue.TYPE_INT_DEC:
|
||||||
|
typedValue.value = this.readS32()
|
||||||
|
typedValue.type = 'int_dec'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_INT_HEX:
|
||||||
|
typedValue.value = this.readS32()
|
||||||
|
typedValue.type = 'int_hex'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_STRING:
|
||||||
|
var ref = this.readS32()
|
||||||
|
typedValue.value = ref > 0 ? this.strings[ref] : ''
|
||||||
|
typedValue.type = 'string'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_REFERENCE:
|
||||||
|
var id = this.readU32()
|
||||||
|
typedValue.value = `resourceId:0x${id.toString(16)}`
|
||||||
|
typedValue.type = 'reference'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_INT_BOOLEAN:
|
||||||
|
typedValue.value = this.readS32() !== 0
|
||||||
|
typedValue.type = 'boolean'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_NULL:
|
||||||
|
this.readU32()
|
||||||
|
typedValue.value = null
|
||||||
|
typedValue.type = 'null'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_INT_COLOR_RGB8:
|
||||||
|
typedValue.value = this.readHex24()
|
||||||
|
typedValue.type = 'rgb8'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_INT_COLOR_RGB4:
|
||||||
|
typedValue.value = this.readHex24()
|
||||||
|
typedValue.type = 'rgb4'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_INT_COLOR_ARGB8:
|
||||||
|
typedValue.value = this.readHex32()
|
||||||
|
typedValue.type = 'argb8'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_INT_COLOR_ARGB4:
|
||||||
|
typedValue.value = this.readHex32()
|
||||||
|
typedValue.type = 'argb4'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_DIMENSION:
|
||||||
|
typedValue.value = this.readDimension()
|
||||||
|
typedValue.type = 'dimension'
|
||||||
|
break
|
||||||
|
case TypedValue.TYPE_FRACTION:
|
||||||
|
typedValue.value = this.readFraction()
|
||||||
|
typedValue.type = 'fraction'
|
||||||
|
break
|
||||||
|
default: {
|
||||||
|
const type = dataType.toString(16)
|
||||||
|
console.debug(`Not sure what to do with typed value of type 0x${type}, falling back to reading an uint32.`)
|
||||||
|
typedValue.value = this.readU32()
|
||||||
|
typedValue.type = 'unknown'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we consume the whole value
|
||||||
|
const end = start + size
|
||||||
|
if (this.cursor !== end) {
|
||||||
|
const type = dataType.toString(16)
|
||||||
|
const diff = end - this.cursor
|
||||||
|
console.debug(`Cursor is off by ${diff} bytes at ${this.cursor} at supposed end \
|
||||||
|
of typed value of type 0x${type}. The typed value started at offset ${start} \
|
||||||
|
and is supposed to end at offset ${end}. Ignoring the rest of the value.`)
|
||||||
|
this.cursor = end
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return typedValue
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://twitter.com/kawasima/status/427730289201139712
|
||||||
|
convertIntToFloat (int) {
|
||||||
|
const buf = new ArrayBuffer(4)
|
||||||
|
;(new Int32Array(buf))[0] = int
|
||||||
|
return (new Float32Array(buf))[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
readString (encoding) {
|
||||||
|
this.debug && console.group('readString', encoding)
|
||||||
|
switch (encoding) {
|
||||||
|
case 'utf-8':
|
||||||
|
var stringLength = this.readLength8(encoding)
|
||||||
|
this.debug && console.debug('stringLength:', stringLength)
|
||||||
|
var byteLength = this.readLength8(encoding)
|
||||||
|
this.debug && console.debug('byteLength:', byteLength)
|
||||||
|
var value = this.buffer.toString(encoding, this.cursor, (this.cursor += byteLength))
|
||||||
|
this.debug && console.debug('value:', value)
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return value
|
||||||
|
case 'ucs2':
|
||||||
|
stringLength = this.readLength16(encoding)
|
||||||
|
this.debug && console.debug('stringLength:', stringLength)
|
||||||
|
byteLength = stringLength * 2
|
||||||
|
this.debug && console.debug('byteLength:', byteLength)
|
||||||
|
value = this.buffer.toString(encoding, this.cursor, (this.cursor += byteLength))
|
||||||
|
this.debug && console.debug('value:', value)
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return value
|
||||||
|
default:
|
||||||
|
throw new Error(`Unsupported encoding '${encoding}'`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
readChunkHeader () {
|
||||||
|
this.debug && console.group('readChunkHeader')
|
||||||
|
var header = {
|
||||||
|
startOffset: this.cursor,
|
||||||
|
chunkType: this.readU16(),
|
||||||
|
headerSize: this.readU16(),
|
||||||
|
chunkSize: this.readU32()
|
||||||
|
}
|
||||||
|
this.debug && console.debug('startOffset:', header.startOffset)
|
||||||
|
this.debug && console.debug('chunkType:', header.chunkType)
|
||||||
|
this.debug && console.debug('headerSize:', header.headerSize)
|
||||||
|
this.debug && console.debug('chunkSize:', header.chunkSize)
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return header
|
||||||
|
}
|
||||||
|
|
||||||
|
readStringPool (header) {
|
||||||
|
this.debug && console.group('readStringPool')
|
||||||
|
|
||||||
|
header.stringCount = this.readU32()
|
||||||
|
this.debug && console.debug('stringCount:', header.stringCount)
|
||||||
|
header.styleCount = this.readU32()
|
||||||
|
this.debug && console.debug('styleCount:', header.styleCount)
|
||||||
|
header.flags = this.readU32()
|
||||||
|
this.debug && console.debug('flags:', header.flags)
|
||||||
|
header.stringsStart = this.readU32()
|
||||||
|
this.debug && console.debug('stringsStart:', header.stringsStart)
|
||||||
|
header.stylesStart = this.readU32()
|
||||||
|
this.debug && console.debug('stylesStart:', header.stylesStart)
|
||||||
|
|
||||||
|
if (header.chunkType !== ChunkType.STRING_POOL) {
|
||||||
|
throw new Error('Invalid string pool header')
|
||||||
|
}
|
||||||
|
|
||||||
|
const offsets = []
|
||||||
|
for (let i = 0, l = header.stringCount; i < l; ++i) {
|
||||||
|
this.debug && console.debug('offset:', i)
|
||||||
|
offsets.push(this.readU32())
|
||||||
|
}
|
||||||
|
|
||||||
|
const sorted = (header.flags & StringFlags.SORTED) === StringFlags.SORTED
|
||||||
|
this.debug && console.debug('sorted:', sorted)
|
||||||
|
const encoding = (header.flags & StringFlags.UTF8) === StringFlags.UTF8
|
||||||
|
? 'utf-8'
|
||||||
|
: 'ucs2'
|
||||||
|
this.debug && console.debug('encoding:', encoding)
|
||||||
|
|
||||||
|
const stringsStart = header.startOffset + header.stringsStart
|
||||||
|
this.cursor = stringsStart
|
||||||
|
for (let i = 0, l = header.stringCount; i < l; ++i) {
|
||||||
|
this.debug && console.debug('string:', i)
|
||||||
|
this.debug && console.debug('offset:', offsets[i])
|
||||||
|
this.cursor = stringsStart + offsets[i]
|
||||||
|
this.strings.push(this.readString(encoding))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip styles
|
||||||
|
this.cursor = header.startOffset + header.chunkSize
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
readResourceMap (header) {
|
||||||
|
this.debug && console.group('readResourceMap')
|
||||||
|
const count = Math.floor((header.chunkSize - header.headerSize) / 4)
|
||||||
|
for (let i = 0; i < count; ++i) {
|
||||||
|
this.resources.push(this.readU32())
|
||||||
|
}
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
readXmlNamespaceStart (/* header */) {
|
||||||
|
this.debug && console.group('readXmlNamespaceStart')
|
||||||
|
|
||||||
|
/* const line = */ this.readU32()
|
||||||
|
/* const commentRef = */ this.readU32()
|
||||||
|
/* const prefixRef = */ this.readS32()
|
||||||
|
/* const uriRef = */ this.readS32()
|
||||||
|
|
||||||
|
// We don't currently care about the values, but they could
|
||||||
|
// be accessed like so:
|
||||||
|
//
|
||||||
|
// namespaceURI.prefix = this.strings[prefixRef] // if prefixRef > 0
|
||||||
|
// namespaceURI.uri = this.strings[uriRef] // if uriRef > 0
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
readXmlNamespaceEnd (/* header */) {
|
||||||
|
this.debug && console.group('readXmlNamespaceEnd')
|
||||||
|
|
||||||
|
/* const line = */ this.readU32()
|
||||||
|
/* const commentRef = */ this.readU32()
|
||||||
|
/* const prefixRef = */ this.readS32()
|
||||||
|
/* const uriRef = */ this.readS32()
|
||||||
|
|
||||||
|
// We don't currently care about the values, but they could
|
||||||
|
// be accessed like so:
|
||||||
|
//
|
||||||
|
// namespaceURI.prefix = this.strings[prefixRef] // if prefixRef > 0
|
||||||
|
// namespaceURI.uri = this.strings[uriRef] // if uriRef > 0
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
readXmlElementStart (/* header */) {
|
||||||
|
this.debug && console.group('readXmlElementStart')
|
||||||
|
|
||||||
|
const node = {
|
||||||
|
namespaceURI: null,
|
||||||
|
nodeType: NodeType.ELEMENT_NODE,
|
||||||
|
nodeName: null,
|
||||||
|
attributes: [],
|
||||||
|
childNodes: []
|
||||||
|
}
|
||||||
|
|
||||||
|
/* const line = */ this.readU32()
|
||||||
|
/* const commentRef = */ this.readU32()
|
||||||
|
const nsRef = this.readS32()
|
||||||
|
const nameRef = this.readS32()
|
||||||
|
|
||||||
|
if (nsRef > 0) {
|
||||||
|
node.namespaceURI = this.strings[nsRef]
|
||||||
|
}
|
||||||
|
|
||||||
|
node.nodeName = this.strings[nameRef]
|
||||||
|
|
||||||
|
/* const attrStart = */ this.readU16()
|
||||||
|
/* const attrSize = */ this.readU16()
|
||||||
|
const attrCount = this.readU16()
|
||||||
|
/* const idIndex = */ this.readU16()
|
||||||
|
/* const classIndex = */ this.readU16()
|
||||||
|
/* const styleIndex = */ this.readU16()
|
||||||
|
|
||||||
|
for (let i = 0; i < attrCount; ++i) {
|
||||||
|
node.attributes.push(this.readXmlAttribute())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.document) {
|
||||||
|
this.parent.childNodes.push(node)
|
||||||
|
this.parent = node
|
||||||
|
} else {
|
||||||
|
this.document = (this.parent = node)
|
||||||
|
}
|
||||||
|
|
||||||
|
this.stack.push(node)
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
readXmlAttribute () {
|
||||||
|
this.debug && console.group('readXmlAttribute')
|
||||||
|
|
||||||
|
const attr = {
|
||||||
|
namespaceURI: null,
|
||||||
|
nodeType: NodeType.ATTRIBUTE_NODE,
|
||||||
|
nodeName: null,
|
||||||
|
name: null,
|
||||||
|
value: null,
|
||||||
|
typedValue: null
|
||||||
|
}
|
||||||
|
|
||||||
|
const nsRef = this.readS32()
|
||||||
|
const nameRef = this.readS32()
|
||||||
|
const valueRef = this.readS32()
|
||||||
|
|
||||||
|
if (nsRef > 0) {
|
||||||
|
attr.namespaceURI = this.strings[nsRef]
|
||||||
|
}
|
||||||
|
|
||||||
|
attr.nodeName = attr.name = this.strings[nameRef]
|
||||||
|
|
||||||
|
if (valueRef > 0) {
|
||||||
|
// some apk have versionName with special characters
|
||||||
|
if (attr.name === 'versionName') {
|
||||||
|
// only keep printable characters
|
||||||
|
// https://www.ascii-code.com/characters/printable-characters
|
||||||
|
this.strings[valueRef] = this.strings[valueRef].replace(/[^\x21-\x7E]/g, '')
|
||||||
|
}
|
||||||
|
attr.value = this.strings[valueRef]
|
||||||
|
}
|
||||||
|
|
||||||
|
attr.typedValue = this.readTypedValue()
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return attr
|
||||||
|
}
|
||||||
|
|
||||||
|
readXmlElementEnd (/* header */) {
|
||||||
|
this.debug && console.group('readXmlCData')
|
||||||
|
|
||||||
|
/* const line = */ this.readU32()
|
||||||
|
/* const commentRef = */ this.readU32()
|
||||||
|
/* const nsRef = */ this.readS32()
|
||||||
|
/* const nameRef = */ this.readS32()
|
||||||
|
|
||||||
|
this.stack.pop()
|
||||||
|
this.parent = this.stack[this.stack.length - 1]
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
readXmlCData (/* header */) {
|
||||||
|
this.debug && console.group('readXmlCData')
|
||||||
|
|
||||||
|
const cdata = {
|
||||||
|
namespaceURI: null,
|
||||||
|
nodeType: NodeType.CDATA_SECTION_NODE,
|
||||||
|
nodeName: '#cdata',
|
||||||
|
data: null,
|
||||||
|
typedValue: null
|
||||||
|
}
|
||||||
|
|
||||||
|
/* const line = */ this.readU32()
|
||||||
|
/* const commentRef = */ this.readU32()
|
||||||
|
const dataRef = this.readS32()
|
||||||
|
|
||||||
|
if (dataRef > 0) {
|
||||||
|
cdata.data = this.strings[dataRef]
|
||||||
|
}
|
||||||
|
|
||||||
|
cdata.typedValue = this.readTypedValue()
|
||||||
|
|
||||||
|
this.parent.childNodes.push(cdata)
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return cdata
|
||||||
|
}
|
||||||
|
|
||||||
|
readNull (header) {
|
||||||
|
this.debug && console.group('readNull')
|
||||||
|
this.cursor += header.chunkSize - header.headerSize
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
parse () {
|
||||||
|
this.debug && console.group('BinaryXmlParser.parse')
|
||||||
|
|
||||||
|
const xmlHeader = this.readChunkHeader()
|
||||||
|
if (xmlHeader.chunkType !== ChunkType.XML) {
|
||||||
|
throw new Error('Invalid XML header')
|
||||||
|
}
|
||||||
|
|
||||||
|
while (this.cursor < this.buffer.length) {
|
||||||
|
this.debug && console.group('chunk')
|
||||||
|
const start = this.cursor
|
||||||
|
const header = this.readChunkHeader()
|
||||||
|
switch (header.chunkType) {
|
||||||
|
case ChunkType.STRING_POOL:
|
||||||
|
this.readStringPool(header)
|
||||||
|
break
|
||||||
|
case ChunkType.XML_RESOURCE_MAP:
|
||||||
|
this.readResourceMap(header)
|
||||||
|
break
|
||||||
|
case ChunkType.XML_START_NAMESPACE:
|
||||||
|
this.readXmlNamespaceStart(header)
|
||||||
|
break
|
||||||
|
case ChunkType.XML_END_NAMESPACE:
|
||||||
|
this.readXmlNamespaceEnd(header)
|
||||||
|
break
|
||||||
|
case ChunkType.XML_START_ELEMENT:
|
||||||
|
this.readXmlElementStart(header)
|
||||||
|
break
|
||||||
|
case ChunkType.XML_END_ELEMENT:
|
||||||
|
this.readXmlElementEnd(header)
|
||||||
|
break
|
||||||
|
case ChunkType.XML_CDATA:
|
||||||
|
this.readXmlCData(header)
|
||||||
|
break
|
||||||
|
case ChunkType.NULL:
|
||||||
|
this.readNull(header)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
throw new Error(`Unsupported chunk type '${header.chunkType}'`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we consume the whole chunk
|
||||||
|
const end = start + header.chunkSize
|
||||||
|
if (this.cursor !== end) {
|
||||||
|
const diff = end - this.cursor
|
||||||
|
const type = header.chunkType.toString(16)
|
||||||
|
console.debug(`Cursor is off by ${diff} bytes at ${this.cursor} at supposed \
|
||||||
|
end of chunk of type 0x${type}. The chunk started at offset ${start} and is \
|
||||||
|
supposed to end at offset ${end}. Ignoring the rest of the chunk.`)
|
||||||
|
this.cursor = end
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug && console.groupEnd()
|
||||||
|
|
||||||
|
return this.document
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = BinaryXmlParser
|
216
src/utils/app-info-parser/xml-parser/manifest.js
Normal file
216
src/utils/app-info-parser/xml-parser/manifest.js
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
// From https://github.com/openstf/adbkit-apkreader
|
||||||
|
const BinaryXmlParser = require('./binary')
|
||||||
|
|
||||||
|
const INTENT_MAIN = 'android.intent.action.MAIN'
|
||||||
|
const CATEGORY_LAUNCHER = 'android.intent.category.LAUNCHER'
|
||||||
|
|
||||||
|
class ManifestParser {
|
||||||
|
constructor (buffer, options = {}) {
|
||||||
|
this.buffer = buffer
|
||||||
|
this.xmlParser = new BinaryXmlParser(this.buffer, options)
|
||||||
|
}
|
||||||
|
|
||||||
|
collapseAttributes (element) {
|
||||||
|
const collapsed = Object.create(null)
|
||||||
|
for (let attr of Array.from(element.attributes)) {
|
||||||
|
collapsed[attr.name] = attr.typedValue.value
|
||||||
|
}
|
||||||
|
return collapsed
|
||||||
|
}
|
||||||
|
|
||||||
|
parseIntents (element, target) {
|
||||||
|
target.intentFilters = []
|
||||||
|
target.metaData = []
|
||||||
|
|
||||||
|
return element.childNodes.forEach(element => {
|
||||||
|
switch (element.nodeName) {
|
||||||
|
case 'intent-filter': {
|
||||||
|
const intentFilter = this.collapseAttributes(element)
|
||||||
|
|
||||||
|
intentFilter.actions = []
|
||||||
|
intentFilter.categories = []
|
||||||
|
intentFilter.data = []
|
||||||
|
|
||||||
|
element.childNodes.forEach(element => {
|
||||||
|
switch (element.nodeName) {
|
||||||
|
case 'action':
|
||||||
|
intentFilter.actions.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'category':
|
||||||
|
intentFilter.categories.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'data':
|
||||||
|
intentFilter.data.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
target.intentFilters.push(intentFilter)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'meta-data':
|
||||||
|
target.metaData.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
parseApplication (element) {
|
||||||
|
const app = this.collapseAttributes(element)
|
||||||
|
|
||||||
|
app.activities = []
|
||||||
|
app.activityAliases = []
|
||||||
|
app.launcherActivities = []
|
||||||
|
app.services = []
|
||||||
|
app.receivers = []
|
||||||
|
app.providers = []
|
||||||
|
app.usesLibraries = []
|
||||||
|
app.metaData = []
|
||||||
|
|
||||||
|
element.childNodes.forEach(element => {
|
||||||
|
switch (element.nodeName) {
|
||||||
|
case 'activity': {
|
||||||
|
const activity = this.collapseAttributes(element)
|
||||||
|
this.parseIntents(element, activity)
|
||||||
|
app.activities.push(activity)
|
||||||
|
if (this.isLauncherActivity(activity)) {
|
||||||
|
app.launcherActivities.push(activity)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'activity-alias': {
|
||||||
|
const activityAlias = this.collapseAttributes(element)
|
||||||
|
this.parseIntents(element, activityAlias)
|
||||||
|
app.activityAliases.push(activityAlias)
|
||||||
|
if (this.isLauncherActivity(activityAlias)) {
|
||||||
|
app.launcherActivities.push(activityAlias)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'service': {
|
||||||
|
const service = this.collapseAttributes(element)
|
||||||
|
this.parseIntents(element, service)
|
||||||
|
app.services.push(service)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'receiver': {
|
||||||
|
const receiver = this.collapseAttributes(element)
|
||||||
|
this.parseIntents(element, receiver)
|
||||||
|
app.receivers.push(receiver)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'provider': {
|
||||||
|
const provider = this.collapseAttributes(element)
|
||||||
|
|
||||||
|
provider.grantUriPermissions = []
|
||||||
|
provider.metaData = []
|
||||||
|
provider.pathPermissions = []
|
||||||
|
|
||||||
|
element.childNodes.forEach(element => {
|
||||||
|
switch (element.nodeName) {
|
||||||
|
case 'grant-uri-permission':
|
||||||
|
provider.grantUriPermissions.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'meta-data':
|
||||||
|
provider.metaData.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'path-permission':
|
||||||
|
provider.pathPermissions.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
app.providers.push(provider)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case 'uses-library':
|
||||||
|
app.usesLibraries.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'meta-data':
|
||||||
|
app.metaData.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return app
|
||||||
|
}
|
||||||
|
|
||||||
|
isLauncherActivity (activity) {
|
||||||
|
return activity.intentFilters.some(function (filter) {
|
||||||
|
const hasMain = filter.actions.some(action => action.name === INTENT_MAIN)
|
||||||
|
if (!hasMain) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return filter.categories.some(category => category.name === CATEGORY_LAUNCHER)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
parse () {
|
||||||
|
const document = this.xmlParser.parse()
|
||||||
|
const manifest = this.collapseAttributes(document)
|
||||||
|
|
||||||
|
manifest.usesPermissions = []
|
||||||
|
manifest.usesPermissionsSDK23 = []
|
||||||
|
manifest.permissions = []
|
||||||
|
manifest.permissionTrees = []
|
||||||
|
manifest.permissionGroups = []
|
||||||
|
manifest.instrumentation = null
|
||||||
|
manifest.usesSdk = null
|
||||||
|
manifest.usesConfiguration = null
|
||||||
|
manifest.usesFeatures = []
|
||||||
|
manifest.supportsScreens = null
|
||||||
|
manifest.compatibleScreens = []
|
||||||
|
manifest.supportsGlTextures = []
|
||||||
|
manifest.application = Object.create(null)
|
||||||
|
|
||||||
|
document.childNodes.forEach(element => {
|
||||||
|
switch (element.nodeName) {
|
||||||
|
case 'uses-permission':
|
||||||
|
manifest.usesPermissions.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'uses-permission-sdk-23':
|
||||||
|
manifest.usesPermissionsSDK23.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'permission':
|
||||||
|
manifest.permissions.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'permission-tree':
|
||||||
|
manifest.permissionTrees.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'permission-group':
|
||||||
|
manifest.permissionGroups.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'instrumentation':
|
||||||
|
manifest.instrumentation = this.collapseAttributes(element)
|
||||||
|
break
|
||||||
|
case 'uses-sdk':
|
||||||
|
manifest.usesSdk = this.collapseAttributes(element)
|
||||||
|
break
|
||||||
|
case 'uses-configuration':
|
||||||
|
manifest.usesConfiguration = this.collapseAttributes(element)
|
||||||
|
break
|
||||||
|
case 'uses-feature':
|
||||||
|
manifest.usesFeatures.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'supports-screens':
|
||||||
|
manifest.supportsScreens = this.collapseAttributes(element)
|
||||||
|
break
|
||||||
|
case 'compatible-screens':
|
||||||
|
element.childNodes.forEach(screen => {
|
||||||
|
return manifest.compatibleScreens.push(this.collapseAttributes(screen))
|
||||||
|
})
|
||||||
|
break
|
||||||
|
case 'supports-gl-texture':
|
||||||
|
manifest.supportsGlTextures.push(this.collapseAttributes(element))
|
||||||
|
break
|
||||||
|
case 'application':
|
||||||
|
manifest.application = this.parseApplication(element)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return manifest
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ManifestParser
|
48
src/utils/app-info-parser/zip.js
Normal file
48
src/utils/app-info-parser/zip.js
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
const Unzip = require('isomorphic-unzip')
|
||||||
|
const { isBrowser, decodeNullUnicode } = require('./utils')
|
||||||
|
|
||||||
|
class Zip {
|
||||||
|
constructor (file) {
|
||||||
|
if (isBrowser()) {
|
||||||
|
if (!(file instanceof window.Blob || typeof file.size !== 'undefined')) {
|
||||||
|
throw new Error('Param error: [file] must be an instance of Blob or File in browser.')
|
||||||
|
}
|
||||||
|
this.file = file
|
||||||
|
} else {
|
||||||
|
if (typeof file !== 'string') {
|
||||||
|
throw new Error('Param error: [file] must be file path in Node.')
|
||||||
|
}
|
||||||
|
this.file = require('path').resolve(file)
|
||||||
|
}
|
||||||
|
this.unzip = new Unzip(this.file)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get entries by regexps, the return format is: { <filename>: <Buffer|Blob> }
|
||||||
|
* @param {Array} regexps // regexps for matching files
|
||||||
|
* @param {String} type // return type, can be buffer or blob, default buffer
|
||||||
|
*/
|
||||||
|
getEntries (regexps, type = 'buffer') {
|
||||||
|
regexps = regexps.map(regex => decodeNullUnicode(regex))
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.unzip.getBuffer(regexps, { type }, (err, buffers) => {
|
||||||
|
err ? reject(err) : resolve(buffers)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* get entry by regex, return an instance of Buffer or Blob
|
||||||
|
* @param {Regex} regex // regex for matching file
|
||||||
|
* @param {String} type // return type, can be buffer or blob, default buffer
|
||||||
|
*/
|
||||||
|
getEntry (regex, type = 'buffer') {
|
||||||
|
regex = decodeNullUnicode(regex)
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.unzip.getBuffer([regex], { type }, (err, buffers) => {
|
||||||
|
err ? reject(err) : resolve(buffers[regex])
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Zip
|
@@ -1,28 +1,21 @@
|
|||||||
/**
|
import fs from 'fs-extra';
|
||||||
* Created by tdzl2003 on 2/13/16.
|
import os from 'node:os';
|
||||||
*/
|
import path from 'node:path';
|
||||||
|
import pkg from '../../package.json';
|
||||||
|
import AppInfoParser from './app-info-parser';
|
||||||
|
import semverSatisfies from 'semver/functions/satisfies';
|
||||||
|
|
||||||
import * as fs from 'fs-extra';
|
import { read } from 'read';
|
||||||
import os from 'os';
|
|
||||||
import path from 'path';
|
|
||||||
const AppInfoParser = require('app-info-parser');
|
|
||||||
|
|
||||||
var read = require('read');
|
export async function question(query, password) {
|
||||||
|
|
||||||
export function question(query, password) {
|
|
||||||
if (NO_INTERACTIVE) {
|
if (NO_INTERACTIVE) {
|
||||||
return Promise.resolve('');
|
return '';
|
||||||
}
|
}
|
||||||
return new Promise((resolve, reject) =>
|
return read({
|
||||||
read(
|
prompt: query,
|
||||||
{
|
silent: password,
|
||||||
prompt: query,
|
replace: password ? '*' : undefined,
|
||||||
silent: password,
|
});
|
||||||
replace: password ? '*' : undefined,
|
|
||||||
},
|
|
||||||
(err, result) => (err ? reject(err) : resolve(result)),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function translateOptions(options) {
|
export function translateOptions(options) {
|
||||||
@@ -30,7 +23,7 @@ export function translateOptions(options) {
|
|||||||
for (let key in options) {
|
for (let key in options) {
|
||||||
const v = options[key];
|
const v = options[key];
|
||||||
if (typeof v === 'string') {
|
if (typeof v === 'string') {
|
||||||
ret[key] = v.replace(/\$\{(\w+)\}/g, function(v, n) {
|
ret[key] = v.replace(/\$\{(\w+)\}/g, function (v, n) {
|
||||||
return options[n] || process.env[n] || v;
|
return options[n] || process.env[n] || v;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
@@ -42,7 +35,11 @@ export function translateOptions(options) {
|
|||||||
|
|
||||||
export function getRNVersion() {
|
export function getRNVersion() {
|
||||||
const version = JSON.parse(
|
const version = JSON.parse(
|
||||||
fs.readFileSync(path.resolve('node_modules/react-native/package.json')),
|
fs.readFileSync(
|
||||||
|
require.resolve('react-native/package.json', {
|
||||||
|
paths: [process.cwd()],
|
||||||
|
}),
|
||||||
|
),
|
||||||
).version;
|
).version;
|
||||||
|
|
||||||
// We only care about major and minor version.
|
// We only care about major and minor version.
|
||||||
@@ -56,6 +53,21 @@ export function getRNVersion() {
|
|||||||
|
|
||||||
export async function getApkInfo(fn) {
|
export async function getApkInfo(fn) {
|
||||||
const appInfoParser = new AppInfoParser(fn);
|
const appInfoParser = new AppInfoParser(fn);
|
||||||
|
const bundleFile = await appInfoParser.parser.getEntry(
|
||||||
|
/assets\/index.android.bundle/,
|
||||||
|
);
|
||||||
|
if (!bundleFile) {
|
||||||
|
throw new Error(
|
||||||
|
'找不到bundle文件。请确保此apk为release版本,且bundle文件名为默认的index.android.bundle',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const updateJsonFile = await appInfoParser.parser.getEntry(
|
||||||
|
/res\/raw\/update.json/,
|
||||||
|
);
|
||||||
|
let appCredential = {};
|
||||||
|
if (updateJsonFile) {
|
||||||
|
appCredential = JSON.parse(updateJsonFile.toString()).android;
|
||||||
|
}
|
||||||
const { versionName, application } = await appInfoParser.parse();
|
const { versionName, application } = await appInfoParser.parse();
|
||||||
let buildTime = 0;
|
let buildTime = 0;
|
||||||
if (Array.isArray(application.metaData)) {
|
if (Array.isArray(application.metaData)) {
|
||||||
@@ -66,16 +78,32 @@ export async function getApkInfo(fn) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (buildTime == 0) {
|
if (buildTime == 0) {
|
||||||
throw new Error('无法获取此包的编译时间戳。请更新react-native-update到最新版本后重新打包上传。');
|
throw new Error(
|
||||||
|
'无法获取此包的编译时间戳。请更新 react-native-update 到最新版本后重新打包上传。',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return { versionName, buildTime };
|
return { versionName, buildTime, ...appCredential };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getIpaInfo(fn) {
|
export async function getIpaInfo(fn) {
|
||||||
const appInfoParser = new AppInfoParser(fn);
|
const appInfoParser = new AppInfoParser(fn);
|
||||||
const {
|
const bundleFile = await appInfoParser.parser.getEntry(
|
||||||
CFBundleShortVersionString: versionName,
|
/payload\/.+?\.app\/main.jsbundle/,
|
||||||
} = await appInfoParser.parse();
|
);
|
||||||
|
if (!bundleFile) {
|
||||||
|
throw new Error(
|
||||||
|
'找不到bundle文件。请确保此ipa为release版本,且bundle文件名为默认的main.jsbundle',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const updateJsonFile = await appInfoParser.parser.getEntry(
|
||||||
|
/payload\/.+?\.app\/assets\/update.json/,
|
||||||
|
);
|
||||||
|
let appCredential = {};
|
||||||
|
if (updateJsonFile) {
|
||||||
|
appCredential = JSON.parse(updateJsonFile.toString()).ios;
|
||||||
|
}
|
||||||
|
const { CFBundleShortVersionString: versionName } =
|
||||||
|
await appInfoParser.parse();
|
||||||
let buildTimeTxtBuffer = await appInfoParser.parser.getEntry(
|
let buildTimeTxtBuffer = await appInfoParser.parser.getEntry(
|
||||||
/payload\/.+?\.app\/pushy_build_time.txt/,
|
/payload\/.+?\.app\/pushy_build_time.txt/,
|
||||||
);
|
);
|
||||||
@@ -86,10 +114,12 @@ export async function getIpaInfo(fn) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (!buildTimeTxtBuffer) {
|
if (!buildTimeTxtBuffer) {
|
||||||
throw new Error('无法获取此包的编译时间戳。请更新react-native-update到最新版本后重新打包上传。');
|
throw new Error(
|
||||||
|
'无法获取此包的编译时间戳。请更新 react-native-update 到最新版本后重新打包上传。',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
const buildTime = buildTimeTxtBuffer.toString().replace('\n', '');
|
const buildTime = buildTimeTxtBuffer.toString().replace('\n', '');
|
||||||
return { versionName, buildTime };
|
return { versionName, buildTime, ...appCredential };
|
||||||
}
|
}
|
||||||
|
|
||||||
const localDir = path.resolve(os.homedir(), '.pushy');
|
const localDir = path.resolve(os.homedir(), '.pushy');
|
||||||
@@ -100,3 +130,35 @@ export function saveToLocal(originPath, destName) {
|
|||||||
// fs.ensureDirSync(path.dirname(destPath));
|
// fs.ensureDirSync(path.dirname(destPath));
|
||||||
// fs.copyFileSync(originPath, destPath);
|
// fs.copyFileSync(originPath, destPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function printVersionCommand() {
|
||||||
|
console.log('react-native-update-cli: ' + pkg.version);
|
||||||
|
let pushyVersion = '';
|
||||||
|
try {
|
||||||
|
const PACKAGE_JSON_PATH = require.resolve(
|
||||||
|
'react-native-update/package.json',
|
||||||
|
{
|
||||||
|
paths: [process.cwd()],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
pushyVersion = require(PACKAGE_JSON_PATH).version;
|
||||||
|
console.log('react-native-update: ' + pushyVersion);
|
||||||
|
} catch (e) {
|
||||||
|
console.log('react-native-update: 无法获取版本号,请在项目目录中运行命令');
|
||||||
|
}
|
||||||
|
if (pushyVersion) {
|
||||||
|
if (semverSatisfies(pushyVersion, '<8.5.1')) {
|
||||||
|
console.warn(
|
||||||
|
`当前版本已不再支持,请至少升级到 v8 的最新小版本后重新打包(代码无需改动): npm i react-native-update@8 .
|
||||||
|
如有使用安装 apk 的功能,请注意添加所需权限 https://pushy.reactnative.cn/docs/api#async-function-downloadandinstallapkurl`,
|
||||||
|
);
|
||||||
|
} else if (semverSatisfies(pushyVersion, '9.0.0 - 9.2.0')) {
|
||||||
|
console.warn(
|
||||||
|
`当前版本已不再支持,请至少升级到 v9 的最新小版本后重新打包(代码无需改动): npm i react-native-update@9 .
|
||||||
|
如有使用安装 apk 的功能,请注意添加所需权限 https://pushy.reactnative.cn/docs/api#async-function-downloadandinstallapkurl`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const pricingPageUrl = 'https://pushy.reactnative.cn/pricing.html';
|
||||||
|
241
src/versions.js
241
src/versions.js
@@ -1,33 +1,32 @@
|
|||||||
/**
|
import { get, post, put, uploadFile } from './api';
|
||||||
* Created by tdzl2003 on 4/2/16.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const {
|
|
||||||
get,
|
|
||||||
post,
|
|
||||||
put,
|
|
||||||
uploadFile,
|
|
||||||
} = require('./api');
|
|
||||||
import { question, saveToLocal } from './utils';
|
import { question, saveToLocal } from './utils';
|
||||||
|
|
||||||
import { checkPlatform, getSelectedApp } from './app';
|
import { checkPlatform, getSelectedApp } from './app';
|
||||||
import { choosePackage } from './package';
|
import { choosePackage } from './package';
|
||||||
|
import { compare } from 'compare-versions';
|
||||||
|
|
||||||
async function showVersion(appId, offset) {
|
async function showVersion(appId, offset) {
|
||||||
const { data, count } = await get(`/app/${appId}/version/list`);
|
const { data, count } = await get(`/app/${appId}/version/list`);
|
||||||
console.log(`Offset ${offset}`);
|
console.log(`Offset ${offset}`);
|
||||||
for (const version of data) {
|
for (const version of data) {
|
||||||
let packageInfo = version.packages.slice(0, 3).map(v=>v.name).join(', ');
|
let packageInfo = version.packages
|
||||||
|
.slice(0, 3)
|
||||||
|
.map((v) => v.name)
|
||||||
|
.join(', ');
|
||||||
const count = version.packages.length;
|
const count = version.packages.length;
|
||||||
if (count > 3) {
|
if (count > 3) {
|
||||||
packageInfo += `...and ${count-3} more`;
|
packageInfo += `...and ${count - 3} more`;
|
||||||
}
|
}
|
||||||
if (count === 0) {
|
if (count === 0) {
|
||||||
packageInfo = `(no package)`;
|
packageInfo = `(no package)`;
|
||||||
} else {
|
} else {
|
||||||
packageInfo = `[${packageInfo}]`;
|
packageInfo = `[${packageInfo}]`;
|
||||||
}
|
}
|
||||||
console.log(`${version.id}) ${version.hash.slice(0, 8)} ${version.name} ${packageInfo}`);
|
console.log(
|
||||||
|
`${version.id}) ${version.hash.slice(0, 8)} ${
|
||||||
|
version.name
|
||||||
|
} ${packageInfo}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@@ -38,10 +37,17 @@ async function listVersions(appId) {
|
|||||||
await showVersion(appId, offset);
|
await showVersion(appId, offset);
|
||||||
const cmd = await question('page Up/page Down/Begin/Quit(U/D/B/Q)');
|
const cmd = await question('page Up/page Down/Begin/Quit(U/D/B/Q)');
|
||||||
switch (cmd.toLowerCase()) {
|
switch (cmd.toLowerCase()) {
|
||||||
case 'u': offset = Math.max(0, offset - 10); break;
|
case 'u':
|
||||||
case 'd': offset += 10; break;
|
offset = Math.max(0, offset - 10);
|
||||||
case 'b': offset = 0; break;
|
break;
|
||||||
case 'q': return;
|
case 'd':
|
||||||
|
offset += 10;
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
offset = 0;
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,14 +56,21 @@ async function chooseVersion(appId) {
|
|||||||
let offset = 0;
|
let offset = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
const data = await showVersion(appId, offset);
|
const data = await showVersion(appId, offset);
|
||||||
const cmd = await question('Enter versionId or page Up/page Down/Begin(U/D/B)');
|
const cmd = await question(
|
||||||
|
'Enter versionId or page Up/page Down/Begin(U/D/B)',
|
||||||
|
);
|
||||||
switch (cmd.toLowerCase()) {
|
switch (cmd.toLowerCase()) {
|
||||||
case 'U': offset = Math.max(0, offset - 10); break;
|
case 'U':
|
||||||
case 'D': offset += 10; break;
|
offset = Math.max(0, offset - 10);
|
||||||
case 'B': offset = 0; break;
|
break;
|
||||||
default:
|
case 'D':
|
||||||
{
|
offset += 10;
|
||||||
const v = data.find(v=>v.id === (cmd | 0));
|
break;
|
||||||
|
case 'B':
|
||||||
|
offset = 0;
|
||||||
|
break;
|
||||||
|
default: {
|
||||||
|
const v = data.find((v) => v.id === (cmd | 0));
|
||||||
if (v) {
|
if (v) {
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
@@ -67,47 +80,191 @@ async function chooseVersion(appId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const commands = {
|
export const commands = {
|
||||||
publish: async function({args, options}) {
|
publish: async function ({ args, options }) {
|
||||||
const fn = args[0];
|
const fn = args[0];
|
||||||
const {name, description, metaInfo } = options;
|
const { name, description, metaInfo } = options;
|
||||||
|
|
||||||
if (!fn || !fn.endsWith('.ppk')) {
|
if (!fn || !fn.endsWith('.ppk')) {
|
||||||
throw new Error('Usage: pushy publish <ppkFile> --platform ios|android');
|
throw new Error(
|
||||||
|
'使用方法: pushy publish ppk后缀文件 --platform ios|android',
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const platform = checkPlatform(options.platform || await question('Platform(ios/android):'));
|
const platform = checkPlatform(
|
||||||
|
options.platform || (await question('平台(ios/android):')),
|
||||||
|
);
|
||||||
const { appId } = await getSelectedApp(platform);
|
const { appId } = await getSelectedApp(platform);
|
||||||
|
|
||||||
const { hash } = await uploadFile(fn);
|
const { hash } = await uploadFile(fn);
|
||||||
|
|
||||||
const { id } = await post(`/app/${appId}/version/create`, {
|
const { id } = await post(`/app/${appId}/version/create`, {
|
||||||
name: name || await question('Enter version name:') || '(未命名)',
|
name: name || (await question('输入版本名称: ')) || '(未命名)',
|
||||||
hash,
|
hash,
|
||||||
description: description || await question('Enter description:'),
|
description: description || (await question('输入版本描述:')),
|
||||||
metaInfo: metaInfo || await question('Enter meta info:'),
|
metaInfo: metaInfo || (await question('输入自定义的 meta info:')),
|
||||||
});
|
});
|
||||||
// TODO local diff
|
// TODO local diff
|
||||||
saveToLocal(fn, `${appId}/ppk/${id}.ppk`);
|
saveToLocal(fn, `${appId}/ppk/${id}.ppk`);
|
||||||
console.log(`Version published: ${id}`);
|
console.log(`已成功上传新热更包(id: ${id})`);
|
||||||
|
|
||||||
const v = await question('Would you like to bind packages to this version?(Y/N)');
|
const v = await question('是否现在将此热更应用到原生包上?(Y/N)');
|
||||||
if (v.toLowerCase() === 'y') {
|
if (v.toLowerCase() === 'y') {
|
||||||
await this.update({args:[], options:{versionId: id, platform}});
|
await this.update({ args: [], options: { versionId: id, platform } });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
versions: async function({options}) {
|
versions: async function ({ options }) {
|
||||||
const platform = checkPlatform(options.platform || await question('Platform(ios/android):'));
|
const platform = checkPlatform(
|
||||||
|
options.platform || (await question('平台(ios/android):')),
|
||||||
|
);
|
||||||
const { appId } = await getSelectedApp(platform);
|
const { appId } = await getSelectedApp(platform);
|
||||||
await listVersions(appId);
|
await listVersions(appId);
|
||||||
},
|
},
|
||||||
update: async function({args, options}) {
|
update: async function ({ args, options }) {
|
||||||
const platform = checkPlatform(options.platform || await question('Platform(ios/android):'));
|
const platform = checkPlatform(
|
||||||
|
options.platform || (await question('平台(ios/android):')),
|
||||||
|
);
|
||||||
const { appId } = await getSelectedApp(platform);
|
const { appId } = await getSelectedApp(platform);
|
||||||
const versionId = options.versionId || (await chooseVersion(appId)).id;
|
let versionId = options.versionId || (await chooseVersion(appId)).id;
|
||||||
const pkgId = options.packageId || (await choosePackage(appId)).id;
|
if (versionId === 'null') {
|
||||||
|
versionId = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let pkgId;
|
||||||
|
let pkgVersion = options.packageVersion;
|
||||||
|
let minPkgVersion = options.minPackageVersion;
|
||||||
|
let maxPkgVersion = options.maxPackageVersion;
|
||||||
|
let rollout = options.rollout;
|
||||||
|
if (rollout === undefined) {
|
||||||
|
rollout = null;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
rollout = parseInt(rollout);
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error('rollout 必须是 1-100 的整数');
|
||||||
|
}
|
||||||
|
if (rollout < 1 || rollout > 100) {
|
||||||
|
throw new Error('rollout 必须是 1-100 的整数');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (minPkgVersion) {
|
||||||
|
minPkgVersion = String(minPkgVersion).trim();
|
||||||
|
const { data } = await get(`/app/${appId}/package/list?limit=1000`);
|
||||||
|
const pkgs = data.filter((d) => compare(d.name, minPkgVersion, '>='));
|
||||||
|
if (pkgs.length === 0) {
|
||||||
|
throw new Error(`未查询到 >= ${minPkgVersion} 的原生版本`);
|
||||||
|
}
|
||||||
|
if (rollout) {
|
||||||
|
const rolloutConfig = {};
|
||||||
|
for (const pkg of pkgs) {
|
||||||
|
rolloutConfig[pkg.name] = rollout;
|
||||||
|
}
|
||||||
|
await put(`/app/${appId}/version/${versionId}`, {
|
||||||
|
config: {
|
||||||
|
rollout: rolloutConfig,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(
|
||||||
|
`已在原生版本 ${pkgs
|
||||||
|
.map((p) => p.name)
|
||||||
|
.join(', ')} 上设置灰度发布 ${rollout}% 热更版本 ${versionId}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for (const pkg of pkgs) {
|
||||||
|
await put(`/app/${appId}/package/${pkg.id}`, {
|
||||||
|
versionId,
|
||||||
|
});
|
||||||
|
console.log(`已将热更版本 ${versionId} 绑定到原生版本 ${pkg.name} (id: ${pkg.id})`);
|
||||||
|
}
|
||||||
|
console.log(`操作完成,共已绑定 ${pkgs.length} 个原生版本`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (maxPkgVersion) {
|
||||||
|
maxPkgVersion = String(maxPkgVersion).trim();
|
||||||
|
const { data } = await get(`/app/${appId}/package/list?limit=1000`);
|
||||||
|
const pkgs = data.filter((d) => compare(d.name, maxPkgVersion, '<='));
|
||||||
|
if (pkgs.length === 0) {
|
||||||
|
throw new Error(`未查询到 <= ${maxPkgVersion} 的原生版本`);
|
||||||
|
}
|
||||||
|
if (rollout) {
|
||||||
|
const rolloutConfig = {};
|
||||||
|
for (const pkg of pkgs) {
|
||||||
|
rolloutConfig[pkg.name] = rollout;
|
||||||
|
}
|
||||||
|
await put(`/app/${appId}/version/${versionId}`, {
|
||||||
|
config: {
|
||||||
|
rollout: rolloutConfig,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(
|
||||||
|
`已在原生版本 ${pkgs
|
||||||
|
.map((p) => p.name)
|
||||||
|
.join(', ')} 上设置灰度发布 ${rollout}% 热更版本 ${versionId}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
for (const pkg of pkgs) {
|
||||||
|
await put(`/app/${appId}/package/${pkg.id}`, {
|
||||||
|
versionId,
|
||||||
|
});
|
||||||
|
console.log(`已将热更版本 ${versionId} 绑定到原生版本 ${pkg.name} (id: ${pkg.id})`);
|
||||||
|
}
|
||||||
|
console.log(`操作完成,共已绑定 ${pkgs.length} 个原生版本`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await get(`/app/${appId}/package/list?limit=1000`);
|
||||||
|
if (pkgVersion) {
|
||||||
|
pkgVersion = pkgVersion.trim();
|
||||||
|
const pkg = data.find((d) => d.name === pkgVersion);
|
||||||
|
if (pkg) {
|
||||||
|
pkgId = pkg.id;
|
||||||
|
} else {
|
||||||
|
throw new Error(`未查询到匹配原生版本:${pkgVersion}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!pkgId) {
|
||||||
|
pkgId = options.packageId || (await choosePackage(appId)).id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pkgId) {
|
||||||
|
throw new Error('请提供 packageId 或 packageVersion 参数');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pkgVersion) {
|
||||||
|
const pkg = data.find((d) => d.id === pkgId);
|
||||||
|
if (pkg) {
|
||||||
|
pkgVersion = pkg.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rollout) {
|
||||||
|
await put(`/app/${appId}/version/${versionId}`, {
|
||||||
|
config: {
|
||||||
|
rollout: {
|
||||||
|
[pkgVersion]: rollout,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(
|
||||||
|
`已将在原生版本 ${pkgVersion} (id: ${pkgId}) 上设置灰度发布 ${rollout}% 热更版本 ${versionId} `,
|
||||||
|
);
|
||||||
|
}
|
||||||
await put(`/app/${appId}/package/${pkgId}`, {
|
await put(`/app/${appId}/package/${pkgId}`, {
|
||||||
versionId,
|
versionId,
|
||||||
});
|
});
|
||||||
console.log('Ok.');
|
console.log(`已将热更版本 ${versionId} 绑定到原生版本 ${pkgVersion} (id: ${pkgId})`);
|
||||||
}
|
},
|
||||||
|
updateVersionInfo: async function ({ args, options }) {
|
||||||
|
const platform = checkPlatform(
|
||||||
|
options.platform || (await question('平台(ios/android):')),
|
||||||
|
);
|
||||||
|
const { appId } = await getSelectedApp(platform);
|
||||||
|
const versionId = options.versionId || (await chooseVersion(appId)).id;
|
||||||
|
|
||||||
|
const updateParams = {};
|
||||||
|
options.name && (updateParams.name = options.name);
|
||||||
|
options.description && (updateParams.description = options.description);
|
||||||
|
options.metaInfo && (updateParams.metaInfo = options.metaInfo);
|
||||||
|
await put(`/app/${appId}/version/${versionId}`, updateParams);
|
||||||
|
console.log('操作成功');
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
75
tsconfig.json
Normal file
75
tsconfig.json
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
/* Basic Options */
|
||||||
|
"target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
|
||||||
|
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
|
||||||
|
"lib": [
|
||||||
|
"ESNext"
|
||||||
|
] /* Specify library files to be included in the compilation. */,
|
||||||
|
"allowJs": true /* Allow javascript files to be compiled. */,
|
||||||
|
// "checkJs": true /* Report errors in .js files. */,
|
||||||
|
"jsx": "react" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
|
||||||
|
// "declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||||
|
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||||
|
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
||||||
|
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||||
|
// "outDir": "lib" /* Redirect output structure to the directory. */,
|
||||||
|
// "rootDir": "src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
|
||||||
|
// "composite": true, /* Enable project compilation */
|
||||||
|
// "removeComments": true, /* Do not emit comments to output. */
|
||||||
|
"noEmit": true /* Do not emit outputs. */,
|
||||||
|
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||||
|
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||||
|
"isolatedModules": true /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */,
|
||||||
|
|
||||||
|
/* Strict Type-Checking Options */
|
||||||
|
"strict": true /* Enable all strict type-checking options. */,
|
||||||
|
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
|
||||||
|
"strictNullChecks": true /* Enable strict null checks. */,
|
||||||
|
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||||
|
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||||
|
"noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
|
||||||
|
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||||
|
|
||||||
|
/* Additional Checks */
|
||||||
|
// "noUnusedLocals": true /* Report errors on unused locals. */,
|
||||||
|
// "noUnusedParameters": true /* Report errors on unused parameters. */,
|
||||||
|
"noImplicitReturns": false /* Report error when not all code paths in function return a value. */,
|
||||||
|
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||||
|
|
||||||
|
/* Module Resolution Options */
|
||||||
|
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||||
|
"baseUrl": "./src" /* Base directory to resolve non-absolute module names. */,
|
||||||
|
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||||
|
// "rootDirs": [
|
||||||
|
// "src",
|
||||||
|
// "db",
|
||||||
|
// "config",
|
||||||
|
// "models"
|
||||||
|
// ] /* List of root folders whose combined content represents the structure of the project at runtime. */,
|
||||||
|
"typeRoots": [
|
||||||
|
"src/types"
|
||||||
|
] /* List of folders to include type definitions from. */,
|
||||||
|
// "types": [], /* Type declaration files to be included in compilation. */
|
||||||
|
"allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
|
||||||
|
// "paths": {
|
||||||
|
// "@/*": ["src/*"]
|
||||||
|
// },
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
|
||||||
|
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||||
|
|
||||||
|
/* Source Map Options */
|
||||||
|
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||||
|
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||||
|
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||||
|
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||||
|
|
||||||
|
/* Experimental Options */
|
||||||
|
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||||
|
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"incremental": true
|
||||||
|
},
|
||||||
|
"exclude": ["node_modules", "lib"]
|
||||||
|
}
|
Reference in New Issue
Block a user