@xfe-repo/mini-app 1.6.5 → 2.0.0-beta.2

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.
@@ -0,0 +1,153 @@
1
+ // src/config.ts
2
+ import fs2 from "fs";
3
+
4
+ // src/config/deploy.ts
5
+ import path2 from "path";
6
+ import { fileURLToPath } from "url";
7
+
8
+ // ../../shared/deploy/config.ts
9
+ import fs from "fs";
10
+ import path from "path";
11
+ import { createRequire } from "module";
12
+ var DeployEnvironment = /* @__PURE__ */ ((DeployEnvironment2) => {
13
+ DeployEnvironment2["Test"] = "test";
14
+ DeployEnvironment2["Stage"] = "stage";
15
+ DeployEnvironment2["Prod"] = "prod";
16
+ DeployEnvironment2["Stable"] = "stable";
17
+ return DeployEnvironment2;
18
+ })(DeployEnvironment || {});
19
+ var generateDeploy = ({
20
+ deploy,
21
+ frameworkRoot,
22
+ isMini = false
23
+ }) => {
24
+ const yaml = createRequire(path.join(frameworkRoot, "package.json"))("yaml");
25
+ const supportedFields = ["values", "environments", "pushHelmChart", ...isMini ? ["platforms"] : []];
26
+ if (deploy === void 0) return;
27
+ if (!deploy || typeof deploy !== "object" || Array.isArray(deploy) || Object.keys(deploy).some(
28
+ /** 运行参数只能来自 values,流水线开关保留独立入口。 */
29
+ (key) => !supportedFields.includes(key)
30
+ )) {
31
+ throw new Error(`xfe.json.deploy \u4EC5\u652F\u6301 ${supportedFields.join("\u3001")}\uFF1B\u8FD0\u884C\u53C2\u6570\u8BF7\u653E\u5165 values`);
32
+ }
33
+ const environments = deploy.environments === void 0 ? Object.values(DeployEnvironment) : deploy.environments;
34
+ if (!Array.isArray(environments) || environments.some(
35
+ /** 只接受已知的环境名称,非法值不能误开启发布。 */
36
+ (environment) => !Object.values(DeployEnvironment).includes(environment)
37
+ ))
38
+ throw new Error("deploy.environments \u5FC5\u987B\u662F test\u3001stage\u3001prod\u3001stable \u7EC4\u6210\u7684\u6570\u7EC4\uFF1B\u7A7A\u6570\u7EC4\u5173\u95ED\u5168\u90E8\u90E8\u7F72");
39
+ if (deploy.pushHelmChart !== void 0 && typeof deploy.pushHelmChart !== "boolean") throw new Error("deploy.pushHelmChart \u5FC5\u987B\u662F\u5E03\u5C14\u503C");
40
+ const platforms = deploy.platforms === void 0 ? ["weapp", "h5"] : deploy.platforms;
41
+ if (!Array.isArray(platforms) || platforms.some((platform) => !["weapp", "h5"].includes(platform))) {
42
+ throw new Error("deploy.platforms \u5FC5\u987B\u662F weapp\u3001h5 \u7EC4\u6210\u7684\u6570\u7EC4\uFF1B\u7A7A\u6570\u7EC4\u5173\u95ED\u5168\u90E8\u53D1\u5E03");
43
+ }
44
+ const h5Environments = platforms.includes("h5") ? environments : [];
45
+ const miniEnvironments = platforms.includes("weapp") ? environments : [];
46
+ const pushHelmChart = deploy.pushHelmChart !== false && h5Environments.includes("prod" /* Prod */);
47
+ const source = path.join(frameworkRoot, "deploy");
48
+ const target = path.join(process.cwd(), "deploy");
49
+ let overrides = deploy.values === void 0 ? {} : deploy.values;
50
+ if (typeof overrides === "string") {
51
+ const valuesPath = fs.realpathSync(path.resolve(overrides));
52
+ if (valuesPath.startsWith(target + path.sep) && valuesPath !== path.join(target, "values.yaml")) {
53
+ throw new Error("\u9879\u76EE values \u6587\u4EF6\u4E0D\u80FD\u653E\u5728\u751F\u6210\u5B50\u76EE\u5F55\u4E2D\uFF0C\u8BF7\u4F7F\u7528 deploy/values.yaml \u6216\u9879\u76EE\u6839\u76EE\u5F55\u7684 values.yaml");
54
+ }
55
+ overrides = yaml.parse(fs.readFileSync(valuesPath, "utf8"));
56
+ }
57
+ if (!overrides || typeof overrides !== "object" || Array.isArray(overrides)) {
58
+ throw new Error("deploy.values \u5FC5\u987B\u662F\u5BF9\u8C61\u6216\u6307\u5411 YAML \u5BF9\u8C61\u6587\u4EF6\u7684\u8DEF\u5F84");
59
+ }
60
+ if (Object.hasOwn(overrides, "environments") || Object.hasOwn(overrides, "pushHelmChart"))
61
+ throw new Error("values \u4E0D\u518D\u652F\u6301 environments \u6216 pushHelmChart\uFF1B\u8BF7\u8FC1\u5165 xfe.json.deploy\uFF0C\u73AF\u5883\u5F00\u5173\u4F7F\u7528\u6570\u7EC4");
62
+ const defaults = yaml.parse(fs.readFileSync(path.join(source, "helm/values.yaml"), "utf8"));
63
+ const values = mergeDeployValues(defaults, overrides);
64
+ if (typeof values.host !== "string" || /[\\/?#@:\s]/.test(values.host)) {
65
+ throw new Error("values.host \u5FC5\u987B\u662F\u4E0D\u542B\u534F\u8BAE\u3001\u7AEF\u53E3\u548C\u8DEF\u5F84\u7684\u4E3B\u673A\u540D\uFF0C\u4F8B\u5982 specdoc.example.com");
66
+ }
67
+ if (!["saas", "infra"].includes(values.targetK8s)) throw new Error("deploy.values.targetK8s \u53EA\u80FD\u4E3A saas \u6216 infra");
68
+ if (target === source) throw new Error("\u90E8\u7F72\u8F93\u51FA\u4E0D\u80FD\u8986\u76D6\u6846\u67B6\u6A21\u677F\u76EE\u5F55");
69
+ if (fs.lstatSync(target, { throwIfNoEntry: false })?.isSymbolicLink()) throw new Error("deploy \u4E0D\u80FD\u4E3A\u7B26\u53F7\u94FE\u63A5\uFF0C\u4EE5\u514D\u8986\u76D6\u5916\u90E8\u76EE\u5F55");
70
+ const disabledEvents = "[custom, tag]";
71
+ const drone = fs.readFileSync(path.join(source, ".drone.yml"), "utf8").replaceAll("<<TARGET_K8S>>", values.targetK8s).replaceAll("<<EXCLUDE_EVENTS_deploy>>", h5Environments.length ? "[]" : disabledEvents).replaceAll("<<EXCLUDE_EVENTS_h5>>", h5Environments.length ? "[]" : disabledEvents).replaceAll(
72
+ "<<EXCLUDE_EVENTS_weapp>>",
73
+ miniEnvironments.some((environment) => environment !== "stable" /* Stable */) ? "[]" : disabledEvents
74
+ ).replace(
75
+ /<<EXCLUDE_EVENTS_weapp_(test|stage|prod)>>/g,
76
+ (_placeholder, environment) => miniEnvironments.includes(environment) ? "[]" : disabledEvents
77
+ ).replaceAll("<<EXCLUDE_EVENTS_push_helm_chart>>", pushHelmChart ? "[]" : disabledEvents).replace(
78
+ /<<EXCLUDE_EVENTS_(test|stage|prod|stable)>>/g,
79
+ /** 开启时不排除事件,关闭时由 Drone 原生 when 跳过该步骤。 */
80
+ (_placeholder, environment) => h5Environments.includes(environment) ? "[]" : disabledEvents
81
+ );
82
+ fs.cpSync(source, target, { recursive: true });
83
+ fs.writeFileSync(
84
+ path.join(target, "helm/values.yaml"),
85
+ yaml.stringify(values, {
86
+ defaultStringType: "QUOTE_SINGLE",
87
+ defaultKeyType: "PLAIN"
88
+ })
89
+ );
90
+ fs.writeFileSync(path.join(target, ".drone.yml"), drone);
91
+ };
92
+ function mergeDeployValues(base, overrides) {
93
+ return Object.fromEntries(
94
+ Object.entries({ ...base, ...overrides }).map(
95
+ /** 仅合并普通对象,列表保持声明的顺序。 */
96
+ ([key, value]) => {
97
+ const previous = base[key];
98
+ const previousObject = previous && typeof previous === "object" && !Array.isArray(previous);
99
+ if (value === true && previousObject) {
100
+ return [key, Object.hasOwn(previous, "enabled") ? { ...previous, enabled: true } : previous];
101
+ }
102
+ const mergeable = value && previousObject && typeof value === "object" && !Array.isArray(value);
103
+ return [key, mergeable ? mergeDeployValues(previous, value) : value];
104
+ }
105
+ )
106
+ );
107
+ }
108
+
109
+ // src/config/deploy.ts
110
+ var generateDeploy2 = (deploy) => {
111
+ const directory = typeof __dirname === "string" ? __dirname : path2.dirname(fileURLToPath(import.meta.url));
112
+ generateDeploy({ deploy, frameworkRoot: path2.resolve(directory, ".."), isMini: true });
113
+ };
114
+
115
+ // src/config.ts
116
+ var loadXfeConfig = () => {
117
+ const xfeConfigPath = "./xfe.json";
118
+ if (!fs2.existsSync(xfeConfigPath)) {
119
+ throw new Error("xfe.json \u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u8BF7\u5728\u9879\u76EE\u6839\u76EE\u5F55\u4E0B\u521B\u5EFA\u8BE5\u6587\u4EF6\u3002");
120
+ }
121
+ const xfeConfigContent = fs2.readFileSync(xfeConfigPath, "utf8");
122
+ return JSON.parse(xfeConfigContent);
123
+ };
124
+ var configDeploy = () => generateDeploy2(loadXfeConfig().deploy);
125
+ var getJwtEnv = async () => {
126
+ const xfeConfig = loadXfeConfig();
127
+ if (!xfeConfig?.jwt) return {};
128
+ const { aud = "" } = xfeConfig.jwt;
129
+ if (!aud) throw new Error("xfe.json \u4E2D\u7684 jwt \u914D\u7F6E\u4E0D\u5B8C\u6574\uFF0C\u8BF7\u786E\u4FDD\u5305\u542B aud \u5B57\u6BB5\u3002");
130
+ const jwkRes = await fetch(`https://app.eshetang.com/jwk?appId=${aud}`);
131
+ if (!jwkRes.ok) {
132
+ throw new Error(`\u8FDC\u7A0B\u83B7\u53D6\u5BC6\u94A5\u5931\u8D25: ${jwkRes.status} ${jwkRes.statusText}`);
133
+ }
134
+ let JwkResponseJson;
135
+ try {
136
+ JwkResponseJson = await jwkRes.json();
137
+ } catch (err) {
138
+ throw new Error(`\u8FDC\u7A0B\u83B7\u53D6\u5BC6\u94A5\u5931\u8D25\uFF0C\u54CD\u5E94\u89E3\u6790\u9519\u8BEF: ${err.message}, response text: ${await jwkRes.text()}`);
139
+ }
140
+ const { keyId, privateKeyPkcs8 } = JwkResponseJson?.data || {};
141
+ if (!keyId || !privateKeyPkcs8) throw new Error(`\u672A\u83B7\u53D6\u5230\u9879\u76EE\u7B7E\u540D\u5BC6\u94A5, \u8BF7\u68C0\u67E5aud\u662F\u5426\u6B63\u786E: ${aud}`);
142
+ return {
143
+ XFE_JWT_AUD: aud,
144
+ XFE_JWT_KEY_ID: keyId,
145
+ XFE_JWT_PRIVATE_KEY: privateKeyPkcs8
146
+ };
147
+ };
148
+
149
+ export {
150
+ loadXfeConfig,
151
+ configDeploy,
152
+ getJwtEnv
153
+ };
@@ -0,0 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ export {
9
+ __require
10
+ };