-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion_generator.js
109 lines (100 loc) · 3.46 KB
/
version_generator.js
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
/*
* @Author: [email protected]
* @Date: 2021-04-28 10:17:37
* @Des:
* @Tips:
*/
let fs = require('fs');
let path = require('path');
let crypto = require('crypto');
const fse = require('fs-extra');
let manifest = {
packageUrl: `123/`,
remoteManifestUrl: `123/test/project.manifest`,
remoteVersionUrl: `123/test/version.manifest`,
version: '1.0.0',
env: "",
assets: {},
searchPaths: []
};
let dest = path.join(__dirname, '../../assets/scr/env/');
let src = path.join(__dirname, '../../build/jsb-link/');
function readDir(dir, obj) {
let stat = fs.statSync(dir);
if (!stat.isDirectory()) {
return;
}
let subpaths = fs.readdirSync(dir), subpath, size, md5, compressed, relative;
for (let i = 0; i < subpaths.length; ++i) {
if (subpaths[i][0] === '.') {
continue;
}
subpath = path.join(dir, subpaths[i]);
stat = fs.statSync(subpath);
if (stat.isDirectory()) {
readDir(subpath, obj);
}
else if (stat.isFile()) {
// Size in Bytes
size = stat['size'];
md5 = crypto.createHash('md5').update(fs.readFileSync(subpath)).digest('hex');
compressed = path.extname(subpath).toLowerCase() === '.zip';
relative = path.relative(src, subpath);
relative = relative.replace(/\\/g, '/');
relative = encodeURI(relative);
obj[relative] = {
'size': size,
'md5': md5
};
if (compressed) {
obj[relative].compressed = true;
}
}
}
}
let mkdirSync = function (path) {
try {
fs.mkdirSync(path);
} catch (e) {
if (e.code != 'EEXIST') throw e;
}
}
/**
* template
* projectManifest
* proEnv
* proVer
* hotUpdateUrl
* dayjs
*/
module.exports = function (item) {
// manifest 修改赋值
src = path.join(__dirname, `../../build/jsb-${item.template}`);
manifest.packageUrl = `${item.hotUpdateUrl}`;
manifest.remoteManifestUrl = `${item.hotUpdateUrl}/project.manifest`;
manifest.remoteVersionUrl = `${item.hotUpdateUrl}/version.manifest`;
manifest.version = item.proVer != null ? item.proVer : manifest.version;
manifest.env = item.proEnv;
manifest.buildTime = item.dayjs().format('YYYY-MM-DD HH:mm:ss');
// 修改(生成)项目内的 manifest
let srcPath = path.join(src, 'src');
let resPath = path.join(src, 'assets');
readDir(srcPath, manifest.assets);
readDir(resPath, manifest.assets);
let destManifest = path.join(dest, 'project.manifest'); // 修改 manifest
let destVersion = path.join(dest, 'version.manifest');
mkdirSync(dest);
fs.writeFileSync(destManifest, JSON.stringify(manifest));
let temp = JSON.parse(JSON.stringify(manifest)); // 直接使用同一个对象会报错, 我也不知道为什么...
delete temp.assets;
delete temp.searchPaths;
fs.writeFileSync(destVersion, JSON.stringify(temp));
// 赋值到一个文件夹下
let remoteAssetsPath = path.join(__dirname, `../../build/jsb-remote-assets`);
fse.emptyDirSync(remoteAssetsPath); // 清空文件夹
remoteAssetsPath = path.join(__dirname, `../../build/jsb-remote-assets`);
fse.copySync(srcPath, `${remoteAssetsPath}/src`)
fse.copySync(resPath, `${remoteAssetsPath}/assets`)
fse.copySync(destManifest, `${remoteAssetsPath}/project.manifest`)
fse.copySync(destVersion, `${remoteAssetsPath}/version.manifest`);
}