@dd-code/uni-tools 1.0.9 → 1.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/dist/cli.js CHANGED
@@ -3,9 +3,11 @@
3
3
 
4
4
  var shared = require('@dd-code/shared');
5
5
  var path = require('path');
6
- require('fs');
6
+ var fs = require('fs');
7
7
  require('crypto');
8
+ require('chokidar');
8
9
  var child_process = require('child_process');
10
+ var inquirer = require('inquirer');
9
11
 
10
12
  /******************************************************************************
11
13
  Copyright (c) Microsoft Corporation.
@@ -24,21 +26,128 @@ PERFORMANCE OF THIS SOFTWARE.
24
26
  /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
25
27
 
26
28
 
29
+ function __awaiter(thisArg, _arguments, P, generator) {
30
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
31
+ return new (P || (P = Promise))(function (resolve, reject) {
32
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
33
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
34
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ }
38
+
39
+ function __generator(thisArg, body) {
40
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
41
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
42
+ function verb(n) { return function (v) { return step([n, v]); }; }
43
+ function step(op) {
44
+ if (f) throw new TypeError("Generator is already executing.");
45
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
46
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
47
+ if (y = 0, t) op = [op[0] & 2, t.value];
48
+ switch (op[0]) {
49
+ case 0: case 1: t = op; break;
50
+ case 4: _.label++; return { value: op[1], done: false };
51
+ case 5: _.label++; y = op[1]; op = [0]; continue;
52
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
53
+ default:
54
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
55
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
56
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
57
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
58
+ if (t[2]) _.ops.pop();
59
+ _.trys.pop(); continue;
60
+ }
61
+ op = body.call(thisArg, _);
62
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
63
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
64
+ }
65
+ }
66
+
27
67
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
28
68
  var e = new Error(message);
29
69
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
30
70
  };
31
71
 
72
+ var MFE_NAME = "mfe.json";
73
+
74
+ /**
75
+ * 读取文件内容
76
+ * 支持JSON文件的自动解析
77
+ * @param {string} filaPath - 文件路径
78
+ * @returns {string|Object} 返回文件内容,JSON文件返回对象,其他返回字符串
79
+ */
80
+ function uniReadFile(filaPath) {
81
+ try {
82
+ if (filaPath.endsWith(".json")) {
83
+ return require(filaPath);
84
+ }
85
+ var res = fs.readFileSync(filaPath, "utf-8");
86
+ return res;
87
+ }
88
+ catch (e) {
89
+ // console.error(`${filaPath} notfound`);
90
+ // console.log(e);
91
+ }
92
+ return "";
93
+ }
94
+ var checkAndgenreDir = function (dir) {
95
+ if (!fs.existsSync(dir)) {
96
+ // 如果路径不存在,创建路径
97
+ fs.mkdirSync(dir, { recursive: true });
98
+ }
99
+ };
32
100
  var loadViteConfig = function (mode) {
33
101
  var loadEnv = require("vite").loadEnv;
34
102
  var ROOT = process.cwd();
35
103
  return loadEnv(mode, ROOT, "MFE_");
36
104
  };
37
105
 
106
+ // 项目 Git 子模块路径
107
+ var PROJECT_GIT_PATH = "src/subtree";
38
108
  // CDN 文件保存路径
39
109
  path.join(process.cwd(), "node_modules/@dd-code/uni-files");
110
+ path.join(process.cwd(), "node_modules/@dd-code/__main-pwd.txt");
111
+ path.join(process.cwd(), "node_modules/@dd-code/manifest-list.json");
40
112
  path.join(process.cwd(), "node_modules/@dd-code/current-files");
41
- var ROOT_APP_CODE = "main"; //"__MFE_APP_ROOT__";
113
+ // 发布目录路径
114
+ path.join(process.cwd(), "dist/publish");
115
+ var ROOT_APP_CODE = "__MFE_APP_ROOT__";
116
+ var EBuildMode;
117
+ (function (EBuildMode) {
118
+ EBuildMode["BUILD"] = "build";
119
+ EBuildMode["SERVE"] = "serve";
120
+ })(EBuildMode || (EBuildMode = {}));
121
+ /**
122
+ * 获取微前端配置 JSON
123
+ * @description 读取并解析 mfe.json 配置文件,同时从环境变量中获取相关配置
124
+ * @returns {Object} 包含微前端配置的对象
125
+ * @returns {boolean} returns.isRoot - 是否为根应用
126
+ * @returns {string} returns.code - 项目代码
127
+ * @returns {string} returns.appCode - 应用代码
128
+ * @example
129
+ * // 返回: { isRoot: true, code: 'myapp', appCode: 'main', ... }
130
+ */
131
+ var getMfeJson = function () {
132
+ // 读取 mfe.json 配置文件
133
+ var jsonPath = path.resolve(process.cwd(), MFE_NAME);
134
+ var json = uniReadFile(jsonPath) || {};
135
+ // const root = process.cwd();
136
+ // const { loadEnv } = require("vite");
137
+ // const viteEnv = loadEnv(mode || "dev", root, "");
138
+ // const envObj = {
139
+ // ...viteEnv,
140
+ // platform: process.env.UNI_PLATFORM || "h5",
141
+ // };
142
+ // console.log("-----", process.env, mode, "viteEnv");
143
+ // // 从环境变量中获取配置
144
+ // json.isRoot = envObj.UNI_IS_ROOT;
145
+ // json.code = envObj.UNI_CODE;
146
+ // json.appCode = envObj.MFE_APP_CODE;
147
+ json.platform = process.env.UNI_PLATFORM || "h5";
148
+ // json.mode = envObj.MODE || "dev";
149
+ return json;
150
+ };
42
151
  var formatCliCommandConfig = function (mode) {
43
152
  var viteEnv = loadViteConfig(mode || "dev");
44
153
  var isRoot = viteEnv.MFE_UNI_IS_ROOT;
@@ -49,6 +158,7 @@ var formatCliCommandConfig = function (mode) {
49
158
  code: viteEnv.MFE_UNI_CODE,
50
159
  mode: mode,
51
160
  cdn: viteEnv.MFE_CDN_HOST,
161
+ serve: viteEnv.MFE_UNI_SERVE, // 是否开启 serve 功能
52
162
  };
53
163
  };
54
164
  var E_WS_TYPE;
@@ -57,39 +167,152 @@ var E_WS_TYPE;
57
167
  E_WS_TYPE["CHANGE"] = "change_files";
58
168
  })(E_WS_TYPE || (E_WS_TYPE = {}));
59
169
 
60
- var excuteUniCommand = function (command, _a) {
61
- var isRoot = _a.isRoot, appCode = _a.appCode;
62
- var cmd = "uni ".concat(command, " ").concat(!isRoot ? "--subpackage=".concat(appCode) : "");
63
- child_process.execSync(cmd, {
64
- stdio: "inherit",
170
+ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0, void 0, function () {
171
+ var _a, isRoot, appCode, cmd;
172
+ return __generator(this, function (_b) {
173
+ process.env.MFE_TARGET_DIR = (opt === null || opt === void 0 ? void 0 : opt.buildDir) || "";
174
+ _a = opt || {}, isRoot = _a.isRoot, appCode = _a.appCode;
175
+ cmd = "".concat(command, " ").concat(!isRoot ? "--subpackage=".concat(appCode) : "");
176
+ // if (isRoot) {
177
+ // const mfeJson = getMfeJson();
178
+ // const apps = mfeJson.apps?.map((i) => i.appCode) || [];
179
+ // apps.forEach((appCode) => {
180
+ // const appCodeDir = appCode.replace(/\//g, "_");
181
+ // // console.log(appCodeDir, 'appCodeDir');
182
+ // const childPath = path.join(process.cwd(), PROJECT_GIT_PATH, appCodeDir);
183
+ // console.log(existsSync(appCodeDir), childPath, "childPath");
184
+ // // if (existsSync(appCodeDir)) {
185
+ // // excuteUniCommand(`cd ${childPath} && ${command}`, {
186
+ // // appCode,
187
+ // // target: appCodeDir,
188
+ // // });
189
+ // // 开一个子线程启动
190
+ // // execSync(cmd, {
191
+ // // stdio: "inherit",
192
+ // // });
193
+ // // }
194
+ // });
195
+ // // setTimeout(() => {
196
+ // // console.log(process.env.UNI_OUTPUT_DIR, "process.env.UNI_OUTPUT_DIR");
197
+ // // });
198
+ // }
199
+ // console.log(cmd, "-------");
200
+ child_process.execSync(cmd, {
201
+ stdio: "inherit",
202
+ });
203
+ return [2 /*return*/];
65
204
  });
66
- };
205
+ }); };
206
+
207
+ //@ts-ignore
208
+ var inquirerPrompt = ((inquirer === null || inquirer === void 0 ? void 0 : inquirer.default) ||
209
+ inquirer);
210
+
211
+ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, function () {
212
+ var mfeJson, apps, answers, selectedApps, selectedGitApps;
213
+ return __generator(this, function (_a) {
214
+ switch (_a.label) {
215
+ case 0:
216
+ mfeJson = getMfeJson();
217
+ apps = (mfeJson === null || mfeJson === void 0 ? void 0 : mfeJson.apps) || [];
218
+ return [4 /*yield*/, inquirerPrompt.prompt([
219
+ {
220
+ type: "checkbox",
221
+ name: "selectedApps",
222
+ message: "请选择要拉取的应用仓库:",
223
+ choices: apps.map(function (app) { return ({
224
+ name: app.appCode,
225
+ value: app.appCode,
226
+ }); }),
227
+ },
228
+ ])];
229
+ case 1:
230
+ answers = _a.sent();
231
+ selectedApps = answers.selectedApps;
232
+ selectedGitApps = apps.filter(function (app) { return selectedApps.includes(app.appCode) && app.repoUrl; });
233
+ console.log("选择的应用仓库:", selectedGitApps);
234
+ // 执行拉取操作
235
+ selectedGitApps.forEach(function (_a) {
236
+ var appCode = _a.appCode, repoUrl = _a.repoUrl;
237
+ if (repoUrl) {
238
+ var fileName = appCode.replace(/\//g, "_");
239
+ var dir = PROJECT_GIT_PATH;
240
+ checkAndgenreDir(dir);
241
+ child_process.execSync("cd ".concat(dir, " && git clone ").concat(repoUrl, " ").concat(fileName), {
242
+ stdio: "inherit",
243
+ });
244
+ }
245
+ });
246
+ return [2 /*return*/];
247
+ }
248
+ });
249
+ }); };
67
250
 
68
251
  var addUniOptions = function (program) {
69
252
  return program
70
253
  .option("-p <platform>", "平台", "h5")
71
- .option("--mode <mode>", "模式", "development");
254
+ .option("--mode <mode>", "模式", "development")
255
+ .option("--b <buildDir>", "目标路径", "");
72
256
  };
73
257
  var dev = shared.program
74
258
  .name("uni-tools")
75
259
  .command("serve")
76
260
  .description("uni 工具方法");
77
261
  var build = shared.program.command("build").description("构建 uni 项目");
262
+ // const pushCdn = program.command("push-cdn").description("推送 uni 项目到 cdn");
263
+ var fetchGit = shared.program.command("fetch").description("拉取 uni 项目到本地");
264
+ fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function () {
265
+ return __generator(this, function (_a) {
266
+ switch (_a.label) {
267
+ case 0: return [4 /*yield*/, fetchAppsRepo()];
268
+ case 1:
269
+ _a.sent();
270
+ return [2 /*return*/];
271
+ }
272
+ });
273
+ }); });
274
+ // pushCdn.option("--mode <mode>", "模式", "dev").action(({ mode }) => {
275
+ // pushDistToCdn(mode);
276
+ // });
78
277
  addUniOptions(dev).action(function (_a) {
79
- var mode = _a.mode, platform = _a.p;
278
+ var mode = _a.mode, platform = _a.p, b = _a.b;
279
+ process.env.MFE_BUILD_MODE = EBuildMode.SERVE;
80
280
  var _b = formatCliCommandConfig(mode), isRoot = _b.isRoot, appCode = _b.appCode;
81
281
  switch (platform) {
82
282
  case "h5":
283
+ excuteUniCommand("uni -p ".concat(platform, " --mode ").concat(mode));
83
284
  break;
84
285
  case "mp-weixin":
85
286
  excuteUniCommand("uni -p ".concat(platform, " --mode ").concat(mode), {
86
287
  isRoot: isRoot,
87
288
  appCode: appCode,
289
+ buildDir: b,
88
290
  });
89
291
  break;
292
+ default:
293
+ excuteUniCommand("uni -p ".concat(platform, " --mode ").concat(mode));
294
+ break;
90
295
  }
91
296
  // debugger;
92
297
  // console.log(JSON.stringify({ mode, platform, mfeJson }), "-111-------------");
93
298
  });
94
- addUniOptions(build).action(function (opt) { });
299
+ addUniOptions(build).action(function (_a) {
300
+ var mode = _a.mode, platform = _a.p;
301
+ process.env.MFE_BUILD_MODE = EBuildMode.BUILD;
302
+ var _b = formatCliCommandConfig(mode), isRoot = _b.isRoot, appCode = _b.appCode;
303
+ switch (platform) {
304
+ case "h5":
305
+ excuteUniCommand("uni build -p ".concat(platform, " --mode ").concat(mode));
306
+ break;
307
+ case "mp-weixin":
308
+ excuteUniCommand("uni build -p ".concat(platform, " --mode ").concat(mode), {
309
+ isRoot: isRoot,
310
+ appCode: appCode,
311
+ });
312
+ break;
313
+ default:
314
+ excuteUniCommand("uni build -p ".concat(platform, " --mode ").concat(mode));
315
+ break;
316
+ }
317
+ });
95
318
  shared.program.parseAsync(process.argv);
@@ -0,0 +1,316 @@
1
+ #!/usr/bin/env node
2
+ import { program } from '@dd-code/shared';
3
+ import path from 'path';
4
+ import fs from 'fs';
5
+ import 'crypto';
6
+ import 'chokidar';
7
+ import { execSync } from 'child_process';
8
+ import inquirer from 'inquirer';
9
+
10
+ /******************************************************************************
11
+ Copyright (c) Microsoft Corporation.
12
+
13
+ Permission to use, copy, modify, and/or distribute this software for any
14
+ purpose with or without fee is hereby granted.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
17
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
19
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
20
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22
+ PERFORMANCE OF THIS SOFTWARE.
23
+ ***************************************************************************** */
24
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
25
+
26
+
27
+ function __awaiter(thisArg, _arguments, P, generator) {
28
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
29
+ return new (P || (P = Promise))(function (resolve, reject) {
30
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
31
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
32
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
33
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
34
+ });
35
+ }
36
+
37
+ function __generator(thisArg, body) {
38
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
39
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
40
+ function verb(n) { return function (v) { return step([n, v]); }; }
41
+ function step(op) {
42
+ if (f) throw new TypeError("Generator is already executing.");
43
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
44
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
45
+ if (y = 0, t) op = [op[0] & 2, t.value];
46
+ switch (op[0]) {
47
+ case 0: case 1: t = op; break;
48
+ case 4: _.label++; return { value: op[1], done: false };
49
+ case 5: _.label++; y = op[1]; op = [0]; continue;
50
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
51
+ default:
52
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
53
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
54
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
55
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
56
+ if (t[2]) _.ops.pop();
57
+ _.trys.pop(); continue;
58
+ }
59
+ op = body.call(thisArg, _);
60
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
61
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
+ }
63
+ }
64
+
65
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
66
+ var e = new Error(message);
67
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
68
+ };
69
+
70
+ var MFE_NAME = "mfe.json";
71
+
72
+ /**
73
+ * 读取文件内容
74
+ * 支持JSON文件的自动解析
75
+ * @param {string} filaPath - 文件路径
76
+ * @returns {string|Object} 返回文件内容,JSON文件返回对象,其他返回字符串
77
+ */
78
+ function uniReadFile(filaPath) {
79
+ try {
80
+ if (filaPath.endsWith(".json")) {
81
+ return require(filaPath);
82
+ }
83
+ var res = fs.readFileSync(filaPath, "utf-8");
84
+ return res;
85
+ }
86
+ catch (e) {
87
+ // console.error(`${filaPath} notfound`);
88
+ // console.log(e);
89
+ }
90
+ return "";
91
+ }
92
+ var checkAndgenreDir = function (dir) {
93
+ if (!fs.existsSync(dir)) {
94
+ // 如果路径不存在,创建路径
95
+ fs.mkdirSync(dir, { recursive: true });
96
+ }
97
+ };
98
+ var loadViteConfig = function (mode) {
99
+ var loadEnv = require("vite").loadEnv;
100
+ var ROOT = process.cwd();
101
+ return loadEnv(mode, ROOT, "MFE_");
102
+ };
103
+
104
+ // 项目 Git 子模块路径
105
+ var PROJECT_GIT_PATH = "src/subtree";
106
+ // CDN 文件保存路径
107
+ path.join(process.cwd(), "node_modules/@dd-code/uni-files");
108
+ path.join(process.cwd(), "node_modules/@dd-code/__main-pwd.txt");
109
+ path.join(process.cwd(), "node_modules/@dd-code/manifest-list.json");
110
+ path.join(process.cwd(), "node_modules/@dd-code/current-files");
111
+ // 发布目录路径
112
+ path.join(process.cwd(), "dist/publish");
113
+ var ROOT_APP_CODE = "__MFE_APP_ROOT__";
114
+ var EBuildMode;
115
+ (function (EBuildMode) {
116
+ EBuildMode["BUILD"] = "build";
117
+ EBuildMode["SERVE"] = "serve";
118
+ })(EBuildMode || (EBuildMode = {}));
119
+ /**
120
+ * 获取微前端配置 JSON
121
+ * @description 读取并解析 mfe.json 配置文件,同时从环境变量中获取相关配置
122
+ * @returns {Object} 包含微前端配置的对象
123
+ * @returns {boolean} returns.isRoot - 是否为根应用
124
+ * @returns {string} returns.code - 项目代码
125
+ * @returns {string} returns.appCode - 应用代码
126
+ * @example
127
+ * // 返回: { isRoot: true, code: 'myapp', appCode: 'main', ... }
128
+ */
129
+ var getMfeJson = function () {
130
+ // 读取 mfe.json 配置文件
131
+ var jsonPath = path.resolve(process.cwd(), MFE_NAME);
132
+ var json = uniReadFile(jsonPath) || {};
133
+ // const root = process.cwd();
134
+ // const { loadEnv } = require("vite");
135
+ // const viteEnv = loadEnv(mode || "dev", root, "");
136
+ // const envObj = {
137
+ // ...viteEnv,
138
+ // platform: process.env.UNI_PLATFORM || "h5",
139
+ // };
140
+ // console.log("-----", process.env, mode, "viteEnv");
141
+ // // 从环境变量中获取配置
142
+ // json.isRoot = envObj.UNI_IS_ROOT;
143
+ // json.code = envObj.UNI_CODE;
144
+ // json.appCode = envObj.MFE_APP_CODE;
145
+ json.platform = process.env.UNI_PLATFORM || "h5";
146
+ // json.mode = envObj.MODE || "dev";
147
+ return json;
148
+ };
149
+ var formatCliCommandConfig = function (mode) {
150
+ var viteEnv = loadViteConfig(mode || "dev");
151
+ var isRoot = viteEnv.MFE_UNI_IS_ROOT;
152
+ // MFE_CDN_HOST
153
+ return {
154
+ isRoot: isRoot,
155
+ appCode: isRoot ? ROOT_APP_CODE : viteEnv.MFE_APP_CODE,
156
+ code: viteEnv.MFE_UNI_CODE,
157
+ mode: mode,
158
+ cdn: viteEnv.MFE_CDN_HOST,
159
+ serve: viteEnv.MFE_UNI_SERVE, // 是否开启 serve 功能
160
+ };
161
+ };
162
+ var E_WS_TYPE;
163
+ (function (E_WS_TYPE) {
164
+ E_WS_TYPE["INIT"] = "init_files";
165
+ E_WS_TYPE["CHANGE"] = "change_files";
166
+ })(E_WS_TYPE || (E_WS_TYPE = {}));
167
+
168
+ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0, void 0, function () {
169
+ var _a, isRoot, appCode, cmd;
170
+ return __generator(this, function (_b) {
171
+ process.env.MFE_TARGET_DIR = (opt === null || opt === void 0 ? void 0 : opt.buildDir) || "";
172
+ _a = opt || {}, isRoot = _a.isRoot, appCode = _a.appCode;
173
+ cmd = "".concat(command, " ").concat(!isRoot ? "--subpackage=".concat(appCode) : "");
174
+ // if (isRoot) {
175
+ // const mfeJson = getMfeJson();
176
+ // const apps = mfeJson.apps?.map((i) => i.appCode) || [];
177
+ // apps.forEach((appCode) => {
178
+ // const appCodeDir = appCode.replace(/\//g, "_");
179
+ // // console.log(appCodeDir, 'appCodeDir');
180
+ // const childPath = path.join(process.cwd(), PROJECT_GIT_PATH, appCodeDir);
181
+ // console.log(existsSync(appCodeDir), childPath, "childPath");
182
+ // // if (existsSync(appCodeDir)) {
183
+ // // excuteUniCommand(`cd ${childPath} && ${command}`, {
184
+ // // appCode,
185
+ // // target: appCodeDir,
186
+ // // });
187
+ // // 开一个子线程启动
188
+ // // execSync(cmd, {
189
+ // // stdio: "inherit",
190
+ // // });
191
+ // // }
192
+ // });
193
+ // // setTimeout(() => {
194
+ // // console.log(process.env.UNI_OUTPUT_DIR, "process.env.UNI_OUTPUT_DIR");
195
+ // // });
196
+ // }
197
+ // console.log(cmd, "-------");
198
+ execSync(cmd, {
199
+ stdio: "inherit",
200
+ });
201
+ return [2 /*return*/];
202
+ });
203
+ }); };
204
+
205
+ //@ts-ignore
206
+ var inquirerPrompt = ((inquirer === null || inquirer === void 0 ? void 0 : inquirer.default) ||
207
+ inquirer);
208
+
209
+ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, function () {
210
+ var mfeJson, apps, answers, selectedApps, selectedGitApps;
211
+ return __generator(this, function (_a) {
212
+ switch (_a.label) {
213
+ case 0:
214
+ mfeJson = getMfeJson();
215
+ apps = (mfeJson === null || mfeJson === void 0 ? void 0 : mfeJson.apps) || [];
216
+ return [4 /*yield*/, inquirerPrompt.prompt([
217
+ {
218
+ type: "checkbox",
219
+ name: "selectedApps",
220
+ message: "请选择要拉取的应用仓库:",
221
+ choices: apps.map(function (app) { return ({
222
+ name: app.appCode,
223
+ value: app.appCode,
224
+ }); }),
225
+ },
226
+ ])];
227
+ case 1:
228
+ answers = _a.sent();
229
+ selectedApps = answers.selectedApps;
230
+ selectedGitApps = apps.filter(function (app) { return selectedApps.includes(app.appCode) && app.repoUrl; });
231
+ console.log("选择的应用仓库:", selectedGitApps);
232
+ // 执行拉取操作
233
+ selectedGitApps.forEach(function (_a) {
234
+ var appCode = _a.appCode, repoUrl = _a.repoUrl;
235
+ if (repoUrl) {
236
+ var fileName = appCode.replace(/\//g, "_");
237
+ var dir = PROJECT_GIT_PATH;
238
+ checkAndgenreDir(dir);
239
+ execSync("cd ".concat(dir, " && git clone ").concat(repoUrl, " ").concat(fileName), {
240
+ stdio: "inherit",
241
+ });
242
+ }
243
+ });
244
+ return [2 /*return*/];
245
+ }
246
+ });
247
+ }); };
248
+
249
+ var addUniOptions = function (program) {
250
+ return program
251
+ .option("-p <platform>", "平台", "h5")
252
+ .option("--mode <mode>", "模式", "development")
253
+ .option("--b <buildDir>", "目标路径", "");
254
+ };
255
+ var dev = program
256
+ .name("uni-tools")
257
+ .command("serve")
258
+ .description("uni 工具方法");
259
+ var build = program.command("build").description("构建 uni 项目");
260
+ // const pushCdn = program.command("push-cdn").description("推送 uni 项目到 cdn");
261
+ var fetchGit = program.command("fetch").description("拉取 uni 项目到本地");
262
+ fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function () {
263
+ return __generator(this, function (_a) {
264
+ switch (_a.label) {
265
+ case 0: return [4 /*yield*/, fetchAppsRepo()];
266
+ case 1:
267
+ _a.sent();
268
+ return [2 /*return*/];
269
+ }
270
+ });
271
+ }); });
272
+ // pushCdn.option("--mode <mode>", "模式", "dev").action(({ mode }) => {
273
+ // pushDistToCdn(mode);
274
+ // });
275
+ addUniOptions(dev).action(function (_a) {
276
+ var mode = _a.mode, platform = _a.p, b = _a.b;
277
+ process.env.MFE_BUILD_MODE = EBuildMode.SERVE;
278
+ var _b = formatCliCommandConfig(mode), isRoot = _b.isRoot, appCode = _b.appCode;
279
+ switch (platform) {
280
+ case "h5":
281
+ excuteUniCommand("uni -p ".concat(platform, " --mode ").concat(mode));
282
+ break;
283
+ case "mp-weixin":
284
+ excuteUniCommand("uni -p ".concat(platform, " --mode ").concat(mode), {
285
+ isRoot: isRoot,
286
+ appCode: appCode,
287
+ buildDir: b,
288
+ });
289
+ break;
290
+ default:
291
+ excuteUniCommand("uni -p ".concat(platform, " --mode ").concat(mode));
292
+ break;
293
+ }
294
+ // debugger;
295
+ // console.log(JSON.stringify({ mode, platform, mfeJson }), "-111-------------");
296
+ });
297
+ addUniOptions(build).action(function (_a) {
298
+ var mode = _a.mode, platform = _a.p;
299
+ process.env.MFE_BUILD_MODE = EBuildMode.BUILD;
300
+ var _b = formatCliCommandConfig(mode), isRoot = _b.isRoot, appCode = _b.appCode;
301
+ switch (platform) {
302
+ case "h5":
303
+ excuteUniCommand("uni build -p ".concat(platform, " --mode ").concat(mode));
304
+ break;
305
+ case "mp-weixin":
306
+ excuteUniCommand("uni build -p ".concat(platform, " --mode ").concat(mode), {
307
+ isRoot: isRoot,
308
+ appCode: appCode,
309
+ });
310
+ break;
311
+ default:
312
+ excuteUniCommand("uni build -p ".concat(platform, " --mode ").concat(mode));
313
+ break;
314
+ }
315
+ });
316
+ program.parseAsync(process.argv);