Repository navigation
Expand file tree
/
Copy pathwuConfig.js
More file actions
158 lines (150 loc) · 7.23 KB
/
Copy pathwuConfig.js
File metadata and controls
158 lines (150 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const wu = require("./wuLib.js");
const fs = require("fs");
const path = require("path");
const crypto = require("crypto");
const {createVM} = require("./wuSandbox.js");
function normalizeSubPackage(subPackage, pages, index = 0) {
const normalized = {...subPackage};
let root = typeof normalized.root === "string" ? normalized.root : "";
root = root.replace(/^\/+/, "");
if (root && !root.endsWith("/")) root += "/";
let sourcePages = normalized.pages;
if (!Array.isArray(sourcePages)) {
console.warn(`Ignore invalid subPackages[${index}].pages: expected an array.`);
sourcePages = [];
}
normalized.root = root;
normalized.pages = sourcePages.map(page => {
const pageName = String(page).replace(/^\/+/, "");
const relativePage = root && pageName.startsWith(root) ? pageName.slice(root.length) : pageName;
const fullPage = root + relativePage;
const pageIndex = pages.indexOf(fullPage);
if (pageIndex !== -1) pages.splice(pageIndex, 1);
return relativePage;
});
return normalized;
}
function getWorkerPath(name) {
let code = fs.readFileSync(name, {encoding: 'utf8'});
let commPath = false;
let vm = createVM({
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 => {
let e = JSON.parse(content);
let k = e.pages;
k.splice(k.indexOf(wu.changeExt(e.entryPagePath)), 1);
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) {
let subPackages = [];
let pages = app.pages;
for (let [index, subPackage] of e.subPackages.entries()) {
subPackages.push(normalizeSubPackage(subPackage, pages, index));
}
app.subPackages = subPackages;
app.pages = pages;
console.log("=======================================================\nNOTICE: SubPackages exist in this package.\nDetails: ", 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));
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)
for (let name in e.page[a].window.usingComponents) {
let componentPath = e.page[a].window.usingComponents[name] + ".html";
let file = componentPath.startsWith('/') ? componentPath.slice(1) : wu.toDir(path.resolve(path.dirname(a), componentPath), cur);
if (!e.page[file]) e.page[file] = {};
if (!e.page[file].window) e.page[file].window = {};
e.page[file].window.component = true;
}
if (fs.existsSync(path.resolve(dir, "app-service.js"))) {
let matches = fs.readFileSync(path.resolve(dir, "app-service.js"), {encoding: 'utf8'}).match(/\_\_wxAppCode\_\_\['[^\.]+\.json[^;]+\;/g);
if (matches) {
let attachInfo = {};
createVM({
__wxAppCode__: attachInfo
}).run(matches.join(""));
for (let name in attachInfo) e.page[wu.changeExt(name, ".html")] = {window: attachInfo[name]};
}
}
let delWeight = 8;
for (let a in e.page) {
let fileName = path.resolve(dir, wu.changeExt(a, ".json"));
wu.save(fileName, JSON.stringify(e.page[a].window, null, 4));
//添加默认的 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 (configFile == fileName) delWeight = 0;
}
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;
}
digestsEvent.add(() => {
for (let e of app.tabBar.list) {
e.pagePath = wu.changeExt(e.pagePath);
if (e.iconData) {
let hash = crypto.createHash("MD5").update(e.iconData, 'base64').digest();
for (let [buf, name] of digests) if (hash.equals(buf)) {
delete e.iconData;
e.iconPath = fixDir(name).replace(/\\/g, '/');
break;
}
}
if (e.selectedIconData) {
let hash = crypto.createHash("MD5").update(e.selectedIconData, 'base64').digest();
for (let [buf, name] of digests) if (hash.equals(buf)) {
delete e.selectedIconData;
e.selectedIconPath = fixDir(name).replace(/\\/g, '/');
break;
}
}
}
wu.save(path.resolve(dir, 'app.json'), JSON.stringify(app, null, 4));
cb({[configFile]: delWeight});
});
for (let name of li) {
digestsEvent.encount();
wu.get(name, data => {
digests.push([crypto.createHash("MD5").update(data).digest(), name]);
digestsEvent.decount();
}, {});
}
}); else {
wu.save(path.resolve(dir, 'app.json'), JSON.stringify(app, null, 4));
cb({[configFile]: delWeight});
}
});
}
module.exports = {doConfig: doConfig, normalizeSubPackage: normalizeSubPackage};
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.");
}