@dd-code/uni-tools 1.0.14 → 1.0.16

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/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  ## 特性
6
6
  - 主/子应用模块化:主应用负责壳与公共能力,子应用以分包形式接入。
7
7
  - 配置自动合并:自动汇总各子应用的 `pages.json` 并生成最终 `app.json`。
8
- - 开发态联调:监听子应用构建输出并同步至主应用分包目录,支持 WS 通知。
8
+ - 开发态联调:监听子应用构建输出并同步至主应用分包目录,支持实时同步。
9
9
  - 运行时暴露:通过 `@dd-code/runtime` 统一导出公共模块(如 `store/index.js`)。
10
10
  - 一体化 CLI:拉取子应用仓库、启动开发、执行构建,开箱即用。
11
11
 
@@ -16,13 +16,12 @@
16
16
  - 可选参数:
17
17
  - `-p <platform>`:平台,默认 `h5`,支持 `mp-weixin`。
18
18
  - `--mode <mode>`:模式,默认 `development`。
19
- - `--b <buildDir>`:指定构建目标输出路径(部分场景有效)。
20
19
 
21
20
  ## 是干嘛的 / 实现的效果是什么
22
21
  - 统一管理主/子应用:自动收集各子应用的页面配置,合并生成主应用最终 `app.json`。
23
22
  - 开发态联动:
24
23
  - 子应用构建产物变更后,自动同步到主应用分包目录;
25
- - 利用 WebSocket 在主子应用之间广播页面/静态资源变更,减少手动刷新与重复构建。
24
+ - 在主子应用之间自动同步页面/静态资源变更,减少手动刷新与重复构建。
26
25
  - 生产构建可控:按环境与模式输出稳定的主应用与分包结构,避免配置漂移与遗漏。
27
26
  - 仓库管理简化:通过交互命令拉取/更新子应用仓库,快速搭建本地联调环境。
28
27
 
@@ -31,13 +30,13 @@
31
30
  - 目录分层:`core/ + plugins/ + server/ + utils`,职责明确、低耦合
32
31
  - `core`:运行时与 `app.json` 处理(如 `core/app-json.ts`、`core/runtime.ts`)
33
32
  - `plugins`:Vite 插件(主应用、exposes 等)
34
- - `server`:HTTP/WS 服务
33
+ - `server`:通信服务
35
34
  - `utils`:通用工具(文件复制、下载等)
36
35
  - Manifest 管理与 `app.json` 合并:`packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:31-76, 105-119, 261-279`
37
36
  - 汇总子应用 `pages.json`,渲染为主应用分包配置并写入 `app.json`
38
37
  - 写入采用内容比较,避免频繁重启与重复打包(`core/app-json.ts`)
39
38
  - 开发态联调:`packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:121-156, 187-245`
40
- - 主应用启动 WS 服务,子应用变更触发同步与增量更新
39
+ - 主应用启动通信服务,子应用变更触发同步与增量更新
41
40
  - 监听拷贝使用内容比较,避免无效触发(`utils/copy.ts`)
42
41
 
43
42
  ## 运行时暴露(Runtime Exposes)
@@ -68,15 +67,12 @@
68
67
  - `MFE_UNI_IS_ROOT=true|false`:是否主应用(影响 `appCode` 填充与插件行为)(packages/uni-tools/src/config/config.ts:130-141)。
69
68
  - `MFE_UNI_CODE=your-project-code`:项目代码,用于 CDN 路径拼接与 Manifest 标识(packages/uni-tools/src/config/config.ts:90-105, 116-141)。
70
69
  - `MFE_APP_CODE=__MFE_APP_ROOT__|modules/manage`:当前应用的 `appCode`。
71
- - `MFE_UNI_SERVE=true|false`:开发态是否启用联调(WS/监听)(packages/uni-tools/src/plugins/modules/mp-weixin/manifest-core.ts:75-87)。
72
70
  - `MFE_CDN_HOST=https://static.example.com`:CDN 域名,供 Manifest 资源定位(packages/uni-tools/src/config/config.ts:116-141)。
73
- - `UNI_PLATFORM=mp-weixin`:平台选择(packages/uni-tools/src/config/config.ts:103-105)。
74
71
  - 运行时自动设置的变量(无需手动维护)
75
72
  - `MFE_SOURCE_OUTPUT_DIR`:当前应用构建输出目录(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:64)。
76
73
  - `MFE_ROOT_OUTPUT_DIR`:主应用根输出目录(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:65-69)。
77
74
  - `UNI_OUTPUT_DIR`:根输出目录,生成/读取 `app.json`(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:72-76)。
78
75
  - `MFE_INNER_BUILD=true`:子应用处于“内部构建”模式(packages/uni-tools/src/utils/utils.ts:204)。
79
- - `MFE_TARGET_DIR=root`:由 `--b root` 触发,用于判断内部构建(packages/uni-tools/src/utils/utils.ts:202)。
80
76
 
81
77
  ## 快速开始
82
78
  1. 安装依赖:
@@ -104,22 +100,18 @@
104
100
  ```
105
101
  MFE_UNI_IS_ROOT=true
106
102
  MFE_UNI_CODE=your-project-code
107
- MFE_UNI_SERVE=true
108
- UNI_PLATFORM=mp-weixin
109
103
  ```
110
104
  - 启动:`npx uni-tools serve -p mp-weixin --mode development`
111
- - 行为:开启 WS 服务与合并逻辑,动态维护最终 `app.json`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:121-143, 261-279)。
105
+ - 行为:开启通信服务与合并逻辑,动态维护最终 `app.json`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:121-143, 261-279)。
112
106
  - 子应用开发(联调)
113
107
  - `.env.development` 示例:
114
108
  ```
115
109
  MFE_UNI_IS_ROOT=false
116
110
  MFE_UNI_CODE=your-project-code
117
111
  MFE_APP_CODE=modules/manage
118
- MFE_UNI_SERVE=true
119
- UNI_PLATFORM=mp-weixin
120
112
  ```
121
113
  - 启动:`npx uni-tools serve -p mp-weixin --mode development`
122
- - 行为:作为 WS 客户端与主应用通信,监听自身输出变更并同步到主应用分包路径(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:145-156, 78-103)。
114
+ - 行为:与主应用服务通信,监听自身输出变更并同步到主应用分包路径(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:145-156, 78-103)。
123
115
  - 子应用构建(不联调,直接写入主应用)
124
116
  - 前置:主应用开发态启动但 `MFE_UNI_SERVE=false`,提供 HTTP 接口返回主应用 `UNI_OUTPUT_DIR`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:190-200)。
125
117
  - 子应用构建命令:`npx uni-tools build -p mp-weixin --mode development --b root`
@@ -131,7 +123,6 @@
131
123
  - Manifest 与分包配置生成:读取各子应用 `pages.json` 并渲染至主应用 `subPackages`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:41-69)。
132
124
 
133
125
  ## 常见问题
134
- - 端口被占用(`MFE_UNI_SERVE=true`):HTTP/WS 服务具备幂等复用与端口探测,避免重复 `listen`(packages/uni-tools/src/plugins/modules/mp-weixin/server/http-server.ts)。
135
126
  - 开发态频繁重启/重复打包:写入 `app.json` 与文件拷贝均进行内容比较,仅在变更时写入(`core/app-json.ts`、`utils/copy.ts`)。
136
127
  - CLI 交互报错(inquirer 兼容):已采用动态导入并移动到依赖,`npx` 下可正常使用(packages/uni-tools/src/plugins/modules/mp-weixin/gitlib/index.ts, packages/uni-tools/package.json)。
137
128
 
@@ -141,7 +132,7 @@
141
132
  - 多仓库协作,希望有一套拉取、开发、构建的统一流程。
142
133
 
143
134
  ## 注意
144
- - 开发态会根据 `mode`、`isRoot` 等状态决定是否启动 WS 服务与目录监听。
135
+ - 开发态会根据 `mode`、`isRoot` 等状态决定是否启动通信服务与目录监听。
145
136
  - 请确保各子应用的 `pages.json` 合法且页面路径正确,便于自动合并。
146
137
  - 使用 `--b root` 时需先启动主应用的 HTTP 服务,以便子应用定位 `UNI_OUTPUT_DIR`(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:39-59)。
147
138
 
@@ -151,12 +142,12 @@
151
142
  ```ts
152
143
  import { defineConfig } from 'vite';
153
144
  import uni from '@dcloudio/vite-plugin-uni';
154
- import uniTools from '@dd-code/uni-tools';
145
+ import {chageeUniModulePlugin} from '@dd-code/uni-tools';
155
146
 
156
147
  export default defineConfig({
157
148
  plugins: [
158
149
  uni(),
159
- uniTools({
150
+ chageeUniModulePlugin({
160
151
  exposes: {
161
152
  '.': '@/store/index', // -> @dd-code/runtime
162
153
  './axios': '@/axios/index', // -> @dd-code/runtime/axios
@@ -178,18 +169,13 @@
178
169
  ```
179
170
  MFE_UNI_IS_ROOT=true
180
171
  MFE_UNI_CODE=your-project-code
181
- MFE_UNI_SERVE=true
182
- UNI_PLATFORM=mp-weixin
183
172
  ```
184
173
  - 子应用 `.env.development`:
185
174
  ```
186
- MFE_UNI_IS_ROOT=false
187
175
  MFE_UNI_CODE=your-project-code
188
176
  MFE_APP_CODE=modules/your-app
189
- MFE_UNI_SERVE=true
190
- UNI_PLATFORM=mp-weixin
191
177
  ```
192
- - 开发:主/子应用分别执行 `npx uni-tools serve -p mp-weixin --mode development`,联调时主应用作为 WS 服务端,子应用作为客户端。
178
+ - 开发:主/子应用分别执行 `npx uni-tools serve -p mp-weixin --mode development`,联调时主应用作为服务端,子应用作为客户端。
193
179
  - 子应用直写主应用(不联调):在主应用启动 HTTP 接口后执行 `npx uni-tools build -p mp-weixin --mode development --b root`,产物将按 `appCode` 写入主应用输出目录。
194
180
  - 验证与排错
195
181
  - 构建后检查根目录是否存在 `__mfe_runtime__.js`。
package/dist/cli.js CHANGED
@@ -9,6 +9,7 @@ require('crypto');
9
9
  require('chokidar');
10
10
  var child_process = require('child_process');
11
11
  var inquirer = require('inquirer');
12
+ var yaml = require('js-yaml');
12
13
 
13
14
  /******************************************************************************
14
15
  Copyright (c) Microsoft Corporation.
@@ -65,6 +66,16 @@ function __generator(thisArg, body) {
65
66
  }
66
67
  }
67
68
 
69
+ function __spreadArray(to, from, pack) {
70
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
71
+ if (ar || !(i in from)) {
72
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
73
+ ar[i] = from[i];
74
+ }
75
+ }
76
+ return to.concat(ar || Array.prototype.slice.call(from));
77
+ }
78
+
68
79
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
69
80
  var e = new Error(message);
70
81
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
@@ -100,6 +111,9 @@ function uniReadFile(filaPath) {
100
111
  * @returns {void}
101
112
  */
102
113
  function writeFiles(filaPath, content) {
114
+ if (typeof content === "object" && content) {
115
+ content = JSON.stringify(content, null, 2);
116
+ }
103
117
  // 检查文件路径是否存在
104
118
  var dir = path.dirname(filaPath);
105
119
  if (!fs.existsSync(dir)) {
@@ -132,7 +146,7 @@ var unlinkDeepDirOrFile = function (filePath) {
132
146
  };
133
147
 
134
148
  // 项目 Git 子模块路径
135
- var PROJECT_GIT_PATH = "src/subtree";
149
+ var PROJECT_GIT_PATH = "./sub-repo";
136
150
  // CDN 文件保存路径
137
151
  path.join(process.cwd(), "node_modules/@dd-code/uni-files");
138
152
  path.join(process.cwd(), "node_modules/@dd-code/__main-pwd.txt");
@@ -201,30 +215,6 @@ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0
201
215
  process.env.MFE_TARGET_DIR = (opt === null || opt === void 0 ? void 0 : opt.buildDir) || "";
202
216
  _a = opt || {}, isRoot = _a.isRoot, appCode = _a.appCode;
203
217
  cmd = "".concat(command, " ").concat(!isRoot ? "--subpackage=".concat(appCode) : "");
204
- // if (isRoot) {
205
- // const mfeJson = getMfeJson();
206
- // const apps = mfeJson.apps?.map((i) => i.appCode) || [];
207
- // apps.forEach((appCode) => {
208
- // const appCodeDir = appCode.replace(/\//g, "_");
209
- // // console.log(appCodeDir, 'appCodeDir');
210
- // const childPath = path.join(process.cwd(), PROJECT_GIT_PATH, appCodeDir);
211
- // console.log(existsSync(appCodeDir), childPath, "childPath");
212
- // // if (existsSync(appCodeDir)) {
213
- // // excuteUniCommand(`cd ${childPath} && ${command}`, {
214
- // // appCode,
215
- // // target: appCodeDir,
216
- // // });
217
- // // 开一个子线程启动
218
- // // execSync(cmd, {
219
- // // stdio: "inherit",
220
- // // });
221
- // // }
222
- // });
223
- // // setTimeout(() => {
224
- // // console.log(process.env.UNI_OUTPUT_DIR, "process.env.UNI_OUTPUT_DIR");
225
- // // });
226
- // }
227
- // console.log(cmd, "-------");
228
218
  child_process.execSync(cmd, {
229
219
  stdio: "inherit",
230
220
  });
@@ -236,14 +226,59 @@ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0
236
226
  var inquirerPrompt = ((inquirer === null || inquirer === void 0 ? void 0 : inquirer.default) ||
237
227
  inquirer);
238
228
 
229
+ var parseYaml = function (content) {
230
+ try {
231
+ return yaml.load(content, { json: true });
232
+ }
233
+ catch (e) {
234
+ return '';
235
+ }
236
+ };
237
+ var dumpYaml = function (content) {
238
+ try {
239
+ return yaml.dump(content, {
240
+ // 可选配置:保证中文等 Unicode 字符正常显示(不转义)
241
+ skipInvalid: true,
242
+ encoding: 'utf8',
243
+ // 可选:设置缩进空格数,增强可读性
244
+ indent: 2
245
+ });
246
+ }
247
+ catch (e) {
248
+ return '';
249
+ }
250
+ };
251
+
252
+ var writeChildWorkspace = function (content) {
253
+ var configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
254
+ var fileContent = parseYaml(uniReadFile(configPath));
255
+ if (!fileContent) {
256
+ // fileContent = "packages:\n";
257
+ fileContent = {
258
+ packages: [
259
+ '.',
260
+ ]
261
+ };
262
+ }
263
+ var packages = new Set(__spreadArray(__spreadArray([], fileContent.packages, true), [content + '/*'], false));
264
+ fileContent.packages = Array.from(packages);
265
+ var str = dumpYaml(fileContent);
266
+ writeFiles(configPath, str);
267
+ };
239
268
  var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, function () {
240
- var mfeJson, apps, answers, selectedApps, selectedGitApps;
269
+ var mfeJson, apps, answers, selectedApps, childPath, selectedGitApps;
241
270
  return __generator(this, function (_a) {
242
271
  switch (_a.label) {
243
272
  case 0:
244
273
  mfeJson = getMfeJson();
245
274
  apps = (mfeJson === null || mfeJson === void 0 ? void 0 : mfeJson.apps) || [];
246
275
  return [4 /*yield*/, inquirerPrompt.prompt([
276
+ {
277
+ type: 'input',
278
+ name: "dir",
279
+ message: "请输入子模块路径:",
280
+ default: PROJECT_GIT_PATH,
281
+ },
247
282
  {
248
283
  type: "checkbox",
249
284
  name: "selectedApps",
@@ -257,19 +292,24 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
257
292
  case 1:
258
293
  answers = _a.sent();
259
294
  selectedApps = answers.selectedApps;
295
+ childPath = answers.dir;
260
296
  selectedGitApps = apps.filter(function (app) { return selectedApps.includes(app.appCode) && app.repoUrl; });
261
- console.log("选择的应用仓库:", selectedGitApps);
297
+ // console.log("选择的应用仓库:", selectedGitApps);
262
298
  // 执行拉取操作
263
299
  selectedGitApps.forEach(function (_a) {
264
300
  var appCode = _a.appCode, repoUrl = _a.repoUrl;
265
301
  if (repoUrl) {
266
302
  var fileName = appCode.replace(/\//g, "_");
267
- var dir = PROJECT_GIT_PATH;
303
+ var dir = childPath;
268
304
  unlinkDeepDirOrFile(path.join(dir, fileName));
269
305
  checkAndgenreDir(dir);
270
306
  child_process.execSync("cd ".concat(dir, " && git clone ").concat(repoUrl, " ").concat(fileName), {
271
307
  stdio: "inherit",
272
308
  });
309
+ writeChildWorkspace(dir);
310
+ // execSync(`cd ${dir} && git clone ${repoUrl} ${fileName}`, {
311
+ // stdio: "inherit",
312
+ // });
273
313
  }
274
314
  });
275
315
  return [2 /*return*/];
@@ -278,20 +318,22 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
278
318
  }); };
279
319
 
280
320
  var runParallelAllUni = function (cmd) { return __awaiter(void 0, void 0, void 0, function () {
281
- var configPath;
282
321
  return __generator(this, function (_a) {
283
- configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
284
- writeFiles(configPath, "packages:\n - '*'\n - 'src/subtree/*'");
285
322
  // pnpm -r --parallel --workspace-root --filter=* run dev:mp-weixin --b root
286
323
  child_process.execSync("pnpm -r --parallel --workspace-root --filter=* run ".concat(cmd, " --b root"), {
287
324
  stdio: "inherit",
288
325
  });
289
- // 删除文件
290
- fs.unlinkSync(configPath);
291
326
  return [2 /*return*/];
292
327
  });
293
328
  }); };
294
329
 
330
+ /**
331
+ * CLI 命令
332
+ * - serve:开发态运行,支持 `-p` 平台、`--mode` 模式、`--b` 目标路径
333
+ * - build:生产构建,支持平台/模式;mp-weixin 下根据 isRoot/appCode 进行差异化参数
334
+ * - fetch:交互式拉取子应用仓库(选择 apps 列表并 clone 到本地)
335
+ * - runAll:并行启动所有 mp-weixin 项目
336
+ */
295
337
  var addUniOptions = function (program) {
296
338
  return program
297
339
  .option("-p <platform>", "平台", "h5")
@@ -319,11 +361,11 @@ fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function
319
361
  // pushCdn.option("--mode <mode>", "模式", "dev").action(({ mode }) => {
320
362
  // pushDistToCdn(mode);
321
363
  // });
322
- addUniOptions(runAll).action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
323
- _b.mode; var platform = _b.p; _b.b;
364
+ addUniOptions(runAll).option("--cmd <execCmd>", "启动命令", "dev:mp-weixin").action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
365
+ _b.mode; var platform = _b.p, cmd = _b.cmd;
324
366
  return __generator(this, function (_c) {
325
367
  if (platform === 'mp-weixin') {
326
- runParallelAllUni("dev:mp-weixin");
368
+ runParallelAllUni(cmd);
327
369
  }
328
370
  return [2 /*return*/];
329
371
  });
package/dist/cli.mjs.js CHANGED
@@ -7,6 +7,7 @@ import 'crypto';
7
7
  import 'chokidar';
8
8
  import { execSync } from 'child_process';
9
9
  import inquirer from 'inquirer';
10
+ import yaml from 'js-yaml';
10
11
 
11
12
  /******************************************************************************
12
13
  Copyright (c) Microsoft Corporation.
@@ -63,6 +64,16 @@ function __generator(thisArg, body) {
63
64
  }
64
65
  }
65
66
 
67
+ function __spreadArray(to, from, pack) {
68
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
69
+ if (ar || !(i in from)) {
70
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
71
+ ar[i] = from[i];
72
+ }
73
+ }
74
+ return to.concat(ar || Array.prototype.slice.call(from));
75
+ }
76
+
66
77
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
67
78
  var e = new Error(message);
68
79
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
@@ -98,6 +109,9 @@ function uniReadFile(filaPath) {
98
109
  * @returns {void}
99
110
  */
100
111
  function writeFiles(filaPath, content) {
112
+ if (typeof content === "object" && content) {
113
+ content = JSON.stringify(content, null, 2);
114
+ }
101
115
  // 检查文件路径是否存在
102
116
  var dir = path.dirname(filaPath);
103
117
  if (!fs.existsSync(dir)) {
@@ -130,7 +144,7 @@ var unlinkDeepDirOrFile = function (filePath) {
130
144
  };
131
145
 
132
146
  // 项目 Git 子模块路径
133
- var PROJECT_GIT_PATH = "src/subtree";
147
+ var PROJECT_GIT_PATH = "./sub-repo";
134
148
  // CDN 文件保存路径
135
149
  path.join(process.cwd(), "node_modules/@dd-code/uni-files");
136
150
  path.join(process.cwd(), "node_modules/@dd-code/__main-pwd.txt");
@@ -199,30 +213,6 @@ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0
199
213
  process.env.MFE_TARGET_DIR = (opt === null || opt === void 0 ? void 0 : opt.buildDir) || "";
200
214
  _a = opt || {}, isRoot = _a.isRoot, appCode = _a.appCode;
201
215
  cmd = "".concat(command, " ").concat(!isRoot ? "--subpackage=".concat(appCode) : "");
202
- // if (isRoot) {
203
- // const mfeJson = getMfeJson();
204
- // const apps = mfeJson.apps?.map((i) => i.appCode) || [];
205
- // apps.forEach((appCode) => {
206
- // const appCodeDir = appCode.replace(/\//g, "_");
207
- // // console.log(appCodeDir, 'appCodeDir');
208
- // const childPath = path.join(process.cwd(), PROJECT_GIT_PATH, appCodeDir);
209
- // console.log(existsSync(appCodeDir), childPath, "childPath");
210
- // // if (existsSync(appCodeDir)) {
211
- // // excuteUniCommand(`cd ${childPath} && ${command}`, {
212
- // // appCode,
213
- // // target: appCodeDir,
214
- // // });
215
- // // 开一个子线程启动
216
- // // execSync(cmd, {
217
- // // stdio: "inherit",
218
- // // });
219
- // // }
220
- // });
221
- // // setTimeout(() => {
222
- // // console.log(process.env.UNI_OUTPUT_DIR, "process.env.UNI_OUTPUT_DIR");
223
- // // });
224
- // }
225
- // console.log(cmd, "-------");
226
216
  execSync(cmd, {
227
217
  stdio: "inherit",
228
218
  });
@@ -234,14 +224,59 @@ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0
234
224
  var inquirerPrompt = ((inquirer === null || inquirer === void 0 ? void 0 : inquirer.default) ||
235
225
  inquirer);
236
226
 
227
+ var parseYaml = function (content) {
228
+ try {
229
+ return yaml.load(content, { json: true });
230
+ }
231
+ catch (e) {
232
+ return '';
233
+ }
234
+ };
235
+ var dumpYaml = function (content) {
236
+ try {
237
+ return yaml.dump(content, {
238
+ // 可选配置:保证中文等 Unicode 字符正常显示(不转义)
239
+ skipInvalid: true,
240
+ encoding: 'utf8',
241
+ // 可选:设置缩进空格数,增强可读性
242
+ indent: 2
243
+ });
244
+ }
245
+ catch (e) {
246
+ return '';
247
+ }
248
+ };
249
+
250
+ var writeChildWorkspace = function (content) {
251
+ var configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
252
+ var fileContent = parseYaml(uniReadFile(configPath));
253
+ if (!fileContent) {
254
+ // fileContent = "packages:\n";
255
+ fileContent = {
256
+ packages: [
257
+ '.',
258
+ ]
259
+ };
260
+ }
261
+ var packages = new Set(__spreadArray(__spreadArray([], fileContent.packages, true), [content + '/*'], false));
262
+ fileContent.packages = Array.from(packages);
263
+ var str = dumpYaml(fileContent);
264
+ writeFiles(configPath, str);
265
+ };
237
266
  var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, function () {
238
- var mfeJson, apps, answers, selectedApps, selectedGitApps;
267
+ var mfeJson, apps, answers, selectedApps, childPath, selectedGitApps;
239
268
  return __generator(this, function (_a) {
240
269
  switch (_a.label) {
241
270
  case 0:
242
271
  mfeJson = getMfeJson();
243
272
  apps = (mfeJson === null || mfeJson === void 0 ? void 0 : mfeJson.apps) || [];
244
273
  return [4 /*yield*/, inquirerPrompt.prompt([
274
+ {
275
+ type: 'input',
276
+ name: "dir",
277
+ message: "请输入子模块路径:",
278
+ default: PROJECT_GIT_PATH,
279
+ },
245
280
  {
246
281
  type: "checkbox",
247
282
  name: "selectedApps",
@@ -255,19 +290,24 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
255
290
  case 1:
256
291
  answers = _a.sent();
257
292
  selectedApps = answers.selectedApps;
293
+ childPath = answers.dir;
258
294
  selectedGitApps = apps.filter(function (app) { return selectedApps.includes(app.appCode) && app.repoUrl; });
259
- console.log("选择的应用仓库:", selectedGitApps);
295
+ // console.log("选择的应用仓库:", selectedGitApps);
260
296
  // 执行拉取操作
261
297
  selectedGitApps.forEach(function (_a) {
262
298
  var appCode = _a.appCode, repoUrl = _a.repoUrl;
263
299
  if (repoUrl) {
264
300
  var fileName = appCode.replace(/\//g, "_");
265
- var dir = PROJECT_GIT_PATH;
301
+ var dir = childPath;
266
302
  unlinkDeepDirOrFile(path.join(dir, fileName));
267
303
  checkAndgenreDir(dir);
268
304
  execSync("cd ".concat(dir, " && git clone ").concat(repoUrl, " ").concat(fileName), {
269
305
  stdio: "inherit",
270
306
  });
307
+ writeChildWorkspace(dir);
308
+ // execSync(`cd ${dir} && git clone ${repoUrl} ${fileName}`, {
309
+ // stdio: "inherit",
310
+ // });
271
311
  }
272
312
  });
273
313
  return [2 /*return*/];
@@ -276,20 +316,22 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
276
316
  }); };
277
317
 
278
318
  var runParallelAllUni = function (cmd) { return __awaiter(void 0, void 0, void 0, function () {
279
- var configPath;
280
319
  return __generator(this, function (_a) {
281
- configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
282
- writeFiles(configPath, "packages:\n - '*'\n - 'src/subtree/*'");
283
320
  // pnpm -r --parallel --workspace-root --filter=* run dev:mp-weixin --b root
284
321
  execSync("pnpm -r --parallel --workspace-root --filter=* run ".concat(cmd, " --b root"), {
285
322
  stdio: "inherit",
286
323
  });
287
- // 删除文件
288
- fs.unlinkSync(configPath);
289
324
  return [2 /*return*/];
290
325
  });
291
326
  }); };
292
327
 
328
+ /**
329
+ * CLI 命令
330
+ * - serve:开发态运行,支持 `-p` 平台、`--mode` 模式、`--b` 目标路径
331
+ * - build:生产构建,支持平台/模式;mp-weixin 下根据 isRoot/appCode 进行差异化参数
332
+ * - fetch:交互式拉取子应用仓库(选择 apps 列表并 clone 到本地)
333
+ * - runAll:并行启动所有 mp-weixin 项目
334
+ */
293
335
  var addUniOptions = function (program) {
294
336
  return program
295
337
  .option("-p <platform>", "平台", "h5")
@@ -317,11 +359,11 @@ fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function
317
359
  // pushCdn.option("--mode <mode>", "模式", "dev").action(({ mode }) => {
318
360
  // pushDistToCdn(mode);
319
361
  // });
320
- addUniOptions(runAll).action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
321
- _b.mode; var platform = _b.p; _b.b;
362
+ addUniOptions(runAll).option("--cmd <execCmd>", "启动命令", "dev:mp-weixin").action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
363
+ _b.mode; var platform = _b.p, cmd = _b.cmd;
322
364
  return __generator(this, function (_c) {
323
365
  if (platform === 'mp-weixin') {
324
- runParallelAllUni("dev:mp-weixin");
366
+ runParallelAllUni(cmd);
325
367
  }
326
368
  return [2 /*return*/];
327
369
  });