@ bagelink/ 工作空间
Bagel项目的工作空间工具----既为单一项目工作,也为单列项目工作.
特征
- 🔧 简单配置: 所有环境的一个配置文件
- 🔀 代理管理: Vite dev 代理 + Netlify 生产重定向
- 🏗️ 构建工具: 使用环境注入的自发遗传网
- 📦 莫诺雷波支持: 多项目设置的连锁配置
安装
pnpm add @bagelink/workspace
快速启动
备选案文1:运行 npx bgl init (建议)
npx bgl init
🔧 No bgl.config.ts found. Let's create one!
? What is your Bagel project ID? › my-project
? Use custom production host? › No / Yes
? Add/update dev scripts in package.json? › Yes
? Create/update vite.config.ts? › Yes
? Generate netlify.toml for deployment? › Yes
✅ Created bgl.config.ts
Production host: https://my-project.bagel.to
Local dev host: http://localhost:8000
✅ Updated package.json with dev scripts
✅ Created vite.config.ts
✅ Generated netlify.toml
这将:
- 创建
bgl.config.ts使用您的工程配置 - 添加/覆盖
dev财务报告和审定财务报表dev:local脚本在package.json - 创建
vite.config.ts带有代理配置( 如果不存在) - 生成
netlify.toml带有生产代理配置
备选案文2:首次使用时自动
你第一次使用的时候 @bagelink/workspace,它将自动提示您创建配置文件,如果它不存在.
备选案文3:手工创建
创建 bgl.config.ts数字 :
import { defineWorkspace } from '@bagelink/workspace'
import type { WorkspaceConfig, WorkspaceEnvironment } from '@bagelink/workspace'
/**
* Define your workspace environments
* You can add as many custom environments as needed (e.g., 'staging', 'preview')
* Use --mode flag to switch: bgl dev --mode <env_name>
*/
const configs: Record<WorkspaceEnvironment, WorkspaceConfig> = {
localhost: {
host: 'http://localhost:8000',
proxy: '/api', // Optional: remove to skip proxy setup
openapi_url: 'http://localhost:8000/openapi.json',
},
development: {
host: 'https://project.bagel.to',
proxy: '/api',
openapi_url: 'https://project.bagel.to/openapi.json',
},
production: {
host: 'https://project.bagel.to',
proxy: '/api',
},
// Add your own custom environments
staging: {
host: 'https://staging.project.bagel.to',
proxy: '/api',
},
}
export default defineWorkspace(configs)
使用量
1. 增加类型定义
添加到您的项目 env.d.ts数字 :
/// <reference types="@bagelink/workspace/env" />
这为注入的环境变量提供了TypeScript类型.
2. 配置 Vite (vite.config.ts(中文(简体) )
建议: 使用 Vite 插件( 保持您的配置干净并标准)
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { bagelink } from '@bagelink/workspace/vite'
import workspace from './bgl.config'
export default defineConfig({
plugins: [
vue(),
bagelink({ workspace }),
],
})
自动插件 :
- 基于
bgl.config.ts - 设置
@别名指向./src - 设置
@shared别名( 单列) - 保持你的 Vite.config.ts 干净和可扩展
高级: 自定义选项
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { bagelink } from '@bagelink/workspace/vite'
import { fileURLToPath } from 'node:url'
import workspace from './bgl.config'
export default defineConfig({
plugins: [
vue(),
bagelink({
workspace,
config: {
sharedPath: '../packages/shared',
additionalAliases: {
'@utils': fileURLToPath(new URL('./src/utils', import.meta.url))
}
}
}),
],
})
遗产: 手动代理设置
import { defineConfig } from 'vite'
import { createViteProxy } from '@bagelink/workspace'
import workspace from './bgl.config'
export default defineConfig(({ mode }) => {
const config = workspace(mode as WorkspaceEnvironment)
return {
plugins: [vue()],
server: {
proxy: createViteProxy(config)
}
}
})
3. 在您的应用程序中使用配置
运行时访问配置 useWorkspace()数字 :
import { useWorkspace, getApiUrl } from '@bagelink/workspace'
// In your app setup
const { baseURL, proxy, host, mode } = useWorkspace()
const auth = createAuth({ baseURL }) // Uses proxy if available, otherwise host
// Or get full API URL
const apiUrl = getApiUrl() // 'https://project.bagel.to/api'
Vue 组件示例 :
<script setup lang="ts">
import { useWorkspace } from '@bagelink/workspace'
import { createAuth } from '@bagelink/auth'
const { baseURL, proxy, host, mode } = useWorkspace()
const auth = createAuth({ baseURL }) // Uses proxy if available, otherwise host
console.log('API Host:', host)
console.log('Environment:', mode)
</script>
访问原始环境变量 :
// These are injected at build time by the bagelink plugin
const proxy = import.meta.env.VITE_BGL_PROXY // '/api'
const host = import.meta.env.VITE_BGL_HOST // 'https://project.bagel.to'
const openapi = import.meta.env.VITE_BGL_OPENAPI_URL // optional
4. 添加脚本到 package.json
{
"scripts": {
"dev": "vite",
"dev:local": "vite --mode localhost",
"dev:staging": "vite --mode staging",
"build": "vite build",
"build:staging": "vite build --mode staging"
}
}
5. 自定义环境
您可以定义您需要的多少自定义环境 。 环境名称是完全灵活的:
const configs: Record<WorkspaceEnvironment, WorkspaceConfig> = {
localhost: {
host: 'http://localhost:8000',
proxy: '/api',
},
development: {
host: 'https://dev.project.bagel.to',
proxy: '/api',
},
staging: {
host: 'https://staging.project.bagel.to',
proxy: '/api',
},
preview: {
host: 'https://preview.project.bagel.to',
// No proxy - will use host directly
},
production: {
host: 'https://project.bagel.to',
proxy: '/api',
},
}
使用 --mode 要切换环境的旗帜 :
# Development
bgl dev --mode development
# Staging
bgl dev --mode staging
# Production build
bgl build --mode production
说明: 这个 proxy 字段是可选的。 如果省略, 将不会为此环境配置代理, 并且 baseURL 从 useWorkspace() 将返回 host 数值.
6. 产生网络化配置
import { writeNetlifyConfig } from '@bagelink/workspace'
import workspace from './bgl.config'
const config = workspace('production')
writeNetlifyConfig(config, './netlify.toml')
莫诺雷波支持
根配置( R)/bgl.config.ts(中文(简体) )
import { defineWorkspace } from '@bagelink/workspace'
export default defineWorkspace({
localhost: {
host: 'http://localhost:8000',
proxy: '/api',
},
development: {
host: 'https://shared.bagel.to',
proxy: '/api',
},
production: {
host: 'https://shared.bagel.to',
proxy: '/api',
},
})
项目重叠( E)/projects/admin/bgl.config.ts(中文(简体) )
import { defineWorkspace } from '@bagelink/workspace'
import rootWorkspace from '../../bgl.config'
export default defineWorkspace({
localhost: rootWorkspace('localhost'),
development: rootWorkspace('development'),
production: {
...rootWorkspace('production'),
host: 'https://admin.bagel.to', // Override
},
})
国 际
单一工程设置
npx bgl init
交互生成项目配置文件 :
bgl.config.ts- 工作空间配置package.json- 添加/更新dev脚本(可选)vite.config.ts- Vite 代理配置( 可选)netlify.toml- 将部署配置网化(可选)
npx bgl init
特征 :
- 项目ID和主机配置提示
- 添加/覆盖
dev财务报告和审定财务报表dev:local软件包中的脚本。 json - 用代理设置创建 vite. config.ts( 如果不存在)
- 生成 netlify.toml 有生产代理配置
- 安全性:在每个步骤前提示
工作空间( 多项目) 设置
npx bgl init --workspace
为多个项目创建一个具有平面结构的工作空间:
npx bgl init --workspace
? Workspace name: › my-workspace
? Bagel project ID: › my-project
? Create first project? › Yes
? First project name: › web
✅ Workspace created successfully!
创建结构 :
my-workspace/
├── package.json # Workspace root
├── bgl.config.ts # Shared config
├── tsconfig.json
├── shared/ # Shared code
│ ├── package.json
│ ├── utils/
│ └── types/
└── web/ # First project
├── bgl.config.ts
├── package.json
├── vite.config.ts
└── src/
bgl add <project-name>
添加新工程到工作空间 :
bgl add admin
bgl add customer
bgl add mobile
每个项目:
- 在根级别获得自己的目录
- 从工作空间根继承共享配置
- 可以从
shared/utils, (中文(简体) ),shared/types等 (中文(简体) ). - 带有 Vite 代理服务器的自动配置
bgl list
列出工作空间中的所有工程 :
bgl list
Projects:
- web
- admin
- customer
运行工作空间项目
# Run all projects concurrently
bun run dev
# Run specific project
bun run dev:admin
bun run dev:customer
# Build specific project
bun run build:admin
# Build all projects
bun run build
Vite 自动指定端口 :
- 第一个项目:
http://localhost:5173 - 第二个项目:
http://localhost:5174 - 第三个项目:
http://localhost:5175 - 包括:.
启动设置
bgl lint init
在您的工程中设置起伏和格式:
bgl lint init
? Select configurations to set up:
✔ ESLint
✔ Prettier
✔ EditorConfig
○ Git Hooks
? Install dependencies? › Yes
✅ Created eslint.config.js
✅ Created .prettierrc
✅ Created .prettierignore
✅ Created .editorconfig
✅ Updated package.json with lint scripts
创建 :
eslint.config.js- ESLint配置(第3版+类型脚本).prettierrc- 高级配置.prettierignore- Pretier 忽略模式.editorconfig- 编辑器配置.lintstagedrc- 分级配置(如果选中了接钩)
添加脚本 :
bun run lint- 运行天线bun run lint:fix- 解决起伏问题bun run format- 格式代码bun run format:check- 检查格式
自动检测工作空间 :
# In workspace root (auto-detected)
bgl lint init
✓ Detected workspace mode
# Sets up at workspace root
# In single project (auto-detected)
bgl lint init
# Sets up in current project
# Force modes:
bgl lint init --workspace # Force workspace mode
bgl lint init --project # Force single project mode
工作空间检测 :
- 检查
workspaces选项 - 检查多个工程目录
- 自动应用最佳模式
SDK 生成
bgl sdk generate
从 OpenAPI 规格生成类型SDK :
bgl sdk generate
? OpenAPI spec URL: › http://localhost:8000/openapi.json
? Output directory: › ./src/api
? Split into organized files? › Yes
📡 Fetching OpenAPI spec from: http://localhost:8000/openapi.json
📁 Output directory: ./src/api
✅ Generated types.d.ts
✅ Generated api.ts
✅ Generated index.ts
🔀 Splitting into organized files...
✅ Files organized into directories
✅ SDK generated successfully!
Import it in your code:
import { api } from './api'
特征 :
- 自动读取
openapi_url从bgl.config.ts - 从 OpenAPI 计划生成类型脚本
- 创建类型安全 API 客户端
- 可选文件组织( 由端点分割)
- 本地和远程 OpenAPI 规格的工程
自动检测工作空间 :
# In workspace root (auto-detected)
bgl sdk generate
✓ Detected workspace mode - will generate for multiple projects
? Select projects to generate SDK for:
✔ admin
✔ customer
✔ mobile
# In single project (auto-detected)
bgl sdk generate
# Generates SDK for current project only
# Force modes:
bgl sdk generate --workspace # Force workspace mode
bgl sdk generate --project # Force single project mode
聪明行为 :
- 自动检测工作空间结构
- 工作空间模式下的项目选择提示
- 读取
openapi_url从每个项目的bgl.config.ts
生成结构 :
src/api/
├── index.ts # Main export
├── types.d.ts # TypeScript types
└── api.ts # API client
# Or with --split:
src/api/
├── endpoints/
│ ├── users.ts
│ ├── auth.ts
│ └── data.ts
├── types/
│ └── index.ts
└── index.ts
npx bgl --help
显示 CLI 帮助 .
API 密码
运行时函数
useWorkspace()
运行时获得工作空间配置 。 配置在构建过程中作为环境变量被注入.
import { useWorkspace } from '@bagelink/workspace'
const { baseURL, proxy, host, openapiUrl, mode } = useWorkspace()
回返情况:
interface RuntimeWorkspaceConfig {
baseURL: string // proxy if available, otherwise host
proxy?: string // '/api' (optional)
host: string // 'https://project.bagel.to'
openapiUrl?: string // optional
mode: string // current environment (e.g., 'localhost', 'development', 'staging')
}
getApiUrl()
通过合并主机和代理机获取完整的API URL.
import { getApiUrl } from '@bagelink/workspace'
const apiUrl = getApiUrl() // 'https://project.bagel.to/api'
配置函数
defineWorkspace(configs)
为所有环境定义工作空间配置 .
const workspace = defineWorkspace({
local: { host: 'http://localhost:8000', proxy: '/api' },
development: { host: 'https://project.bagel.to', proxy: '/api' },
production: { host: 'https://project.bagel.to', proxy: '/api' },
})
generateWorkspaceConfig(root?, configFile?)
交互式生成 bgl.config.ts 文件(在找不到配置时称为自动).
generateWorkspaceConfigSync(projectId, root?, configFile?, customHost?)
程序生成 bgl.config.ts 文件 .
import { generateWorkspaceConfigSync } from '@bagelink/workspace'
generateWorkspaceConfigSync('my-project')
// Creates bgl.config.ts with host: https://my-project.bagel.to
createWorkspace(options?)
创建具有async配置分辨率的工作空间实例(用于高级单列配置).
const workspace = createWorkspace({ root: process.cwd() })
const config = await workspace.getConfig('production')
const proxy = workspace.createProxy(config)
workspace.generateNetlify(config, './netlify.toml')
构建时函数
可从下列网址获得这些资料: @bagelink/workspace/vite数字 :
createViteProxy(config)
生成 Vite 代理配置 .
import { createViteProxy } from '@bagelink/workspace/vite'
writeNetlifyConfig(config, outPath?, additionalConfig?)
生成并写入netlify.toml文件.
import { writeNetlifyConfig } from '@bagelink/workspace/vite'
setBuildEnvVars(config)
设定构建进程的环境变量 :
BGL_PROXY_PATH- 代理路径(例如,/api(中文(简体) )BGL_API_HOST- API 主机 URLBGL_OPENAPI_URL- OpenAPI 规格 URL
import { setBuildEnvVars } from '@bagelink/workspace/vite'
许可证
麻省理工学院巴盖尔工作室