This repository was archived by the owner on Feb 6, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathhandFile.js
More file actions
222 lines (193 loc) · 5.64 KB
/
Copy pathhandFile.js
File metadata and controls
222 lines (193 loc) · 5.64 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var path = require("path");
var fs = require("fs");
var plist = require("plist");
var URL = require("url");
let plistFileName = "manifest.plist";
/**
* 读取路径信息
* @param {string} path 路径
*/
function getStat(path) {
return new Promise((resolve, reject) => {
fs.stat(path, (err, stats) => {
if (err) {
resolve(false);
} else {
resolve(stats);
}
});
});
}
/**
* 创建路径
* @param {string} dir 路径
*/
function mkdir(dir) {
return new Promise((resolve, reject) => {
fs.mkdir(dir, err => {
if (err) {
resolve(false);
} else {
resolve(true);
}
});
});
}
/**
* 路径是否存在,不存在则创建
* @param {string} dir 路径
*/
async function dirExists(dir) {
let isExists = await getStat(dir);
//如果该路径且不是文件,返回true
if (isExists && isExists.isDirectory()) {
return true;
} else if (isExists) {
//如果该路径存在但是文件,返回false
return false;
}
//如果该路径不存在
let tempDir = path.parse(dir).dir; //拿到上级路径
//递归判断,如果上级目录也不存在,则会代码会在此处继续循环执行,直到目录存在
let status = await dirExists(tempDir);
let mkdirStatus;
if (status) {
mkdirStatus = await mkdir(dir);
}
return mkdirStatus;
}
/**
* 检查创建路径,如果没有就进行创建
* @param {*} destDirPath 需要检查的文件路径
*/
async function checkDirAndMkdirIfNeed(destDirPath) {
let dirRes = await dirExists(path.join(__dirname, destDirPath));
if (!dirRes) {
console.log("【检查路径是否存在】 - " + dirRes);
return false;
}
return true;
}
/**
* 移动上传的安装包到目标位置
* @param {string} destDir 存储目标相对位置
* @param {Multer.File} file 文件处理
*/
function handleInstallationPackage(destDir, file, cb) {
console.log("安装包 处理开始");
if (!checkDirAndMkdirIfNeed(destDir)) {
cb(false);
return false;
}
// 移动 安装包
let sourceFileAbsolutePath = path.join(__dirname, file.path);
let destPath = path.join(destDir, file.originalname);
let destAbsolutePath = path.join(__dirname, destPath);
fs.readFile(sourceFileAbsolutePath, function(err, data) {
if (err) throw err;
fs.writeFile(destAbsolutePath, data, function(err) {
if (err) throw err;
fs.unlinkSync(sourceFileAbsolutePath);
cb(destPath);
});
});
}
/**
* 处理下载使用的 plist
* @param {string} destDir 存储目标相对位置
* @param {string} ipaURL ipa 对应的下载位置
*/
function handlePlist(destDir, ipaURL, title, version,identifier) {
console.log("plist 处理开始");
if (!checkDirAndMkdirIfNeed(destDir)) {
return false;
}
// 处理 plist
let plistSourceAbsolutePath = path.join(
__dirname,
"source",
"plist",
plistFileName
); // 原始相对路径
// 读取 plist
let plistFile = fs.readFileSync(plistSourceAbsolutePath, "utf8");
// 解析为 json 「jsonPlist 的内容」看最下面
let jsonPlist = plist.parse(plistFile);
// 修改内容
jsonPlist.items[0].assets[0].url = ipaURL; // 修改 ipa 下载地址
jsonPlist.items[0].metadata["bundle-version"] = version; // version
jsonPlist.items[0].metadata["bundle-identifier"] = identifier; // version
jsonPlist.items[0].metadata.title = title; // title
// 转换为 plist
var builder = plist.build(jsonPlist);
// 存储
let plistDestPath = path.join(destDir, plistFileName); // 目标相对路径
let plistDestAbsolutePath = path.join(__dirname, plistDestPath);
fs.writeFileSync(plistDestAbsolutePath, builder);
return plistDestPath;
}
/**
* 在本机 IP 被修改后可以通过此接口进行 infoPlist 中域名的全部替换
*/
function updatedInfoPlistUrl(newHost) {
let dirPath = path.join(__dirname, "store");
var pa = fs.readdirSync(dirPath);
pa.forEach(function(ele) {
let subDirPath = path.join(dirPath, ele);
var info = fs.statSync(subDirPath);
if (info.isDirectory()) {
let infoPlistPath = path.join(subDirPath, plistFileName);
updatedInfoPlistUrlWith(newHost, infoPlistPath);
}
});
}
async function updatedInfoPlistUrlWith(newHost, filePath) {
let isExists = await getStat(filePath);
if (!isExists){
return;
}
// 读取 plist
let plistFile = fs.readFileSync(filePath, "utf8");
// 解析为 json 「jsonPlist 的内容」看最下面
let jsonPlist = plist.parse(plistFile);
// 修改内容
let oldUrlString = jsonPlist.items[0].assets[0].url;
let url = URL.parse(oldUrlString);
let newURLString = url.protocol + "//" + newHost + ":" + url.port + url.path;
jsonPlist.items[0].assets[0].url = newURLString;
// 转换为 plist
let builder = plist.build(jsonPlist);
fs.writeFileSync(filePath, builder);
}
/** jsonPlist 的内容
"items": [
{
"assets": [
{
"kind": "software-package",
"url": "https://127.0.0.1/matest.ipa"
},
{
"kind": "display-image",
"url": "https://127.0.0.1/matest.ipa"
},
{
"kind": "full-size-image",
"url": "https://127.0.0.1/matest.ipa"
}
],
"metadata": {
"bundle-identifier": "com.napm.AppXC",
"bundle-version": "2.5.3",
"kind": "software",
"title": "MAMTest"
}
}
]
}
*/
module.exports = {
handleInstallationPackage: handleInstallationPackage,
handlePlist: handlePlist,
updatedInfoPlistUrl: updatedInfoPlistUrl,
};