jh5_app_build 1.0.20 → 1.0.23

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 CHANGED
@@ -9,77 +9,232 @@ let AdmZip = require('adm-zip');
9
9
  let child_process = require('child_process');
10
10
 
11
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
+
12
36
  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);
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
+ if (!apkinfoObj.appName) apkinfoObj.appName = "智能终端"
59
+
60
+ // 清理数据
61
+ console.log(`delete temp file ...`);
62
+ await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/device_webapi");
63
+ await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/h5app");
64
+ await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/jext");
65
+ await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/screen");
66
+ await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/sounds");
67
+ await jutils.jfile_utils.deleteFolder(stdBinDir + "/build");
68
+
69
+ // 修改版本号
70
+ let platformVer = jutils.getUUID();
71
+ try {
72
+ console.log(`change dist ver ...`);
73
+ let repFile = `${buildCfg.rootPath}\\dist\\index.html`;
74
+ let indexStr = fs.readFileSync(repFile, "utf8");
75
+ indexStr = indexStr.replace(/#J_APPNAME#/g, buildCfg.appName);
76
+ indexStr = indexStr.replace(/#J_DATE#/g, buildCfg.dateStr);
77
+
78
+ // 读取H5版本
79
+ try {
80
+ let bInfoStr = fs.readFileSync(`${buildCfg.rootPath}/jbuild_data/info.txt`, "utf8");
81
+ let bInfoObj = JSON.parse(bInfoStr);
82
+ if (bInfoObj.version) {
83
+ platformVer = bInfoObj.version;
84
+ indexStr = indexStr.replace(/#J_H5_VER#/g, bInfoObj.version);
32
85
  }
86
+ } catch (error) {
87
+ console.log(`no h5Ver!`);
88
+ }
89
+
90
+ // 保存
91
+ fs.writeFileSync(repFile, indexStr);
92
+ } catch (error) {
93
+ }
94
+
95
+ // 添加用户文件
96
+ {
97
+ let pathSub = "dist";
98
+ console.log(`scaning ${pathSub} ...`);
99
+ let fileList = glob(`${pathSub}/**/*`, {
100
+ dot: true,
101
+ sync: true,
102
+ nodir: true,
103
+ cwd: buildCfg.rootPath,
104
+ ignore: []
33
105
  });
34
- archive.on('error', function (err) {
35
- reject(err);
106
+ if (fileList.length === 0) {
107
+ console.error("no dist files!");
108
+ return;
109
+ }
110
+ console.log(`add [${fileList.length}] files ...`);
111
+ let baseDir = makeAndGetDir(stdBinDir + "/assets/h5app");
112
+ for (let index = 0; index < fileList.length; index++) {
113
+ let element = fileList[index].substr(pathSub.length + 1);
114
+
115
+ // 添加文件
116
+ await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
117
+ }
118
+ }
119
+
120
+ // 添加启动屏文件
121
+ {
122
+ let pathSub = "static/screen";
123
+ console.log(`scaning ${pathSub} ...`);
124
+ let fileList = glob(`${pathSub}/**/*`, {
125
+ dot: true,
126
+ sync: true,
127
+ nodir: true,
128
+ cwd: buildCfg.rootPath,
129
+ ignore: []
36
130
  });
37
- archive.pipe(output);
131
+ if (fileList.length === 0) {
132
+ console.error("no dist files!");
133
+ return;
134
+ }
135
+ console.log(`add [${fileList.length}] files ...`);
136
+ let baseDir = makeAndGetDir(stdBinDir + "/assets/screen");
137
+ for (let index = 0; index < fileList.length; index++) {
138
+ let element = fileList[index].substr(pathSub.length + 1);
38
139
 
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
- }
140
+ // 添加文件
141
+ await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
142
+ }
143
+ }
59
144
 
60
- // 保存
61
- fs.writeFileSync(repFile, indexStr);
62
- } catch (error) {
145
+ // 添加音频文件
146
+ {
147
+ let pathSub = "jbuild_res/audio_wav";
148
+ console.log(`scaning ${pathSub} ...`);
149
+ let fileList = glob(`${pathSub}/**/*`, {
150
+ dot: true,
151
+ sync: true,
152
+ nodir: true,
153
+ cwd: buildCfg.rootPath,
154
+ ignore: []
155
+ });
156
+ if (fileList.length === 0) {
157
+ console.error("no dist files!");
158
+ return;
63
159
  }
160
+ console.log(`add [${fileList.length}] files ...`);
161
+ let baseDir = makeAndGetDir(stdBinDir + "/assets/sounds");
162
+ for (let index = 0; index < fileList.length; index++) {
163
+ let element = fileList[index].substr(pathSub.length + 1);
64
164
 
65
- // 载入apk
66
- {
67
- console.log(`load files ...`);
68
- let myZip = new AdmZip(`${buildCfg.rootPath}/jbuild_data/std_bins/app-release.apk`);
165
+ // 添加文件
166
+ await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
167
+ }
168
+ }
69
169
 
70
- // 循环添加
71
- let zipEntries = myZip.getEntries();
72
- zipEntries.forEach(function (zipEntry) {
73
- // 过滤签名
74
- if (!["META-INF/CERT.RSA", "META-INF/CERT.SF", "META-INF/MANIFEST.MF"].includes(zipEntry.entryName.toUpperCase())) {
75
- archive.append(zipEntry.getData(), { name: zipEntry.entryName });
170
+ // 添加so文件
171
+ {
172
+ let pathSub = "static/ndkutils";
173
+ console.log(`scaning ${pathSub} ...`);
174
+ let fileList = glob(`${pathSub}/**/*`, {
175
+ dot: true,
176
+ sync: true,
177
+ nodir: true,
178
+ cwd: buildCfg.rootPath,
179
+ ignore: []
180
+ });
181
+ if (fileList.length === 0) {
182
+ console.error("no dist files!");
183
+ return;
184
+ }
185
+ console.log(`add [${fileList.length}] files ...`);
186
+ let baseDir = makeAndGetDir(stdBinDir + "/lib");
187
+ for (let index = 0; index < fileList.length; index++) {
188
+ let element = fileList[index].substr(pathSub.length + 1);
189
+
190
+ // 添加文件
191
+ await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
192
+ }
193
+ }
194
+
195
+ // 添加图标
196
+ try {
197
+ await makeDirAndCopy(`${apkConfig}/icon.png`, stdBinDir + "/res/mipmap/icon.png");
198
+ } catch (error) {
199
+ }
200
+
201
+ // 修改其他信息
202
+ try {
203
+ var AndroidManifestPath = stdBinDir + "/AndroidManifest.xml";
204
+ var AndroidManifestPathBak = stdBinDir + "/AndroidManifestBak.xml";
205
+ // 备份
206
+ if (!fs.existsSync(AndroidManifestPathBak)) {
207
+ fs.copyFileSync(AndroidManifestPath, AndroidManifestPathBak);
208
+ }
209
+ var anfData = fs.readFileSync(AndroidManifestPathBak);
210
+
211
+ // 修改应用名
212
+ if (apkinfoObj.appName) {
213
+ let tmpName = Buffer.from("7A3657B017788420AE749039F9D25A2E", 'utf16le');
214
+ var nameIndex = anfData.indexOf(tmpName);
215
+ if (nameIndex > 0) {
216
+ let toName = Buffer.from(apkinfoObj.appName, 'utf16le');
217
+ // 清空
218
+ for (let index = 0; index < tmpName.length; index++) {
219
+ anfData[nameIndex + index] = 0;
76
220
  }
77
- });
221
+ // 写入名
222
+ for (let index = 0; index < toName.length; index++) {
223
+ anfData[nameIndex + index] = toName[index];
224
+ }
225
+ }
78
226
  }
79
227
 
80
- // 添加用户文件
228
+ // 写入
229
+ fs.writeFileSync(AndroidManifestPath, anfData);
230
+ } catch (error) {
231
+ }
232
+
233
+ // 需要web
234
+ if (!buildCfg.platform.includes("noweb")) {
235
+ // 添加框架二进制
81
236
  {
82
- let pathSub = "dist";
237
+ let pathSub = "jbuild_data/jext";
83
238
  console.log(`scaning ${pathSub} ...`);
84
239
  let fileList = glob(`${pathSub}/**/*`, {
85
240
  dot: true,
@@ -88,129 +243,113 @@ module.exports = async (cmd, buildCfg) => {
88
243
  cwd: buildCfg.rootPath,
89
244
  ignore: []
90
245
  });
91
- if (fileList.length === 0) {
92
- console.error("no dist files!");
93
- return;
94
- }
95
246
  console.log(`add [${fileList.length}] files ...`);
247
+ let baseDir = makeAndGetDir(stdBinDir + "/assets/jext");
96
248
  for (let index = 0; index < fileList.length; index++) {
97
249
  let element = fileList[index].substr(pathSub.length + 1);
98
250
 
99
251
  // 添加文件
100
- archive.append(fs.readFileSync(`${buildCfg.rootPath}/${pathSub}/${element}`), { name: "assets/h5app/" + element });
252
+ await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
101
253
  }
102
254
  }
103
255
 
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
- }
256
+ // 添加后台服务
257
+ {
258
+ console.log(`add device_webapi files ...`);
259
+ let baseDir = makeAndGetDir(stdBinDir + "/assets/device_webapi");
260
+ // 载入zip
261
+ let myZip = new AdmZip(`${buildCfg.rootPath}/jbuild_data/device_webapi.zip`);
125
262
 
126
- // 添加后台服务
127
- {
128
- console.log(`add device_webapi files ...`);
129
- // 载入zip
130
- let myZip = new AdmZip(`${buildCfg.rootPath}/jbuild_data/device_webapi.zip`);
263
+ // 循环添加
264
+ let zipEntries = myZip.getEntries();
265
+ zipEntries.forEach(function (zipEntry) {
266
+ fs.writeFileSync(baseDir + zipEntry.entryName, zipEntry.getData());
267
+ });
131
268
 
132
- // 循环添加
133
- let zipEntries = myZip.getEntries();
134
- zipEntries.forEach(function (zipEntry) {
135
- archive.append(zipEntry.getData(), { name: "assets/device_webapi/" + zipEntry.entryName });
136
- });
269
+ // 版本号
270
+ fs.writeFileSync(baseDir + "info.txt", Buffer.from(platformVer));
271
+ }
137
272
 
138
- // 版本号
139
- archive.append(Buffer.from(platformVer), { name: "assets/device_webapi/info.txt" });
140
- }
273
+ }
141
274
 
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
- }
275
+ // 添加设备信息
276
+ try {
277
+ let pkgInfo = os.networkInterfaces();
278
+ pkgInfo.buildTime = jutils.getNowDate();
279
+ fs.writeFileSync(stdBinDir + "/assets/info.txt", JSON.stringify(pkgInfo, null, 4));
280
+ } catch (error) {
281
+ }
150
282
 
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
- }
283
+ // 打包
284
+ console.log(`making apk...`);
285
+ child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\java.exe", [
286
+ "-jar", "C:\\jdev_develop\\h5_bin\\apktool.jar",
287
+ "b", stdBinDir,
288
+ "-o", buildCfg.outpath
289
+ ], {
290
+ cwd: jreCwd
159
291
  });
160
- });
161
292
 
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",
293
+ // 生成apk签名
294
+ let keyStoreFile = `${apkConfig}/apk_cert.keystore`;
295
+ if (!fs.existsSync(keyStoreFile)) {
296
+ console.log(`not find keystore,making ...`);
297
+ // 生成签名
298
+ child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\keytool.exe", [
299
+ "-genkey",
300
+ "-storepass", "123456",
301
+ "-keypass", "123456",
302
+ "-alias", "cert",
303
+ "-keyalg", "RSA",
304
+ "-validity", "20000",
305
+ "-keystore", keyStoreFile,
306
+ "-dname", "CN=10.10.6.100,OU=utils,O=utils,L=beijing,ST=beijing,c=cn"
307
+ ], {
308
+ cwd: jreCwd
309
+ });
310
+ }
311
+
312
+ // 签名
313
+ console.log(`sign apk ...`);
314
+ var singApkTmp = buildCfg.outpath + "_sign.apk";
315
+ child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\jarsigner.exe", [
316
+ "-verbose",
174
317
  "-storepass", "123456",
175
318
  "-keypass", "123456",
176
- "-alias", "apk_cert",
177
- "-keyalg", "RSA",
178
- "-validity", "20000",
179
319
  "-keystore", keyStoreFile,
180
- "-dname", "CN=10.10.6.100,OU=utils,O=utils,L=beijing,ST=beijing,c=cn"
320
+ "-signedjar", singApkTmp,
321
+ buildCfg.outpath,
322
+ "cert"
181
323
  ], {
182
324
  cwd: jreCwd
183
325
  });
184
- }
326
+ fs.unlinkSync(buildCfg.outpath); // 删除未签名的
327
+
328
+ // 对齐apk
329
+ console.log(`zip align ...`);
330
+ child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\zipalign.exe", [
331
+ "-v", "4",
332
+ singApkTmp,
333
+ buildCfg.outpath
334
+ ], {
335
+ cwd: jreCwd
336
+ });
337
+ fs.unlinkSync(singApkTmp); // 删除未对齐的
185
338
 
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
- "apk_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}`);
339
+ // 完成
340
+ console.log(`build success:${buildCfg.outpath}`);
341
+ } catch (error) {
342
+ // 清理文件
343
+ try {
344
+ jutils.jfile_utils.deleteFile(buildCfg.outpath);
345
+ } catch (error2) {
346
+ }
347
+ // 延迟报错
348
+ setTimeout(() => {
349
+ console.error("build error,please run [jh5 update] first.");
350
+ }, 500);
351
+ // 向上抛出
352
+ throw error;
353
+ }
215
354
  }
216
355
 
@@ -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
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jh5_app_build",
3
- "version": "1.0.20",
3
+ "version": "1.0.23",
4
4
  "description": "jaskle jh5_app_build",
5
5
  "main": "./bin/jh5_app_build.js",
6
6
  "registry": true,