@dd-code/uni-tools 1.0.9 → 1.0.11
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 +129 -0
- package/dist/cli.js +275 -11
- package/dist/cli.mjs.js +357 -0
- package/dist/index.js +434 -129
- package/dist/index.mjs.js +434 -129
- package/package.json +6 -2
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# @dd-code/uni-tools
|
|
2
|
+
|
|
3
|
+
`@dd-code/uni-tools` 是一个针对 UniApp 的微信小程序(mp-weixin)模块化解决方案与工具集,目标是让「主应用 + 多子应用(分包)」的开发、联调与构建更顺畅、更自动化。
|
|
4
|
+
|
|
5
|
+
## 项目简介
|
|
6
|
+
- 面向微信小程序的模块化架构:主应用负责壳与公共能力,子应用以分包形式集成。
|
|
7
|
+
- 自动管理主/子应用的 `app.json`、`pages.json` 等配置合并,减少人工维护成本。
|
|
8
|
+
- 开发态具备文件监听、自动同步与应用间通信能力,提升联调效率。
|
|
9
|
+
- 提供统一 CLI 命令,简化拉取子应用仓库、开发、构建等流程。
|
|
10
|
+
|
|
11
|
+
## 有哪些命令
|
|
12
|
+
- `npx uni-tools fetch`:交互式拉取已配置的子应用仓库到本地工作空间(packages/uni-tools/src/cli.ts:20)。
|
|
13
|
+
- `npx uni-tools serve -p mp-weixin --mode development`:启动开发态;自动识别主/子应用并启动对应插件逻辑(packages/uni-tools/src/cli.ts:14)。
|
|
14
|
+
- `npx uni-tools build -p mp-weixin --mode production`:执行生产构建,按模块化架构输出主应用及分包(packages/uni-tools/src/cli.ts:52)。
|
|
15
|
+
- 可选参数:
|
|
16
|
+
- `-p <platform>`:平台,默认 `h5`,支持 `mp-weixin`。
|
|
17
|
+
- `--mode <mode>`:模式,默认 `development`。
|
|
18
|
+
- `--b <buildDir>`:指定构建目标输出路径(部分场景有效)。
|
|
19
|
+
|
|
20
|
+
## 是干嘛的 / 实现的效果是什么
|
|
21
|
+
- 统一管理主/子应用:自动收集各子应用的页面配置,合并生成主应用最终 `app.json`。
|
|
22
|
+
- 开发态联动:
|
|
23
|
+
- 子应用构建产物变更后,自动同步到主应用分包目录;
|
|
24
|
+
- 利用 WebSocket 在主子应用之间广播页面/静态资源变更,减少手动刷新与重复构建。
|
|
25
|
+
- 生产构建可控:按环境与模式输出稳定的主应用与分包结构,避免配置漂移与遗漏。
|
|
26
|
+
- 仓库管理简化:通过交互命令拉取/更新子应用仓库,快速搭建本地联调环境。
|
|
27
|
+
|
|
28
|
+
## 大致的实现逻辑
|
|
29
|
+
- 插件入口与平台分发(packages/uni-tools/src/plugins/mp-weixin.ts:8-22)
|
|
30
|
+
- 检测当前平台是否为 `mp-weixin`,是则注册一组 Vite 插件:环境初始化、资源处理、Manifest 管理、主应用逻辑等。
|
|
31
|
+
- Manifest 管理与 `app.json` 合并(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:31-76, 105-119, 261-279)
|
|
32
|
+
- 读取各子应用的 `pages.json`,生成分包结构并合并到主应用的 `app.json`。
|
|
33
|
+
- 在 `closeBundle` 钩子中根据当前模式(开发/构建)与是否主应用,决定是否生成或更新最终的 `app.json`。
|
|
34
|
+
- 开发态联调(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:121-156, 187-245)
|
|
35
|
+
- 主应用启动 WebSocket 服务,子应用作为客户端连接;
|
|
36
|
+
- 监听子应用构建输出目录变更(`DistWatcher`),变更时拷贝至主应用对应分包路径,并通过 WS 通知;
|
|
37
|
+
- 收到子应用变更通知后,主应用实时增量更新 `app.json` 中的对应分包页面配置。
|
|
38
|
+
- 交互式拉取子应用仓库(packages/uni-tools/src/plugins/modules/mp-weixin/gitlib/index.ts)
|
|
39
|
+
- 使用交互框选择需要拉取的应用,自动将其下载/更新至本地指定目录,便于快速联调。
|
|
40
|
+
|
|
41
|
+
## 配置与环境变量
|
|
42
|
+
- `mfe.json`(项目根目录,packages/uni-tools/src/config/const.ts:1)
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"platform": "mp-weixin",
|
|
46
|
+
"apps": [
|
|
47
|
+
{ "appCode": "modules/manage", "repoUrl": "git@xxx:manage.git" },
|
|
48
|
+
{ "appCode": "modules/bwzb", "repoUrl": "git@xxx:bwzb.git" },
|
|
49
|
+
{ "appCode": "login" }
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
- `platform`:目标平台;插件入口会根据此值启用对应模块(packages/uni-tools/src/plugins/mp-weixin.ts:16-20)。
|
|
54
|
+
- `apps`:主应用下要集成的子应用分包列表;主应用读取并参与合并(packages/uni-tools/src/plugins/modules/mp-weixin/manifest-core.ts:75-87)。
|
|
55
|
+
- `.env.*` 中的 `MFE_` 前缀变量(packages/uni-tools/src/utils/utils.ts:93-97)
|
|
56
|
+
- `MFE_UNI_IS_ROOT=true|false`:是否主应用(影响 `appCode` 填充与插件行为)(packages/uni-tools/src/config/config.ts:130-141)。
|
|
57
|
+
- `MFE_UNI_CODE=your-project-code`:项目代码,用于 CDN 路径拼接与 Manifest 标识(packages/uni-tools/src/config/config.ts:90-105, 116-141)。
|
|
58
|
+
- `MFE_APP_CODE=__MFE_APP_ROOT__|modules/manage`:当前应用的 `appCode`。
|
|
59
|
+
- `MFE_UNI_SERVE=true|false`:开发态是否启用联调(WS/监听)(packages/uni-tools/src/plugins/modules/mp-weixin/manifest-core.ts:75-87)。
|
|
60
|
+
- `MFE_CDN_HOST=https://static.example.com`:CDN 域名,供 Manifest 资源定位(packages/uni-tools/src/config/config.ts:116-141)。
|
|
61
|
+
- `UNI_PLATFORM=mp-weixin`:平台选择(packages/uni-tools/src/config/config.ts:103-105)。
|
|
62
|
+
- 运行时自动设置的变量(无需手动维护)
|
|
63
|
+
- `MFE_SOURCE_OUTPUT_DIR`:当前应用构建输出目录(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:64)。
|
|
64
|
+
- `MFE_ROOT_OUTPUT_DIR`:主应用根输出目录(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:65-69)。
|
|
65
|
+
- `UNI_OUTPUT_DIR`:根输出目录,生成/读取 `app.json`(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:72-76)。
|
|
66
|
+
- `MFE_INNER_BUILD=true`:子应用处于“内部构建”模式(packages/uni-tools/src/utils/utils.ts:204)。
|
|
67
|
+
- `MFE_TARGET_DIR=root`:由 `--b root` 触发,用于判断内部构建(packages/uni-tools/src/utils/utils.ts:202)。
|
|
68
|
+
|
|
69
|
+
## 快速开始
|
|
70
|
+
1. 安装依赖:
|
|
71
|
+
- 使用你的包管理器安装 `@dd-code/uni-tools`。
|
|
72
|
+
2. 在 `vite.config.ts` 中启用插件:
|
|
73
|
+
```ts
|
|
74
|
+
import { defineConfig } from 'vite';
|
|
75
|
+
import uni from '@dcloudio/vite-plugin-uni';
|
|
76
|
+
import uniTools from '@dd-code/uni-tools';
|
|
77
|
+
|
|
78
|
+
export default defineConfig({
|
|
79
|
+
plugins: [
|
|
80
|
+
uni(),
|
|
81
|
+
uniTools()
|
|
82
|
+
],
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
3. 拉取子应用仓库:`npx uni-tools fetch`
|
|
86
|
+
4. 启动开发:`npx uni-tools serve -p mp-weixin --mode development`
|
|
87
|
+
5. 构建生产:`npx uni-tools build -p mp-weixin --mode production`
|
|
88
|
+
|
|
89
|
+
## 开发流程(主/子应用)
|
|
90
|
+
- 主应用开发
|
|
91
|
+
- `.env.development` 示例:
|
|
92
|
+
```
|
|
93
|
+
MFE_UNI_IS_ROOT=true
|
|
94
|
+
MFE_UNI_CODE=your-project-code
|
|
95
|
+
MFE_UNI_SERVE=true
|
|
96
|
+
UNI_PLATFORM=mp-weixin
|
|
97
|
+
```
|
|
98
|
+
- 启动:`npx uni-tools serve -p mp-weixin --mode development`
|
|
99
|
+
- 行为:开启 WS 服务与合并逻辑,动态维护最终 `app.json`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:121-143, 261-279)。
|
|
100
|
+
- 子应用开发(联调)
|
|
101
|
+
- `.env.development` 示例:
|
|
102
|
+
```
|
|
103
|
+
MFE_UNI_IS_ROOT=false
|
|
104
|
+
MFE_UNI_CODE=your-project-code
|
|
105
|
+
MFE_APP_CODE=modules/manage
|
|
106
|
+
MFE_UNI_SERVE=true
|
|
107
|
+
UNI_PLATFORM=mp-weixin
|
|
108
|
+
```
|
|
109
|
+
- 启动:`npx uni-tools serve -p mp-weixin --mode development`
|
|
110
|
+
- 行为:作为 WS 客户端与主应用通信,监听自身输出变更并同步到主应用分包路径(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:145-156, 78-103)。
|
|
111
|
+
- 子应用构建(不联调,直接写入主应用)
|
|
112
|
+
- 前置:主应用开发态启动但 `MFE_UNI_SERVE=false`,提供 HTTP 接口返回主应用 `UNI_OUTPUT_DIR`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:190-200)。
|
|
113
|
+
- 子应用构建命令:`npx uni-tools build -p mp-weixin --mode development --b root`
|
|
114
|
+
- 行为:自动把子应用 `outDir` 改写到主应用 `UNI_OUTPUT_DIR/<appCode>`,并设置内部构建环境(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:39-59, 64-76)。
|
|
115
|
+
|
|
116
|
+
## 目录与约定
|
|
117
|
+
- 主应用:负责最终 `app.json` 合并输出。
|
|
118
|
+
- 子应用:分包形式接入,拥有独立 `pages.json` 与资源;构建输出由插件监听并同步。
|
|
119
|
+
- Manifest 与分包配置生成:读取各子应用 `pages.json` 并渲染至主应用 `subPackages`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:41-69)。
|
|
120
|
+
|
|
121
|
+
## 适用场景
|
|
122
|
+
- 同一个小程序中承载多个业务模块/团队的功能分包,需要高效联调与统一发布。
|
|
123
|
+
- 希望减少主/子应用配置的重复劳动与易错区域。
|
|
124
|
+
- 多仓库协作,希望有一套拉取、开发、构建的统一流程。
|
|
125
|
+
|
|
126
|
+
## 注意
|
|
127
|
+
- 开发态会根据 `mode`、`isRoot` 等状态决定是否启动 WS 服务与目录监听。
|
|
128
|
+
- 请确保各子应用的 `pages.json` 合法且页面路径正确,便于自动合并。
|
|
129
|
+
- 使用 `--b root` 时需先启动主应用的 HTTP 服务,以便子应用定位 `UNI_OUTPUT_DIR`(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:39-59)。
|
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,144 @@ 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
|
+
/**
|
|
95
|
+
* 写入文件内容
|
|
96
|
+
* 自动创建目录结构,支持对象和字符串内容
|
|
97
|
+
* @param {string} filaPath - 文件路径
|
|
98
|
+
* @param {string|Object} content - 文件内容
|
|
99
|
+
* @returns {void}
|
|
100
|
+
*/
|
|
101
|
+
function writeFiles(filaPath, content) {
|
|
102
|
+
// 检查文件路径是否存在
|
|
103
|
+
var dir = path.dirname(filaPath);
|
|
104
|
+
if (!fs.existsSync(dir)) {
|
|
105
|
+
// 如果路径不存在,创建路径
|
|
106
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
107
|
+
}
|
|
108
|
+
return fs.writeFileSync(filaPath, content, "utf-8");
|
|
109
|
+
}
|
|
110
|
+
var checkAndgenreDir = function (dir) {
|
|
111
|
+
if (!fs.existsSync(dir)) {
|
|
112
|
+
// 如果路径不存在,创建路径
|
|
113
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
114
|
+
}
|
|
115
|
+
};
|
|
32
116
|
var loadViteConfig = function (mode) {
|
|
33
117
|
var loadEnv = require("vite").loadEnv;
|
|
34
118
|
var ROOT = process.cwd();
|
|
35
119
|
return loadEnv(mode, ROOT, "MFE_");
|
|
36
120
|
};
|
|
37
121
|
|
|
122
|
+
// 项目 Git 子模块路径
|
|
123
|
+
var PROJECT_GIT_PATH = "src/subtree";
|
|
38
124
|
// CDN 文件保存路径
|
|
39
125
|
path.join(process.cwd(), "node_modules/@dd-code/uni-files");
|
|
126
|
+
path.join(process.cwd(), "node_modules/@dd-code/__main-pwd.txt");
|
|
127
|
+
path.join(process.cwd(), "node_modules/@dd-code/manifest-list.json");
|
|
40
128
|
path.join(process.cwd(), "node_modules/@dd-code/current-files");
|
|
41
|
-
|
|
129
|
+
// 发布目录路径
|
|
130
|
+
path.join(process.cwd(), "dist/publish");
|
|
131
|
+
var ROOT_APP_CODE = "__MFE_APP_ROOT__";
|
|
132
|
+
var EBuildMode;
|
|
133
|
+
(function (EBuildMode) {
|
|
134
|
+
EBuildMode["BUILD"] = "build";
|
|
135
|
+
EBuildMode["SERVE"] = "serve";
|
|
136
|
+
})(EBuildMode || (EBuildMode = {}));
|
|
137
|
+
/**
|
|
138
|
+
* 获取微前端配置 JSON
|
|
139
|
+
* @description 读取并解析 mfe.json 配置文件,同时从环境变量中获取相关配置
|
|
140
|
+
* @returns {Object} 包含微前端配置的对象
|
|
141
|
+
* @returns {boolean} returns.isRoot - 是否为根应用
|
|
142
|
+
* @returns {string} returns.code - 项目代码
|
|
143
|
+
* @returns {string} returns.appCode - 应用代码
|
|
144
|
+
* @example
|
|
145
|
+
* // 返回: { isRoot: true, code: 'myapp', appCode: 'main', ... }
|
|
146
|
+
*/
|
|
147
|
+
var getMfeJson = function () {
|
|
148
|
+
// 读取 mfe.json 配置文件
|
|
149
|
+
var jsonPath = path.resolve(process.cwd(), MFE_NAME);
|
|
150
|
+
var json = uniReadFile(jsonPath) || {};
|
|
151
|
+
// const root = process.cwd();
|
|
152
|
+
// const { loadEnv } = require("vite");
|
|
153
|
+
// const viteEnv = loadEnv(mode || "dev", root, "");
|
|
154
|
+
// const envObj = {
|
|
155
|
+
// ...viteEnv,
|
|
156
|
+
// platform: process.env.UNI_PLATFORM || "h5",
|
|
157
|
+
// };
|
|
158
|
+
// console.log("-----", process.env, mode, "viteEnv");
|
|
159
|
+
// // 从环境变量中获取配置
|
|
160
|
+
// json.isRoot = envObj.UNI_IS_ROOT;
|
|
161
|
+
// json.code = envObj.UNI_CODE;
|
|
162
|
+
// json.appCode = envObj.MFE_APP_CODE;
|
|
163
|
+
json.platform = process.env.UNI_PLATFORM || "h5";
|
|
164
|
+
// json.mode = envObj.MODE || "dev";
|
|
165
|
+
return json;
|
|
166
|
+
};
|
|
42
167
|
var formatCliCommandConfig = function (mode) {
|
|
43
168
|
var viteEnv = loadViteConfig(mode || "dev");
|
|
44
169
|
var isRoot = viteEnv.MFE_UNI_IS_ROOT;
|
|
@@ -49,6 +174,7 @@ var formatCliCommandConfig = function (mode) {
|
|
|
49
174
|
code: viteEnv.MFE_UNI_CODE,
|
|
50
175
|
mode: mode,
|
|
51
176
|
cdn: viteEnv.MFE_CDN_HOST,
|
|
177
|
+
serve: viteEnv.MFE_UNI_SERVE, // 是否开启 serve 功能
|
|
52
178
|
};
|
|
53
179
|
};
|
|
54
180
|
var E_WS_TYPE;
|
|
@@ -57,39 +183,177 @@ var E_WS_TYPE;
|
|
|
57
183
|
E_WS_TYPE["CHANGE"] = "change_files";
|
|
58
184
|
})(E_WS_TYPE || (E_WS_TYPE = {}));
|
|
59
185
|
|
|
60
|
-
var excuteUniCommand = function (command,
|
|
61
|
-
var
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
186
|
+
var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0, void 0, function () {
|
|
187
|
+
var _a, isRoot, appCode, cmd;
|
|
188
|
+
return __generator(this, function (_b) {
|
|
189
|
+
process.env.MFE_TARGET_DIR = (opt === null || opt === void 0 ? void 0 : opt.buildDir) || "";
|
|
190
|
+
_a = opt || {}, isRoot = _a.isRoot, appCode = _a.appCode;
|
|
191
|
+
cmd = "".concat(command, " ").concat(!isRoot ? "--subpackage=".concat(appCode) : "");
|
|
192
|
+
// if (isRoot) {
|
|
193
|
+
// const mfeJson = getMfeJson();
|
|
194
|
+
// const apps = mfeJson.apps?.map((i) => i.appCode) || [];
|
|
195
|
+
// apps.forEach((appCode) => {
|
|
196
|
+
// const appCodeDir = appCode.replace(/\//g, "_");
|
|
197
|
+
// // console.log(appCodeDir, 'appCodeDir');
|
|
198
|
+
// const childPath = path.join(process.cwd(), PROJECT_GIT_PATH, appCodeDir);
|
|
199
|
+
// console.log(existsSync(appCodeDir), childPath, "childPath");
|
|
200
|
+
// // if (existsSync(appCodeDir)) {
|
|
201
|
+
// // excuteUniCommand(`cd ${childPath} && ${command}`, {
|
|
202
|
+
// // appCode,
|
|
203
|
+
// // target: appCodeDir,
|
|
204
|
+
// // });
|
|
205
|
+
// // 开一个子线程启动
|
|
206
|
+
// // execSync(cmd, {
|
|
207
|
+
// // stdio: "inherit",
|
|
208
|
+
// // });
|
|
209
|
+
// // }
|
|
210
|
+
// });
|
|
211
|
+
// // setTimeout(() => {
|
|
212
|
+
// // console.log(process.env.UNI_OUTPUT_DIR, "process.env.UNI_OUTPUT_DIR");
|
|
213
|
+
// // });
|
|
214
|
+
// }
|
|
215
|
+
// console.log(cmd, "-------");
|
|
216
|
+
child_process.execSync(cmd, {
|
|
217
|
+
stdio: "inherit",
|
|
218
|
+
});
|
|
219
|
+
return [2 /*return*/];
|
|
65
220
|
});
|
|
66
|
-
};
|
|
221
|
+
}); };
|
|
222
|
+
|
|
223
|
+
//@ts-ignore
|
|
224
|
+
var inquirerPrompt = ((inquirer === null || inquirer === void 0 ? void 0 : inquirer.default) ||
|
|
225
|
+
inquirer);
|
|
226
|
+
|
|
227
|
+
var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
228
|
+
var mfeJson, apps, answers, selectedApps, selectedGitApps;
|
|
229
|
+
return __generator(this, function (_a) {
|
|
230
|
+
switch (_a.label) {
|
|
231
|
+
case 0:
|
|
232
|
+
mfeJson = getMfeJson();
|
|
233
|
+
apps = (mfeJson === null || mfeJson === void 0 ? void 0 : mfeJson.apps) || [];
|
|
234
|
+
return [4 /*yield*/, inquirerPrompt.prompt([
|
|
235
|
+
{
|
|
236
|
+
type: "checkbox",
|
|
237
|
+
name: "selectedApps",
|
|
238
|
+
message: "请选择要拉取的应用仓库:",
|
|
239
|
+
choices: apps.map(function (app) { return ({
|
|
240
|
+
name: app.appCode,
|
|
241
|
+
value: app.appCode,
|
|
242
|
+
}); }),
|
|
243
|
+
},
|
|
244
|
+
])];
|
|
245
|
+
case 1:
|
|
246
|
+
answers = _a.sent();
|
|
247
|
+
selectedApps = answers.selectedApps;
|
|
248
|
+
selectedGitApps = apps.filter(function (app) { return selectedApps.includes(app.appCode) && app.repoUrl; });
|
|
249
|
+
console.log("选择的应用仓库:", selectedGitApps);
|
|
250
|
+
// 执行拉取操作
|
|
251
|
+
selectedGitApps.forEach(function (_a) {
|
|
252
|
+
var appCode = _a.appCode, repoUrl = _a.repoUrl;
|
|
253
|
+
if (repoUrl) {
|
|
254
|
+
var fileName = appCode.replace(/\//g, "_");
|
|
255
|
+
var dir = PROJECT_GIT_PATH;
|
|
256
|
+
checkAndgenreDir(dir);
|
|
257
|
+
child_process.execSync("cd ".concat(dir, " && git clone ").concat(repoUrl, " ").concat(fileName), {
|
|
258
|
+
stdio: "inherit",
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
return [2 /*return*/];
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
}); };
|
|
266
|
+
|
|
267
|
+
var runParallelAllUni = function (cmd) { return __awaiter(void 0, void 0, void 0, function () {
|
|
268
|
+
var configPath;
|
|
269
|
+
return __generator(this, function (_a) {
|
|
270
|
+
configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
|
|
271
|
+
writeFiles(configPath, "packages:\n - '*'\n - 'src/subtree/*'");
|
|
272
|
+
// pnpm -r --parallel --workspace-root --filter=* run dev:mp-weixin --b root
|
|
273
|
+
child_process.execSync("pnpm -r --parallel --workspace-root --filter=* run ".concat(cmd, " --b root"), {
|
|
274
|
+
stdio: "inherit",
|
|
275
|
+
});
|
|
276
|
+
// 删除文件
|
|
277
|
+
fs.unlinkSync(configPath);
|
|
278
|
+
return [2 /*return*/];
|
|
279
|
+
});
|
|
280
|
+
}); };
|
|
67
281
|
|
|
68
282
|
var addUniOptions = function (program) {
|
|
69
283
|
return program
|
|
70
284
|
.option("-p <platform>", "平台", "h5")
|
|
71
|
-
.option("--mode <mode>", "模式", "development")
|
|
285
|
+
.option("--mode <mode>", "模式", "development")
|
|
286
|
+
.option("--b <buildDir>", "目标路径", "");
|
|
72
287
|
};
|
|
73
288
|
var dev = shared.program
|
|
74
289
|
.name("uni-tools")
|
|
75
290
|
.command("serve")
|
|
76
291
|
.description("uni 工具方法");
|
|
77
292
|
var build = shared.program.command("build").description("构建 uni 项目");
|
|
293
|
+
// const pushCdn = program.command("push-cdn").description("推送 uni 项目到 cdn");
|
|
294
|
+
var fetchGit = shared.program.command("fetch").description("拉取 uni 项目到本地");
|
|
295
|
+
var runAll = shared.program.command("runAll").description("运行所有 uni 项目");
|
|
296
|
+
fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
297
|
+
return __generator(this, function (_a) {
|
|
298
|
+
switch (_a.label) {
|
|
299
|
+
case 0: return [4 /*yield*/, fetchAppsRepo()];
|
|
300
|
+
case 1:
|
|
301
|
+
_a.sent();
|
|
302
|
+
return [2 /*return*/];
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
}); });
|
|
306
|
+
// pushCdn.option("--mode <mode>", "模式", "dev").action(({ mode }) => {
|
|
307
|
+
// pushDistToCdn(mode);
|
|
308
|
+
// });
|
|
309
|
+
addUniOptions(runAll).action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
310
|
+
_b.mode; var platform = _b.p; _b.b;
|
|
311
|
+
return __generator(this, function (_c) {
|
|
312
|
+
if (platform === 'mp-weixin') {
|
|
313
|
+
runParallelAllUni("dev:mp-weixin");
|
|
314
|
+
}
|
|
315
|
+
return [2 /*return*/];
|
|
316
|
+
});
|
|
317
|
+
}); });
|
|
78
318
|
addUniOptions(dev).action(function (_a) {
|
|
79
|
-
var mode = _a.mode, platform = _a.p;
|
|
319
|
+
var mode = _a.mode, platform = _a.p, b = _a.b;
|
|
320
|
+
process.env.MFE_BUILD_MODE = EBuildMode.SERVE;
|
|
80
321
|
var _b = formatCliCommandConfig(mode), isRoot = _b.isRoot, appCode = _b.appCode;
|
|
81
322
|
switch (platform) {
|
|
82
323
|
case "h5":
|
|
324
|
+
excuteUniCommand("uni -p ".concat(platform, " --mode ").concat(mode));
|
|
83
325
|
break;
|
|
84
326
|
case "mp-weixin":
|
|
85
327
|
excuteUniCommand("uni -p ".concat(platform, " --mode ").concat(mode), {
|
|
86
328
|
isRoot: isRoot,
|
|
87
329
|
appCode: appCode,
|
|
330
|
+
buildDir: b,
|
|
88
331
|
});
|
|
89
332
|
break;
|
|
333
|
+
default:
|
|
334
|
+
excuteUniCommand("uni -p ".concat(platform, " --mode ").concat(mode));
|
|
335
|
+
break;
|
|
90
336
|
}
|
|
91
337
|
// debugger;
|
|
92
338
|
// console.log(JSON.stringify({ mode, platform, mfeJson }), "-111-------------");
|
|
93
339
|
});
|
|
94
|
-
addUniOptions(build).action(function (
|
|
340
|
+
addUniOptions(build).action(function (_a) {
|
|
341
|
+
var mode = _a.mode, platform = _a.p;
|
|
342
|
+
process.env.MFE_BUILD_MODE = EBuildMode.BUILD;
|
|
343
|
+
var _b = formatCliCommandConfig(mode), isRoot = _b.isRoot, appCode = _b.appCode;
|
|
344
|
+
switch (platform) {
|
|
345
|
+
case "h5":
|
|
346
|
+
excuteUniCommand("uni build -p ".concat(platform, " --mode ").concat(mode));
|
|
347
|
+
break;
|
|
348
|
+
case "mp-weixin":
|
|
349
|
+
excuteUniCommand("uni build -p ".concat(platform, " --mode ").concat(mode), {
|
|
350
|
+
isRoot: isRoot,
|
|
351
|
+
appCode: appCode,
|
|
352
|
+
});
|
|
353
|
+
break;
|
|
354
|
+
default:
|
|
355
|
+
excuteUniCommand("uni build -p ".concat(platform, " --mode ").concat(mode));
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
});
|
|
95
359
|
shared.program.parseAsync(process.argv);
|