jh5_app_build 1.0.18 → 1.0.21
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 +234 -120
- 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,84 +3,162 @@ 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');
|
|
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
|
+
}
|
|
8
35
|
|
|
9
36
|
module.exports = async (cmd, buildCfg) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
} else {
|
|
29
|
-
reject(err);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
archive.on('error', function (err) {
|
|
33
|
-
reject(err);
|
|
34
|
-
});
|
|
35
|
-
archive.pipe(output);
|
|
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
|
+
console.log(`delete temp file ...`);
|
|
51
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/device_webapi");
|
|
52
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/h5app");
|
|
53
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/jext");
|
|
54
|
+
await jutils.jfile_utils.deleteFolder(stdBinDir + "/assets/screen");
|
|
36
55
|
|
|
37
|
-
|
|
38
|
-
|
|
56
|
+
// 修改版本号
|
|
57
|
+
let platformVer = jutils.getUUID();
|
|
58
|
+
try {
|
|
59
|
+
console.log(`change dist ver ...`);
|
|
60
|
+
let repFile = `${buildCfg.rootPath}\\dist\\index.html`;
|
|
61
|
+
let indexStr = fs.readFileSync(repFile, "utf8");
|
|
62
|
+
indexStr = indexStr.replace(/#J_APPNAME#/g, buildCfg.appName);
|
|
63
|
+
indexStr = indexStr.replace(/#J_DATE#/g, buildCfg.dateStr);
|
|
64
|
+
|
|
65
|
+
// 读取H5版本
|
|
39
66
|
try {
|
|
40
|
-
|
|
41
|
-
let
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
// 读取H5版本
|
|
47
|
-
try {
|
|
48
|
-
let bInfoStr = fs.readFileSync(`${buildCfg.rootPath}/jbuild_data/info.txt`, "utf8");
|
|
49
|
-
let bInfoObj = JSON.parse(bInfoStr);
|
|
50
|
-
if (bInfoObj.version) {
|
|
51
|
-
platformVer = bInfoObj.version;
|
|
52
|
-
indexStr = indexStr.replace(/#J_H5_VER#/g, bInfoObj.version);
|
|
53
|
-
}
|
|
54
|
-
} catch (error) {
|
|
55
|
-
console.log(`no h5Ver!`);
|
|
67
|
+
let bInfoStr = fs.readFileSync(`${buildCfg.rootPath}/jbuild_data/info.txt`, "utf8");
|
|
68
|
+
let bInfoObj = JSON.parse(bInfoStr);
|
|
69
|
+
if (bInfoObj.version) {
|
|
70
|
+
platformVer = bInfoObj.version;
|
|
71
|
+
indexStr = indexStr.replace(/#J_H5_VER#/g, bInfoObj.version);
|
|
56
72
|
}
|
|
57
|
-
|
|
58
|
-
// 保存
|
|
59
|
-
fs.writeFileSync(repFile, indexStr);
|
|
60
73
|
} catch (error) {
|
|
74
|
+
console.log(`no h5Ver!`);
|
|
61
75
|
}
|
|
62
76
|
|
|
63
|
-
//
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
77
|
+
// 保存
|
|
78
|
+
fs.writeFileSync(repFile, indexStr);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
}
|
|
67
81
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
82
|
+
// 添加用户文件
|
|
83
|
+
{
|
|
84
|
+
let pathSub = "dist";
|
|
85
|
+
console.log(`scaning ${pathSub} ...`);
|
|
86
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
87
|
+
dot: true,
|
|
88
|
+
sync: true,
|
|
89
|
+
nodir: true,
|
|
90
|
+
cwd: buildCfg.rootPath,
|
|
91
|
+
ignore: []
|
|
92
|
+
});
|
|
93
|
+
if (fileList.length === 0) {
|
|
94
|
+
console.error("no dist files!");
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
98
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/h5app");
|
|
99
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
100
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
101
|
+
|
|
102
|
+
// 添加文件
|
|
103
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 添加启动屏文件
|
|
108
|
+
{
|
|
109
|
+
let pathSub = "static/screen";
|
|
110
|
+
console.log(`scaning ${pathSub} ...`);
|
|
111
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
112
|
+
dot: true,
|
|
113
|
+
sync: true,
|
|
114
|
+
nodir: true,
|
|
115
|
+
cwd: buildCfg.rootPath,
|
|
116
|
+
ignore: []
|
|
117
|
+
});
|
|
118
|
+
if (fileList.length === 0) {
|
|
119
|
+
console.error("no dist files!");
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
123
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/screen");
|
|
124
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
125
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
126
|
+
|
|
127
|
+
// 添加文件
|
|
128
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 添加so文件
|
|
133
|
+
{
|
|
134
|
+
let pathSub = "static/ndkutils";
|
|
135
|
+
console.log(`scaning ${pathSub} ...`);
|
|
136
|
+
let fileList = glob(`${pathSub}/**/*`, {
|
|
137
|
+
dot: true,
|
|
138
|
+
sync: true,
|
|
139
|
+
nodir: true,
|
|
140
|
+
cwd: buildCfg.rootPath,
|
|
141
|
+
ignore: []
|
|
142
|
+
});
|
|
143
|
+
if (fileList.length === 0) {
|
|
144
|
+
console.error("no dist files!");
|
|
145
|
+
return;
|
|
79
146
|
}
|
|
147
|
+
console.log(`add [${fileList.length}] files ...`);
|
|
148
|
+
let baseDir = makeAndGetDir(stdBinDir + "/lib");
|
|
149
|
+
for (let index = 0; index < fileList.length; index++) {
|
|
150
|
+
let element = fileList[index].substr(pathSub.length + 1);
|
|
80
151
|
|
|
81
|
-
|
|
152
|
+
// 添加文件
|
|
153
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// 需要web
|
|
158
|
+
if (!buildCfg.platform.includes("noweb")) {
|
|
159
|
+
// 添加框架二进制
|
|
82
160
|
{
|
|
83
|
-
let pathSub = "
|
|
161
|
+
let pathSub = "jbuild_data/jext";
|
|
84
162
|
console.log(`scaning ${pathSub} ...`);
|
|
85
163
|
let fileList = glob(`${pathSub}/**/*`, {
|
|
86
164
|
dot: true,
|
|
@@ -89,76 +167,112 @@ module.exports = async (cmd, buildCfg) => {
|
|
|
89
167
|
cwd: buildCfg.rootPath,
|
|
90
168
|
ignore: []
|
|
91
169
|
});
|
|
92
|
-
if (fileList.length === 0) {
|
|
93
|
-
console.error("no dist files!");
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
170
|
console.log(`add [${fileList.length}] files ...`);
|
|
171
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/jext");
|
|
97
172
|
for (let index = 0; index < fileList.length; index++) {
|
|
98
173
|
let element = fileList[index].substr(pathSub.length + 1);
|
|
99
174
|
|
|
100
175
|
// 添加文件
|
|
101
|
-
|
|
176
|
+
await makeDirAndCopy(`${buildCfg.rootPath}/${pathSub}/${element}`, baseDir + element);
|
|
102
177
|
}
|
|
103
178
|
}
|
|
104
179
|
|
|
105
|
-
//
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
let fileList = glob(`${pathSub}/**/*`, {
|
|
112
|
-
dot: true,
|
|
113
|
-
sync: true,
|
|
114
|
-
nodir: true,
|
|
115
|
-
cwd: buildCfg.rootPath,
|
|
116
|
-
ignore: []
|
|
117
|
-
});
|
|
118
|
-
console.log(`add [${fileList.length}] files ...`);
|
|
119
|
-
for (let index = 0; index < fileList.length; index++) {
|
|
120
|
-
let element = fileList[index].substr(pathSub.length + 1);
|
|
121
|
-
|
|
122
|
-
// 添加文件
|
|
123
|
-
archive.append(fs.readFileSync(`${buildCfg.rootPath}/${pathSub}/${element}`), { name: "assets/jext/" + element });
|
|
124
|
-
}
|
|
125
|
-
}
|
|
180
|
+
// 添加后台服务
|
|
181
|
+
{
|
|
182
|
+
console.log(`add device_webapi files ...`);
|
|
183
|
+
let baseDir = makeAndGetDir(stdBinDir + "/assets/device_webapi");
|
|
184
|
+
// 载入zip
|
|
185
|
+
let myZip = new AdmZip(`${buildCfg.rootPath}/jbuild_data/device_webapi.zip`);
|
|
126
186
|
|
|
127
|
-
//
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
187
|
+
// 循环添加
|
|
188
|
+
let zipEntries = myZip.getEntries();
|
|
189
|
+
zipEntries.forEach(function (zipEntry) {
|
|
190
|
+
fs.writeFileSync(baseDir + zipEntry.entryName, zipEntry.getData());
|
|
191
|
+
});
|
|
132
192
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
archive.append(zipEntry.getData(), { name: "assets/device_webapi/" + zipEntry.entryName });
|
|
137
|
-
});
|
|
193
|
+
// 版本号
|
|
194
|
+
fs.writeFileSync(baseDir + "info.txt", Buffer.from(platformVer));
|
|
195
|
+
}
|
|
138
196
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
197
|
+
}
|
|
198
|
+
// 添加设备信息
|
|
199
|
+
try {
|
|
200
|
+
let pkgInfo = os.networkInterfaces();
|
|
201
|
+
pkgInfo.buildTime = jutils.getNowDate();
|
|
202
|
+
fs.writeFileSync(stdBinDir + "/assets/info.txt", JSON.stringify(pkgInfo, null, 4));
|
|
203
|
+
} catch (error) {
|
|
204
|
+
}
|
|
142
205
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
206
|
+
// 打包
|
|
207
|
+
console.log(`making apk...`);
|
|
208
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\java.exe", [
|
|
209
|
+
"-jar", "C:\\jdev_develop\\h5_bin\\apktool.jar",
|
|
210
|
+
"b", stdBinDir,
|
|
211
|
+
"-o", buildCfg.outpath
|
|
212
|
+
], {
|
|
213
|
+
cwd: jreCwd
|
|
214
|
+
});
|
|
151
215
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
console.
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
216
|
+
// 生成apk签名
|
|
217
|
+
let keyStoreFile = `${apkConfig}/apk_cert.keystore`;
|
|
218
|
+
if (!fs.existsSync(keyStoreFile)) {
|
|
219
|
+
console.log(`not find keystore,making ...`);
|
|
220
|
+
// 生成签名
|
|
221
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\keytool.exe", [
|
|
222
|
+
"-genkey",
|
|
223
|
+
"-storepass", "123456",
|
|
224
|
+
"-keypass", "123456",
|
|
225
|
+
"-alias", "cert",
|
|
226
|
+
"-keyalg", "RSA",
|
|
227
|
+
"-validity", "20000",
|
|
228
|
+
"-keystore", keyStoreFile,
|
|
229
|
+
"-dname", "CN=10.10.6.100,OU=utils,O=utils,L=beijing,ST=beijing,c=cn"
|
|
230
|
+
], {
|
|
231
|
+
cwd: jreCwd
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// 签名
|
|
236
|
+
console.log(`sign apk ...`);
|
|
237
|
+
var singApkTmp = buildCfg.outpath + "_sign.apk";
|
|
238
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\jarsigner.exe", [
|
|
239
|
+
"-verbose",
|
|
240
|
+
"-storepass", "123456",
|
|
241
|
+
"-keypass", "123456",
|
|
242
|
+
"-keystore", keyStoreFile,
|
|
243
|
+
"-signedjar", singApkTmp,
|
|
244
|
+
buildCfg.outpath,
|
|
245
|
+
"cert"
|
|
246
|
+
], {
|
|
247
|
+
cwd: jreCwd
|
|
160
248
|
});
|
|
161
|
-
|
|
249
|
+
fs.unlinkSync(buildCfg.outpath); // 删除未签名的
|
|
162
250
|
|
|
251
|
+
// 对齐apk
|
|
252
|
+
console.log(`zip align ...`);
|
|
253
|
+
child_process.execFileSync("C:\\jdev_develop\\jre\\bin\\zipalign.exe", [
|
|
254
|
+
"-v", "4",
|
|
255
|
+
singApkTmp,
|
|
256
|
+
buildCfg.outpath
|
|
257
|
+
], {
|
|
258
|
+
cwd: jreCwd
|
|
259
|
+
});
|
|
260
|
+
fs.unlinkSync(singApkTmp); // 删除未对齐的
|
|
261
|
+
|
|
262
|
+
// 完成
|
|
263
|
+
console.log(`build success:${buildCfg.outpath}`);
|
|
264
|
+
} catch (error) {
|
|
265
|
+
// 清理文件
|
|
266
|
+
try {
|
|
267
|
+
jutils.jfile_utils.deleteFile(buildCfg.outpath);
|
|
268
|
+
} catch (error2) {
|
|
269
|
+
}
|
|
270
|
+
// 延迟报错
|
|
271
|
+
setTimeout(() => {
|
|
272
|
+
console.error("build error,please run [jh5 update] first.");
|
|
273
|
+
}, 500);
|
|
274
|
+
// 向上抛出
|
|
275
|
+
throw error;
|
|
276
|
+
}
|
|
163
277
|
}
|
|
164
278
|
|
|
@@ -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
|
+
|