qdmp-cli 0.0.8 → 0.0.10
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/actions.js +2 -2
- package/package.json +7 -3
- package/utils/common.js +120 -29
package/actions.js
CHANGED
|
@@ -110,10 +110,10 @@ export async function uploadAction(option) {
|
|
|
110
110
|
const code = +remoteApp.latest + 1;
|
|
111
111
|
const uploadPath = getUploadPath(appId, code);
|
|
112
112
|
const url = `https://crane.qiandaocdn.com/${uploadPath}`;
|
|
113
|
-
|
|
113
|
+
const loader = remoteApp.loader;
|
|
114
114
|
// 预上传
|
|
115
115
|
const targetName = `${appId}_${code}`;
|
|
116
|
-
const localPath = await preUpload(targetName);
|
|
116
|
+
const localPath = await preUpload(loader, targetName);
|
|
117
117
|
if (!localPath) {
|
|
118
118
|
error("预上传失败");
|
|
119
119
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qdmp-cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"description": "qdmp-cli",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
9
|
},
|
|
10
10
|
"bin": {
|
|
11
|
-
"qdmp": "
|
|
12
|
-
"qdmp-cli": "
|
|
11
|
+
"qdmp": "index.js",
|
|
12
|
+
"qdmp-cli": "index.js"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
15
15
|
"千岛小程序",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"author": "zhangyanran",
|
|
19
19
|
"license": "ISC",
|
|
20
20
|
"dependencies": {
|
|
21
|
+
"adm-zip": "^0.5.16",
|
|
21
22
|
"chalk": "^5.4.1",
|
|
22
23
|
"commander": "^14.0.0",
|
|
23
24
|
"figlet": "^1.8.1",
|
|
@@ -26,5 +27,8 @@
|
|
|
26
27
|
"ora": "^8.2.0",
|
|
27
28
|
"shelljs": "^0.10.0",
|
|
28
29
|
"table": "^6.9.0"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=20"
|
|
29
33
|
}
|
|
30
34
|
}
|
package/utils/common.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { info, error, success } from "./logHandler.js";
|
|
2
|
-
import
|
|
3
|
-
import chalk from "chalk";
|
|
2
|
+
import AdmZip from "adm-zip";
|
|
4
3
|
import { WORKSPACE_DIR } from "../constants.js";
|
|
5
4
|
import fs from "fs";
|
|
6
5
|
import path from "path";
|
|
@@ -48,7 +47,7 @@ export const isUrlAccessible = async (url) => {
|
|
|
48
47
|
export async function renameDistFolder(dirname, newname) {
|
|
49
48
|
try {
|
|
50
49
|
if (fs.existsSync(newname)) return true;
|
|
51
|
-
|
|
50
|
+
fs.renameSync(dirname, newname);
|
|
52
51
|
return true;
|
|
53
52
|
} catch (e) {
|
|
54
53
|
error("重命名文件夹失败:", e);
|
|
@@ -61,8 +60,10 @@ export async function renameDistFolder(dirname, newname) {
|
|
|
61
60
|
export async function createWorkSpaceFolder(dirname = "") {
|
|
62
61
|
info("创建工作区...");
|
|
63
62
|
try {
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
if (fs.existsSync(dirname)) {
|
|
64
|
+
fs.rmSync(dirname, { recursive: true, force: true });
|
|
65
|
+
}
|
|
66
|
+
fs.mkdirSync(dirname, { recursive: true });
|
|
66
67
|
success("创建工作区成功!");
|
|
67
68
|
return true;
|
|
68
69
|
} catch (e) {
|
|
@@ -74,11 +75,25 @@ export async function createWorkSpaceFolder(dirname = "") {
|
|
|
74
75
|
* @param {*} dirname 文件夹名
|
|
75
76
|
*/
|
|
76
77
|
// 压缩项目
|
|
77
|
-
async function compressProject(output, dirname = "") {
|
|
78
|
+
async function compressProject(output, dirname = "", loader) {
|
|
78
79
|
try {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
80
|
+
if (loader !== "EMP") {
|
|
81
|
+
const zip = new AdmZip();
|
|
82
|
+
const h5Path = path.join(dirname, "h5");
|
|
83
|
+
|
|
84
|
+
if (!fs.existsSync(h5Path)) {
|
|
85
|
+
throw new Error("h5 目录不存在");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 添加文件夹时指定目标路径为 h5
|
|
89
|
+
zip.addLocalFolder(h5Path, "h5");
|
|
90
|
+
zip.writeZip(path.join(dirname, `${output}.zip`));
|
|
91
|
+
} else {
|
|
92
|
+
// 直接压缩dist
|
|
93
|
+
const zip = new AdmZip();
|
|
94
|
+
zip.addLocalFolder(path.join(dirname, "dist"), "dist");
|
|
95
|
+
zip.writeZip(path.join(dirname, `${output}.zip`));
|
|
96
|
+
}
|
|
82
97
|
return true;
|
|
83
98
|
} catch (error) {
|
|
84
99
|
return false;
|
|
@@ -89,41 +104,115 @@ async function compressProject(output, dirname = "") {
|
|
|
89
104
|
*/
|
|
90
105
|
async function copyDistFolder(dirname) {
|
|
91
106
|
try {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
107
|
+
info("正在查找 index.html 文件...");
|
|
108
|
+
let indexPath = null;
|
|
109
|
+
function findIndexHtml(dir) {
|
|
110
|
+
const files = fs.readdirSync(dir);
|
|
111
|
+
for (const file of files) {
|
|
112
|
+
const fullPath = path.join(dir, file);
|
|
113
|
+
const stat = fs.statSync(fullPath);
|
|
114
|
+
if (stat.isDirectory()) {
|
|
115
|
+
const found = findIndexHtml(fullPath);
|
|
116
|
+
if (found) return found;
|
|
117
|
+
} else if (file === "index.html") {
|
|
118
|
+
return fullPath;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
indexPath = findIndexHtml(dirname);
|
|
95
124
|
if (!indexPath) {
|
|
96
125
|
throw new Error("未找到index.html文件");
|
|
97
126
|
}
|
|
127
|
+
success("找到 index.html 文件");
|
|
128
|
+
|
|
98
129
|
const parentDir = path.dirname(indexPath);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
130
|
+
const targetDir = path.join(WORKSPACE_DIR, "dist");
|
|
131
|
+
|
|
132
|
+
info("正在复制文件...");
|
|
133
|
+
// 直接复制整个目录
|
|
134
|
+
fs.cpSync(parentDir, targetDir, { recursive: true });
|
|
135
|
+
success("文件复制完成");
|
|
102
136
|
return true;
|
|
103
137
|
} catch (e) {
|
|
104
138
|
error(e);
|
|
105
139
|
return false;
|
|
106
140
|
}
|
|
107
141
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
*
|
|
142
|
+
/** 复制EMP文件夹
|
|
143
|
+
* @param {*} dirname 文件夹名
|
|
111
144
|
*/
|
|
112
|
-
|
|
145
|
+
async function copyEmpDistFolder() {
|
|
146
|
+
//直接复制根目录下dist文件夹
|
|
113
147
|
try {
|
|
114
|
-
|
|
148
|
+
fs.cpSync("dist", `${WORKSPACE_DIR}/dist`, { recursive: true });
|
|
149
|
+
success("文件复制完成");
|
|
150
|
+
return true;
|
|
151
|
+
} catch (e) {
|
|
152
|
+
error(e);
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* 预上传准备工作
|
|
158
|
+
* 1. 创建工作空间
|
|
159
|
+
* 2. 复制构建产物
|
|
160
|
+
* 3. 重命名文件夹(非EMP构建模式)
|
|
161
|
+
* 4. 压缩项目
|
|
162
|
+
* @param {string} loader - 构建加载器类型
|
|
163
|
+
* @param {string} targetName - 目标文件名
|
|
164
|
+
* @returns {Promise<string|boolean>} 压缩文件路径或操作失败返回false
|
|
165
|
+
*/
|
|
166
|
+
export async function preUpload(loader, targetName) {
|
|
167
|
+
try {
|
|
168
|
+
// 1. 创建工作空间文件夹
|
|
169
|
+
if (!(await createWorkSpaceFolder(WORKSPACE_DIR))) {
|
|
170
|
+
error("创建工作空间失败");
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
115
173
|
|
|
116
|
-
|
|
174
|
+
// 2. 根据构建类型复制对应产物
|
|
175
|
+
const isCopySuccess =
|
|
176
|
+
loader === "EMP"
|
|
177
|
+
? await copyEmpDistFolder()
|
|
178
|
+
: await copyAndRenameDistFolder();
|
|
117
179
|
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
)
|
|
180
|
+
if (!isCopySuccess) {
|
|
181
|
+
error("复制构建产物失败");
|
|
121
182
|
return false;
|
|
183
|
+
}
|
|
122
184
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
185
|
+
// 3. 压缩项目
|
|
186
|
+
if (!(await compressProject(targetName, WORKSPACE_DIR, loader))) {
|
|
187
|
+
error("压缩项目失败");
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// 4. 返回压缩文件路径
|
|
192
|
+
const zipFilePath = `./${WORKSPACE_DIR}/${targetName}.zip`;
|
|
193
|
+
success(`预上传准备完成,压缩包路径: ${zipFilePath}`);
|
|
194
|
+
return zipFilePath;
|
|
195
|
+
} catch (error) {
|
|
196
|
+
error(`预上传过程出错: ${error.message}`);
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 复制并创建标准H5文件夹结构
|
|
203
|
+
* 对于非EMP构建模式,复制dist内容并重命名为h5
|
|
204
|
+
* @returns {Promise<boolean>} 操作是否成功
|
|
205
|
+
*/
|
|
206
|
+
async function copyAndRenameDistFolder() {
|
|
207
|
+
try {
|
|
208
|
+
if (!(await copyDistFolder("dist"))) {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return renameDistFolder(`${WORKSPACE_DIR}/dist`, `${WORKSPACE_DIR}/h5`);
|
|
213
|
+
} catch (error) {
|
|
214
|
+
error("复制并重命名文件夹过程出错:", error);
|
|
215
|
+
return false;
|
|
127
216
|
}
|
|
128
217
|
}
|
|
129
218
|
/** 清理工作文件夹
|
|
@@ -131,7 +220,9 @@ export async function preUpload(targetName) {
|
|
|
131
220
|
*/
|
|
132
221
|
export async function cleanWorkSpaceFolder(dirname) {
|
|
133
222
|
try {
|
|
134
|
-
|
|
223
|
+
if (fs.existsSync(WORKSPACE_DIR)) {
|
|
224
|
+
fs.rmSync(WORKSPACE_DIR, { recursive: true, force: true });
|
|
225
|
+
}
|
|
135
226
|
success("清理工作空间成功!");
|
|
136
227
|
return true;
|
|
137
228
|
} catch (error) {
|