no message
This commit is contained in:
parent
82624e977f
commit
00045bf81b
@ -1,25 +0,0 @@
|
||||
发表 Issue 前请仔细阅读以下内容:
|
||||
|
||||
1. 如果你是要反馈 bug, 请按以下`模板`书写 Issue;
|
||||
2. 如果你遇到的是 Node.js 使用问题, 请尽可能依赖搜索引擎解决问题;
|
||||
3. 遇到包依赖问题,请联系对应项目;
|
||||
4. 任何对某类小程序包的适配问题都应提供 wxapkg 程序包,否则直接 Close 处理;
|
||||
5. 提交前请确认 wxapkg 程序包版本不小于 v0.6vv_20180111_fbi (直接用文本编辑器打开 wxapkg包搜索 v0.6vv 或 v0.5vv 即可查到,注意版本大小主要比较的是日期), 旧版本不提供支持, 相关 Issue 直接 Close 处理;
|
||||
6. 直接分包和直接处理含插件的包两个功能暂不支持, 请勿重复发表 Issue, 新样例可在已存在的 Issue 下提出;
|
||||
7. 请不要在其他 Issue 下发表与该 Issue 无关的回复, 否则将有可能被删除。
|
||||
|
||||
模板内容如下:
|
||||
|
||||
程序执行命令(可选):
|
||||
|
||||
程序执行错误信息(如果反馈是抛出异常的错误,必填):
|
||||
|
||||
```
|
||||
复制到这里
|
||||
```
|
||||
|
||||
程序结果错误信息(如果反馈不是抛出异常的错误, 必填, 请尽可能详细描述):
|
||||
|
||||
程序包(你所要解压的程序包地址, 可为网盘链接, 也可直接上传[上传前请先打包]. 必填):
|
||||
|
||||
其他附加内容:
|
||||
56
README.md
56
README.md
@ -1,3 +1,4 @@
|
||||
|
||||
# wxappUnpacker
|
||||
|
||||
  
|
||||
@ -14,27 +15,10 @@ Android 手机最近使用过的微信小程序所对应的 wxapkg 包文件都
|
||||
|
||||
其中`{User}` 为当前用户的用户名,类似于 `2bc**************b65`。
|
||||
|
||||
### 使用
|
||||
|
||||
node wuWxapkg.js .wxapkg 的文件路径 // 例子 D:/node/wxappUnpacker-master._1123949441_321.wxapkg
|
||||
|
||||
|
||||
### 另注
|
||||
|
||||
所有命令上都可以使用`-f`指令来提高一定的并行度,但输出信息会混乱。
|
||||
|
||||
如果发现包内文件“缺失”,请先检查解包时是否出现提示`NOTICE: SubPackages exist in this package.`。如存在,请在寻找好分包后,按上文提示操作。(小程序需要访问特定页面;小游戏需要触发特定函数,然后分包才会被下载。)
|
||||
|
||||
### 局限(包括但可能不限于以下内容)
|
||||
|
||||
- 实现中很多功能基于特定的版本(`wcc-v0.6vv_20180111_fbi`, 且不考虑面向低版本适配)和字符串搜索,所以不能很好的适应各种特殊情况。
|
||||
- wxml 文件拥有不同于 xml 和 html 文件的字符转义规则,且尚未公开(并非"没有"),因此未能很好的还原相关内容。
|
||||
- js 文件被压缩后会丢失原始变量名等信息内容无法还原;wxss 文件压缩后的注释也会丢失。
|
||||
- wxs 文件会将所有的变量如 Math 改为 nv_Math ,这里仅通过字符串替换去除。
|
||||
- 一些被引用 wxss 文件本身的源文件丢失,因此无法恢复原始目录。
|
||||
- 有些项目开启了难以复原的`es6转es5`选项,检验本项目结果是否正确时需要关闭项目中的`es6转es5`选项。
|
||||
- wxml 中一些无法找到相对应 的正向语句的内容无法还原。
|
||||
- json 中`components`项丢失,仅会标注被其他包引用的自定义组件。
|
||||
# 安装
|
||||
```
|
||||
npm install
|
||||
```
|
||||
|
||||
## 依赖
|
||||
|
||||
@ -53,7 +37,31 @@ Android 手机最近使用过的微信小程序所对应的 wxapkg 包文件都
|
||||
|
||||
此外,这些 node.js 程序之间也有一定的依赖关系,比如他们都依赖于 wuLib.js 。
|
||||
|
||||
### 使用
|
||||
|
||||
## 技术支持
|
||||
Star 一下可以加微信
|
||||
微信: li1664771111 //备注:反编译
|
||||
node wuWxapkg.js .wxapkg 的文件路径 // 例子 D:/node/wxappUnpacker-master._1123949441_321.wxapkg
|
||||
|
||||
# 分包功能
|
||||
|
||||
当检测到 wxapkg 为子包时, 添加-s 参数指定主包源码路径即可自动将子包的 wxss,wxml,js 解析到主包的对应位置下. 完整流程大致如下:
|
||||
1. 获取主包和若干子包
|
||||
2. 解包主包 `./bingo.sh testpkg/master-xxx.wxapkg`
|
||||
3. 解包子包 `./bingo.sh testpkg/sub-1-xxx.wxapkg -s=../master-xxx`
|
||||
|
||||
TIP
|
||||
> -s 参数可为相对路径或绝对路径, 推荐使用绝对路径, 因为相对路径的起点不是当前目录 而是子包解包后的目录
|
||||
|
||||
```
|
||||
├── testpkg
|
||||
│ ├── sub-1-xxx.wxapkg #被解析子包
|
||||
│ └── sub-1-xxx #相对路径的起点
|
||||
│ ├── app-service.js
|
||||
│ ├── master-xxx.wxapkg
|
||||
│ └── master-xxx # ../master-xxx 就是这个目录
|
||||
│ ├── app.json
|
||||
```
|
||||
|
||||
# 支持
|
||||
```
|
||||
Star 一个吧
|
||||
```
|
||||
48
bingo.bat
Normal file
48
bingo.bat
Normal file
@ -0,0 +1,48 @@
|
||||
# WXAPPUNPACKER_PATH 项目路径
|
||||
set WXAPPUNPACKER_PATH=%~dp0
|
||||
|
||||
set FILE_FORMAT=wxapkg
|
||||
|
||||
:wxappUnpacker_pkg
|
||||
|
||||
echo "node %WXAPPUNPACKER_PATH%/wuWxapkg.js %fname%"
|
||||
|
||||
node %WXAPPUNPACKER_PATH%/wuWxapkg.js %2 %1
|
||||
|
||||
goto:eof
|
||||
|
||||
:wxappUnpacker
|
||||
|
||||
set de_dir=%1
|
||||
|
||||
if "%de_dir%" == ""
|
||||
|
||||
set de_dir=%WXAPPUNPACKER_PATH%
|
||||
|
||||
echo %de_dir%
|
||||
|
||||
echo "for wxapkg in `find %de_dir% "*.${FILE_FORMAT}"`"
|
||||
|
||||
for %%fname in `/f %de_dir% -name "*.%FILE_FORMAT%"`
|
||||
|
||||
do
|
||||
|
||||
(call :wxappUnpacker_pkg %fname% %2)
|
||||
|
||||
goto:eof
|
||||
|
||||
:de_pkg
|
||||
|
||||
if "-d" == "%1"
|
||||
|
||||
(call :wxappUnpacker %1 %2)
|
||||
|
||||
else
|
||||
|
||||
(call :wxappUnpacker_pkg %1 %2)
|
||||
|
||||
goto:eof
|
||||
|
||||
# $1: pkg file or pkg dir; $2: order
|
||||
|
||||
call :de_pkg %1 %2
|
||||
41
bingo.sh
Normal file
41
bingo.sh
Normal file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# MyWxappUnpacker 项目路径
|
||||
WXAPPUNPACKER_PATH=`pwd`
|
||||
|
||||
FILE_FORMAT=wxapkg
|
||||
|
||||
wxappUnpacker_pkg() {
|
||||
echo "node ${WXAPPUNPACKER_PATH}/wuWxapkg.js ${fname}"
|
||||
node ${WXAPPUNPACKER_PATH}/wuWxapkg.js $2 $1
|
||||
return 0;
|
||||
}
|
||||
|
||||
wxappUnpacker() {
|
||||
de_dir=$1
|
||||
if [ -z "$1" ]
|
||||
then
|
||||
de_dir=`pwd`
|
||||
fi
|
||||
echo "${de_dir}"
|
||||
echo "for wxapkg in `find ${de_dir} -name "*.${FILE_FORMAT}"`"
|
||||
for fname in `find ${de_dir} -name "*.${FILE_FORMAT}"`
|
||||
do
|
||||
wxappUnpacker_pkg ${fname} $2
|
||||
done
|
||||
return 0;
|
||||
}
|
||||
|
||||
de_pkg() {
|
||||
if [ "-d" == "$1" ]
|
||||
then
|
||||
wxappUnpacker $1 $2
|
||||
else
|
||||
wxappUnpacker_pkg $1 $2
|
||||
fi
|
||||
return 0;
|
||||
}
|
||||
# $1: pkg file or pkg dir; $2: order
|
||||
de_pkg $1 $2
|
||||
|
||||
|
||||
20
package.json
20
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "wxapp-unpacker",
|
||||
"version": "0.3",
|
||||
"version": "1.0.1",
|
||||
"description": "Wechat App(微信小程序, .wxapkg)解包及相关文件(.wxss, .json, .wxs, .wxml)还原工具",
|
||||
"main": "wuWxapkg.js",
|
||||
"repository": {
|
||||
@ -12,14 +12,22 @@
|
||||
"bugs": {
|
||||
"url": "https://github.com/qwerty472123/wxappUnpacker/issues"
|
||||
},
|
||||
"scripts": {
|
||||
"pkg": "node wuWxapkg.js",
|
||||
"css": "node wuWxss.js",
|
||||
"xml": "node wuWxml.js",
|
||||
"js": "node wuJs.js",
|
||||
"cfg": "node wuConfig.js"
|
||||
},
|
||||
"homepage": "https://github.com/qwerty472123/wxappUnpacker#readme",
|
||||
"dependencies": {
|
||||
"css-tree": "^1.0.0-alpha.37",
|
||||
"cheerio": "^1.0.0-rc.3",
|
||||
"css-tree": "^1.0.0-alpha.28",
|
||||
"cssbeautify": "^0.3.1",
|
||||
"escodegen": "^1.12.0",
|
||||
"esprima": "^4.0.1",
|
||||
"js-beautify": "^1.10.2",
|
||||
"escodegen": "^1.11.0",
|
||||
"esprima": "^4.0.0",
|
||||
"js-beautify": "^1.7.5",
|
||||
"uglify-es": "^3.3.9",
|
||||
"vm2": "^3.8.4"
|
||||
"vm2": "^3.6.0"
|
||||
}
|
||||
}
|
||||
|
||||
81
wuConfig.js
81
wuConfig.js
@ -3,22 +3,27 @@ const fs=require("fs");
|
||||
const path = require("path");
|
||||
const crypto = require("crypto");
|
||||
const {VM} = require('vm2');
|
||||
|
||||
function getWorkerPath(name) {
|
||||
let code = fs.readFileSync(name, {encoding: 'utf8'});
|
||||
let commPath = false;
|
||||
let vm=new VM({sandbox:{
|
||||
require(){},
|
||||
let vm = new VM({
|
||||
sandbox: {
|
||||
require() {
|
||||
},
|
||||
define(name) {
|
||||
name = path.dirname(name) + '/';
|
||||
if (commPath === false) commPath = name;
|
||||
commPath = wu.commonDir(commPath, name);
|
||||
}
|
||||
}});
|
||||
}
|
||||
});
|
||||
vm.run(code.slice(code.indexOf("define(")));
|
||||
if (commPath.length > 0) commPath = commPath.slice(0, -1);
|
||||
console.log("Worker path: \"" + commPath + "\"");
|
||||
return commPath;
|
||||
}
|
||||
|
||||
function doConfig(configFile, cb) {
|
||||
let dir = path.dirname(configFile);
|
||||
wu.get(configFile, content => {
|
||||
@ -28,13 +33,43 @@ function doConfig(configFile,cb){
|
||||
k.unshift(wu.changeExt(e.entryPagePath));
|
||||
let app = {pages: k, window: e.global && e.global.window, tabBar: e.tabBar, networkTimeout: e.networkTimeout};
|
||||
if (e.subPackages) {
|
||||
app.subPackages=e.subPackages;
|
||||
console.log("=======================================================\nNOTICE: SubPackages exist in this package.\nDetails: ",app.subPackages,"\n=======================================================");
|
||||
let subPackages = [];
|
||||
let pages = app.pages;
|
||||
for (let subPackage of e.subPackages) {
|
||||
let root = subPackage.root;
|
||||
let lastChar = root.substr(root.length - 1, 1);
|
||||
if (lastChar !== '/') {
|
||||
root = root + '/';
|
||||
}
|
||||
let firstChar = root.substr(0, 1);
|
||||
if (firstChar === '/') {
|
||||
root = root.substring(1);
|
||||
}
|
||||
let newPages = [];
|
||||
for (let page of subPackage.pages) {
|
||||
let items = page.replace(root, '');
|
||||
newPages.push(items);
|
||||
let subIndex = pages.indexOf(root + items);
|
||||
if (subIndex !== -1) {
|
||||
pages.splice(subIndex, 1);
|
||||
}
|
||||
}
|
||||
subPackage.root = root;
|
||||
subPackage.pages = newPages;
|
||||
subPackages.push(subPackage);
|
||||
}
|
||||
app.subPackages = subPackages;
|
||||
app.pages = pages;
|
||||
console.log("=======================================================\n这个小程序采用了分包\n子包个数为: ", app.subPackages.length, "\n=======================================================");
|
||||
}
|
||||
if (e.navigateToMiniProgramAppIdList) app.navigateToMiniProgramAppIdList = e.navigateToMiniProgramAppIdList;
|
||||
if (fs.existsSync(path.resolve(dir, "workers.js"))) app.workers = getWorkerPath(path.resolve(dir, "workers.js"));
|
||||
if (e.extAppid)
|
||||
wu.save(path.resolve(dir,'ext.json'),JSON.stringify({extEnable:true,extAppid:e.extAppid,ext:e.ext},null,4));
|
||||
wu.save(path.resolve(dir, 'ext.json'), JSON.stringify({
|
||||
extEnable: true,
|
||||
extAppid: e.extAppid,
|
||||
ext: e.ext
|
||||
}, null, 4));
|
||||
if (typeof e.debug != "undefined") app.debug = e.debug;
|
||||
let cur = path.resolve("./file");
|
||||
for (let a in e.page) if (e.page[a].window.usingComponents)
|
||||
@ -49,9 +84,11 @@ function doConfig(configFile,cb){
|
||||
let matches = fs.readFileSync(path.resolve(dir, "app-service.js"), {encoding: 'utf8'}).match(/\_\_wxAppCode\_\_\['[^\.]+\.json[^;]+\;/g);
|
||||
if (matches) {
|
||||
let attachInfo = {};
|
||||
(new VM({sandbox:{
|
||||
(new VM({
|
||||
sandbox: {
|
||||
__wxAppCode__: attachInfo
|
||||
}})).run(matches.join(""));
|
||||
}
|
||||
})).run(matches.join(""));
|
||||
for (let name in attachInfo) e.page[wu.changeExt(name, ".html")] = {window: attachInfo[name]};
|
||||
}
|
||||
}
|
||||
@ -61,9 +98,34 @@ function doConfig(configFile,cb){
|
||||
wu.save(fileName, JSON.stringify(e.page[a].window, null, 4));
|
||||
if (configFile == fileName) delWeight = 0;
|
||||
}
|
||||
if (app.subPackages) {
|
||||
for (let subPackage of app.subPackages) {
|
||||
if (subPackage.pages) {
|
||||
for (let item of subPackage.pages) {
|
||||
let a = subPackage.root + item + '.xx';
|
||||
//添加默认的 wxs, wxml, wxss
|
||||
let jsName = wu.changeExt(a, ".js");
|
||||
let fileNameOfWxs = path.resolve(dir, jsName);
|
||||
wu.save(fileNameOfWxs, "// " + jsName + "\nPage({data: {}})");
|
||||
let wxmlName = wu.changeExt(a, ".wxml");
|
||||
let fileNameOfWxml = path.resolve(dir, wxmlName);
|
||||
wu.save(fileNameOfWxml, "<!--" + wxmlName + "--><text>" + wxmlName + "</text>");
|
||||
let cssName = wu.changeExt(a, ".wxss");
|
||||
let fileNameOfWxss = path.resolve(dir, cssName);
|
||||
wu.save(fileNameOfWxss, "/* " + cssName + " */");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (app.tabBar && app.tabBar.list) wu.scanDirByExt(dir, "", li => {//search all files
|
||||
let digests = [], digestsEvent = new wu.CntEvent, rdir = path.resolve(dir);
|
||||
function fixDir(dir){return dir.startsWith(rdir)?dir.slice(rdir.length+1):dir;}
|
||||
|
||||
function fixDir(dir) {
|
||||
return dir.startsWith(rdir) ? dir.slice(rdir.length + 1) : dir;
|
||||
}
|
||||
|
||||
digestsEvent.add(() => {
|
||||
for (let e of app.tabBar.list) {
|
||||
e.pagePath = wu.changeExt(e.pagePath);
|
||||
@ -100,6 +162,7 @@ function doConfig(configFile,cb){
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {doConfig: doConfig};
|
||||
if (require.main === module) {
|
||||
wu.commandExecute(doConfig, "Split and make up weapp app-config.json file.\n\n<files...>\n\n<files...> app-config.json files to split and make up.");
|
||||
|
||||
29
wuJs.js
29
wuJs.js
@ -3,15 +3,23 @@ const path=require("path");
|
||||
const UglifyJS = require("uglify-es");
|
||||
const {js_beautify} = require("js-beautify");
|
||||
const {VM} = require('vm2');
|
||||
|
||||
function jsBeautify(code) {
|
||||
return UglifyJS.minify(code, {mangle: false, compress: false, output: {beautify: true, comments: true}}).code;
|
||||
}
|
||||
function splitJs(name,cb){
|
||||
|
||||
function splitJs(name, cb, mainDir) {
|
||||
let isSubPkg = mainDir && mainDir.length > 0;
|
||||
let dir = path.dirname(name);
|
||||
if (isSubPkg) {
|
||||
dir = mainDir;
|
||||
}
|
||||
wu.get(name, code => {
|
||||
let needDelList = {};
|
||||
let vm=new VM({sandbox:{
|
||||
require(){},
|
||||
let vm = new VM({
|
||||
sandbox: {
|
||||
require() {
|
||||
},
|
||||
define(name, func) {
|
||||
let code = func.toString();
|
||||
code = code.slice(code.indexOf("{") + 1, code.lastIndexOf("}") - 1).trim();
|
||||
@ -23,16 +31,27 @@ function splitJs(name,cb){
|
||||
console.log("Fail to delete 'use strict' in \"" + name + "\".");
|
||||
res = jsBeautify(bcode);
|
||||
}
|
||||
console.log(dir, name);
|
||||
needDelList[path.resolve(dir, name)] = -8;
|
||||
wu.save(path.resolve(dir, name), jsBeautify(res));
|
||||
},
|
||||
definePlugin() {
|
||||
},
|
||||
requirePlugin() {
|
||||
}
|
||||
}});
|
||||
vm.run(code.slice(code.indexOf("define(")));
|
||||
}
|
||||
});
|
||||
if (isSubPkg) {
|
||||
code = code.slice(code.indexOf("define("));
|
||||
}
|
||||
console.log('splitJs: ' + name);
|
||||
vm.run(code);
|
||||
console.log("Splitting \"" + name + "\" done.");
|
||||
if (!needDelList[name]) needDelList[name] = 8;
|
||||
cb(needDelList);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {jsBeautify: jsBeautify, wxsBeautify: js_beautify, splitJs: splitJs};
|
||||
if (require.main === module) {
|
||||
wu.commandExecute(splitJs, "Split and beautify weapp js file.\n\n<files...>\n\n<files...> js files to split and beautify.");
|
||||
|
||||
53
wuLib.js
53
wuLib.js
@ -1,5 +1,6 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
class CntEvent {
|
||||
constructor() {
|
||||
this.cnt = 0;
|
||||
@ -8,9 +9,11 @@ class CntEvent{
|
||||
this.decount = this.decount.bind(this);
|
||||
this.add = this.add.bind(this);
|
||||
}
|
||||
|
||||
encount(delta = 1) {
|
||||
this.cnt += delta;
|
||||
}
|
||||
|
||||
decount() {
|
||||
if (this.cnt > 0) --this.cnt;
|
||||
if (this.cnt == 0) {
|
||||
@ -18,20 +21,24 @@ class CntEvent{
|
||||
this.emptyEvent = [];
|
||||
}
|
||||
}
|
||||
|
||||
add(cb, ...attach) {
|
||||
this.emptyEvent.push([cb, attach]);
|
||||
}
|
||||
|
||||
check(cb, ...attach) {
|
||||
if (this.cnt == 0) cb(...attach);
|
||||
else this.add(cb, ...attach);
|
||||
}
|
||||
}
|
||||
|
||||
class LimitedRunner {
|
||||
constructor(limit) {
|
||||
this.limit = limit;
|
||||
this.cnt = 0;
|
||||
this.funcs = [];
|
||||
}
|
||||
|
||||
run(func) {
|
||||
if (this.cnt < this.limit) {
|
||||
this.cnt++;
|
||||
@ -40,6 +47,7 @@ class LimitedRunner{
|
||||
this.funcs.push(func);
|
||||
}
|
||||
}
|
||||
|
||||
done() {
|
||||
if (this.cnt > 0) this.cnt--;
|
||||
if (this.funcs.length > 0) {
|
||||
@ -47,18 +55,23 @@ class LimitedRunner{
|
||||
setTimeout(this.funcs.shift(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
runWithCb(func, ...args) {
|
||||
let cb = args.pop(), self = this;
|
||||
|
||||
function agent(...args) {
|
||||
self.done();
|
||||
return cb.apply(this, args);
|
||||
}
|
||||
|
||||
args.push(agent);
|
||||
this.run(() => func(...args));
|
||||
}
|
||||
}
|
||||
|
||||
let ioEvent = new CntEvent;
|
||||
let ioLimit = new LimitedRunner(4096);
|
||||
|
||||
function mkdirs(dir, cb) {
|
||||
ioLimit.runWithCb(fs.stat.bind(fs), dir, (err, stats) => {
|
||||
if (err) mkdirs(path.dirname(dir), () => fs.mkdir(dir, cb));
|
||||
@ -66,6 +79,7 @@ function mkdirs(dir,cb){
|
||||
else cb();
|
||||
});
|
||||
}
|
||||
|
||||
function save(name, content) {
|
||||
ioEvent.encount();
|
||||
mkdirs(path.dirname(name), () => ioLimit.runWithCb(fs.writeFile.bind(fs), name, content, err => {
|
||||
@ -73,6 +87,7 @@ function save(name,content){
|
||||
ioEvent.decount();
|
||||
}));
|
||||
}
|
||||
|
||||
function get(name, cb, opt = {encoding: 'utf8'}) {
|
||||
ioEvent.encount();
|
||||
ioLimit.runWithCb(fs.readFile.bind(fs), name, opt, (err, data) => {
|
||||
@ -81,15 +96,19 @@ function get(name,cb,opt={encoding:'utf8'}){
|
||||
ioEvent.decount();
|
||||
});
|
||||
}
|
||||
|
||||
function del(name) {
|
||||
ioEvent.encount();
|
||||
ioLimit.runWithCb(fs.unlink.bind(fs), name, ioEvent.decount);
|
||||
}
|
||||
|
||||
function changeExt(name, ext = "") {
|
||||
return name.slice(0, name.lastIndexOf(".")) + ext;
|
||||
}
|
||||
|
||||
function scanDirByExt(dir, ext, cb) {
|
||||
let result = [], scanEvent = new CntEvent;
|
||||
|
||||
function helper(dir) {
|
||||
scanEvent.encount();
|
||||
ioLimit.runWithCb(fs.readdir.bind(fs), dir, (err, files) => {
|
||||
@ -107,15 +126,21 @@ function scanDirByExt(dir,ext,cb){
|
||||
scanEvent.decount();
|
||||
});
|
||||
}
|
||||
|
||||
scanEvent.add(cb, result);
|
||||
helper(dir, ext, scanEvent);
|
||||
}
|
||||
|
||||
function toDir(to, from) {//get relative path without posix/win32 problem
|
||||
if (from[0] == ".") from = from.slice(1);
|
||||
if (to[0] == ".") to = to.slice(1);
|
||||
from=from.replace(/\\/g,'/');to=to.replace(/\\/g,'/');
|
||||
from = from.replace(/\\/g, '/');
|
||||
to = to.replace(/\\/g, '/');
|
||||
let a = Math.min(to.length, from.length);
|
||||
for(let i=1,m=Math.min(to.length,from.length);i<=m;i++)if(!to.startsWith(from.slice(0,i))){a=i-1;break;}
|
||||
for (let i = 1, m = Math.min(to.length, from.length); i <= m; i++) if (!to.startsWith(from.slice(0, i))) {
|
||||
a = i - 1;
|
||||
break;
|
||||
}
|
||||
let pub = from.slice(0, a);
|
||||
let len = pub.lastIndexOf("/") + 1;
|
||||
let k = from.slice(len);
|
||||
@ -123,28 +148,38 @@ function toDir(to,from){//get relative path without posix/win32 problem
|
||||
for (let i = 0; i < k.length; i++) if (k[i] == '/') ret += '../';
|
||||
return ret + to.slice(len);
|
||||
}
|
||||
|
||||
function commonDir(pathA, pathB) {
|
||||
if (pathA[0] == ".") pathA = pathA.slice(1);
|
||||
if (pathB[0] == ".") pathB = pathB.slice(1);
|
||||
pathA=pathA.replace(/\\/g,'/');pathB=pathB.replace(/\\/g,'/');
|
||||
pathA = pathA.replace(/\\/g, '/');
|
||||
pathB = pathB.replace(/\\/g, '/');
|
||||
let a = Math.min(pathA.length, pathB.length);
|
||||
for(let i=1,m=Math.min(pathA.length,pathB.length);i<=m;i++)if(!pathA.startsWith(pathB.slice(0,i))){a=i-1;break;}
|
||||
for (let i = 1, m = Math.min(pathA.length, pathB.length); i <= m; i++) if (!pathA.startsWith(pathB.slice(0, i))) {
|
||||
a = i - 1;
|
||||
break;
|
||||
}
|
||||
let pub = pathB.slice(0, a);
|
||||
let len = pub.lastIndexOf("/") + 1;
|
||||
return pathA.slice(0, len);
|
||||
}
|
||||
|
||||
function commandExecute(cb, helper) {
|
||||
console.time("Total use");
|
||||
|
||||
function endTime() {
|
||||
ioEvent.check(() => console.timeEnd("Total use"));
|
||||
}
|
||||
|
||||
let orders = [];
|
||||
for (let order of process.argv) if (order.startsWith("-")) orders.push(order.slice(1));
|
||||
let iter=process.argv[Symbol.iterator](),nxt=iter.next(),called=false,faster=orders.includes("f"),fastCnt;
|
||||
let iter = process.argv[Symbol.iterator](), nxt = iter.next(), called = false, faster = orders.includes("f"),
|
||||
fastCnt;
|
||||
if (faster) {
|
||||
fastCnt = new CntEvent;
|
||||
fastCnt.add(endTime);
|
||||
}
|
||||
|
||||
function doNext() {
|
||||
let nxt = iter.next();
|
||||
while (!nxt.done && nxt.value.startsWith("-")) nxt = iter.next();
|
||||
@ -157,9 +192,13 @@ function commandExecute(cb,helper){
|
||||
else cb(nxt.value, doNext, orders);
|
||||
}
|
||||
}
|
||||
|
||||
while (!nxt.done && !nxt.value.endsWith(".js")) nxt = iter.next();
|
||||
doNext();
|
||||
}
|
||||
module.exports={mkdirs:mkdirs,get:get,save:save,toDir:toDir,del:del,addIO:ioEvent.add,
|
||||
|
||||
module.exports = {
|
||||
mkdirs: mkdirs, get: get, save: save, toDir: toDir, del: del, addIO: ioEvent.add,
|
||||
changeExt: changeExt, CntEvent: CntEvent, scanDirByExt: scanDirByExt, commonDir: commonDir,
|
||||
commandExecute:commandExecute};
|
||||
commandExecute: commandExecute
|
||||
};
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
const wu = require("./wuLib.js");
|
||||
const {VM} = require('vm2');
|
||||
|
||||
function catchZGroup(code, groupPreStr, cb) {
|
||||
const debugPre = "(function(z){var a=11;function Z(ops,debugLine){";
|
||||
let zArr = {};
|
||||
@ -14,25 +15,31 @@ function catchZGroup(code,groupPreStr,cb){
|
||||
}
|
||||
cb({"mul": zArr});
|
||||
}
|
||||
|
||||
function catchZ(code, cb) {
|
||||
let groupTest = code.match(/function gz\$gwx(\d*\_\d+)\(\)\{\s*if\( __WXML_GLOBAL__\.ops_cached\.\$gwx\d*\_\d+\)/g);
|
||||
if (groupTest !== null) return catchZGroup(code, groupTest, cb);
|
||||
let z=[],vm=new VM({sandbox:{
|
||||
let z = [], vm = new VM({
|
||||
sandbox: {
|
||||
z: z,
|
||||
debugInfo: []
|
||||
}});
|
||||
}
|
||||
});
|
||||
let lastPtr = code.lastIndexOf("(z);__WXML_GLOBAL__.ops_set.$gwx=z;");
|
||||
if (lastPtr == -1) lastPtr = code.lastIndexOf("(z);__WXML_GLOBAL__.ops_set.$gwx");
|
||||
code=code.slice(code.indexOf('(function(z){var a=11;function Z(ops){z.push(ops)}'),lastPtr+4);
|
||||
code = code.slice(code.lastIndexOf('(function(z){var a=11;function Z(ops){z.push(ops)}'), lastPtr + 4);
|
||||
vm.run(code);
|
||||
cb(z);
|
||||
}
|
||||
|
||||
function restoreSingle(ops, withScope = false) {
|
||||
if (typeof ops == "undefined") return "";
|
||||
|
||||
function scope(value) {
|
||||
if (value.startsWith('{') && value.endsWith('}')) return withScope ? value : "{" + value + "}";
|
||||
return withScope ? value : "{{" + value + "}}";
|
||||
}
|
||||
|
||||
function enBrace(value, type = '{') {
|
||||
if (value.startsWith('{') || value.startsWith('[') || value.startsWith('(') || value.endsWith('}') || value.endsWith(']') || value.endsWith(')')) value = ' ' + value + ' ';
|
||||
switch (type) {
|
||||
@ -46,9 +53,11 @@ function restoreSingle(ops,withScope=false){
|
||||
throw Error("Unknown brace type " + type);
|
||||
}
|
||||
}
|
||||
|
||||
function restoreNext(ops, w = withScope) {
|
||||
return restoreSingle(ops, w);
|
||||
}
|
||||
|
||||
function jsoToWxon(obj) {//convert JS Object to Wechat Object Notation(No quotes@key+str)
|
||||
let ans = "";
|
||||
if (typeof obj === "undefined") {
|
||||
@ -73,6 +82,7 @@ function restoreSingle(ops,withScope=false){
|
||||
return "'" + ret.join('"') + "'";
|
||||
} else return JSON.stringify(obj);
|
||||
}
|
||||
|
||||
let op = ops[0];
|
||||
if (typeof op != "object") {
|
||||
switch (op) {
|
||||
@ -93,20 +103,43 @@ function restoreSingle(ops,withScope=false){
|
||||
{
|
||||
function getPrior(op, len) {
|
||||
const priorList = {
|
||||
"?:":4,"&&":6,"||":5,"+":13,"*":14,"/":14,"%":14,"|":7,"^":8,"&":9,"!":16,"~":16,
|
||||
"===":10,"==":10,"!=":10,"!==":10,">=":11,"<=":11,">":11,"<":11,"<<":12,">>":12,
|
||||
"?:": 4,
|
||||
"&&": 6,
|
||||
"||": 5,
|
||||
"+": 13,
|
||||
"*": 14,
|
||||
"/": 14,
|
||||
"%": 14,
|
||||
"|": 7,
|
||||
"^": 8,
|
||||
"&": 9,
|
||||
"!": 16,
|
||||
"~": 16,
|
||||
"===": 10,
|
||||
"==": 10,
|
||||
"!=": 10,
|
||||
"!==": 10,
|
||||
">=": 11,
|
||||
"<=": 11,
|
||||
">": 11,
|
||||
"<": 11,
|
||||
"<<": 12,
|
||||
">>": 12,
|
||||
"-": len == 3 ? 13 : 16
|
||||
};
|
||||
return priorList[op] ? priorList[op] : 0;
|
||||
}
|
||||
|
||||
function getOp(i) {
|
||||
let ret = restoreNext(ops[i], true);
|
||||
if (ops[i] instanceof Object && typeof ops[i][0] == "object" && ops[i][0][0] == 2) {
|
||||
//Add brackets if we need
|
||||
if(getPrior(op[1],ops.length)>getPrior(ops[i][0][1],ops[i].length))ret=enBrace(ret,'(');;
|
||||
if (getPrior(op[1], ops.length) > getPrior(ops[i][0][1], ops[i].length)) ret = enBrace(ret, '(');
|
||||
;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
switch (op[1]) {
|
||||
case"?:":
|
||||
ans = getOp(1) + "?" + getOp(2) + ":" + getOp(3);
|
||||
@ -137,8 +170,7 @@ function restoreSingle(ops,withScope=false){
|
||||
case 1:
|
||||
ans = '[]';
|
||||
break;
|
||||
default:
|
||||
{
|
||||
default: {
|
||||
let a = restoreNext(ops[1], true);
|
||||
//console.log(a,a.startsWith('[')&&a.endsWith(']'));
|
||||
if (a.startsWith('[') && a.endsWith(']')) {
|
||||
@ -194,6 +226,7 @@ function restoreSingle(ops,withScope=false){
|
||||
if (x.startsWith('{') && x.endsWith('}')) return 0;
|
||||
return 2;
|
||||
}
|
||||
|
||||
let a = restoreNext(ops[1], true);
|
||||
let b = restoreNext(ops[2], true);
|
||||
let xa = type(a), xb = type(b);
|
||||
@ -209,8 +242,7 @@ function restoreSingle(ops,withScope=false){
|
||||
case 10://...object
|
||||
ans = '...' + restoreNext(ops[1], true);
|
||||
break;
|
||||
case 12:
|
||||
{
|
||||
case 12: {
|
||||
let arr = restoreNext(ops[2], true);
|
||||
if (arr.startsWith('[') && arr.endsWith(']'))
|
||||
ans = restoreNext(ops[1], true) + enBrace(arr.slice(1, -1).trim(), '(');
|
||||
@ -223,6 +255,7 @@ function restoreSingle(ops,withScope=false){
|
||||
return scope(ans);
|
||||
}
|
||||
}
|
||||
|
||||
function restoreGroup(z) {
|
||||
let ans = [];
|
||||
for (let g in z.mul) {
|
||||
@ -234,12 +267,16 @@ function restoreGroup(z){
|
||||
ret.mul = ans;
|
||||
return ret;
|
||||
}
|
||||
|
||||
function restoreAll(z) {
|
||||
if (z.mul) return restoreGroup(z);
|
||||
let ans = [];
|
||||
for (let e of z) ans.push(restoreSingle(e, false));
|
||||
return ans;
|
||||
}
|
||||
module.exports={getZ(code,cb){
|
||||
|
||||
module.exports = {
|
||||
getZ(code, cb) {
|
||||
catchZ(code, z => cb(restoreAll(z)));
|
||||
}};
|
||||
}
|
||||
};
|
||||
|
||||
106
wuWxapkg.js
106
wuWxapkg.js
@ -5,6 +5,7 @@ const wuMl=require("./wuWxml.js");
|
||||
const wuSs = require("./wuWxss.js");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
|
||||
function header(buf) {
|
||||
console.log("\nHeader info:");
|
||||
let firstMark = buf.readUInt8(0);
|
||||
@ -20,6 +21,7 @@ function header(buf){
|
||||
if (firstMark != 0xbe || lastMark != 0xed) throw Error("Magic number is not correct!");
|
||||
return [infoListLength, dataLength];
|
||||
}
|
||||
|
||||
function genList(buf) {
|
||||
console.log("\nFile list info:");
|
||||
let fileCount = buf.readUInt32BE(0);
|
||||
@ -36,15 +38,16 @@ function genList(buf){
|
||||
info.size = buf.readUInt32BE(off);
|
||||
off += 4;
|
||||
fileInfo.push(info);
|
||||
console.log(info);
|
||||
}
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
function saveFile(dir, buf, list) {
|
||||
console.log("Saving files...");
|
||||
for (let info of list)
|
||||
wu.save(path.resolve(dir, (info.name.startsWith("/") ? "." : "") + info.name), buf.slice(info.off, info.off + info.size));
|
||||
}
|
||||
|
||||
function packDone(dir, cb, order) {
|
||||
console.log("Unpack done.");
|
||||
let weappEvent = new wu.CntEvent, needDelete = {};
|
||||
@ -60,6 +63,7 @@ function packDone(dir,cb,order){
|
||||
cb();
|
||||
});
|
||||
});
|
||||
|
||||
function doBack(deletable) {
|
||||
for (let key in deletable) {
|
||||
if (!needDelete[key]) needDelete[key] = 0;
|
||||
@ -67,21 +71,58 @@ function packDone(dir,cb,order){
|
||||
}
|
||||
weappEvent.decount();
|
||||
}
|
||||
//This will be the only func running this time, so async is needless.
|
||||
if(fs.existsSync(path.resolve(dir,"app-service.js"))){//weapp
|
||||
|
||||
function dealThreeThings(dir, mainDir, nowDir) {
|
||||
console.log("Split app-service.js and make up configs & wxss & wxml & wxs...");
|
||||
|
||||
//deal config
|
||||
if (fs.existsSync(path.resolve(dir, "app-config.json"))) {
|
||||
wuCfg.doConfig(path.resolve(dir, "app-config.json"), doBack);
|
||||
wuJs.splitJs(path.resolve(dir,"app-service.js"),doBack);
|
||||
if(fs.existsSync(path.resolve(dir,"workers.js")))
|
||||
wuJs.splitJs(path.resolve(dir,"workers.js"),doBack);
|
||||
if(fs.existsSync(path.resolve(dir,"page-frame.html")))
|
||||
wuMl.doFrame(path.resolve(dir,"page-frame.html"),doBack,order);
|
||||
else if(fs.existsSync(path.resolve(dir,"app-wxss.js"))) {
|
||||
wuMl.doFrame(path.resolve(dir,"app-wxss.js"),doBack,order);
|
||||
if(!needDelete[path.resolve(dir,"page-frame.js")])needDelete[path.resolve(dir,"page-frame.js")]=8;
|
||||
} else throw Error("page-frame-like file is not found in the package by auto.");
|
||||
wuSs.doWxss(dir,doBack);//Force it run at last, becuase lots of error occured in this part
|
||||
}else if(fs.existsSync(path.resolve(dir,"game.js"))){//wegame
|
||||
console.log('deal config ok');
|
||||
}
|
||||
//deal js
|
||||
if (fs.existsSync(path.resolve(dir, "app-service.js"))) {
|
||||
wuJs.splitJs(path.resolve(dir, "app-service.js"), doBack, mainDir);
|
||||
console.log('deal js ok');
|
||||
}
|
||||
if (fs.existsSync(path.resolve(dir, "workers.js"))) {
|
||||
wuJs.splitJs(path.resolve(dir, "workers.js"), doBack, mainDir);
|
||||
console.log('deal js2 ok');
|
||||
}
|
||||
//deal html
|
||||
if (mainDir) {
|
||||
if (fs.existsSync(path.resolve(dir, "page-frame.js"))) {
|
||||
wuMl.doFrame(path.resolve(dir, "page-frame.js"), doBack, order, mainDir);
|
||||
console.log('deal sub html ok');
|
||||
}
|
||||
wuSs.doWxss(dir, doBack, mainDir, nowDir);
|
||||
} else {
|
||||
if (fs.existsSync(path.resolve(dir, "page-frame.html"))) {
|
||||
wuMl.doFrame(path.resolve(dir, "page-frame.html"), doBack, order, mainDir);
|
||||
console.log('deal html ok');
|
||||
} else if (fs.existsSync(path.resolve(dir, "app-wxss.js"))) {
|
||||
wuMl.doFrame(path.resolve(dir, "app-wxss.js"), doBack, order, mainDir);
|
||||
if (!needDelete[path.resolve(dir, "page-frame.js")]) {
|
||||
needDelete[path.resolve(dir, "page-frame.js")] = 8;
|
||||
}
|
||||
console.log('deal wxss.js ok');
|
||||
} else {
|
||||
throw Error("page-frame-like file is not found in the package by auto.");
|
||||
}
|
||||
//Force it run at last, becuase lots of error occured in this part
|
||||
wuSs.doWxss(dir, doBack);
|
||||
|
||||
console.log('deal css ok');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//This will be the only func running this time, so async is needless.
|
||||
if (fs.existsSync(path.resolve(dir, "app-service.js"))) {
|
||||
//weapp
|
||||
dealThreeThings(dir);
|
||||
} else if (fs.existsSync(path.resolve(dir, "game.js"))) {
|
||||
//wegame
|
||||
console.log("Split game.js and rewrite game.json...");
|
||||
let gameCfg = path.resolve(dir, "app-config.json");
|
||||
wu.get(gameCfg, cfgPlain => {
|
||||
@ -101,8 +142,42 @@ function packDone(dir,cb,order){
|
||||
cb();
|
||||
});
|
||||
});
|
||||
}else throw Error("This package is unrecognizable.\nMay be this package is a subPackage which should be unpacked with -s=<MainDir>.\nOtherwise, please decrypted every type of file by hand.")
|
||||
} else {//分包
|
||||
let doSubPkg = false;
|
||||
for (const orderElement of order) {
|
||||
if (orderElement.indexOf('s=') !== -1) {
|
||||
let mainDir = orderElement.substring(2, orderElement.length);
|
||||
console.log("now dir: " + dir);
|
||||
console.log("param of mainDir: " + mainDir);
|
||||
|
||||
let findDir = function (dir, oldDir) {
|
||||
let files = fs.readdirSync(dir);
|
||||
for (const file of files) {
|
||||
let workDir = path.join(dir, file);
|
||||
if (fs.existsSync(path.resolve(workDir, "app-service.js"))) {
|
||||
console.log("sub package word dir: " + workDir);
|
||||
mainDir = path.resolve(oldDir, mainDir);
|
||||
console.log("real mainDir: " + mainDir);
|
||||
dealThreeThings(workDir, mainDir, oldDir);
|
||||
doSubPkg = true;
|
||||
return true;
|
||||
} else {
|
||||
findDir(workDir, oldDir);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
findDir(dir, dir);
|
||||
|
||||
}
|
||||
}
|
||||
if (!doSubPkg) {
|
||||
throw new Error("检测到此包是分包后的子包, 请通过 -s 参数指定存放路径后重试, 如 node wuWxapkg.js -s=/xxx/xxx ./testpkg/test-pkg-sub.wxapkg");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function doFile(name, cb, order) {
|
||||
for (let ord of order) if (ord.startsWith("s=")) global.subPack = ord.slice(3);
|
||||
console.log("Unpack file " + name + "...");
|
||||
@ -114,6 +189,7 @@ function doFile(name,cb,order){
|
||||
saveFile(dir, buf, genList(buf.slice(14, infoListLength + 14)));
|
||||
}, {});
|
||||
}
|
||||
|
||||
module.exports = {doFile: doFile};
|
||||
if (require.main === module) {
|
||||
wu.commandExecute(doFile, "Unpack a wxapkg file.\n\n[-o] [-d] [-s=<Main Dir>] <files...>\n\n-d Do not delete transformed unpacked files.\n-o Do not execute any operation after unpack.\n-s=<Main Dir> Regard all packages provided as subPackages and\n regard <Main Dir> as the directory of sources of the main package.\n<files...> wxapkg files to unpack");
|
||||
|
||||
92
wuWxml.js
92
wuWxml.js
@ -6,21 +6,25 @@ const path=require("path");
|
||||
const esprima = require('esprima');
|
||||
const {VM} = require('vm2');
|
||||
const escodegen = require('escodegen');
|
||||
|
||||
function analyze(core, z, namePool, xPool, fakePool = {}, zMulName = "0") {
|
||||
function anaRecursion(core, fakePool = {}) {
|
||||
return analyze(core, z, namePool, xPool, fakePool, zMulName);
|
||||
}
|
||||
|
||||
function push(name, elem) {
|
||||
namePool[name] = elem;
|
||||
}
|
||||
|
||||
function pushSon(pname, son) {
|
||||
if (fakePool[pname]) fakePool[pname].son.push(son);
|
||||
else namePool[pname].son.push(son);
|
||||
}
|
||||
for(let ei=0;ei<core.length;ei++){let e=core[ei];
|
||||
|
||||
for (let ei = 0; ei < core.length; ei++) {
|
||||
let e = core[ei];
|
||||
switch (e.type) {
|
||||
case "ExpressionStatement":
|
||||
{
|
||||
case "ExpressionStatement": {
|
||||
let f = e.expression;
|
||||
if (f.callee) {
|
||||
if (f.callee.type == "Identifier") {
|
||||
@ -34,8 +38,7 @@ function analyze(core,z,namePool,xPool,fakePool={},zMulName="0"){
|
||||
case "_":
|
||||
pushSon(f.arguments[0].name, namePool[f.arguments[1].name]);
|
||||
break;
|
||||
case "_2":
|
||||
{
|
||||
case "_2": {
|
||||
let item = f.arguments[6].value;//def:item
|
||||
let index = f.arguments[7].value;//def:index
|
||||
let data = z[f.arguments[0].value];
|
||||
@ -52,8 +55,7 @@ function analyze(core,z,namePool,xPool,fakePool={},zMulName="0"){
|
||||
if (key != "") obj.v["wx:key"] = key;
|
||||
}
|
||||
break;
|
||||
case "_2z":
|
||||
{
|
||||
case "_2z": {
|
||||
let item = f.arguments[7].value;//def:item
|
||||
let index = f.arguments[8].value;//def:index
|
||||
let data = z.mul[zMulName][f.arguments[1].value];
|
||||
@ -71,19 +73,27 @@ function analyze(core,z,namePool,xPool,fakePool={},zMulName="0"){
|
||||
}
|
||||
break;
|
||||
case "_ic":
|
||||
pushSon(f.arguments[5].name,{tag:"include",son:[],v:{src:xPool[f.arguments[0].property.value]}});
|
||||
pushSon(f.arguments[5].name, {
|
||||
tag: "include",
|
||||
son: [],
|
||||
v: {src: xPool[f.arguments[0].property.value]}
|
||||
});
|
||||
break;
|
||||
case "_ai":
|
||||
{//template import
|
||||
case "_ai": {//template import
|
||||
let to = Object.keys(fakePool)[0];
|
||||
if(to)pushSon(to,{tag:"import",son:[],v:{src:xPool[f.arguments[1].property.value]}});
|
||||
if (to) pushSon(to, {
|
||||
tag: "import",
|
||||
son: [],
|
||||
v: {src: xPool[f.arguments[1].property.value]}
|
||||
});
|
||||
else throw Error("Unexpected fake pool");
|
||||
}
|
||||
break;
|
||||
case "_af":
|
||||
//ignore _af
|
||||
break;
|
||||
default:throw Error("Unknown expression callee name "+f.callee.name);
|
||||
default:
|
||||
throw Error("Unknown expression callee name " + f.callee.name);
|
||||
}
|
||||
} else if (f.callee.type == "MemberExpression") {
|
||||
if (f.callee.object.name == "cs" || f.callee.property.name == "pop") break;
|
||||
@ -105,13 +115,20 @@ function analyze(core,z,namePool,xPool,fakePool={},zMulName="0"){
|
||||
push(dec.id.name, {tag: "block", son: [], v: {}});
|
||||
break;
|
||||
case "_o":
|
||||
push(dec.id.name,{tag:"__textNode__",textNode:true,content:z[dec.init.arguments[0].value]});
|
||||
push(dec.id.name, {
|
||||
tag: "__textNode__",
|
||||
textNode: true,
|
||||
content: z[dec.init.arguments[0].value]
|
||||
});
|
||||
break;
|
||||
case "_oz":
|
||||
push(dec.id.name,{tag:"__textNode__",textNode:true,content:z.mul[zMulName][dec.init.arguments[1].value]});
|
||||
push(dec.id.name, {
|
||||
tag: "__textNode__",
|
||||
textNode: true,
|
||||
content: z.mul[zMulName][dec.init.arguments[1].value]
|
||||
});
|
||||
break;
|
||||
case "_m":
|
||||
{
|
||||
case "_m": {
|
||||
if (dec.init.arguments[2].elements.length > 0)
|
||||
throw Error("Noticable generics content: " + dec.init.arguments[2].toString());
|
||||
let mv = {};
|
||||
@ -135,8 +152,7 @@ function analyze(core,z,namePool,xPool,fakePool={},zMulName="0"){
|
||||
push(dec.id.name, {tag: dec.init.arguments[0].value, son: [], v: mv});
|
||||
}
|
||||
break;
|
||||
case "_mz":
|
||||
{
|
||||
case "_mz": {
|
||||
if (dec.init.arguments[3].elements.length > 0)
|
||||
throw Error("Noticable generics content: " + dec.init.arguments[3].toString());
|
||||
let mv = {};
|
||||
@ -184,8 +200,7 @@ function analyze(core,z,namePool,xPool,fakePool={},zMulName="0"){
|
||||
Object.assign(namePool[obj].v, {is: is, data: data});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
{
|
||||
default: {
|
||||
let funName = dec.init.callee.name;
|
||||
if (funName.startsWith("gz$gwx")) {
|
||||
zMulName = funName.slice(6);
|
||||
@ -212,6 +227,7 @@ function analyze(core,z,namePool,xPool,fakePool={},zMulName="0"){
|
||||
else if (e.test.callee.name == "_oz") return z.mul[zMulName][e.test.arguments[1].value];
|
||||
else throw Error("Unknown if statement test callee name:" + e.test.callee.name);
|
||||
}
|
||||
|
||||
let vname = e.consequent.body[0].expression.left.object.name;
|
||||
let nif = {tag: "block", v: {"wx:if": parse_OFun(e)}, son: []};
|
||||
anaRecursion(e.consequent.body, {[vname]: nif});
|
||||
@ -237,20 +253,25 @@ function analyze(core,z,namePool,xPool,fakePool={},zMulName="0"){
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function wxmlify(str, isText) {
|
||||
if (typeof str == "undefined" || str === null) return "Empty";//throw Error("Empty str in "+(isText?"text":"prop"));
|
||||
if (isText) return str;//may have some bugs in some specific case(undocumented by tx)
|
||||
else return str.replace(/"/g, '\\"');
|
||||
}
|
||||
|
||||
function elemToString(elem, dep, moreInfo = false) {
|
||||
const longerList = [];//put tag name which can't be <x /> style.
|
||||
const indent = ' '.repeat(4);
|
||||
|
||||
function isTextTag(elem) {
|
||||
return elem.tag == "__textNode__" && elem.textNode;
|
||||
}
|
||||
|
||||
function elemRecursion(elem, dep) {
|
||||
return elemToString(elem, dep, moreInfo);
|
||||
}
|
||||
|
||||
function trimMerge(rets) {
|
||||
let needTrimLeft = false, ans = "";
|
||||
for (let ret of rets) {
|
||||
@ -267,6 +288,7 @@ function elemToString(elem,dep,moreInfo=false){
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
if (isTextTag(elem)) {
|
||||
//In comment, you can use typify text node, which beautify its code, but may destroy ui.
|
||||
//So, we use a "hack" way to solve this problem by letting typify program stop when face textNode
|
||||
@ -303,6 +325,7 @@ function elemToString(elem,dep,moreInfo=false){
|
||||
rets.push(indent.repeat(dep) + "</" + elem.tag + ">\n");
|
||||
return trimMerge(rets);
|
||||
}
|
||||
|
||||
function doWxml(state, dir, name, code, z, xPool, rDs, wxsList, moreInfo) {
|
||||
let rname = code.slice(code.lastIndexOf("return") + 6).replace(/[\;\}]/g, "").trim();
|
||||
code = code.slice(code.indexOf("\n"), code.lastIndexOf("return")).trim();
|
||||
@ -331,6 +354,7 @@ function doWxml(state,dir,name,code,z,xPool,rDs,wxsList,moreInfo){
|
||||
if (wxsList[name]) result.push(wxsList[name]);
|
||||
wu.save(name, result.join(""));
|
||||
}
|
||||
|
||||
function tryWxml(dir, name, code, z, xPool, rDs, ...args) {
|
||||
console.log("Decompile " + name + "...");
|
||||
let state = [null];
|
||||
@ -343,32 +367,41 @@ function tryWxml(dir,name,code,z,xPool,rDs,...args){
|
||||
else wu.save(path.resolve(dir, name + ".tem-" + state[0] + ".ori.js"), rDs[state[0]].toString());
|
||||
}
|
||||
}
|
||||
function doWxs(code){
|
||||
|
||||
function doWxs(code, name) {
|
||||
name = name || '';
|
||||
name = name.substring(0, name.lastIndexOf('/') + 1);
|
||||
const before = 'nv_module={nv_exports:{}};';
|
||||
return wxsBeautify(code.slice(code.indexOf(before)+before.length,code.lastIndexOf('return nv_module.nv_exports;}')).replace(/nv\_/g,''));
|
||||
return wxsBeautify(code.slice(code.indexOf(before) + before.length, code.lastIndexOf('return nv_module.nv_exports;}')).replace(eval('/' + ('p_' + name).replace(/\//g, '\\/') + '/g'), '').replace(/nv\_/g, '').replace(/(require\(.*?\))\(\)/g,'$1'));
|
||||
}
|
||||
function doFrame(name,cb,order){
|
||||
|
||||
function doFrame(name, cb, order, mainDir) {
|
||||
let moreInfo = order.includes("m");
|
||||
wxsList = {};
|
||||
wu.get(name, code => {
|
||||
getZ(code, z => {
|
||||
const before = "\nvar nv_require=function(){var nnm=";
|
||||
code=code.slice(code.indexOf(before)+before.length,code.lastIndexOf("if(path&&e_[path]){"));
|
||||
code = code.slice(code.lastIndexOf(before) + before.length, code.lastIndexOf("if(path&&e_[path]){"));
|
||||
json = code.slice(0, code.indexOf("};") + 1);
|
||||
let endOfRequire = code.indexOf("()\r\n") + 4;
|
||||
if (endOfRequire == 4 - 1) endOfRequire = code.indexOf("()\n") + 3;
|
||||
code = code.slice(endOfRequire);
|
||||
let rD={},rE={},rF={},requireInfo,x,vm=new VM({sandbox:{d_:rD,e_:rE,f_:rF,_vmRev_(data){
|
||||
let rD = {}, rE = {}, rF = {}, requireInfo = {}, x, vm = new VM({
|
||||
sandbox: {
|
||||
d_: rD, e_: rE, f_: rF, _vmRev_(data) {
|
||||
[x, requireInfo] = data;
|
||||
}, nv_require(path) {
|
||||
return () => path;
|
||||
}}});
|
||||
vm.run(code+"\n_vmRev_([x,"+json+"])");
|
||||
let dir=path.dirname(name),pF=[];
|
||||
}
|
||||
}
|
||||
});
|
||||
let vmCode = code + "\n_vmRev_([x," + json + "])";
|
||||
vm.run(vmCode);
|
||||
let dir = mainDir || path.dirname(name), pF = [];
|
||||
for (let info in rF) if (typeof rF[info] == "function") {
|
||||
let name = path.resolve(dir, (info[0] == '/' ? '.' : '') + info), ref = rF[info]();
|
||||
pF[ref] = info;
|
||||
wu.save(name,doWxs(requireInfo[ref].toString()));
|
||||
wu.save(name, doWxs(requireInfo[ref].toString(), info));
|
||||
}
|
||||
for (let info in rF) if (typeof rF[info] == "object") {
|
||||
let name = path.resolve(dir, (info[0] == '/' ? '.' : '') + info);
|
||||
@ -386,6 +419,7 @@ function doFrame(name,cb,order){
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {doFrame: doFrame};
|
||||
if (require.main === module) {
|
||||
wu.commandExecute(doFrame, "Restore wxml files.\n\n<files...>\n\n<files...> restore wxml file from page-frame.html or app-wxss.js.");
|
||||
|
||||
136
wuWxss.js
136
wuWxss.js
@ -4,21 +4,38 @@ const fs=require("fs");
|
||||
const {VM} = require('vm2');
|
||||
const cssbeautify = require('cssbeautify');
|
||||
const csstree = require('css-tree');
|
||||
function doWxss(dir,cb){
|
||||
function GwxCfg(){}
|
||||
GwxCfg.prototype={$gwx(){}};
|
||||
const cheerio = require('cheerio');
|
||||
|
||||
function doWxss(dir, cb, mainDir, nowDir) {
|
||||
let saveDir = dir;
|
||||
let isSubPkg = mainDir && mainDir.length > 0;
|
||||
if (isSubPkg) {
|
||||
saveDir = mainDir
|
||||
}
|
||||
|
||||
function GwxCfg() {
|
||||
}
|
||||
|
||||
GwxCfg.prototype = {
|
||||
$gwx() {
|
||||
}
|
||||
};
|
||||
for (let i = 0; i < 300; i++) GwxCfg.prototype["$gwx" + i] = GwxCfg.prototype.$gwx;
|
||||
let runList={},pureData={},result={},actualPure={},importCnt={},frameName="",onlyTest=true,blockCss=[];//custom block css file which won't be imported by others.(no extension name)
|
||||
let runList = {}, pureData = {}, result = {}, actualPure = {}, importCnt = {}, frameName = "", onlyTest = true,
|
||||
blockCss = [];//custom block css file which won't be imported by others.(no extension name)
|
||||
function cssRebuild(data) {//need to bind this as {cssFile:__name__} before call
|
||||
let cssFile;
|
||||
|
||||
function statistic(data) {
|
||||
function addStat(id) {
|
||||
if (!importCnt[id]) importCnt[id] = 1, statistic(pureData[id]);
|
||||
else ++importCnt[id];
|
||||
}
|
||||
|
||||
if (typeof data === "number") return addStat(data);
|
||||
for (let content of data) if (typeof content === "object" && content[0] == 2) addStat(content[1]);
|
||||
}
|
||||
|
||||
function makeup(data) {
|
||||
var isPure = typeof data === "number";
|
||||
if (onlyTest) {
|
||||
@ -57,53 +74,62 @@ function doWxss(dir,cb){
|
||||
} else res.push(content);
|
||||
return res.join("") + attach;
|
||||
}
|
||||
|
||||
return () => {
|
||||
cssFile = this.cssFile;
|
||||
if (!result[cssFile]) result[cssFile] = "";
|
||||
result[cssFile] += makeup(data);
|
||||
};
|
||||
}
|
||||
function runVM(name,code){
|
||||
// let wxAppCode={},handle={cssFile:name};
|
||||
// let vm=new VM({sandbox:Object.assign(new GwxCfg(),{__wxAppCode__:wxAppCode,setCssToHead:cssRebuild.bind(handle)})});
|
||||
// vm.run(code);
|
||||
// for(let name in wxAppCode)if(name.endsWith(".wxss")){
|
||||
// handle.cssFile=path.resolve(frameName,"..",name);
|
||||
// wxAppCode[name]();
|
||||
// }
|
||||
let wxAppCode = {};
|
||||
let handle = {cssFile: name};
|
||||
let gg = new GwxCfg();
|
||||
let tsandbox = {
|
||||
$gwx: GwxCfg.prototype["$gwx"],
|
||||
__mainPageFrameReady__: GwxCfg.prototype["$gwx"], //解决 $gwx is not defined
|
||||
__vd_version_info__: GwxCfg.prototype["$gwx"], //解决 __vd_version_info__ is not defined
|
||||
__wxAppCode__: wxAppCode,
|
||||
setCssToHead: cssRebuild.bind(handle)
|
||||
}
|
||||
|
||||
let vm = new VM({sandbox: tsandbox});
|
||||
function runVM(name, code) {
|
||||
let wxAppCode = {}, handle = {cssFile: name};
|
||||
let vm = new VM({
|
||||
sandbox: Object.assign(new GwxCfg(), {
|
||||
__wxAppCode__: wxAppCode,
|
||||
setCssToHead: cssRebuild.bind(handle),
|
||||
$gwx(path, global) {
|
||||
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// console.log('do css runVm: ' + name);
|
||||
vm.run(code);
|
||||
for (let name in wxAppCode) {
|
||||
handle.cssFile = path.resolve(saveDir, name);
|
||||
if (name.endsWith(".wxss")) {
|
||||
handle.cssFile = path.resolve(frameName, "..", name);
|
||||
wxAppCode[name]();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function preRun(dir, frameFile, mainCode, files, cb) {
|
||||
wu.addIO(cb);
|
||||
runList[path.resolve(dir, "./app.wxss")] = mainCode;
|
||||
for(let name of files)if(name!=frameFile){
|
||||
|
||||
for (let name of files) {
|
||||
if (name != frameFile) {
|
||||
wu.get(name, code => {
|
||||
code = code.replace(/display:-webkit-box;display:-webkit-flex;/gm, '');
|
||||
code = code.slice(0, code.indexOf("\n"));
|
||||
if(code.indexOf("setCssToHead")>-1)runList[name]=code.slice(code.indexOf("setCssToHead"));
|
||||
if (code.indexOf("setCssToHead(") > -1) {
|
||||
let lastName = name;
|
||||
let dirSplit = name.split(nowDir + '/');
|
||||
if (dirSplit.length > 1) {
|
||||
lastName = path.resolve(saveDir, dirSplit[1]);
|
||||
}
|
||||
runList[lastName] = code.slice(code.indexOf("setCssToHead("));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function runOnce() {
|
||||
for (let name in runList) runVM(name, runList[name]);
|
||||
}
|
||||
|
||||
function transformCss(style) {
|
||||
let ast = csstree.parse(style);
|
||||
csstree.walk(ast, function (node) {
|
||||
@ -129,7 +155,8 @@ function doWxss(dir,cb){
|
||||
node.children.remove(b);
|
||||
ans = a;
|
||||
} else {
|
||||
let xValue=x.value.children&&x.value.children.head&&x.value.children.head.data.name,yValue=y.value.children&&y.value.children.head&&y.value.children.head.data.name;
|
||||
let xValue = x.value.children && x.value.children.head && x.value.children.head.data.name,
|
||||
yValue = y.value.children && y.value.children.head && y.value.children.head.data.name;
|
||||
if (xValue && yValue) for (let type of removeType) if (xValue == `-${type}-${yValue}`) {
|
||||
node.children.remove(a);
|
||||
ans = b;
|
||||
@ -161,6 +188,7 @@ function doWxss(dir,cb){
|
||||
});
|
||||
return cssbeautify(csstree.generate(ast), {indent: ' ', autosemicolon: true});
|
||||
}
|
||||
|
||||
wu.scanDirByExt(dir, ".html", files => {
|
||||
let frameFile = "";
|
||||
if (fs.existsSync(path.resolve(dir, "page-frame.html")))
|
||||
@ -171,13 +199,49 @@ function doWxss(dir,cb){
|
||||
frameFile = path.resolve(dir, "page-frame.js");
|
||||
else throw Error("page-frame-like file is not found in the package by auto.");
|
||||
wu.get(frameFile, code => {
|
||||
code=code.slice(code.indexOf('var setCssToHead = function(file, _xcInvalid'));
|
||||
code=code.slice(code.indexOf('\nvar _C= ')+1);
|
||||
let oriCode=code;
|
||||
code = code.replace(/display:-webkit-box;display:-webkit-flex;/gm, '');
|
||||
let scriptCode = code;
|
||||
//extract script content from html
|
||||
if (frameFile.endsWith(".html")) {
|
||||
try {
|
||||
const $ = cheerio.load(code);
|
||||
scriptCode = [].join.apply($('html').find('script').map(function (item) {
|
||||
return $(this).html();
|
||||
}, "\n"));
|
||||
} catch (e) {
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
|
||||
let window = {
|
||||
screen: {
|
||||
width: 720,
|
||||
height: 1028,
|
||||
orientation: {
|
||||
type: 'vertical'
|
||||
}
|
||||
}
|
||||
};
|
||||
let navigator = {
|
||||
userAgent: "iPhone"
|
||||
};
|
||||
|
||||
scriptCode = scriptCode.slice(scriptCode.lastIndexOf('window.__wcc_version__'));
|
||||
let mainCode = 'window= ' + JSON.stringify(window) +
|
||||
';\nnavigator=' + JSON.stringify(navigator) +
|
||||
';\nvar __mainPageFrameReady__ = window.__mainPageFrameReady__ || function(){};var __WXML_GLOBAL__={entrys:{},defines:{},modules:{},ops:[],wxs_nf_init:undefined,total_ops:0};var __vd_version_info__=__vd_version_info__||{}' +
|
||||
";\n" + scriptCode;
|
||||
|
||||
//remove setCssToHead function
|
||||
mainCode = mainCode.replace('var setCssToHead = function', 'var setCssToHead2 = function');
|
||||
|
||||
code = code.slice(code.lastIndexOf('var setCssToHead = function(file, _xcInvalid'));
|
||||
code = code.slice(code.lastIndexOf('\nvar _C= ') + 1);
|
||||
|
||||
code = code.slice(0, code.indexOf('\n'));
|
||||
let vm = new VM({sandbox: {}});
|
||||
pureData = vm.run(code + "\n_C");
|
||||
let mainCode=oriCode.slice(oriCode.indexOf("setCssToHead"),oriCode.lastIndexOf(";var __pageFrameEndTime__"));
|
||||
|
||||
console.log("Guess wxss(first turn)...");
|
||||
preRun(dir, frameFile, mainCode, files, () => {
|
||||
frameName = frameFile;
|
||||
@ -190,7 +254,7 @@ function doWxss(dir,cb){
|
||||
if (importCnt[id] <= 1) {
|
||||
console.log("Cannot find pure import for _C[" + id + "] which is only imported " + importCnt[id] + " times. Let importing become copying.");
|
||||
} else {
|
||||
let newFile=path.resolve(dir,"__wuBaseWxss__/"+id+".wxss");
|
||||
let newFile = path.resolve(saveDir, "__wuBaseWxss__/" + id + ".wxss");
|
||||
console.log("Cannot find pure import for _C[" + id + "], force to save it in (" + newFile + ").");
|
||||
id = Number.parseInt(id);
|
||||
actualPure[id] = newFile;
|
||||
@ -200,7 +264,12 @@ function doWxss(dir,cb){
|
||||
console.log("Guess wxss(first turn) done.\nGenerate wxss(second turn)...");
|
||||
runOnce()
|
||||
console.log("Generate wxss(second turn) done.\nSave wxss...");
|
||||
for(let name in result)wu.save(wu.changeExt(name,".wxss"),transformCss(result[name]));
|
||||
|
||||
console.log('saveDir: ' + saveDir);
|
||||
for (let name in result) {
|
||||
let pathFile = path.resolve(saveDir, wu.changeExt(name, ".wxss"));
|
||||
wu.save(pathFile, transformCss(result[name]));
|
||||
}
|
||||
let delFiles = {};
|
||||
for (let name of files) delFiles[name] = 8;
|
||||
delFiles[frameFile] = 4;
|
||||
@ -209,6 +278,7 @@ function doWxss(dir,cb){
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {doWxss: doWxss};
|
||||
if (require.main === module) {
|
||||
wu.commandExecute(doWxss, "Restore wxss files.\n\n<dirs...>\n\n<dirs...> restore wxss file from a unpacked directory(Have page-frame.html (or app-wxss.js) and other html file).");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user