jh5_app_build 1.0.19 → 1.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/build_std.js +273 -155
- package/lib/build_std_apk_tool.js +230 -0
- package/lib/build_std_zip.js +216 -0
- package/package.json +1 -1
package/lib/build_std.js
CHANGED
|
@@ -3,82 +3,210 @@ let archiver = require('archiver');
|
|
|
3
3
|
let jutils = require('base_parts');
|
|
4
4
|
|
|
5
5
|
let fs = require('fs');
|
|
6
|
+
let path = require('path');
|
|
6
7
|
let os = require('os');
|
|
7
8
|
let AdmZip = require('adm-zip');
|
|
8
9
|
let child_process = require('child_process');
|
|
9
10
|
|
|
10
11
|
|
|
12
|
+
/**
|
|
13
|
+
* 创建目录并获取尾部带斜线的路径
|
|
14
|
+
* @param {string} dirPath 路径
|
|
15
|
+
* @returns {string} 路径(尾部带斜线)
|
|
16
|
+
*/
|
|
17
|
+
function makeAndGetDir(dirPath) {
|
|
18
|
+
try {
|
|
19
|
+
fs.mkdirSync(dirPath);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
}
|
|
22
|
+
return path.resolve(dirPath) + "\\";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 创建目录并复制文件
|
|
27
|
+
* @param {string} srcFile 源文件
|
|
28
|
+
* @param {string} dstFile 目标文件
|
|
29
|
+
* @returns {void}
|
|
30
|
+
*/
|
|
31
|
+
async function makeDirAndCopy(srcFile, dstFile) {
|
|
32
|
+
await jutils.jfile_utils.createDirs(dstFile, true);
|
|
33
|
+
fs.copyFileSync(srcFile, dstFile);
|
|
34
|
+
}
|
|
35
|
+
|
|
11
36
|
module.exports = async (cmd, buildCfg) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
37
|
+
console.log(`build file to ${buildCfg.outpath}`);
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
// 模板目录
|
|
41
|
+
var stdBinDir = `${buildCfg.rootPath}/jbuild_data/std_bins`;
|
|
42
|
+
|
|
43
|
+
// jre目录
|
|
44
|
+
let jreCwd = "C:\\jdev_develop\\jre\\bin";
|
|
45
|
+
|
|
46
|
+
// apk配置
|
|
47
|
+
let apkConfig = makeAndGetDir(`${buildCfg.rootPath}/apk_cfg`);
|
|
48
|
+
|
|
49
|
+
// 读取数据
|
|
50
|
+
var apkinfoObj = {
|
|
51
|
+
appName: "" // apk名称
|
|
52
|
+
};
|
|
53
|
+
try {
|
|
54
|
+
apkinfoObj = JSON.parse(fs.readFileSync(apkConfig + "/apkinfo.json", 'utf-8'));
|
|
55
|
+
} catch (error) {
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 清理数据
|
|
59
|
+
console.log(`delete temp file ...`);
|
|
60
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/device_webapi");
|
|
61
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/h5app");
|
|
62
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/jext");
|
|
63
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/screen");
|
|
64
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/build");
|
|
65
|
+
|
|
66
|
+
// 修改版本号
|
|
67
|
+
let platformVer = jutils.getUUID();
|
|
68
|
+
try {
|
|
69
|
+
console.log(`change dist ver ...`);
|
|
70
|
+
let repFile = `${buildCfg.rootPath}\\dist\\index.html`;
|
|
71
|
+
let indexStr = fs.readFileSync(repFile, "utf8");
|
|
72
|
+
indexStr = indexStr.replace(/#J_APPNAME#/g, buildCfg.appName);
|
|
73
|
+
indexStr = indexStr.replace(/#J_DATE#/g, buildCfg.dateStr);
|
|
74
|
+
|
|
75
|
+
// 读取H5版本
|
|
76
|
+
try {
|
|
77
|
+
let bInfoStr = fs.readFileSync(`${buildCfg.rootPath}/jbuild_data/info.txt`, "utf8");
|
|
78
|
+
let bInfoObj = JSON.parse(bInfoStr);
|
|
79
|
+
if (bInfoObj.version) {
|
|
80
|
+
platformVer = bInfoObj.version;
|
|
81
|
+
indexStr = indexStr.replace(/#J_H5_VER#/g, bInfoObj.version);
|
|
31
82
|
}
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.log(`no h5Ver!`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 保存
|
|
88
|
+
fs.writeFileSync(repFile, indexStr);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 添加用户文件
|
|
93
|
+
{
|
|
94
|
+
let pathSub = "dist";
|
|
95
|
+
console.log(`scaning ${pathSub} ...`);
|
|
96
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
97
|
+
dot: true,
|
|
98
|
+
sync: true,
|
|
99
|
+
nodir: true,
|
|
100
|
+
cwd: buildCfg.rootPath,
|
|
101
|
+
ignore: []
|
|
32
102
|
});
|
|
33
|
-
|
|
34
|
-
|
|
103
|
+
if (fileList.length === 0) {
|
|
104
|
+
console.error("no dist files!");
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
108
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/h5app");
|
|
109
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
110
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
111
|
+
|
|
112
|
+
// 添加文件
|
|
113
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 添加启动屏文件
|
|
118
|
+
{
|
|
119
|
+
let pathSub = "static/screen";
|
|
120
|
+
console.log(`scaning ${pathSub} ...`);
|
|
121
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
122
|
+
dot: true,
|
|
123
|
+
sync: true,
|
|
124
|
+
nodir: true,
|
|
125
|
+
cwd: buildCfg.rootPath,
|
|
126
|
+
ignore: []
|
|
35
127
|
});
|
|
36
|
-
|
|
128
|
+
if (fileList.length === 0) {
|
|
129
|
+
console.error("no dist files!");
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
133
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/screen");
|
|
134
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
135
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
37
136
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
let repFile = `${buildCfg.rootPath}\\dist\\index.html`;
|
|
43
|
-
let indexStr = fs.readFileSync(repFile, "utf8");
|
|
44
|
-
indexStr = indexStr.replace(/#J_APPNAME#/g, buildCfg.appName);
|
|
45
|
-
indexStr = indexStr.replace(/#J_DATE#/g, buildCfg.dateStr);
|
|
46
|
-
|
|
47
|
-
// 读取H5版本
|
|
48
|
-
try {
|
|
49
|
-
let bInfoStr = fs.readFileSync(`${buildCfg.rootPath}/jbuild_data/info.txt`, "utf8");
|
|
50
|
-
let bInfoObj = JSON.parse(bInfoStr);
|
|
51
|
-
if (bInfoObj.version) {
|
|
52
|
-
platformVer = bInfoObj.version;
|
|
53
|
-
indexStr = indexStr.replace(/#J_H5_VER#/g, bInfoObj.version);
|
|
54
|
-
}
|
|
55
|
-
} catch (error) {
|
|
56
|
-
console.log(`no h5Ver!`);
|
|
57
|
-
}
|
|
137
|
+
// 添加文件
|
|
138
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
58
141
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
142
|
+
// 添加so文件
|
|
143
|
+
{
|
|
144
|
+
let pathSub = "static/ndkutils";
|
|
145
|
+
console.log(`scaning ${pathSub} ...`);
|
|
146
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
147
|
+
dot: true,
|
|
148
|
+
sync: true,
|
|
149
|
+
nodir: true,
|
|
150
|
+
cwd: buildCfg.rootPath,
|
|
151
|
+
ignore: []
|
|
152
|
+
});
|
|
153
|
+
if (fileList.length === 0) {
|
|
154
|
+
console.error("no dist files!");
|
|
155
|
+
return;
|
|
62
156
|
}
|
|
157
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
158
|
+
let baseDir = makeAndGetDir(stdBinDir + "/lib");
|
|
159
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
160
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
63
161
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
162
|
+
// 添加文件
|
|
163
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
68
166
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
167
|
+
// 添加图标
|
|
168
|
+
try {
|
|
169
|
+
await makeDirAndCopy(`${apkConfig}/icon.png`, stdBinDir + "/res/mipmap/icon.png");
|
|
170
|
+
} catch (error) {
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 修改其他信息
|
|
174
|
+
try {
|
|
175
|
+
var AndroidManifestPath = stdBinDir + "/AndroidManifest.xml";
|
|
176
|
+
var AndroidManifestPathBak = stdBinDir + "/AndroidManifestBak.xml";
|
|
177
|
+
// 备份
|
|
178
|
+
if (!fs.existsSync(AndroidManifestPathBak)) {
|
|
179
|
+
fs.copyFileSync(AndroidManifestPath, AndroidManifestPathBak);
|
|
180
|
+
}
|
|
181
|
+
var anfData = fs.readFileSync(AndroidManifestPathBak);
|
|
182
|
+
|
|
183
|
+
// 修改应用名
|
|
184
|
+
if (apkinfoObj.appName) {
|
|
185
|
+
let tmpName = Buffer.from("7A3657B017788420AE749039F9D25A2E", 'utf16le');
|
|
186
|
+
var nameIndex = anfData.indexOf(tmpName);
|
|
187
|
+
if (nameIndex > 0) {
|
|
188
|
+
let toName = Buffer.from(apkinfoObj.appName, 'utf16le');
|
|
189
|
+
// 清空
|
|
190
|
+
for (let index = 0; index < tmpName.length; index++) {
|
|
191
|
+
anfData[nameIndex + index] = 0;
|
|
75
192
|
}
|
|
76
|
-
|
|
193
|
+
// 写入名
|
|
194
|
+
for (let index = 0; index < toName.length; index++) {
|
|
195
|
+
anfData[nameIndex + index] = toName[index];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
77
198
|
}
|
|
78
199
|
|
|
79
|
-
//
|
|
200
|
+
// 写入
|
|
201
|
+
fs.writeFileSync(AndroidManifestPath, anfData);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// 需要web
|
|
206
|
+
if (!buildCfg.platform.includes("noweb")) {
|
|
207
|
+
// 添加框架二进制
|
|
80
208
|
{
|
|
81
|
-
let pathSub = "
|
|
209
|
+
let pathSub = "jbuild_data/jext";
|
|
82
210
|
console.log(`scaning ${pathSub} ...`);
|
|
83
211
|
let fileList = glob(`${pathSub}/**/*`, {
|
|
84
212
|
dot: true,
|
|
@@ -87,123 +215,113 @@ module.exports = async (cmd, buildCfg) => {
|
|
|
87
215
|
cwd: buildCfg.rootPath,
|
|
88
216
|
ignore: []
|
|
89
217
|
});
|
|
90
|
-
if (fileList.length === 0) {
|
|
91
|
-
console.error("no dist files!");
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
218
|
console.log(`add [${fileList.length}] files ...`);
|
|
219
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/jext");
|
|
95
220
|
for (let index = 0; index < fileList.length; index++) {
|
|
96
221
|
let element = fileList[index].substr(pathSub.length + 1);
|
|
97
222
|
|
|
98
223
|
// 添加文件
|
|
99
|
-
|
|
224
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
100
225
|
}
|
|
101
226
|
}
|
|
102
227
|
|
|
103
|
-
//
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
let fileList = glob(`${pathSub}/**/*`, {
|
|
110
|
-
dot: true,
|
|
111
|
-
sync: true,
|
|
112
|
-
nodir: true,
|
|
113
|
-
cwd: buildCfg.rootPath,
|
|
114
|
-
ignore: []
|
|
115
|
-
});
|
|
116
|
-
console.log(`add [${fileList.length}] files ...`);
|
|
117
|
-
for (let index = 0; index < fileList.length; index++) {
|
|
118
|
-
let element = fileList[index].substr(pathSub.length + 1);
|
|
119
|
-
|
|
120
|
-
// 添加文件
|
|
121
|
-
archive.append(fs.readFileSync(`${buildCfg.rootPath}/${pathSub}/${element}`), { name: "assets/jext/" + element });
|
|
122
|
-
}
|
|
123
|
-
}
|
|
228
|
+
// 添加后台服务
|
|
229
|
+
{
|
|
230
|
+
console.log(`add device_webapi files ...`);
|
|
231
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/device_webapi");
|
|
232
|
+
// 载入zip
|
|
233
|
+
let myZip = new AdmZip(`${buildCfg.rootPath}/jbuild_data/device_webapi.zip`);
|
|
124
234
|
|
|
125
|
-
//
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
235
|
+
// 循环添加
|
|
236
|
+
let zipEntries = myZip.getEntries();
|
|
237
|
+
zipEntries.forEach(function (zipEntry) {
|
|
238
|
+
fs.writeFileSync(baseDir + zipEntry.entryName, zipEntry.getData());
|
|
239
|
+
});
|
|
130
240
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
archive.append(zipEntry.getData(), { name: "assets/device_webapi/" + zipEntry.entryName });
|
|
135
|
-
});
|
|
241
|
+
// 版本号
|
|
242
|
+
fs.writeFileSync(baseDir + "info.txt", Buffer.from(platformVer));
|
|
243
|
+
}
|
|
136
244
|
|
|
137
|
-
|
|
138
|
-
archive.append(Buffer.from(platformVer), { name: "assets/device_webapi/info.txt" });
|
|
139
|
-
}
|
|
245
|
+
}
|
|
140
246
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
247
|
+
// 添加设备信息
|
|
248
|
+
try {
|
|
249
|
+
let pkgInfo = os.networkInterfaces();
|
|
250
|
+
pkgInfo.buildTime = jutils.getNowDate();
|
|
251
|
+
fs.writeFileSync(stdBinDir + "/assets/info.txt", JSON.stringify(pkgInfo, null, 4));
|
|
252
|
+
} catch (error) {
|
|
253
|
+
}
|
|
149
254
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
255
|
+
// 打包
|
|
256
|
+
console.log(`making apk...`);
|
|
257
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\java.exe", [
|
|
258
|
+
"-jar", "C:\\jdev_develop\\h5_bin\\apktool.jar",
|
|
259
|
+
"b", stdBinDir,
|
|
260
|
+
"-o", buildCfg.outpath
|
|
261
|
+
], {
|
|
262
|
+
cwd: jreCwd
|
|
158
263
|
});
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
264
|
+
|
|
265
|
+
// 生成apk签名
|
|
266
|
+
let keyStoreFile = `${apkConfig}/apk_cert.keystore`;
|
|
267
|
+
if (!fs.existsSync(keyStoreFile)) {
|
|
268
|
+
console.log(`not find keystore,making ...`);
|
|
269
|
+
// 生成签名
|
|
270
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\keytool.exe", [
|
|
271
|
+
"-genkey",
|
|
272
|
+
"-storepass", "123456",
|
|
273
|
+
"-keypass", "123456",
|
|
274
|
+
"-alias", "cert",
|
|
275
|
+
"-keyalg", "RSA",
|
|
276
|
+
"-validity", "20000",
|
|
277
|
+
"-keystore", keyStoreFile,
|
|
278
|
+
"-dname", "CN=10.10.6.100,OU=utils,O=utils,L=beijing,ST=beijing,c=cn"
|
|
279
|
+
], {
|
|
280
|
+
cwd: jreCwd
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// 签名
|
|
285
|
+
console.log(`sign apk ...`);
|
|
286
|
+
var singApkTmp = buildCfg.outpath + "_sign.apk";
|
|
287
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\jarsigner.exe", [
|
|
288
|
+
"-verbose",
|
|
169
289
|
"-storepass", "123456",
|
|
170
290
|
"-keypass", "123456",
|
|
171
|
-
"-alias", "apk_cert.keystore",
|
|
172
|
-
"-keyalg", "RSA",
|
|
173
|
-
"-validity", "20000",
|
|
174
291
|
"-keystore", keyStoreFile,
|
|
175
|
-
"-
|
|
292
|
+
"-signedjar", singApkTmp,
|
|
293
|
+
buildCfg.outpath,
|
|
294
|
+
"cert"
|
|
176
295
|
], {
|
|
177
296
|
cwd: jreCwd
|
|
178
297
|
});
|
|
179
|
-
|
|
298
|
+
fs.unlinkSync(buildCfg.outpath); // 删除未签名的
|
|
180
299
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
console.log(`build success:${buildCfg.outpath}`);
|
|
300
|
+
// 对齐apk
|
|
301
|
+
console.log(`zip align ...`);
|
|
302
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\zipalign.exe", [
|
|
303
|
+
"-v", "4",
|
|
304
|
+
singApkTmp,
|
|
305
|
+
buildCfg.outpath
|
|
306
|
+
], {
|
|
307
|
+
cwd: jreCwd
|
|
308
|
+
});
|
|
309
|
+
fs.unlinkSync(singApkTmp); // 删除未对齐的
|
|
310
|
+
|
|
311
|
+
// 完成
|
|
312
|
+
console.log(`build success:${buildCfg.outpath}`);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
// 清理文件
|
|
315
|
+
try {
|
|
316
|
+
jutils.jfile_utils.deleteFile(buildCfg.outpath);
|
|
317
|
+
} catch (error2) {
|
|
318
|
+
}
|
|
319
|
+
// 延迟报错
|
|
320
|
+
setTimeout(() => {
|
|
321
|
+
console.error("build error,please run [jh5 update] first.");
|
|
322
|
+
}, 500);
|
|
323
|
+
// 向上抛出
|
|
324
|
+
throw error;
|
|
325
|
+
}
|
|
208
326
|
}
|
|
209
327
|
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
let glob = require("glob")
|
|
2
|
+
let archiver = require('archiver');
|
|
3
|
+
let jutils = require('base_parts');
|
|
4
|
+
|
|
5
|
+
let fs = require('fs');
|
|
6
|
+
let path = require('path');
|
|
7
|
+
let os = require('os');
|
|
8
|
+
let AdmZip = require('adm-zip');
|
|
9
|
+
let child_process = require('child_process');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 创建目录并获取尾部带斜线的路径
|
|
14
|
+
* @param {string} dirPath 路径
|
|
15
|
+
* @returns {string} 路径(尾部带斜线)
|
|
16
|
+
*/
|
|
17
|
+
function makeAndGetDir(dirPath) {
|
|
18
|
+
try {
|
|
19
|
+
fs.mkdirSync(dirPath);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
}
|
|
22
|
+
return path.resolve(dirPath) + "\\";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 创建目录并复制文件
|
|
27
|
+
* @param {string} srcFile 源文件
|
|
28
|
+
* @param {string} dstFile 目标文件
|
|
29
|
+
* @returns {void}
|
|
30
|
+
*/
|
|
31
|
+
async function makeDirAndCopy(srcFile, dstFile) {
|
|
32
|
+
await jutils.jfile_utils.createDirs(dstFile, true);
|
|
33
|
+
fs.copyFileSync(srcFile, dstFile);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = async (cmd, buildCfg) => {
|
|
37
|
+
console.log(`build file to ${buildCfg.outpath}`);
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
// 模板目录
|
|
41
|
+
var stdBinDir = `${buildCfg.rootPath}/jbuild_data/std_bins`;
|
|
42
|
+
|
|
43
|
+
// jre目录
|
|
44
|
+
let jreCwd = "C:\\jdev_develop\\jre\\bin";
|
|
45
|
+
|
|
46
|
+
// 清理数据
|
|
47
|
+
console.log(`delete temp file ...`);
|
|
48
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/device_webapi");
|
|
49
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/h5app");
|
|
50
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/jext");
|
|
51
|
+
|
|
52
|
+
// 修改版本号
|
|
53
|
+
let platformVer = jutils.getUUID();
|
|
54
|
+
try {
|
|
55
|
+
console.log(`change dist ver ...`);
|
|
56
|
+
let repFile = `${buildCfg.rootPath}\\dist\\index.html`;
|
|
57
|
+
let indexStr = fs.readFileSync(repFile, "utf8");
|
|
58
|
+
indexStr = indexStr.replace(/#J_APPNAME#/g, buildCfg.appName);
|
|
59
|
+
indexStr = indexStr.replace(/#J_DATE#/g, buildCfg.dateStr);
|
|
60
|
+
|
|
61
|
+
// 读取H5版本
|
|
62
|
+
try {
|
|
63
|
+
let bInfoStr = fs.readFileSync(`${buildCfg.rootPath}/jbuild_data/info.txt`, "utf8");
|
|
64
|
+
let bInfoObj = JSON.parse(bInfoStr);
|
|
65
|
+
if (bInfoObj.version) {
|
|
66
|
+
platformVer = bInfoObj.version;
|
|
67
|
+
indexStr = indexStr.replace(/#J_H5_VER#/g, bInfoObj.version);
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.log(`no h5Ver!`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 保存
|
|
74
|
+
fs.writeFileSync(repFile, indexStr);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 添加用户文件
|
|
79
|
+
{
|
|
80
|
+
let pathSub = "dist";
|
|
81
|
+
console.log(`scaning ${pathSub} ...`);
|
|
82
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
83
|
+
dot: true,
|
|
84
|
+
sync: true,
|
|
85
|
+
nodir: true,
|
|
86
|
+
cwd: buildCfg.rootPath,
|
|
87
|
+
ignore: []
|
|
88
|
+
});
|
|
89
|
+
if (fileList.length === 0) {
|
|
90
|
+
console.error("no dist files!");
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
94
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/h5app");
|
|
95
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
96
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
97
|
+
|
|
98
|
+
// 添加文件
|
|
99
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 需要web
|
|
104
|
+
if (!buildCfg.platform.includes("noweb")) {
|
|
105
|
+
// 添加框架二进制
|
|
106
|
+
{
|
|
107
|
+
let pathSub = "jbuild_data/jext";
|
|
108
|
+
console.log(`scaning ${pathSub} ...`);
|
|
109
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
110
|
+
dot: true,
|
|
111
|
+
sync: true,
|
|
112
|
+
nodir: true,
|
|
113
|
+
cwd: buildCfg.rootPath,
|
|
114
|
+
ignore: []
|
|
115
|
+
});
|
|
116
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
117
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/jext");
|
|
118
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
119
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
120
|
+
|
|
121
|
+
// 添加文件
|
|
122
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 添加后台服务
|
|
127
|
+
{
|
|
128
|
+
console.log(`add device_webapi files ...`);
|
|
129
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/device_webapi");
|
|
130
|
+
// 载入zip
|
|
131
|
+
let myZip = new AdmZip(`${buildCfg.rootPath}/jbuild_data/device_webapi.zip`);
|
|
132
|
+
|
|
133
|
+
// 循环添加
|
|
134
|
+
let zipEntries = myZip.getEntries();
|
|
135
|
+
zipEntries.forEach(function (zipEntry) {
|
|
136
|
+
fs.writeFileSync(baseDir + zipEntry.entryName, zipEntry.getData());
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// 版本号
|
|
140
|
+
fs.writeFileSync(baseDir + "info.txt", Buffer.from(platformVer));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
}
|
|
144
|
+
// 添加设备信息
|
|
145
|
+
try {
|
|
146
|
+
let pkgInfo = os.networkInterfaces();
|
|
147
|
+
pkgInfo.buildTime = jutils.getNowDate();
|
|
148
|
+
fs.writeFileSync(stdBinDir + "/assets/info.txt", JSON.stringify(pkgInfo, null, 4));
|
|
149
|
+
} catch (error) {
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// 打包
|
|
153
|
+
console.log(`making apk...`);
|
|
154
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\java.exe", [
|
|
155
|
+
"-jar", "C:\\jdev_develop\\h5_bin\\apktool.jar",
|
|
156
|
+
"b", stdBinDir,
|
|
157
|
+
"-o", buildCfg.outpath
|
|
158
|
+
], {
|
|
159
|
+
cwd: jreCwd
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// 生成apk签名
|
|
163
|
+
console.log(`not find keystore,making ...`);
|
|
164
|
+
let keyStoreFile = `${buildCfg.rootPath}/sigin/apk_cert.keystore`;
|
|
165
|
+
try {
|
|
166
|
+
fs.mkdirSync(path.dirname(keyStoreFile));
|
|
167
|
+
} catch (error) {
|
|
168
|
+
}
|
|
169
|
+
if (!fs.existsSync(keyStoreFile)) {
|
|
170
|
+
// 生成签名
|
|
171
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\keytool.exe", [
|
|
172
|
+
"-genkey",
|
|
173
|
+
"-storepass", "123456",
|
|
174
|
+
"-keypass", "123456",
|
|
175
|
+
"-alias", "cert",
|
|
176
|
+
"-keyalg", "RSA",
|
|
177
|
+
"-validity", "20000",
|
|
178
|
+
"-keystore", keyStoreFile,
|
|
179
|
+
"-dname", "CN=10.10.6.100,OU=utils,O=utils,L=beijing,ST=beijing,c=cn"
|
|
180
|
+
], {
|
|
181
|
+
cwd: jreCwd
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// 签名
|
|
186
|
+
console.log(`sign apk ...`);
|
|
187
|
+
var singApkTmp = buildCfg.outpath + "_sign.apk";
|
|
188
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\jarsigner.exe", [
|
|
189
|
+
"-verbose",
|
|
190
|
+
"-storepass", "123456",
|
|
191
|
+
"-keypass", "123456",
|
|
192
|
+
"-keystore", keyStoreFile,
|
|
193
|
+
"-signedjar", singApkTmp,
|
|
194
|
+
buildCfg.outpath,
|
|
195
|
+
"cert"
|
|
196
|
+
], {
|
|
197
|
+
cwd: jreCwd
|
|
198
|
+
});
|
|
199
|
+
fs.unlinkSync(buildCfg.outpath); // 删除未签名的
|
|
200
|
+
|
|
201
|
+
// 对齐apk
|
|
202
|
+
console.log(`zip align ...`);
|
|
203
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\zipalign.exe", [
|
|
204
|
+
"-v", "4",
|
|
205
|
+
singApkTmp,
|
|
206
|
+
buildCfg.outpath
|
|
207
|
+
], {
|
|
208
|
+
cwd: jreCwd
|
|
209
|
+
});
|
|
210
|
+
fs.unlinkSync(singApkTmp); // 删除未对齐的
|
|
211
|
+
|
|
212
|
+
// 完成
|
|
213
|
+
console.log(`build success:${buildCfg.outpath}`);
|
|
214
|
+
} catch (error) {
|
|
215
|
+
// 延迟报错
|
|
216
|
+
setTimeout(() => {
|
|
217
|
+
console.error("build error,please run [jh5 update] first.");
|
|
218
|
+
}, 500);
|
|
219
|
+
// 向上抛出
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
// 清理文件
|
|
224
|
+
try {
|
|
225
|
+
jutils.jfile_utils.deleteFile(buildCfg.outpath);
|
|
226
|
+
} catch (error) {
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
let glob = require("glob")
|
|
2
|
+
let archiver = require('archiver');
|
|
3
|
+
let jutils = require('base_parts');
|
|
4
|
+
|
|
5
|
+
let fs = require('fs');
|
|
6
|
+
let path = require('path');
|
|
7
|
+
let os = require('os');
|
|
8
|
+
let AdmZip = require('adm-zip');
|
|
9
|
+
let child_process = require('child_process');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
module.exports = async (cmd, buildCfg) => {
|
|
13
|
+
// 生成无签名的apk
|
|
14
|
+
await new Promise((resolve, reject) => {
|
|
15
|
+
(async () => {
|
|
16
|
+
// 建立压缩文件
|
|
17
|
+
console.log(`build file to ${buildCfg.outpath}`);
|
|
18
|
+
let output = fs.createWriteStream(buildCfg.outpath);
|
|
19
|
+
let archive = archiver('zip', {
|
|
20
|
+
zlib: { level: 9 }
|
|
21
|
+
});
|
|
22
|
+
output.on('close', function () {
|
|
23
|
+
console.log(archive.pointer() + ' total bytes');
|
|
24
|
+
resolve();
|
|
25
|
+
});
|
|
26
|
+
output.on('end', function () {
|
|
27
|
+
});
|
|
28
|
+
archive.on('warning', function (err) {
|
|
29
|
+
if (err.code === 'ENOENT') {
|
|
30
|
+
} else {
|
|
31
|
+
reject(err);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
archive.on('error', function (err) {
|
|
35
|
+
reject(err);
|
|
36
|
+
});
|
|
37
|
+
archive.pipe(output);
|
|
38
|
+
|
|
39
|
+
// 修改版本号
|
|
40
|
+
let platformVer = jutils.getUUID();
|
|
41
|
+
try {
|
|
42
|
+
console.log(`change dist ver ...`);
|
|
43
|
+
let repFile = `${buildCfg.rootPath}\\dist\\index.html`;
|
|
44
|
+
let indexStr = fs.readFileSync(repFile, "utf8");
|
|
45
|
+
indexStr = indexStr.replace(/#J_APPNAME#/g, buildCfg.appName);
|
|
46
|
+
indexStr = indexStr.replace(/#J_DATE#/g, buildCfg.dateStr);
|
|
47
|
+
|
|
48
|
+
// 读取H5版本
|
|
49
|
+
try {
|
|
50
|
+
let bInfoStr = fs.readFileSync(`${buildCfg.rootPath}/jbuild_data/info.txt`, "utf8");
|
|
51
|
+
let bInfoObj = JSON.parse(bInfoStr);
|
|
52
|
+
if (bInfoObj.version) {
|
|
53
|
+
platformVer = bInfoObj.version;
|
|
54
|
+
indexStr = indexStr.replace(/#J_H5_VER#/g, bInfoObj.version);
|
|
55
|
+
}
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.log(`no h5Ver!`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 保存
|
|
61
|
+
fs.writeFileSync(repFile, indexStr);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 载入apk
|
|
66
|
+
{
|
|
67
|
+
console.log(`load files ...`);
|
|
68
|
+
let myZip = new AdmZip(`${buildCfg.rootPath}/jbuild_data/std_bins/app-release.apk`);
|
|
69
|
+
|
|
70
|
+
// 循环添加
|
|
71
|
+
let zipEntries = myZip.getEntries();
|
|
72
|
+
zipEntries.forEach(function (zipEntry) {
|
|
73
|
+
// 过滤签名
|
|
74
|
+
if (zipEntry.entryName.toUpperCase().indexOf("META-INF/") !== 0) {
|
|
75
|
+
archive.append(zipEntry.getData(), { name: zipEntry.entryName });
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 添加用户文件
|
|
81
|
+
{
|
|
82
|
+
let pathSub = "dist";
|
|
83
|
+
console.log(`scaning ${pathSub} ...`);
|
|
84
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
85
|
+
dot: true,
|
|
86
|
+
sync: true,
|
|
87
|
+
nodir: true,
|
|
88
|
+
cwd: buildCfg.rootPath,
|
|
89
|
+
ignore: []
|
|
90
|
+
});
|
|
91
|
+
if (fileList.length === 0) {
|
|
92
|
+
console.error("no dist files!");
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
96
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
97
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
98
|
+
|
|
99
|
+
// 添加文件
|
|
100
|
+
archive.append(fs.readFileSync(`${buildCfg.rootPath}/${pathSub}/${element}`), { name: "assets/h5app/" + element });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// 需要web
|
|
105
|
+
if (!buildCfg.platform.includes("noweb")) {
|
|
106
|
+
// 添加框架二进制
|
|
107
|
+
{
|
|
108
|
+
let pathSub = "jbuild_data/jext";
|
|
109
|
+
console.log(`scaning ${pathSub} ...`);
|
|
110
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
111
|
+
dot: true,
|
|
112
|
+
sync: true,
|
|
113
|
+
nodir: true,
|
|
114
|
+
cwd: buildCfg.rootPath,
|
|
115
|
+
ignore: []
|
|
116
|
+
});
|
|
117
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
118
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
119
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
120
|
+
|
|
121
|
+
// 添加文件
|
|
122
|
+
archive.append(fs.readFileSync(`${buildCfg.rootPath}/${pathSub}/${element}`), { name: "assets/jext/" + element });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 添加后台服务
|
|
127
|
+
{
|
|
128
|
+
console.log(`add device_webapi files ...`);
|
|
129
|
+
// 载入zip
|
|
130
|
+
let myZip = new AdmZip(`${buildCfg.rootPath}/jbuild_data/device_webapi.zip`);
|
|
131
|
+
|
|
132
|
+
// 循环添加
|
|
133
|
+
let zipEntries = myZip.getEntries();
|
|
134
|
+
zipEntries.forEach(function (zipEntry) {
|
|
135
|
+
archive.append(zipEntry.getData(), { name: "assets/device_webapi/" + zipEntry.entryName });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// 版本号
|
|
139
|
+
archive.append(Buffer.from(platformVer), { name: "assets/device_webapi/info.txt" });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
}
|
|
143
|
+
// 添加设备信息
|
|
144
|
+
try {
|
|
145
|
+
let pkgInfo = os.networkInterfaces();
|
|
146
|
+
pkgInfo.buildTime = jutils.getNowDate();
|
|
147
|
+
archive.append(JSON.stringify(pkgInfo, null, 4), { name: "assets/build_info.txt" });
|
|
148
|
+
} catch (error) {
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 开始压缩
|
|
152
|
+
archive.finalize();
|
|
153
|
+
})().catch((err) => {
|
|
154
|
+
console.error("build error,please run [jh5 update] first.");
|
|
155
|
+
try {
|
|
156
|
+
jutils.jfile_utils.deleteFile(buildCfg.outpath);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// 生成apk签名
|
|
163
|
+
console.log(`not find keystore,making ...`);
|
|
164
|
+
let jreCwd = "C:\\jdev_develop\\jre\\bin";
|
|
165
|
+
let keyStoreFile = `${buildCfg.rootPath}/sigin/apk_cert.keystore`;
|
|
166
|
+
try {
|
|
167
|
+
fs.mkdirSync(path.dirname(keyStoreFile));
|
|
168
|
+
} catch (error) {
|
|
169
|
+
}
|
|
170
|
+
if (!fs.existsSync(keyStoreFile)) {
|
|
171
|
+
// 生成签名
|
|
172
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\keytool.exe", [
|
|
173
|
+
"-genkey",
|
|
174
|
+
"-storepass", "123456",
|
|
175
|
+
"-keypass", "123456",
|
|
176
|
+
"-alias", "cert",
|
|
177
|
+
"-keyalg", "RSA",
|
|
178
|
+
"-validity", "20000",
|
|
179
|
+
"-keystore", keyStoreFile,
|
|
180
|
+
"-dname", "CN=10.10.6.100,OU=utils,O=utils,L=beijing,ST=beijing,c=cn"
|
|
181
|
+
], {
|
|
182
|
+
cwd: jreCwd
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 签名
|
|
187
|
+
console.log(`sign apk ...`);
|
|
188
|
+
var singApkTmp = buildCfg.outpath + "_sign.apk";
|
|
189
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\jarsigner.exe", [
|
|
190
|
+
"-verbose",
|
|
191
|
+
"-storepass", "123456",
|
|
192
|
+
"-keypass", "123456",
|
|
193
|
+
"-keystore", keyStoreFile,
|
|
194
|
+
"-signedjar", singApkTmp,
|
|
195
|
+
buildCfg.outpath,
|
|
196
|
+
"cert"
|
|
197
|
+
], {
|
|
198
|
+
cwd: jreCwd
|
|
199
|
+
});
|
|
200
|
+
fs.unlinkSync(buildCfg.outpath); // 删除未签名的
|
|
201
|
+
|
|
202
|
+
// 对齐apk
|
|
203
|
+
console.log(`zip align ...`);
|
|
204
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\zipalign.exe", [
|
|
205
|
+
"-v", "4",
|
|
206
|
+
singApkTmp,
|
|
207
|
+
buildCfg.outpath
|
|
208
|
+
], {
|
|
209
|
+
cwd: jreCwd
|
|
210
|
+
});
|
|
211
|
+
fs.unlinkSync(singApkTmp); // 删除未对齐的
|
|
212
|
+
|
|
213
|
+
// 完成
|
|
214
|
+
console.log(`build success:${buildCfg.outpath}`);
|
|
215
|
+
}
|
|
216
|
+
|