@openwebf/webf 0.1.0
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/CLAUDE.md +206 -0
- package/README-zhCN.md +256 -0
- package/README.md +232 -0
- package/bin/webf.js +25 -0
- package/coverage/clover.xml +1295 -0
- package/coverage/coverage-final.json +12 -0
- package/coverage/lcov-report/IDLBlob.ts.html +142 -0
- package/coverage/lcov-report/analyzer.ts.html +2158 -0
- package/coverage/lcov-report/analyzer_original.ts.html +1450 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/commands.ts.html +700 -0
- package/coverage/lcov-report/dart.ts.html +490 -0
- package/coverage/lcov-report/declaration.ts.html +337 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/generator.ts.html +1171 -0
- package/coverage/lcov-report/index.html +266 -0
- package/coverage/lcov-report/logger.ts.html +424 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/react.ts.html +619 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov-report/utils.ts.html +466 -0
- package/coverage/lcov-report/vue.ts.html +613 -0
- package/coverage/lcov.info +2149 -0
- package/global.d.ts +2 -0
- package/jest.config.js +24 -0
- package/package.json +36 -0
- package/src/IDLBlob.ts +20 -0
- package/src/analyzer.ts +692 -0
- package/src/commands.ts +645 -0
- package/src/dart.ts +170 -0
- package/src/declaration.ts +84 -0
- package/src/generator.ts +454 -0
- package/src/logger.ts +114 -0
- package/src/react.ts +186 -0
- package/src/utils.ts +127 -0
- package/src/vue.ts +176 -0
- package/templates/class.dart.tpl +86 -0
- package/templates/gitignore.tpl +2 -0
- package/templates/react.component.tsx.tpl +53 -0
- package/templates/react.createComponent.tpl +286 -0
- package/templates/react.index.ts.tpl +8 -0
- package/templates/react.package.json.tpl +26 -0
- package/templates/react.tsconfig.json.tpl +16 -0
- package/templates/react.tsup.config.ts.tpl +10 -0
- package/templates/tsconfig.json.tpl +8 -0
- package/templates/vue.component.partial.tpl +31 -0
- package/templates/vue.components.d.ts.tpl +49 -0
- package/templates/vue.package.json.tpl +11 -0
- package/templates/vue.tsconfig.json.tpl +15 -0
- package/test/IDLBlob.test.ts +75 -0
- package/test/analyzer.test.ts +370 -0
- package/test/commands.test.ts +1253 -0
- package/test/generator.test.ts +460 -0
- package/test/logger.test.ts +215 -0
- package/test/react.test.ts +49 -0
- package/test/utils.test.ts +316 -0
- package/tsconfig.json +30 -0
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# WebF CLI Development Guide
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
The WebF CLI is a code generation tool that creates type-safe bindings between Flutter/Dart and JavaScript frameworks (React, Vue). It analyzes TypeScript definition files and generates corresponding Dart classes and JavaScript/TypeScript components.
|
|
5
|
+
|
|
6
|
+
## Architecture
|
|
7
|
+
|
|
8
|
+
### Core Components
|
|
9
|
+
- `analyzer.ts` - TypeScript AST analysis with multi-level caching
|
|
10
|
+
- `generator.ts` - Orchestrates code generation for Dart, React, and Vue
|
|
11
|
+
- `dart.ts` - Dart code generation from TypeScript definitions
|
|
12
|
+
- `react.ts` - React component generation
|
|
13
|
+
- `vue.ts` - Vue component generation
|
|
14
|
+
- `commands.ts` - CLI command implementations
|
|
15
|
+
- `logger.ts` - Logging utility without external dependencies
|
|
16
|
+
|
|
17
|
+
### Key Features
|
|
18
|
+
- Multi-level caching for performance optimization
|
|
19
|
+
- Parallel file processing
|
|
20
|
+
- Type-safe attribute handling with automatic conversions
|
|
21
|
+
- Comprehensive error handling and recovery
|
|
22
|
+
|
|
23
|
+
## Code Generation Patterns
|
|
24
|
+
|
|
25
|
+
### TypeScript to Dart Type Mapping
|
|
26
|
+
```typescript
|
|
27
|
+
// Boolean attributes are always non-nullable in Dart
|
|
28
|
+
interface Props {
|
|
29
|
+
open?: boolean; // Generates: bool get open;
|
|
30
|
+
title?: string; // Generates: String? get title;
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Attribute Type Conversion
|
|
35
|
+
HTML attributes are always strings, so the generator includes automatic type conversion:
|
|
36
|
+
- Boolean: `value == 'true' || value == ''`
|
|
37
|
+
- Integer: `int.tryParse(value) ?? 0`
|
|
38
|
+
- Double: `double.tryParse(value) ?? 0.0`
|
|
39
|
+
- String: Direct assignment
|
|
40
|
+
|
|
41
|
+
## Performance Optimizations
|
|
42
|
+
|
|
43
|
+
### Caching Strategy
|
|
44
|
+
1. **Source File Cache**: Parsed TypeScript AST files are cached
|
|
45
|
+
2. **Type Conversion Cache**: Frequently used type conversions are cached
|
|
46
|
+
3. **File Content Cache**: File contents are cached to detect changes
|
|
47
|
+
|
|
48
|
+
### Batch Processing
|
|
49
|
+
Files are processed in batches for optimal parallelism:
|
|
50
|
+
```typescript
|
|
51
|
+
await processFilesInBatch(items, batchSize, processor);
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Testing Guidelines
|
|
55
|
+
|
|
56
|
+
### Test Structure
|
|
57
|
+
- Unit tests for all core modules
|
|
58
|
+
- Mock file system operations before module imports
|
|
59
|
+
- Test coverage threshold: 70%
|
|
60
|
+
|
|
61
|
+
### Running Tests
|
|
62
|
+
```bash
|
|
63
|
+
npm test # Run all tests
|
|
64
|
+
npm test -- test/analyzer.test.ts # Run specific test
|
|
65
|
+
npm run test:coverage # Run with coverage report
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Mock Patterns
|
|
69
|
+
For modules that read files at load time:
|
|
70
|
+
```typescript
|
|
71
|
+
jest.mock('fs');
|
|
72
|
+
import fs from 'fs';
|
|
73
|
+
const mockFs = fs as jest.Mocked<typeof fs>;
|
|
74
|
+
mockFs.readFileSync = jest.fn().mockImplementation((path) => {
|
|
75
|
+
// Return appropriate content
|
|
76
|
+
});
|
|
77
|
+
// Now import the module
|
|
78
|
+
import { moduleUnderTest } from './module';
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## CLI Usage
|
|
82
|
+
|
|
83
|
+
### Commands
|
|
84
|
+
```bash
|
|
85
|
+
# Generate code from TypeScript definitions (auto-creates project if needed)
|
|
86
|
+
webf codegen <output-dir> --flutter-package-src=<path> [--framework=react|vue] [--package-name=<name>] [--publish-to-npm] [--npm-registry=<url>]
|
|
87
|
+
|
|
88
|
+
# Create a new project without code generation
|
|
89
|
+
webf codegen <output-dir> [--framework=react|vue] [--package-name=<name>]
|
|
90
|
+
|
|
91
|
+
# Generate and publish to npm
|
|
92
|
+
webf codegen <output-dir> --flutter-package-src=<path> --publish-to-npm
|
|
93
|
+
|
|
94
|
+
# Generate and publish to custom registry
|
|
95
|
+
webf codegen <output-dir> --flutter-package-src=<path> --publish-to-npm --npm-registry=https://custom.registry.com/
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Auto-creation Behavior
|
|
99
|
+
The `generate` command now automatically detects if a project needs to be created:
|
|
100
|
+
- If required files (package.json, global.d.ts, tsconfig.json) are missing, it will create a new project
|
|
101
|
+
- If framework or package name are not provided, it will prompt interactively
|
|
102
|
+
- If an existing project is detected, it will use the existing configuration
|
|
103
|
+
- Framework can be auto-detected from existing package.json dependencies
|
|
104
|
+
|
|
105
|
+
### Metadata Synchronization
|
|
106
|
+
When creating typing projects, the CLI automatically synchronizes metadata from the Flutter package:
|
|
107
|
+
- Reads `pubspec.yaml` from the Flutter package source directory
|
|
108
|
+
- Extracts version and description information
|
|
109
|
+
- Applies this metadata to the generated `package.json` files
|
|
110
|
+
- Ensures typing packages match the same version as the Flutter package
|
|
111
|
+
|
|
112
|
+
### Automatic Build
|
|
113
|
+
After code generation, the CLI automatically runs `npm run build` if a build script is present in the package.json. This ensures the generated package is immediately ready for use or publishing. The build process:
|
|
114
|
+
- Checks for the presence of a `build` script in package.json
|
|
115
|
+
- Runs the build command if available
|
|
116
|
+
- Continues successfully even if the build fails (with a warning)
|
|
117
|
+
- Provides clear console output about the build status
|
|
118
|
+
|
|
119
|
+
### NPM Publishing
|
|
120
|
+
The CLI supports automatic npm publishing with the following features:
|
|
121
|
+
- **--publish-to-npm**: Automatically publishes the generated package to npm (build is run automatically)
|
|
122
|
+
- **--npm-registry**: Specify a custom npm registry URL (defaults to https://registry.npmjs.org/)
|
|
123
|
+
- **Interactive publishing**: If not using the --publish-to-npm flag, the CLI will ask if you want to publish after generation
|
|
124
|
+
- **Registry configuration**: When choosing to publish interactively, you can specify a custom registry URL
|
|
125
|
+
- Checks if user is logged in before attempting to publish
|
|
126
|
+
- Temporarily sets and resets registry configuration when custom registry is used
|
|
127
|
+
|
|
128
|
+
Requirements for publishing:
|
|
129
|
+
- Must be logged in to npm (`npm login`)
|
|
130
|
+
- Package must have a valid package.json
|
|
131
|
+
- Package will be built automatically before publishing (if build script exists)
|
|
132
|
+
|
|
133
|
+
Publishing workflow:
|
|
134
|
+
1. If `--publish-to-npm` is not specified, CLI prompts after successful generation
|
|
135
|
+
2. If user chooses to publish, CLI asks for registry URL (optional)
|
|
136
|
+
3. Validates npm login status
|
|
137
|
+
4. Publishes to specified registry (no need to build separately)
|
|
138
|
+
|
|
139
|
+
### Output Directory Behavior
|
|
140
|
+
- Dart files are generated in the Flutter package source directory
|
|
141
|
+
- React/Vue files are generated in the specified output directory
|
|
142
|
+
- If no output directory is specified, a temporary directory is created
|
|
143
|
+
- Temporary directories are created in the system temp folder with prefix `webf-typings-`
|
|
144
|
+
- When using temporary directories, the path is displayed at the end of generation
|
|
145
|
+
- Paths can be absolute or relative to current working directory
|
|
146
|
+
|
|
147
|
+
### Generated Files Structure
|
|
148
|
+
When running `dartGen`:
|
|
149
|
+
- Dart binding files are generated with `_bindings_generated.dart` suffix
|
|
150
|
+
- Original `.d.ts` files are copied to the output directory maintaining their structure
|
|
151
|
+
- An `index.d.ts` file is generated with:
|
|
152
|
+
- TypeScript triple-slash references to all `.d.ts` files
|
|
153
|
+
- ES module exports for all type definitions
|
|
154
|
+
- Package metadata from `pubspec.yaml` in documentation comments
|
|
155
|
+
- Directory structure from source is preserved in the output
|
|
156
|
+
|
|
157
|
+
## Development Workflow
|
|
158
|
+
|
|
159
|
+
### Adding New Features
|
|
160
|
+
1. Update TypeScript interfaces/types
|
|
161
|
+
2. Implement feature with tests
|
|
162
|
+
3. Update templates if needed
|
|
163
|
+
4. Ensure all tests pass
|
|
164
|
+
5. Update this documentation
|
|
165
|
+
|
|
166
|
+
### Debugging
|
|
167
|
+
Use the logger for debugging:
|
|
168
|
+
```typescript
|
|
169
|
+
import { debug, info, warn, error } from './logger';
|
|
170
|
+
debug('Processing file:', filename);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Template Modification
|
|
174
|
+
Templates are in `/templates/*.tpl`. When modifying:
|
|
175
|
+
1. Update the template file
|
|
176
|
+
2. Update the corresponding generator function
|
|
177
|
+
3. Ensure generated code follows style guidelines
|
|
178
|
+
|
|
179
|
+
## Common Issues and Solutions
|
|
180
|
+
|
|
181
|
+
### Issue: Boolean attributes treated as strings
|
|
182
|
+
**Solution**: Use `generateAttributeSetter` which handles type conversion
|
|
183
|
+
|
|
184
|
+
### Issue: Null type handling
|
|
185
|
+
**Solution**: Check for literal types containing null:
|
|
186
|
+
```typescript
|
|
187
|
+
if (type.kind === ts.SyntaxKind.LiteralType) {
|
|
188
|
+
const literalType = type as ts.LiteralTypeNode;
|
|
189
|
+
if (literalType.literal.kind === ts.SyntaxKind.NullKeyword) {
|
|
190
|
+
return FunctionArgumentType.null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Issue: File changes not detected
|
|
196
|
+
**Solution**: Clear caches before generation:
|
|
197
|
+
```typescript
|
|
198
|
+
clearCaches();
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Code Style
|
|
202
|
+
- Use async/await for asynchronous operations
|
|
203
|
+
- Implement proper error handling with try-catch
|
|
204
|
+
- Add descriptive error messages
|
|
205
|
+
- Use TypeScript strict mode
|
|
206
|
+
- Follow existing patterns in the codebase
|
package/README-zhCN.md
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# WebF CLI
|
|
2
|
+
|
|
3
|
+
一个用于生成 Flutter 类和 Vue/React 组件声明文件的命令行工具。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @openwebf/webf
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用方法
|
|
12
|
+
|
|
13
|
+
WebF CLI 提供三个主要命令:
|
|
14
|
+
|
|
15
|
+
### 1. 初始化类型定义
|
|
16
|
+
|
|
17
|
+
在项目中初始化 WebF 类型定义:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
webf init <目标目录>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
这将在指定目录中创建必要的类型定义文件。
|
|
24
|
+
|
|
25
|
+
### 2. 创建新项目
|
|
26
|
+
|
|
27
|
+
创建一个新的 Vue 或 React 项目:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
webf create <目标目录> --framework <框架> --package-name <包名>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
选项:
|
|
34
|
+
- `--framework`:选择 'vue' 或 'react'
|
|
35
|
+
- `--package-name`:指定包名
|
|
36
|
+
|
|
37
|
+
示例:
|
|
38
|
+
```bash
|
|
39
|
+
webf create my-webf-app --framework react --package-name @myorg/my-webf-app
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 3. 生成代码
|
|
43
|
+
|
|
44
|
+
生成 Flutter 类和组件声明文件:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
webf generate <目标路径> --flutter-package-src <Flutter源码路径> --framework <框架>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
选项:
|
|
51
|
+
- `--flutter-package-src`:Flutter 包源码路径
|
|
52
|
+
- `--framework`:选择 'vue' 或 'react'
|
|
53
|
+
|
|
54
|
+
示例:
|
|
55
|
+
```bash
|
|
56
|
+
webf generate ./src --flutter-package-src ./flutter_package --framework react
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## 实现细节
|
|
60
|
+
|
|
61
|
+
### 类型系统
|
|
62
|
+
|
|
63
|
+
CLI 使用复杂的类型系统来处理各种数据类型:
|
|
64
|
+
|
|
65
|
+
- 基本类型:
|
|
66
|
+
- `string`(DOM 字符串)
|
|
67
|
+
- `number`(整数/浮点数)
|
|
68
|
+
- `boolean`(布尔值)
|
|
69
|
+
- `any`(任意类型)
|
|
70
|
+
- `void`(空类型)
|
|
71
|
+
- `null`(空值)
|
|
72
|
+
- `undefined`(未定义)
|
|
73
|
+
|
|
74
|
+
- 复杂类型:
|
|
75
|
+
- 数组:`Type[]`
|
|
76
|
+
- 函数:`Function`
|
|
77
|
+
- Promise:`Promise<Type>`
|
|
78
|
+
- 自定义事件:`CustomEvent`
|
|
79
|
+
- 布局依赖类型:`DependentsOnLayout<Type>`
|
|
80
|
+
|
|
81
|
+
### 命名约定和文件结构
|
|
82
|
+
|
|
83
|
+
#### 接口命名模式
|
|
84
|
+
|
|
85
|
+
CLI 遵循特定的接口命名模式:
|
|
86
|
+
|
|
87
|
+
1. **组件接口**:
|
|
88
|
+
- 属性接口:`{组件名称}Properties`
|
|
89
|
+
- 事件接口:`{组件名称}Events`
|
|
90
|
+
- 示例:`ButtonProperties`、`ButtonEvents`
|
|
91
|
+
|
|
92
|
+
2. **生成的文件名**:
|
|
93
|
+
- React 组件:`{组件名称}.tsx`
|
|
94
|
+
- Vue 组件:`{组件名称}.vue`
|
|
95
|
+
- Flutter 类:`{组件名称}.dart`
|
|
96
|
+
- 类型定义:`{组件名称}.d.ts`
|
|
97
|
+
|
|
98
|
+
3. **名称转换**:
|
|
99
|
+
- 组件名称提取:
|
|
100
|
+
- 从 `{名称}Properties` → `{名称}`
|
|
101
|
+
- 从 `{名称}Events` → `{名称}`
|
|
102
|
+
- 示例:`ButtonProperties` → `Button`
|
|
103
|
+
|
|
104
|
+
#### 生成的组件名称
|
|
105
|
+
|
|
106
|
+
1. **React 组件**:
|
|
107
|
+
- 标签名:`<{组件名称} />`
|
|
108
|
+
- 文件名:`{组件名称}.tsx`
|
|
109
|
+
- 示例:`ButtonProperties` → `<Button />` 在 `button.tsx` 中
|
|
110
|
+
|
|
111
|
+
2. **Vue 组件**:
|
|
112
|
+
- 标签名:`<{组件名称}-component />`
|
|
113
|
+
- 文件名:`{组件名称}.vue`
|
|
114
|
+
- 示例:`ButtonProperties` → `<button-component />` 在 `button.vue` 中
|
|
115
|
+
|
|
116
|
+
3. **Flutter 类**:
|
|
117
|
+
- 类名:`{组件名称}`
|
|
118
|
+
- 文件名:`{组件名称}.dart`
|
|
119
|
+
- 示例:`ButtonProperties` → `Button` 类在 `button.dart` 中
|
|
120
|
+
|
|
121
|
+
#### 类型定义文件
|
|
122
|
+
|
|
123
|
+
1. **文件位置**:
|
|
124
|
+
- React:`src/components/{组件名称}.d.ts`
|
|
125
|
+
- Vue:`src/components/{组件名称}.d.ts`
|
|
126
|
+
- Flutter:`lib/src/{组件名称}.dart`
|
|
127
|
+
|
|
128
|
+
2. **接口结构**:
|
|
129
|
+
```typescript
|
|
130
|
+
interface {组件名称}Properties {
|
|
131
|
+
// 属性
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
interface {组件名称}Events {
|
|
135
|
+
// 事件
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
3. **组件注册**:
|
|
140
|
+
- React:在 `index.ts` 中导出
|
|
141
|
+
- Vue:在组件声明文件中注册
|
|
142
|
+
- Flutter:在库文件中导出
|
|
143
|
+
|
|
144
|
+
### 组件生成
|
|
145
|
+
|
|
146
|
+
#### React 组件
|
|
147
|
+
- 生成带有适当类型定义的 TypeScript React 组件
|
|
148
|
+
- 处理带有正确事件类型的事件绑定
|
|
149
|
+
- 支持带有基于 Promise 返回类型的异步方法
|
|
150
|
+
- 将事件名称转换为 React 约定(如 `onClick`、`onChange`)
|
|
151
|
+
|
|
152
|
+
#### Vue 组件
|
|
153
|
+
- 生成 Vue 组件类型声明
|
|
154
|
+
- 支持 Vue 的事件系统
|
|
155
|
+
- 使用适当的 TypeScript 类型处理属性和事件
|
|
156
|
+
- 生成组件注册代码
|
|
157
|
+
|
|
158
|
+
#### Flutter 类
|
|
159
|
+
- 生成带有适当类型映射的 Dart 类
|
|
160
|
+
- 使用正确的参数类型处理方法声明
|
|
161
|
+
- 支持异步操作
|
|
162
|
+
- 生成适当的事件处理器类型
|
|
163
|
+
|
|
164
|
+
### 类型分析
|
|
165
|
+
|
|
166
|
+
CLI 使用 TypeScript 的编译器 API 来分析和处理类型定义:
|
|
167
|
+
|
|
168
|
+
1. 解析 TypeScript 接口声明
|
|
169
|
+
2. 分析类关系和继承
|
|
170
|
+
3. 处理方法签名和参数类型
|
|
171
|
+
4. 处理联合类型和复杂类型表达式
|
|
172
|
+
5. 为每个目标平台生成适当的类型映射
|
|
173
|
+
|
|
174
|
+
### 代码生成约定
|
|
175
|
+
|
|
176
|
+
1. **命名约定**:
|
|
177
|
+
- 属性:camelCase
|
|
178
|
+
- 事件:带 'on' 前缀的 camelCase
|
|
179
|
+
- 方法:camelCase
|
|
180
|
+
- 类:PascalCase
|
|
181
|
+
|
|
182
|
+
2. **类型映射**:
|
|
183
|
+
- TypeScript → Dart:
|
|
184
|
+
- `string` → `String`
|
|
185
|
+
- `number` → `int`/`double`
|
|
186
|
+
- `boolean` → `bool`
|
|
187
|
+
- `any` → `dynamic`
|
|
188
|
+
- `void` → `void`
|
|
189
|
+
|
|
190
|
+
- TypeScript → React/Vue:
|
|
191
|
+
- `string` → `string`
|
|
192
|
+
- `number` → `number`
|
|
193
|
+
- `boolean` → `boolean`
|
|
194
|
+
- `any` → `any`
|
|
195
|
+
- `void` → `void`
|
|
196
|
+
|
|
197
|
+
3. **事件处理**:
|
|
198
|
+
- React:`EventHandler<SyntheticEvent<Element>>`
|
|
199
|
+
- Vue:`Event`/`CustomEvent`
|
|
200
|
+
- Flutter:`EventHandler<Event>`
|
|
201
|
+
|
|
202
|
+
## 项目结构
|
|
203
|
+
|
|
204
|
+
运行命令后,您的项目将具有以下结构:
|
|
205
|
+
|
|
206
|
+
### React 项目
|
|
207
|
+
```
|
|
208
|
+
my-webf-app/
|
|
209
|
+
├── src/
|
|
210
|
+
│ ├── components/
|
|
211
|
+
│ │ ├── button.tsx
|
|
212
|
+
│ │ ├── button.d.ts
|
|
213
|
+
│ │ └── index.ts
|
|
214
|
+
│ ├── utils/
|
|
215
|
+
│ │ └── createComponent.ts
|
|
216
|
+
│ └── index.ts
|
|
217
|
+
├── package.json
|
|
218
|
+
├── tsconfig.json
|
|
219
|
+
└── tsup.config.ts
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Vue 项目
|
|
223
|
+
```
|
|
224
|
+
my-webf-app/
|
|
225
|
+
├── src/
|
|
226
|
+
│ ├── components/
|
|
227
|
+
│ │ ├── button.vue
|
|
228
|
+
│ │ └── button.d.ts
|
|
229
|
+
├── package.json
|
|
230
|
+
└── tsconfig.json
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## 依赖项
|
|
234
|
+
|
|
235
|
+
CLI 会自动为所选框架安装必要的依赖项:
|
|
236
|
+
- React:React 及相关类型定义
|
|
237
|
+
- Vue:Vue 及相关类型定义
|
|
238
|
+
|
|
239
|
+
## 开发
|
|
240
|
+
|
|
241
|
+
### 从源码构建
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
npm install
|
|
245
|
+
npm run build
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### 测试
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
npm test
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## 许可证
|
|
255
|
+
|
|
256
|
+
ISC
|
package/README.md
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# WebF CLI
|
|
2
|
+
|
|
3
|
+
A powerful code generation tool for WebF that creates type-safe bindings between Flutter/Dart and JavaScript frameworks (React, Vue). It analyzes TypeScript definition files and generates corresponding Dart classes and JavaScript/TypeScript components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @openwebf/webf
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Generate Code
|
|
14
|
+
|
|
15
|
+
The `webf codegen` command generates Dart abstract classes and React/Vue components from TypeScript definitions. It automatically creates a project if needed.
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
webf codegen [output-dir] [options]
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
#### Arguments:
|
|
22
|
+
- `output-dir`: Path to output generated files (default: ".")
|
|
23
|
+
|
|
24
|
+
#### Options:
|
|
25
|
+
- `--flutter-package-src <path>`: Flutter package source path containing TypeScript definitions
|
|
26
|
+
- `--framework <framework>`: Target framework - 'react' or 'vue'
|
|
27
|
+
- `--package-name <name>`: Package name for the webf typings
|
|
28
|
+
- `--publish-to-npm`: Automatically publish the generated package to npm
|
|
29
|
+
- `--npm-registry <url>`: Custom npm registry URL (defaults to https://registry.npmjs.org/)
|
|
30
|
+
|
|
31
|
+
#### Examples:
|
|
32
|
+
|
|
33
|
+
**Generate code with auto-creation of project:**
|
|
34
|
+
```bash
|
|
35
|
+
# Generate React components from Flutter package
|
|
36
|
+
webf codegen my-typings --flutter-package-src=../webf_cupertino_ui --framework=react
|
|
37
|
+
|
|
38
|
+
# Generate Vue components with custom package name
|
|
39
|
+
webf codegen my-vue-app --flutter-package-src=./flutter_pkg --framework=vue --package-name=@myorg/webf-vue
|
|
40
|
+
|
|
41
|
+
# Use temporary directory (auto-created)
|
|
42
|
+
webf codegen --flutter-package-src=../webf_cupertino_ui
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**Create a new project without code generation:**
|
|
46
|
+
```bash
|
|
47
|
+
# Create React project structure
|
|
48
|
+
webf codegen my-project --framework=react --package-name=my-webf-react
|
|
49
|
+
|
|
50
|
+
# Create Vue project structure
|
|
51
|
+
webf codegen my-project --framework=vue --package-name=my-webf-vue
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Generate and publish to npm:**
|
|
55
|
+
```bash
|
|
56
|
+
# Publish to default npm registry
|
|
57
|
+
webf codegen my-typings --flutter-package-src=../webf_ui --publish-to-npm
|
|
58
|
+
|
|
59
|
+
# Publish to custom registry
|
|
60
|
+
webf codegen my-typings --flutter-package-src=../webf_ui --publish-to-npm --npm-registry=https://custom.registry.com/
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Interactive Mode
|
|
64
|
+
|
|
65
|
+
If you don't provide all required options, the CLI will prompt you interactively:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
webf codegen my-app
|
|
69
|
+
# CLI will ask:
|
|
70
|
+
# - Which framework would you like to use? (react/vue)
|
|
71
|
+
# - What is your package name?
|
|
72
|
+
# - Would you like to publish this package to npm?
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Key Features
|
|
76
|
+
|
|
77
|
+
### 1. Auto-creation
|
|
78
|
+
The CLI automatically detects if a project needs to be created based on the presence of required files (package.json, global.d.ts, tsconfig.json).
|
|
79
|
+
|
|
80
|
+
### 2. Flutter Package Detection
|
|
81
|
+
When `--flutter-package-src` is not provided, the CLI searches for a Flutter package in the current directory or parent directories by looking for `pubspec.yaml`.
|
|
82
|
+
|
|
83
|
+
### 3. Metadata Synchronization
|
|
84
|
+
The CLI reads version and description from the Flutter package's `pubspec.yaml` and applies them to the generated `package.json`.
|
|
85
|
+
|
|
86
|
+
### 4. Automatic Build
|
|
87
|
+
After code generation, the CLI automatically runs `npm run build` if a build script exists in the package.json.
|
|
88
|
+
|
|
89
|
+
### 5. Framework Detection
|
|
90
|
+
For existing projects, the CLI can auto-detect the framework from package.json dependencies.
|
|
91
|
+
|
|
92
|
+
### 6. TypeScript Environment Validation
|
|
93
|
+
The CLI validates that the Flutter package has proper TypeScript configuration:
|
|
94
|
+
- Checks for `tsconfig.json`
|
|
95
|
+
- Verifies `.d.ts` files exist
|
|
96
|
+
- Offers to create `tsconfig.json` if missing
|
|
97
|
+
|
|
98
|
+
## Generated Project Structure
|
|
99
|
+
|
|
100
|
+
### React Project
|
|
101
|
+
```
|
|
102
|
+
my-webf-app/
|
|
103
|
+
├── src/
|
|
104
|
+
│ ├── components/
|
|
105
|
+
│ │ ├── Button.tsx
|
|
106
|
+
│ │ ├── Input.tsx
|
|
107
|
+
│ │ └── ...
|
|
108
|
+
│ ├── utils/
|
|
109
|
+
│ │ └── createComponent.ts
|
|
110
|
+
│ └── index.ts
|
|
111
|
+
├── package.json
|
|
112
|
+
├── tsconfig.json
|
|
113
|
+
├── tsup.config.ts
|
|
114
|
+
├── global.d.ts
|
|
115
|
+
└── .gitignore
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Vue Project
|
|
119
|
+
```
|
|
120
|
+
my-webf-app/
|
|
121
|
+
├── src/
|
|
122
|
+
│ ├── components/
|
|
123
|
+
│ │ ├── Button.vue
|
|
124
|
+
│ │ ├── Input.vue
|
|
125
|
+
│ │ └── ...
|
|
126
|
+
│ └── index.ts
|
|
127
|
+
├── package.json
|
|
128
|
+
├── tsconfig.json
|
|
129
|
+
├── global.d.ts
|
|
130
|
+
└── .gitignore
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Flutter Package (Generated Dart Files)
|
|
134
|
+
```
|
|
135
|
+
flutter_package/
|
|
136
|
+
├── lib/
|
|
137
|
+
│ ├── src/
|
|
138
|
+
│ │ ├── button_bindings_generated.dart
|
|
139
|
+
│ │ ├── input_bindings_generated.dart
|
|
140
|
+
│ │ └── ...
|
|
141
|
+
│ └── widgets.dart
|
|
142
|
+
└── pubspec.yaml
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Type System
|
|
146
|
+
|
|
147
|
+
The CLI handles comprehensive type mapping between TypeScript and Dart:
|
|
148
|
+
|
|
149
|
+
### Basic Types
|
|
150
|
+
- `string` → `String` (Dart) / `string` (JS)
|
|
151
|
+
- `number` → `int`/`double` (Dart) / `number` (JS)
|
|
152
|
+
- `boolean` → `bool` (Dart) / `boolean` (JS)
|
|
153
|
+
- `any` → `dynamic` (Dart) / `any` (JS)
|
|
154
|
+
- `void` → `void`
|
|
155
|
+
- `null` → `null`
|
|
156
|
+
- `undefined` → handled specially
|
|
157
|
+
|
|
158
|
+
### Complex Types
|
|
159
|
+
- Arrays: `Type[]` → `List<Type>` (Dart)
|
|
160
|
+
- Functions: Proper signature conversion
|
|
161
|
+
- Promises: `Promise<Type>` → `Future<Type>` (Dart)
|
|
162
|
+
- Union types: Handled with appropriate conversions
|
|
163
|
+
- Custom types: Preserved with proper imports
|
|
164
|
+
|
|
165
|
+
### Attribute Type Conversion
|
|
166
|
+
HTML attributes are always strings, so the generator includes automatic type conversion:
|
|
167
|
+
- Boolean: `value == 'true' || value == ''`
|
|
168
|
+
- Integer: `int.tryParse(value) ?? 0`
|
|
169
|
+
- Double: `double.tryParse(value) ?? 0.0`
|
|
170
|
+
- String: Direct assignment
|
|
171
|
+
|
|
172
|
+
## Performance Optimizations
|
|
173
|
+
|
|
174
|
+
The CLI implements several performance optimizations:
|
|
175
|
+
|
|
176
|
+
### Multi-level Caching
|
|
177
|
+
1. **Source File Cache**: Parsed TypeScript AST files
|
|
178
|
+
2. **Type Conversion Cache**: Frequently used type conversions
|
|
179
|
+
3. **File Content Cache**: Detect changes efficiently
|
|
180
|
+
|
|
181
|
+
### Parallel Processing
|
|
182
|
+
Files are processed in batches for optimal performance, maximizing CPU utilization.
|
|
183
|
+
|
|
184
|
+
## Development
|
|
185
|
+
|
|
186
|
+
### Prerequisites
|
|
187
|
+
- Node.js >= 14
|
|
188
|
+
- npm or yarn
|
|
189
|
+
|
|
190
|
+
### Building from Source
|
|
191
|
+
```bash
|
|
192
|
+
git clone https://github.com/openwebf/webf
|
|
193
|
+
cd webf/cli
|
|
194
|
+
npm install
|
|
195
|
+
npm run build
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Running Tests
|
|
199
|
+
```bash
|
|
200
|
+
npm test # Run all tests
|
|
201
|
+
npm test -- --coverage # Run with coverage
|
|
202
|
+
npm test -- analyzer.test # Run specific test file
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Development Workflow
|
|
206
|
+
```bash
|
|
207
|
+
# Make changes to source files
|
|
208
|
+
npm run build # Compile TypeScript
|
|
209
|
+
npm test # Run tests
|
|
210
|
+
npm link # Link for local testing
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## Troubleshooting
|
|
214
|
+
|
|
215
|
+
### Common Issues
|
|
216
|
+
|
|
217
|
+
**Missing TypeScript definitions:**
|
|
218
|
+
- Ensure your Flutter package has `.d.ts` files in the `lib/` directory
|
|
219
|
+
- Create a `tsconfig.json` in your Flutter package root
|
|
220
|
+
|
|
221
|
+
**Build failures:**
|
|
222
|
+
- Check that all dependencies are installed: `npm install`
|
|
223
|
+
- Verify TypeScript compilation: `npm run build`
|
|
224
|
+
|
|
225
|
+
**Publishing errors:**
|
|
226
|
+
- Ensure you're logged in to npm: `npm login`
|
|
227
|
+
- Verify package name availability
|
|
228
|
+
- Check registry URL if using custom registry
|
|
229
|
+
|
|
230
|
+
## License
|
|
231
|
+
|
|
232
|
+
ISC
|