@taole/deploy-helper 1.0.5 → 1.1.0

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 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
- async function getVersion() {
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.version;
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 version = await getVersion();
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);
293
+ }
289
294
  const needHandleAssets = config.assets && config.assets.dest;
290
295
 
291
296
  // 需要处理entry
@@ -0,0 +1,103 @@
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
+ export async function lightDeploy(config, mode) {
52
+ log(`light模式, 开始部署`, { mode, prefix: config.prefix, includeHtml: config.includeHtml });
53
+
54
+ if (!mode || (mode !== "test" && mode !== "prod")) {
55
+ throw new Error("部署环境无效,仅支持 test 或 prod");
56
+ }
57
+
58
+ const prefix = config.prefix;
59
+ if (!prefix) {
60
+ throw new Error("配置缺少 prefix");
61
+ }
62
+
63
+ const userCache = await getLoginCache();
64
+ if (!userCache?.userInfo?.uid) {
65
+ throw new Error("请先登录");
66
+ }
67
+
68
+ const workDir = process.cwd();
69
+ const distDir = config.distDir || "dist";
70
+ const distPath = path.join(workDir, distDir);
71
+ if (!fs.existsSync(distPath)) {
72
+ throw new Error(`${distPath} 不存在,请先执行构建`);
73
+ }
74
+
75
+ const distZipPath = path.join(workDir, "dist.zip");
76
+ if (fs.existsSync(distZipPath)) {
77
+ fs.unlinkSync(distZipPath);
78
+ }
79
+
80
+ log(`开始打包 ${distPath}`);
81
+ await genArchive(distZipPath, distPath);
82
+ log(`打包完成: ${distZipPath}`);
83
+
84
+ const includeHtml = config.includeHtml === true;
85
+ log(`开始上传至 CDN,mode=${mode}, prefix=${prefix}, includeHtml=${includeHtml}`);
86
+ const result = await apiCdnUpload(
87
+ {
88
+ distZipPath,
89
+ prefix,
90
+ mode,
91
+ includeHtml,
92
+ entryHtmlMap: config.entryHtmlMap,
93
+ type: config.cdnType,
94
+ },
95
+ userCache
96
+ );
97
+
98
+ if (fs.existsSync(distZipPath)) {
99
+ fs.unlinkSync(distZipPath);
100
+ }
101
+
102
+ log(`CDN 上传成功`, result);
103
+ }
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", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/deploy-helper",
3
- "version": "1.0.5",
3
+ "version": "1.1.0",
4
4
  "description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
5
5
  "main": "index.mjs",
6
6
  "type": "module",