@taole/deploy-helper 1.1.8-beta.3 → 1.2.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 CHANGED
@@ -289,7 +289,7 @@ async function main() {
289
289
 
290
290
  const needHandleLight = config.type === "ddfe:cdn";
291
291
  if(needHandleLight){
292
- await lightDeploy(config, mode);
292
+ await lightDeploy(config, mode, configFileName || 'deploy.config.json');
293
293
  }
294
294
  const needHandleAssets = config.assets && config.assets.dest;
295
295
 
@@ -1,5 +1,6 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
+ import inquirer from "inquirer";
3
4
  import { log } from "./util.mjs";
4
5
  import { getCache as getLoginCache } from "./login.mjs";
5
6
  import {
@@ -22,11 +23,100 @@ function getErrorMessage(resJson, fallback) {
22
23
  return fallback;
23
24
  }
24
25
 
26
+ function getAuthHeaders(userCache) {
27
+ return {
28
+ Cookie: `Tuwan_Passport=${userCache.Tuwan_Passport}`,
29
+ };
30
+ }
31
+
32
+ async function apiCdnRegister({ prefix, type }, userCache) {
33
+ const res = await fetch(`${apiHost}/cdn/projects/register`, {
34
+ method: "POST",
35
+ headers: {
36
+ ...getAuthHeaders(userCache),
37
+ "Content-Type": "application/json",
38
+ },
39
+ body: JSON.stringify({ prefix, type }),
40
+ });
41
+ const resJson = await res.json();
42
+ return { ok: res.ok, status: res.status, data: resJson };
43
+ }
44
+
45
+ async function apiCdnBind({ prefix, type }, userCache) {
46
+ const res = await fetch(`${apiHost}/cdn/projects/bind`, {
47
+ method: "POST",
48
+ headers: {
49
+ ...getAuthHeaders(userCache),
50
+ "Content-Type": "application/json",
51
+ },
52
+ body: JSON.stringify({ prefix, type }),
53
+ });
54
+ const resJson = await res.json();
55
+ if (!res.ok) {
56
+ throw new Error(getErrorMessage(resJson, "CDN 项目绑定失败"));
57
+ }
58
+ return resJson;
59
+ }
60
+
61
+ function writeCdnProjectId(config, configFileName, projectId) {
62
+ const configPath = path.join(process.cwd(), configFileName);
63
+ const currentConfig = JSON.parse(fs.readFileSync(configPath, "utf-8"));
64
+ currentConfig.cdnProjectId = projectId;
65
+ fs.writeFileSync(configPath, `${JSON.stringify(currentConfig, null, 4)}\n`);
66
+ config.cdnProjectId = projectId;
67
+ log(
68
+ `已将 cdnProjectId=${projectId} 写入 ${configFileName},请提交该配置变更。`
69
+ );
70
+ }
71
+
72
+ async function ensureProjectId(config, configFileName, userCache) {
73
+ const existingId = Number(config.cdnProjectId);
74
+ if (Number.isInteger(existingId) && existingId > 0) {
75
+ return existingId;
76
+ }
77
+
78
+ const prefix = config.prefix;
79
+ const type = config.cdnType || "project";
80
+ const registerResult = await apiCdnRegister({ prefix, type }, userCache);
81
+
82
+ if (registerResult.ok) {
83
+ const projectId = registerResult.data.projectId;
84
+ writeCdnProjectId(config, configFileName, projectId);
85
+ return projectId;
86
+ }
87
+
88
+ if (registerResult.status !== 409) {
89
+ throw new Error(
90
+ getErrorMessage(registerResult.data, "CDN 项目注册失败")
91
+ );
92
+ }
93
+
94
+ const { confirm } = await inquirer.prompt([
95
+ {
96
+ type: "confirm",
97
+ name: "confirm",
98
+ message: `prefix "${prefix}" 已在服务端存在。若这是已有项目的首次补 id,确认绑定?`,
99
+ default: false,
100
+ },
101
+ ]);
102
+
103
+ if (!confirm) {
104
+ throw new Error(
105
+ `prefix "${prefix}" 已被占用,请修改 prefix 后重试`
106
+ );
107
+ }
108
+
109
+ const bindResult = await apiCdnBind({ prefix, type }, userCache);
110
+ writeCdnProjectId(config, configFileName, bindResult.projectId);
111
+ return bindResult.projectId;
112
+ }
113
+
25
114
  async function apiCdnUpload(
26
- { distZipBuffer, prefix, mode, includeHtml, entryHtmlMap, type, version },
115
+ { distZipBuffer, prefix, projectId, mode, includeHtml, type, version },
27
116
  userCache
28
117
  ) {
29
118
  const formData = new FormData();
119
+ formData.append("projectId", String(projectId));
30
120
  formData.append("prefix", prefix);
31
121
  formData.append("mode", mode);
32
122
  formData.append("includeHtml", includeHtml ? "true" : "false");
@@ -36,13 +126,6 @@ async function apiCdnUpload(
36
126
  if (version) {
37
127
  formData.append("version", version);
38
128
  }
39
- if (entryHtmlMap) {
40
- const entryHtmlMapStr =
41
- typeof entryHtmlMap === "string"
42
- ? entryHtmlMap
43
- : JSON.stringify(entryHtmlMap);
44
- formData.append("entryHtmlMap", entryHtmlMapStr);
45
- }
46
129
  formData.append(
47
130
  "file",
48
131
  new Blob([distZipBuffer], { type: "application/zip" }),
@@ -52,9 +135,7 @@ async function apiCdnUpload(
52
135
  const res = await fetch(`${apiHost}/cdn/upload`, {
53
136
  method: "POST",
54
137
  body: formData,
55
- headers: {
56
- Cookie: `Tuwan_Passport=${userCache.Tuwan_Passport}`,
57
- },
138
+ headers: getAuthHeaders(userCache),
58
139
  });
59
140
  const resJson = await res.json();
60
141
  if (!res.ok) {
@@ -67,14 +148,15 @@ async function apiCdnUpload(
67
148
  * light模式部署
68
149
  * @param {*} config 配置
69
150
  * @param {*} mode 部署环境
151
+ * @param {string} configFileName 配置文件名
70
152
  */
71
- export async function lightDeploy(config, mode) {
153
+ export async function lightDeploy(config, mode, configFileName = "deploy.config.json") {
72
154
  const projectJson = JSON.parse(
73
155
  fs.readFileSync(path.join(process.cwd(), "package.json"), "utf-8")
74
156
  );
75
157
  const version = projectJson.version;
76
- // 校验cdnType只能是lib或者project
77
- const cdnType = config.cdnType || 'project';
158
+ config.includeHtml = config.includeHtml || false;
159
+ const cdnType = config.cdnType || "project";
78
160
  if (cdnType !== "lib" && cdnType !== "project") {
79
161
  throw new Error("配置项 cdnType 仅支持 'lib' 或 'project'");
80
162
  }
@@ -100,6 +182,8 @@ export async function lightDeploy(config, mode) {
100
182
  throw new Error("请先登录");
101
183
  }
102
184
 
185
+ const projectId = await ensureProjectId(config, configFileName, userCache);
186
+
103
187
  const workDir = process.cwd();
104
188
  const distDir = config.distDir || "dist";
105
189
  const distPath = path.join(workDir, distDir);
@@ -118,7 +202,7 @@ export async function lightDeploy(config, mode) {
118
202
 
119
203
  const includeHtml = config.includeHtml === true;
120
204
  log(
121
- `开始上传至 CDN,mode=${mode}, prefix=${prefix}, type=${
205
+ `开始上传至 CDN,mode=${mode}, prefix=${prefix}, projectId=${projectId}, type=${
122
206
  config.cdnType || "project"
123
207
  }, version=${version}, includeHtml=${includeHtml}`
124
208
  );
@@ -126,9 +210,9 @@ export async function lightDeploy(config, mode) {
126
210
  {
127
211
  distZipBuffer,
128
212
  prefix,
213
+ projectId,
129
214
  mode,
130
215
  includeHtml,
131
- entryHtmlMap: config.entryHtmlMap,
132
216
  type: config.cdnType,
133
217
  version,
134
218
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/deploy-helper",
3
- "version": "1.1.8-beta.3",
3
+ "version": "1.2.1",
4
4
  "description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
5
5
  "main": "index.mjs",
6
6
  "type": "module",