Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
monorepo
/
venus
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
1
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 2576916e
authored
Nov 21, 2025
by
Lin
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat:子应用模版构建流程优化(交互式)
1 parent
52fe1c52
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
467 additions
and
45 deletions
apps/frontend-micro/venus-base/src/layouts/BasicLayout.vue
docs/COMMUNICATION_REFACTOR.md
package.json
packages/cli/package.json
packages/cli/src/index.ts
pnpm-lock.yaml
scripts/turbo-run/src/index.ts
apps/frontend-micro/venus-base/src/layouts/BasicLayout.vue
View file @
2576916
<
script
setup
lang=
"ts"
>
<
script
setup
lang=
"ts"
>
import
type
{
BreadcrumbItem
,
MenuItem
}
from
'@/types/menu'
;
import
type
{
BreadcrumbItem
,
MenuItem
}
from
'@/types/menu'
;
import
{
computed
,
ref
,
watch
}
from
'vue'
;
import
{
computed
,
ref
,
watch
}
from
'vue'
;
import
{
useRoute
,
useRouter
}
from
'vue-router'
;
import
{
useRoute
,
useRouter
}
from
'vue-router'
;
import
NotificationCenter
from
'@/components/NotificationCenter.vue'
;
import
NotificationCenter
from
'@/components/NotificationCenter.vue'
;
import
SidebarMenuItem
from
'@/components/SidebarMenuItem.vue'
;
import
SidebarMenuItem
from
'@/components/SidebarMenuItem.vue'
;
import
UserInfo
from
'@/components/UserInfo.vue'
;
import
UserInfo
from
'@/components/UserInfo.vue'
;
...
...
docs/COMMUNICATION_REFACTOR.md
0 → 100644
View file @
2576916
# 子应用通信模块重构说明
## 重构目标
将子应用的
`communication.ts`
重构为轻量级版本,直接使用基座通过 props 下发的通信管理器实例,避免维护本地实现。
## 主要变化
### 1. **communication.ts 重构**
#### 之前的实现
-
包含完整的本地通信管理器实现(
`LocalSubAppCommunicationManager`
)
-
包含降级方案逻辑
-
代码量约 287 行,逻辑复杂
#### 现在的实现
-
仅保留类型定义和接口(与基座保持一致)
-
移除所有本地实现逻辑
-
通过全局变量存储基座传递的实例
-
提供简洁的工具函数封装
-
代码量约 200 行,逻辑清晰
**核心代码结构:**
```
typescript
// 1. 类型定义(与基座保持一致)
export
interface
GlobalState
{
...
}
export
interface
Notification
{
...
}
export
interface
SubAppCommunicationManager
{
...
}
// 2. 全局实例存储
let
communicationManagerInstance
:
SubAppCommunicationManager
|
null
=
null
;
// 3. 实例管理函数
export
function
setCommunicationManager
(
manager
)
{
...
}
export
function
getCommunicationManager
()
{
...
}
// 4. 便捷工具函数
export
function
useGlobalState
(
callback
)
{
...
}
export
function
notify
(
type
,
title
,
message
)
{
...
}
export
const
subAppCommunication
=
new
Proxy
(...)
// 兼容旧代码
```
### 2. **main.ts 简化**
#### 之前的逻辑
```
typescript
// 复杂的判断逻辑,包含降级方案
if
(
props
.
communicationManager
)
{
setCommunicationManager
(
props
.
communicationManager
);
props
.
communicationManager
.
init
(
props
);
}
else
if
(
props
.
onGlobalStateChange
)
{
// 降级方案
subAppCommunication
.
init
(
props
);
}
```
#### 现在的逻辑
```
typescript
// 简洁直接,只使用基座传递的实例
if
(
props
.
communicationManager
)
{
setCommunicationManager
(
props
.
communicationManager
);
props
.
communicationManager
.
init
(
props
);
app
.
config
.
globalProperties
.
$communication
=
props
.
communicationManager
;
}
else
{
console
.
error
(
'[子应用] 基座未传递通信管理器'
);
}
```
### 3. **类型安全增强**
-
所有类型定义在子应用中明确声明,避免跨项目导入问题
-
与基座类型定义保持完全一致
-
保持完整的 TypeScript 类型检查
## 架构流程
```
┌─────────────────────────────────────────────────────────┐
│ 微前端基座 │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ CommunicationManager (基座通信管理器) │ │
│ │ - 管理全局状态 │ │
│ │ - 监听状态变化 │ │
│ │ - 广播通知 │ │
│ └──────────────────┬───────────────────────────────┘ │
│ │ │
│ ┌──────────────────▼───────────────────────────────┐ │
│ │ SubAppCommunicationAdapter (子应用适配器) │ │
│ │ - 适配基座管理器到子应用接口 │ │
│ │ - 处理状态同步 │ │
│ │ - 实现 SubAppCommunicationManager 接口 │ │
│ └──────────────────┬───────────────────────────────┘ │
│ │ │
└─────────────────────┼───────────────────────────────────┘
│ props.communicationManager
│ (通过 qiankun 传递)
┌─────────────────────▼───────────────────────────────────┐
│ Vue3 子应用 │
│ │
│ ┌──────────────────────────────────────────────────┐ │
│ │ main.ts │ │
│ │ - 接收 props.communicationManager │ │
│ │ - 调用 setCommunicationManager() │ │
│ │ - 初始化实例 │ │
│ └──────────────────┬───────────────────────────────┘ │
│ │ │
│ ┌──────────────────▼───────────────────────────────┐ │
│ │ communication.ts (轻量级) │ │
│ │ - 存储实例引用 │ │
│ │ - 提供工具函数 │ │
│ │ - 导出类型定义 │ │
│ └──────────────────┬───────────────────────────────┘ │
│ │ │
│ ┌──────────────────▼───────────────────────────────┐ │
│ │ 组件使用 │ │
│ │ - useGlobalState(callback) │ │
│ │ - notify(type, title, message) │ │
│ │ - subAppCommunication.xxx() │ │
│ └──────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────┘
```
## 优势
### 1. **代码简化**
-
移除 287 行的复杂实现,保留核心功能
-
不再需要维护本地降级方案
-
代码更易理解和维护
### 2. **统一管理**
-
通信逻辑完全由基座控制
-
确保所有子应用使用相同的通信机制
-
便于统一升级和修改
### 3. **类型安全**
-
类型定义在子应用中明确声明
-
避免跨项目导入的复杂性
-
保持完整的 TypeScript 支持
### 4. **向后兼容**
-
保留
`useGlobalState`
、
`notify`
、
`subAppCommunication`
等 API
-
组件代码无需修改
-
平滑迁移
## 使用示例
### 在组件中监听全局状态
```
typescript
import
{
useGlobalState
}
from
'@/utils/communication'
;
import
type
{
GlobalState
}
from
'@/utils/communication'
;
// 在 setup 中使用
useGlobalState
((
state
:
GlobalState
,
prev
:
GlobalState
)
=>
{
console
.
log
(
'状态更新:'
,
state
);
// 处理状态变化
});
```
### 发送通知到基座
```
typescript
import
{
notify
}
from
'@/utils/communication'
;
// 发送成功通知
notify
(
'success'
,
'操作成功'
,
'数据已保存'
);
// 发送错误通知
notify
(
'error'
,
'操作失败'
,
'网络连接错误'
);
```
### 直接使用通信管理器
```
typescript
import
{
subAppCommunication
}
from
'@/utils/communication'
;
// 获取当前状态
const
state
=
subAppCommunication
.
getCurrentState
();
// 设置全局状态
subAppCommunication
.
setGlobalState
({
theme
:
{
mode
:
'dark'
,
primaryColor
:
'#1890ff'
}
});
```
## 注意事项
### 1. **基座依赖**
子应用现在完全依赖基座传递通信管理器实例,如果基座未传递:
-
会在控制台输出错误日志
-
通信功能不可用
-
不会影响应用其他功能
### 2. **类型同步**
当基座的类型定义发生变化时,需要同步更新子应用的类型定义,保持一致性。
### 3. **独立运行**
在独立运行模式下(不通过基座加载),通信功能不可用,但不会影响应用正常运行。
## 后续优化建议
### 1. **类型共享**
可以考虑将类型定义提取到独立的 npm 包或 monorepo 共享包中,避免重复定义。
### 2. **TypeScript Path Mapping**
配置 TypeScript 路径映射,直接从基座导入类型:
```
json
{
"compilerOptions"
:
{
"paths"
:
{
"@base/*"
:
[
"../venus-base/src/*"
]
}
}
}
```
### 3. **运行时类型检查**
添加运行时类型检查,确保基座传递的实例符合预期接口。
## 总结
此次重构将子应用的通信模块从"自主实现+降级方案"改为"完全依赖基座",实现了:
-
✅ 代码大幅简化
-
✅ 职责更加清晰
-
✅ 维护成本降低
-
✅ 保持向后兼容
-
✅ 类型安全保证
这是微前端架构中"基座控制,子应用依赖"模式的最佳实践。
package.json
View file @
2576916
...
@@ -25,29 +25,18 @@
...
@@ -25,29 +25,18 @@
"build:filter"
:
"node scripts/build/buildToRoot.mjs"
,
"build:filter"
:
"node scripts/build/buildToRoot.mjs"
,
"build:docs"
:
"pnpm run build --filter=@venus/docs"
,
"build:docs"
:
"pnpm run build --filter=@venus/docs"
,
"changeset"
:
"pnpm exec changeset"
,
"changeset"
:
"pnpm exec changeset"
,
"check"
:
"pnpm run check:circular && pnpm run check:dep && pnpm run check:type && pnpm check:cspell"
,
"check:circular"
:
"vsh check-circular"
,
"check:cspell"
:
"cspell lint **/*.ts **/README.md .changeset/*.md --no-progress"
,
"check:dep"
:
"vsh check-dep"
,
"check:type"
:
"turbo run typecheck"
,
"clean"
:
"node ./scripts/clean.mjs"
,
"clean"
:
"node ./scripts/clean.mjs"
,
"commit"
:
"powershell -ExecutionPolicy Bypass -File ./scripts/commit.ps1"
,
"format"
:
"vsh lint --format"
,
"lint"
:
"vsh lint"
,
"postinstall"
:
"pnpm -r run stub --if-present"
,
"postinstall"
:
"pnpm -r run stub --if-present"
,
"prepare"
:
"husky"
,
"prepare"
:
"husky"
,
"preinstall"
:
"npx only-allow pnpm"
,
"preinstall"
:
"npx only-allow pnpm"
,
"preview"
:
"turbo-run preview"
,
"publint"
:
"vsh publint"
,
"reinstall"
:
"pnpm clean --del-lock && pnpm install"
,
"reinstall"
:
"pnpm clean --del-lock && pnpm install"
,
"test:unit"
:
"vitest run --dom"
,
"test:e2e"
:
"turbo run test:e2e"
,
"update:deps"
:
"npx taze -r -w"
,
"update:deps"
:
"npx taze -r -w"
,
"version"
:
"pnpm exec changeset version && pnpm install --no-frozen-lockfile"
,
"version"
:
"pnpm exec changeset version && pnpm install --no-frozen-lockfile"
,
"catalog"
:
"pnpx codemod pnpm/catalog"
,
"catalog"
:
"pnpx codemod pnpm/catalog"
,
"venus"
:
"node packages/cli/dist/index.cjs.js"
,
"venus"
:
"node packages/cli/dist/index.cjs.js"
,
"venus:build"
:
"node scripts/build/buildSingle.js cli"
,
"venus:build"
:
"node scripts/build/buildSingle.js cli"
,
"venus:create"
:
"pnpm venus create"
,
"venus:create"
:
"pnpm venus create"
,
"venus:create:interactive"
:
"pnpm venus create"
,
"venus:ls"
:
"pnpm venus ls-templates"
,
"venus:ls"
:
"pnpm venus ls-templates"
,
"command:test"
:
"node scripts/build/commandTest.mjs"
"command:test"
:
"node scripts/build/commandTest.mjs"
},
},
...
...
packages/cli/package.json
View file @
2576916
...
@@ -35,5 +35,8 @@
...
@@ -35,5 +35,8 @@
},
},
"devDependencies"
:
{
"devDependencies"
:
{
"typescript"
:
"*"
"typescript"
:
"*"
},
"dependencies"
:
{
"@clack/prompts"
:
"catalog:"
}
}
}
}
packages/cli/src/index.ts
View file @
2576916
...
@@ -3,6 +3,7 @@ import fs from 'node:fs';
...
@@ -3,6 +3,7 @@ import fs from 'node:fs';
import
fsp
from
'node:fs/promises'
;
import
fsp
from
'node:fs/promises'
;
import
path
from
'node:path'
;
import
path
from
'node:path'
;
import
{
fileURLToPath
}
from
'node:url'
;
import
{
fileURLToPath
}
from
'node:url'
;
import
{
cancel
,
confirm
,
isCancel
,
select
,
text
}
from
'@clack/prompts'
;
export
const
version
=
'0.0.1'
;
export
const
version
=
'0.0.1'
;
type
Flags
=
Record
<
string
,
boolean
|
string
>
;
type
Flags
=
Record
<
string
,
boolean
|
string
>
;
type
JsonPrimitive
=
boolean
|
null
|
number
|
string
;
type
JsonPrimitive
=
boolean
|
null
|
number
|
string
;
...
@@ -11,11 +12,13 @@ function parseArgs(argv: string[]): Flags {
...
@@ -11,11 +12,13 @@ function parseArgs(argv: string[]): Flags {
const
out
:
Flags
=
{};
const
out
:
Flags
=
{};
for
(
let
i
=
0
;
i
<
argv
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
argv
.
length
;
i
++
)
{
const
token
=
argv
[
i
];
const
token
=
argv
[
i
];
if
(
!
token
.
startsWith
(
'--'
))
continue
;
if
(
!
token
||
!
token
.
startsWith
(
'--'
))
continue
;
const
withoutDashes
=
token
.
slice
(
2
);
const
withoutDashes
=
token
.
slice
(
2
);
if
(
withoutDashes
.
includes
(
'='
))
{
if
(
withoutDashes
.
includes
(
'='
))
{
const
[
rawKey
,
rawVal
]
=
withoutDashes
.
split
(
'='
);
const
[
rawKey
,
rawVal
]
=
withoutDashes
.
split
(
'='
);
if
(
rawKey
)
{
out
[
rawKey
.
trim
()]
=
(
rawVal
??
''
).
trim
()
||
true
;
out
[
rawKey
.
trim
()]
=
(
rawVal
??
''
).
trim
()
||
true
;
}
continue
;
continue
;
}
}
const
key
=
withoutDashes
.
trim
();
const
key
=
withoutDashes
.
trim
();
...
@@ -113,7 +116,8 @@ async function mergeJson(
...
@@ -113,7 +116,8 @@ async function mergeJson(
await
fsp
.
writeFile
(
targetPath
,
JSON
.
stringify
(
merged
,
null
,
2
));
await
fsp
.
writeFile
(
targetPath
,
JSON
.
stringify
(
merged
,
null
,
2
));
}
}
function
deepMerge
(
a
:
JsonValue
,
b
:
JsonValue
):
JsonValue
{
function
deepMerge
(
a
:
JsonValue
,
b
:
JsonValue
|
undefined
):
JsonValue
{
if
(
b
===
undefined
)
return
a
;
if
(
Array
.
isArray
(
a
)
&&
Array
.
isArray
(
b
))
{
if
(
Array
.
isArray
(
a
)
&&
Array
.
isArray
(
b
))
{
return
[...
new
Set
([...
a
,
...
b
])];
return
[...
new
Set
([...
a
,
...
b
])];
}
}
...
@@ -122,11 +126,15 @@ function deepMerge(a: JsonValue, b: JsonValue): JsonValue {
...
@@ -122,11 +126,15 @@ function deepMerge(a: JsonValue, b: JsonValue): JsonValue {
const
bObj
=
b
as
Record
<
string
,
JsonValue
>
;
const
bObj
=
b
as
Record
<
string
,
JsonValue
>
;
const
out
:
Record
<
string
,
JsonValue
>
=
{
...
aObj
};
const
out
:
Record
<
string
,
JsonValue
>
=
{
...
aObj
};
for
(
const
key
of
Object
.
keys
(
bObj
))
{
for
(
const
key
of
Object
.
keys
(
bObj
))
{
out
[
key
]
=
key
in
aObj
?
deepMerge
(
aObj
[
key
],
bObj
[
key
])
:
bObj
[
key
];
const
aValue
=
aObj
[
key
];
const
bValue
=
bObj
[
key
];
if
(
bValue
!==
undefined
)
{
out
[
key
]
=
key
in
aObj
&&
aValue
!==
undefined
?
deepMerge
(
aValue
,
bValue
)
:
bValue
;
}
}
}
return
out
;
return
out
;
}
}
return
b
??
a
;
return
b
;
}
}
function
isPlainObject
(
v
:
unknown
):
v
is
Record
<
string
,
JsonValue
>
{
function
isPlainObject
(
v
:
unknown
):
v
is
Record
<
string
,
JsonValue
>
{
...
@@ -172,27 +180,41 @@ async function applyOptionalFeature(
...
@@ -172,27 +180,41 @@ async function applyOptionalFeature(
}
}
}
}
async
function
runCreate
(
argv
:
string
[])
{
interface
CreateOptions
{
const
[,
,
,
name
,
...
rest
]
=
argv
;
name
:
string
;
const
flags
=
parseArgs
(
rest
);
template
:
string
;
if
(
!
name
)
{
dir
?:
string
;
console
.
error
(
'请输入项目名称,例如: venus create my-app --template react'
);
root
?:
string
;
printHelp
();
noInstall
?:
boolean
;
process
.
exit
(
1
);
router
?:
boolean
;
}
store
?:
boolean
;
const
template
=
String
(
flags
.
template
||
'react'
);
ui
?:
string
;
eslint
?:
boolean
;
force
?:
boolean
;
}
async
function
executeCreate
(
options
:
CreateOptions
)
{
const
{
name
,
template
,
dir
,
root
,
noInstall
=
false
,
router
=
false
,
store
=
false
,
ui
=
''
,
eslint
=
false
,
force
=
false
,
}
=
options
;
const
cwd
=
process
.
cwd
();
const
cwd
=
process
.
cwd
();
const
rootDir
=
const
rootDir
=
root
?
path
.
resolve
(
cwd
,
root
)
:
cwd
;
typeof
flags
.
root
===
'string'
?
path
.
resolve
(
cwd
,
String
(
flags
.
root
))
:
cwd
;
const
defaultDirPrefix
=
(()
=>
{
const
defaultDirPrefix
=
(()
=>
{
if
(
template
===
'react'
)
return
path
.
join
(
'apps'
,
'frontend'
,
'react'
);
if
(
template
===
'react'
)
return
path
.
join
(
'apps'
,
'frontend'
,
'react'
);
if
(
template
===
'vue'
)
return
path
.
join
(
'apps'
,
'frontend'
,
'vue'
);
if
(
template
===
'vue'
)
return
path
.
join
(
'apps'
,
'frontend'
,
'vue'
);
return
''
;
return
''
;
})();
})();
const
dirPrefix
=
const
dirPrefix
=
dir
||
defaultDirPrefix
;
typeof
flags
.
dir
===
'string'
?
String
(
flags
.
dir
)
:
defaultDirPrefix
;
const
destRoot
=
path
.
resolve
(
const
destRoot
=
path
.
resolve
(
rootDir
,
rootDir
,
dirPrefix
?
path
.
join
(
dirPrefix
,
name
)
:
name
,
dirPrefix
?
path
.
join
(
dirPrefix
,
name
)
:
name
,
...
@@ -214,7 +236,7 @@ async function runCreate(argv: string[]) {
...
@@ -214,7 +236,7 @@ async function runCreate(argv: string[]) {
const
stat
=
fs
.
statSync
(
destRoot
);
const
stat
=
fs
.
statSync
(
destRoot
);
if
(
stat
.
isDirectory
())
{
if
(
stat
.
isDirectory
())
{
const
files
=
await
fsp
.
readdir
(
destRoot
);
const
files
=
await
fsp
.
readdir
(
destRoot
);
if
(
files
.
length
>
0
&&
!
f
lags
.
f
orce
)
{
if
(
files
.
length
>
0
&&
!
force
)
{
console
.
error
(
console
.
error
(
`目标目录已存在且非空:
${
destRoot
}
。如需覆盖请添加 --force`
,
`目标目录已存在且非空:
${
destRoot
}
。如需覆盖请添加 --force`
,
);
);
...
@@ -235,15 +257,10 @@ async function runCreate(argv: string[]) {
...
@@ -235,15 +257,10 @@ async function runCreate(argv: string[]) {
});
});
// Apply optional features
// Apply optional features
const
hasRouter
=
!!
flags
.
router
;
if
(
router
)
const
hasStore
=
!!
flags
.
store
;
const
hasEslint
=
!!
flags
.
eslint
;
const
ui
=
typeof
flags
.
ui
===
'string'
?
flags
.
ui
:
''
;
if
(
hasRouter
)
await
applyOptionalFeature
(
destRoot
,
tplRoot
,
'optional/router'
);
await
applyOptionalFeature
(
destRoot
,
tplRoot
,
'optional/router'
);
if
(
hasS
tore
)
await
applyOptionalFeature
(
destRoot
,
tplRoot
,
'optional/store'
);
if
(
s
tore
)
await
applyOptionalFeature
(
destRoot
,
tplRoot
,
'optional/store'
);
if
(
hasE
slint
)
if
(
e
slint
)
await
applyOptionalFeature
(
destRoot
,
tplRoot
,
'optional/eslint'
);
await
applyOptionalFeature
(
destRoot
,
tplRoot
,
'optional/eslint'
);
if
(
ui
)
if
(
ui
)
await
applyOptionalFeature
(
destRoot
,
tplRoot
,
path
.
join
(
'optional/ui'
,
ui
));
await
applyOptionalFeature
(
destRoot
,
tplRoot
,
path
.
join
(
'optional/ui'
,
ui
));
...
@@ -264,14 +281,13 @@ async function runCreate(argv: string[]) {
...
@@ -264,14 +281,13 @@ async function runCreate(argv: string[]) {
);
);
}
}
const
noInstall
=
!!
flags
[
'no-install'
];
if
(
!
noInstall
)
{
if
(
!
noInstall
)
{
console
.
log
(
'安装依赖中 (pnpm install)...'
);
console
.
log
(
'安装依赖中 (pnpm install)...'
);
try
{
try
{
child_process
.
execSync
(
'pnpm install'
,
{
child_process
.
execSync
(
'pnpm install'
,
{
stdio
:
'inherit'
,
stdio
:
'inherit'
,
cwd
:
destRoot
,
cwd
:
destRoot
,
shell
:
true
,
shell
:
process
.
platform
===
'win32'
?
'powershell.exe'
:
'/bin/sh'
,
});
});
}
catch
{
}
catch
{
console
.
warn
(
'依赖安装失败,请手动在项目目录执行 pnpm install。'
);
console
.
warn
(
'依赖安装失败,请手动在项目目录执行 pnpm install。'
);
...
@@ -281,7 +297,7 @@ async function runCreate(argv: string[]) {
...
@@ -281,7 +297,7 @@ async function runCreate(argv: string[]) {
console
.
log
(
`项目创建成功:
${
destRoot
}
`
);
console
.
log
(
`项目创建成功:
${
destRoot
}
`
);
console
.
log
(
`模板:
${
template
}
`
);
console
.
log
(
`模板:
${
template
}
`
);
console
.
log
(
console
.
log
(
`可选功能: router=
${
hasRouter
?
'on'
:
'off'
}
, store=
${
hasStore
?
'on'
:
'off'
}
, eslint=
${
hasE
slint
?
'on'
:
'off'
}
, ui=
${
ui
||
'none'
}
`
,
`可选功能: router=
${
router
?
'on'
:
'off'
}
, store=
${
store
?
'on'
:
'off'
}
, eslint=
${
e
slint
?
'on'
:
'off'
}
, ui=
${
ui
||
'none'
}
`
,
);
);
console
.
log
(
'下一步:'
);
console
.
log
(
'下一步:'
);
console
.
log
(
` cd
${
destRoot
}
`
);
console
.
log
(
` cd
${
destRoot
}
`
);
...
@@ -291,6 +307,176 @@ async function runCreate(argv: string[]) {
...
@@ -291,6 +307,176 @@ async function runCreate(argv: string[]) {
console
.
log
(
' pnpm run dev'
);
console
.
log
(
' pnpm run dev'
);
}
}
async
function
runCreate
(
argv
:
string
[])
{
const
[,
,
,
name
,
...
rest
]
=
argv
;
const
flags
=
parseArgs
(
rest
);
if
(
!
name
)
{
console
.
error
(
'请输入项目名称,例如: venus create my-app --template react'
);
printHelp
();
process
.
exit
(
1
);
}
const
options
:
CreateOptions
=
{
name
,
template
:
String
(
flags
.
template
||
'react'
),
dir
:
typeof
flags
.
dir
===
'string'
?
String
(
flags
.
dir
)
:
undefined
,
root
:
typeof
flags
.
root
===
'string'
?
String
(
flags
.
root
)
:
undefined
,
noInstall
:
!!
flags
[
'no-install'
],
router
:
!!
flags
.
router
,
store
:
!!
flags
.
store
,
ui
:
typeof
flags
.
ui
===
'string'
?
flags
.
ui
:
undefined
,
eslint
:
!!
flags
.
eslint
,
force
:
!!
flags
.
force
,
};
await
executeCreate
(
options
);
}
// 进入构建项目执行流程
async
function
runCreateInteractive
()
{
// 步骤1: 输入项目名
const
projectName
=
await
text
({
message
:
'请输入项目名称:'
,
placeholder
:
'venus-my-app'
,
validate
(
value
)
{
if
(
!
value
||
value
.
trim
().
length
===
0
)
{
return
'项目名称不能为空'
;
}
if
(
!
/^
[
a-z0-9-
]
+$/
.
test
(
value
))
{
return
'项目名称只能包含小写字母、数字和连字符'
;
}
},
});
if
(
!
projectName
)
{
cancel
(
'项目名称不能为空'
);
process
.
exit
(
0
);
}
if
(
isCancel
(
projectName
))
{
cancel
(
'操作已取消'
);
process
.
exit
(
0
);
}
// 步骤2: 选择模板类型
const
template
=
await
select
<
string
>
({
message
:
'请选择模板类型:'
,
options
:
[
{
value
:
'react'
,
label
:
'React'
},
{
value
:
'vue'
,
label
:
'Vue'
},
],
});
if
(
isCancel
(
template
)
||
typeof
template
!==
'string'
)
{
cancel
(
'操作已取消'
);
process
.
exit
(
0
);
}
// 步骤3: 输入构建目录
const
defaultDirPrefix
=
template
===
'react'
?
path
.
join
(
'apps'
,
'frontend'
,
'react'
)
:
path
.
join
(
'apps'
,
'frontend'
,
'vue'
);
const
dirInput
=
await
text
({
message
:
'请输入构建目录前缀:'
,
placeholder
:
defaultDirPrefix
,
initialValue
:
defaultDirPrefix
,
});
if
(
isCancel
(
dirInput
))
{
cancel
(
'操作已取消'
);
process
.
exit
(
0
);
}
// 步骤4: 选择是否安装依赖
const
shouldInstall
=
await
confirm
({
message
:
'是否立即安装依赖?'
,
initialValue
:
true
,
});
if
(
isCancel
(
shouldInstall
))
{
cancel
(
'操作已取消'
);
process
.
exit
(
0
);
}
// 步骤5: 选择可选功能 - Router
const
hasRouter
=
await
confirm
({
message
:
'是否启用路由 (Router)?'
,
initialValue
:
false
,
});
if
(
isCancel
(
hasRouter
))
{
cancel
(
'操作已取消'
);
process
.
exit
(
0
);
}
// 步骤6: 选择可选功能 - Store
const
hasStore
=
await
confirm
({
message
:
'是否启用状态管理 (Store)?'
,
initialValue
:
false
,
});
if
(
isCancel
(
hasStore
))
{
cancel
(
'操作已取消'
);
process
.
exit
(
0
);
}
// 步骤7: 选择 UI 库
const
runtimeDir
=
typeof
__dirname
===
'undefined'
?
path
.
dirname
(
fileURLToPath
(
import
.
meta
.
url
))
:
__dirname
;
const
tplRoot
=
path
.
resolve
(
runtimeDir
,
'../templates'
,
template
);
const
uiDir
=
path
.
join
(
tplRoot
,
'optional'
,
'ui'
);
let
selectedUi
:
string
|
undefined
=
undefined
;
if
(
fs
.
existsSync
(
uiDir
))
{
const
uiOptions
=
await
fsp
.
readdir
(
uiDir
,
{
withFileTypes
:
true
});
const
availableUis
=
uiOptions
.
filter
((
u
)
=>
u
.
isDirectory
())
.
map
((
u
)
=>
({
value
:
u
.
name
,
label
:
u
.
name
}));
if
(
availableUis
.
length
>
0
)
{
const
uiChoice
=
await
select
<
string
>
({
message
:
'请选择 UI 库 (可选):'
,
options
:
[
{
value
:
''
,
label
:
'不使用 UI 库'
},
...
availableUis
,
],
});
if
(
isCancel
(
uiChoice
)
||
typeof
uiChoice
!==
'string'
)
{
cancel
(
'操作已取消'
);
process
.
exit
(
0
);
}
selectedUi
=
uiChoice
===
''
?
undefined
:
uiChoice
;
}
}
// 步骤8: 选择 ESLint
const
hasEslint
=
await
confirm
({
message
:
'是否启用 ESLint?'
,
initialValue
:
false
,
});
if
(
isCancel
(
hasEslint
))
{
cancel
(
'操作已取消'
);
process
.
exit
(
0
);
}
// 执行创建
const
options
:
CreateOptions
=
{
name
:
String
(
projectName
),
template
:
String
(
template
),
dir
:
dirInput
?
String
(
dirInput
)
:
undefined
,
noInstall
:
!
shouldInstall
,
router
:
Boolean
(
hasRouter
),
store
:
Boolean
(
hasStore
),
ui
:
selectedUi
,
eslint
:
Boolean
(
hasEslint
),
};
await
executeCreate
(
options
);
}
async
function
runBuild
()
{
async
function
runBuild
()
{
const
buildScript
=
path
.
resolve
(
process
.
cwd
(),
'scripts/build.js'
);
const
buildScript
=
path
.
resolve
(
process
.
cwd
(),
'scripts/build.js'
);
if
(
!
fs
.
existsSync
(
buildScript
))
{
if
(
!
fs
.
existsSync
(
buildScript
))
{
...
@@ -300,7 +486,7 @@ async function runBuild() {
...
@@ -300,7 +486,7 @@ async function runBuild() {
try
{
try
{
child_process
.
execSync
(
`node
${
JSON
.
stringify
(
buildScript
)}
`
,
{
child_process
.
execSync
(
`node
${
JSON
.
stringify
(
buildScript
)}
`
,
{
stdio
:
'inherit'
,
stdio
:
'inherit'
,
shell
:
true
,
shell
:
process
.
platform
===
'win32'
?
'powershell.exe'
:
'/bin/sh'
,
});
});
}
catch
{
}
catch
{
process
.
exit
(
1
);
process
.
exit
(
1
);
...
@@ -319,7 +505,13 @@ export async function main() {
...
@@ -319,7 +505,13 @@ export async function main() {
}
}
const
cmd
=
cmdOrFlag
;
const
cmd
=
cmdOrFlag
;
if
(
cmd
===
'create'
)
{
if
(
cmd
===
'create'
)
{
// 如果没有提供项目名,则进入交互式模式
const
name
=
process
.
argv
[
3
];
if
(
!
name
||
name
.
startsWith
(
'--'
))
{
await
runCreateInteractive
();
}
else
{
await
runCreate
(
process
.
argv
);
await
runCreate
(
process
.
argv
);
}
return
;
return
;
}
}
if
(
cmd
===
'build'
)
{
if
(
cmd
===
'build'
)
{
...
...
pnpm-lock.yaml
View file @
2576916
...
@@ -892,6 +892,10 @@ importers:
...
@@ -892,6 +892,10 @@ importers:
version
:
5.9.3
version
:
5.9.3
packages/cli
:
packages/cli
:
dependencies
:
'
@clack/prompts'
:
specifier
:
'
catalog:'
version
:
0.10.1
devDependencies
:
devDependencies
:
typescript
:
typescript
:
specifier
:
'
*'
specifier
:
'
*'
...
...
scripts/turbo-run/src/index.ts
View file @
2576916
...
@@ -6,7 +6,7 @@ import { run } from './run';
...
@@ -6,7 +6,7 @@ import { run } from './run';
try
{
try
{
const
turboRun
=
cac
(
'turbo-run'
);
const
turboRun
=
cac
(
'turbo-run'
);
console
.
log
(
process
.
argv
);
turboRun
turboRun
.
command
(
'[script]'
)
.
command
(
'[script]'
)
.
usage
(
`Run turbo interactively.`
)
.
usage
(
`Run turbo interactively.`
)
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment