@taole/deploy-helper 1.0.5 → 1.1.1
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/index.mjs +9 -4
- package/lib/lightDeploy.mjs +110 -0
- package/lib/project.mjs +1 -1
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -11,6 +11,7 @@ import { fileURLToPath } from 'url';
|
|
|
11
11
|
import { cmdWhoami, cmdLogout, cmdLogin } from './lib/login.mjs';
|
|
12
12
|
import { cmdProjectCreate, cmdProjectPublish, cmdProjectPull } from './lib/project.mjs';
|
|
13
13
|
import { cmdCommit } from './lib/git.mjs';
|
|
14
|
+
import { lightDeploy } from './lib/lightDeploy.mjs';
|
|
14
15
|
|
|
15
16
|
const __filename = fileURLToPath(import.meta.url);
|
|
16
17
|
const __dirname = path.dirname(__filename);
|
|
@@ -107,10 +108,10 @@ async function getScpClient() {
|
|
|
107
108
|
return scpClient;
|
|
108
109
|
}
|
|
109
110
|
|
|
110
|
-
|
|
111
|
+
function getPackageJson() {
|
|
111
112
|
const packageJsonPath = join(__dirname, "package.json");
|
|
112
113
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
|
|
113
|
-
return packageJson
|
|
114
|
+
return packageJson;
|
|
114
115
|
}
|
|
115
116
|
|
|
116
117
|
const commandMap = {};
|
|
@@ -138,7 +139,8 @@ function doRegisterCommands() {
|
|
|
138
139
|
doRegisterCommands();
|
|
139
140
|
|
|
140
141
|
async function main() {
|
|
141
|
-
const
|
|
142
|
+
const packageJson = getPackageJson();
|
|
143
|
+
const version = packageJson.version;
|
|
142
144
|
|
|
143
145
|
// process.argv.includes("--debug") ||
|
|
144
146
|
// 默认开启debug debug目前只是打印日志的时候额外打印时间
|
|
@@ -285,7 +287,10 @@ async function main() {
|
|
|
285
287
|
process.exit(1);
|
|
286
288
|
}
|
|
287
289
|
|
|
288
|
-
|
|
290
|
+
const needHandleLight = config.type === "ddfe:cdn";
|
|
291
|
+
if(needHandleLight){
|
|
292
|
+
await lightDeploy(config, mode, version);
|
|
293
|
+
}
|
|
289
294
|
const needHandleAssets = config.assets && config.assets.dest;
|
|
290
295
|
|
|
291
296
|
// 需要处理entry
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { log } from "./util.mjs";
|
|
4
|
+
import { getCache as getLoginCache } from "./login.mjs";
|
|
5
|
+
import { genArchive } from "./project.mjs";
|
|
6
|
+
|
|
7
|
+
const isDev = false;
|
|
8
|
+
const apiHost = isDev ? "http://localhost:9000" : "https://fapi.tuwan.com";
|
|
9
|
+
|
|
10
|
+
function getErrorMessage(resJson, fallback) {
|
|
11
|
+
const message = resJson?.message;
|
|
12
|
+
if (Array.isArray(message)) {
|
|
13
|
+
return message.join(", ");
|
|
14
|
+
}
|
|
15
|
+
if (message) {
|
|
16
|
+
return String(message);
|
|
17
|
+
}
|
|
18
|
+
return fallback;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function apiCdnUpload({ distZipPath, prefix, mode, includeHtml, entryHtmlMap }, userCache) {
|
|
22
|
+
const formData = new FormData();
|
|
23
|
+
formData.append("prefix", prefix);
|
|
24
|
+
formData.append("mode", mode);
|
|
25
|
+
formData.append("includeHtml", includeHtml ? "true" : "false");
|
|
26
|
+
if (entryHtmlMap) {
|
|
27
|
+
const entryHtmlMapStr =
|
|
28
|
+
typeof entryHtmlMap === "string" ? entryHtmlMap : JSON.stringify(entryHtmlMap);
|
|
29
|
+
formData.append("entryHtmlMap", entryHtmlMapStr);
|
|
30
|
+
}
|
|
31
|
+
formData.append(
|
|
32
|
+
"file",
|
|
33
|
+
new Blob([fs.readFileSync(distZipPath)], { type: "application/zip" }),
|
|
34
|
+
"dist.zip"
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const res = await fetch(`${apiHost}/cdn/upload`, {
|
|
38
|
+
method: "POST",
|
|
39
|
+
body: formData,
|
|
40
|
+
headers: {
|
|
41
|
+
Cookie: `Tuwan_Passport=${userCache.Tuwan_Passport}`,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
const resJson = await res.json();
|
|
45
|
+
if (!res.ok) {
|
|
46
|
+
throw new Error(getErrorMessage(resJson, "CDN 上传失败"));
|
|
47
|
+
}
|
|
48
|
+
return resJson;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* light模式部署
|
|
53
|
+
* @param {*} config 配置
|
|
54
|
+
* @param {*} mode 部署环境
|
|
55
|
+
* @param {*} version 版本号
|
|
56
|
+
*/
|
|
57
|
+
export async function lightDeploy(config, mode, version) {
|
|
58
|
+
log(`light模式, 开始部署`, { mode, prefix: config.prefix, includeHtml: config.includeHtml, version });
|
|
59
|
+
|
|
60
|
+
if (!mode || (mode !== "test" && mode !== "prod")) {
|
|
61
|
+
throw new Error("部署环境无效,仅支持 test 或 prod");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const prefix = config.prefix;
|
|
65
|
+
if (!prefix) {
|
|
66
|
+
throw new Error("配置缺少 prefix");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const userCache = await getLoginCache();
|
|
70
|
+
if (!userCache?.userInfo?.uid) {
|
|
71
|
+
throw new Error("请先登录");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const workDir = process.cwd();
|
|
75
|
+
const distDir = config.distDir || "dist";
|
|
76
|
+
const distPath = path.join(workDir, distDir);
|
|
77
|
+
if (!fs.existsSync(distPath)) {
|
|
78
|
+
throw new Error(`${distPath} 不存在,请先执行构建`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const distZipPath = path.join(workDir, "dist.zip");
|
|
82
|
+
if (fs.existsSync(distZipPath)) {
|
|
83
|
+
fs.unlinkSync(distZipPath);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
log(`开始打包 ${distPath}`);
|
|
87
|
+
await genArchive(distZipPath, distPath);
|
|
88
|
+
log(`打包完成: ${distZipPath}`);
|
|
89
|
+
|
|
90
|
+
const includeHtml = config.includeHtml === true;
|
|
91
|
+
log(`开始上传至 CDN,mode=${mode}, prefix=${prefix}, includeHtml=${includeHtml}`);
|
|
92
|
+
const result = await apiCdnUpload(
|
|
93
|
+
{
|
|
94
|
+
distZipPath,
|
|
95
|
+
prefix,
|
|
96
|
+
mode,
|
|
97
|
+
includeHtml,
|
|
98
|
+
entryHtmlMap: config.entryHtmlMap,
|
|
99
|
+
type: config.cdnType,
|
|
100
|
+
version,
|
|
101
|
+
},
|
|
102
|
+
userCache
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
if (fs.existsSync(distZipPath)) {
|
|
106
|
+
fs.unlinkSync(distZipPath);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
log(`CDN 上传成功`, result);
|
|
110
|
+
}
|
package/lib/project.mjs
CHANGED
|
@@ -9,7 +9,7 @@ import { getCache as getLoginCache } from "./login.mjs";
|
|
|
9
9
|
const isDev = false;
|
|
10
10
|
const apiHost = isDev ? "http://localhost:9000" : "https://fapi.tuwan.com";
|
|
11
11
|
|
|
12
|
-
function genArchive(outputPath, dir) {
|
|
12
|
+
export function genArchive(outputPath, dir) {
|
|
13
13
|
return new Promise((resolve, reject) => {
|
|
14
14
|
const output = fs.createWriteStream(outputPath);
|
|
15
15
|
const archive = archiver("zip", {
|