flu-cli 2.0.0 → 2.0.2
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/CLI.md +226 -63
- package/README.md +56 -29
- package/index.js +61 -12
- package/lib/commands/add.js +177 -54
- package/lib/commands/completion.js +0 -2
- package/lib/commands/config.js +70 -0
- package/lib/commands/newClack.js +9 -6
- package/lib/commands/template.js +113 -0
- package/lib/utils/templateSelectorEnquirer.js +13 -10
- package/package.json +5 -3
- package/templates/snippets/dart.code-snippets +168 -263
- package/lib/commands/generate.js +0 -26
- package/lib/generators/component_generator.js +0 -93
- package/lib/generators/module_generator.js +0 -141
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Component 生成器
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
|
6
|
-
import { join } from 'path';
|
|
7
|
-
import { logger } from '../utils/logger.js';
|
|
8
|
-
import { toPascalCase, toSnakeCase } from '../utils/string_helper.js';
|
|
9
|
-
import { updateIndexFile } from '../utils/index_updater.js';
|
|
10
|
-
import { getSnippetContent } from '../utils/snippet_loader.js';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 生成 Component 文件
|
|
14
|
-
*/
|
|
15
|
-
export function generateComponent (name, options = {}) {
|
|
16
|
-
try {
|
|
17
|
-
const {
|
|
18
|
-
feature = null,
|
|
19
|
-
outputDir = process.cwd()
|
|
20
|
-
} = options;
|
|
21
|
-
|
|
22
|
-
// 转换命名
|
|
23
|
-
const namePascal = toPascalCase(name);
|
|
24
|
-
const nameSnake = toSnakeCase(name);
|
|
25
|
-
|
|
26
|
-
// 确定输出路径
|
|
27
|
-
let componentsDir;
|
|
28
|
-
if (feature) {
|
|
29
|
-
// Modular/Clean 架构
|
|
30
|
-
componentsDir = join(outputDir, 'lib', 'features', feature, 'components');
|
|
31
|
-
} else {
|
|
32
|
-
// Lite 架构
|
|
33
|
-
componentsDir = join(outputDir, 'lib', 'components');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// 创建目录
|
|
37
|
-
if (!existsSync(componentsDir)) {
|
|
38
|
-
mkdirSync(componentsDir, { recursive: true });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// 生成文件内容(优先片段)
|
|
42
|
-
const content = getSnippetContent(outputDir, 'flu.component', { Name: namePascal }) || generateComponentContent(namePascal);
|
|
43
|
-
|
|
44
|
-
// 写入文件
|
|
45
|
-
const filePath = join(componentsDir, `${nameSnake}_component.dart`);
|
|
46
|
-
if (existsSync(filePath)) {
|
|
47
|
-
logger.error(`文件已存在: ${filePath}`);
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
writeFileSync(filePath, content, 'utf8');
|
|
52
|
-
logger.success(`Component 创建成功: ${filePath}`);
|
|
53
|
-
|
|
54
|
-
// 更新 index.dart
|
|
55
|
-
updateIndexFile(componentsDir, `${nameSnake}_component.dart`);
|
|
56
|
-
|
|
57
|
-
return true;
|
|
58
|
-
|
|
59
|
-
} catch (error) {
|
|
60
|
-
logger.error(`生成 Component 失败: ${error.message}`);
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 生成 Component 内容
|
|
67
|
-
*/
|
|
68
|
-
function generateComponentContent (namePascal) {
|
|
69
|
-
return `import 'package:flutter/material.dart';
|
|
70
|
-
|
|
71
|
-
class ${namePascal}Component extends StatelessWidget {
|
|
72
|
-
const ${namePascal}Component({super.key});
|
|
73
|
-
|
|
74
|
-
@override
|
|
75
|
-
Widget build(BuildContext context) {
|
|
76
|
-
return Container(
|
|
77
|
-
padding: const EdgeInsets.all(16),
|
|
78
|
-
child: Column(
|
|
79
|
-
crossAxisAlignment: CrossAxisAlignment.start,
|
|
80
|
-
children: [
|
|
81
|
-
Text(
|
|
82
|
-
'${namePascal}Component',
|
|
83
|
-
style: Theme.of(context).textTheme.titleLarge,
|
|
84
|
-
),
|
|
85
|
-
const SizedBox(height: 8),
|
|
86
|
-
const Text('Component content goes here'),
|
|
87
|
-
],
|
|
88
|
-
),
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
`;
|
|
93
|
-
}
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 模块生成器
|
|
3
|
-
* 用于创建完整的功能模块结构
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
|
7
|
-
import { join } from 'path';
|
|
8
|
-
import { logger } from '../utils/logger.js';
|
|
9
|
-
import { toSnakeCase } from '../utils/string_helper.js';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 生成模块
|
|
13
|
-
*/
|
|
14
|
-
export function generateModule (name, options = {}) {
|
|
15
|
-
try {
|
|
16
|
-
const {
|
|
17
|
-
outputDir = process.cwd()
|
|
18
|
-
} = options;
|
|
19
|
-
|
|
20
|
-
// 转换命名
|
|
21
|
-
const nameSnake = toSnakeCase(name);
|
|
22
|
-
|
|
23
|
-
// 确定输出路径
|
|
24
|
-
const moduleDir = join(outputDir, 'lib', 'features', nameSnake);
|
|
25
|
-
|
|
26
|
-
// 检查模块是否已存在
|
|
27
|
-
if (existsSync(moduleDir)) {
|
|
28
|
-
logger.error(`模块已存在: ${moduleDir}`);
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// 创建模块目录结构
|
|
33
|
-
const directories = [
|
|
34
|
-
'pages',
|
|
35
|
-
'viewmodels',
|
|
36
|
-
'widgets',
|
|
37
|
-
'services',
|
|
38
|
-
'models'
|
|
39
|
-
];
|
|
40
|
-
|
|
41
|
-
logger.info(`创建模块: ${nameSnake}`);
|
|
42
|
-
|
|
43
|
-
// 创建所有目录
|
|
44
|
-
for (const dir of directories) {
|
|
45
|
-
const dirPath = join(moduleDir, dir);
|
|
46
|
-
mkdirSync(dirPath, { recursive: true });
|
|
47
|
-
|
|
48
|
-
// 创建 index.dart 文件
|
|
49
|
-
const indexPath = join(dirPath, 'index.dart');
|
|
50
|
-
const indexContent = generateIndexContent(dir, nameSnake);
|
|
51
|
-
writeFileSync(indexPath, indexContent, 'utf8');
|
|
52
|
-
|
|
53
|
-
logger.success(`创建目录: ${dir}/`);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// 创建模块根目录的 index.dart
|
|
57
|
-
const moduleIndexPath = join(moduleDir, 'index.dart');
|
|
58
|
-
const moduleIndexContent = generateModuleIndexContent(nameSnake);
|
|
59
|
-
writeFileSync(moduleIndexPath, moduleIndexContent, 'utf8');
|
|
60
|
-
|
|
61
|
-
logger.newLine();
|
|
62
|
-
logger.success(`✅ 模块创建成功: features/${nameSnake}/`);
|
|
63
|
-
logger.newLine();
|
|
64
|
-
|
|
65
|
-
// 显示模块结构
|
|
66
|
-
logger.info('模块结构:');
|
|
67
|
-
console.log(` features/${nameSnake}/`);
|
|
68
|
-
console.log(` ├── pages/`);
|
|
69
|
-
console.log(` │ └── index.dart`);
|
|
70
|
-
console.log(` ├── viewmodels/`);
|
|
71
|
-
console.log(` │ └── index.dart`);
|
|
72
|
-
console.log(` ├── widgets/`);
|
|
73
|
-
console.log(` │ └── index.dart`);
|
|
74
|
-
console.log(` ├── services/`);
|
|
75
|
-
console.log(` │ └── index.dart`);
|
|
76
|
-
console.log(` ├── models/`);
|
|
77
|
-
console.log(` │ └── index.dart`);
|
|
78
|
-
console.log(` └── index.dart`);
|
|
79
|
-
|
|
80
|
-
logger.newLine();
|
|
81
|
-
logger.info('下一步:');
|
|
82
|
-
console.log(` 1. 使用 "flu-cli add page <name> -f ${nameSnake}" 添加页面`);
|
|
83
|
-
console.log(` 2. 使用 "flu-cli add service <name> -f ${nameSnake}" 添加服务`);
|
|
84
|
-
console.log(` 3. 使用 "flu-cli add model <name> -f ${nameSnake}" 添加模型`);
|
|
85
|
-
|
|
86
|
-
return true;
|
|
87
|
-
|
|
88
|
-
} catch (error) {
|
|
89
|
-
logger.error(`生成模块失败: ${error.message}`);
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 生成目录的 index.dart 内容
|
|
96
|
-
*/
|
|
97
|
-
function generateIndexContent (dirName, moduleName) {
|
|
98
|
-
const comments = {
|
|
99
|
-
pages: '// 导出所有页面',
|
|
100
|
-
viewmodels: '// 导出所有 ViewModel',
|
|
101
|
-
widgets: '// 导出所有 Widget',
|
|
102
|
-
services: '// 导出所有 Service',
|
|
103
|
-
models: '// 导出所有 Model'
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
const examples = {
|
|
107
|
-
pages: `// export 'home_page.dart';`,
|
|
108
|
-
viewmodels: `// export 'home_viewmodel.dart';`,
|
|
109
|
-
widgets: `// export 'custom_widget.dart';`,
|
|
110
|
-
services: `// export 'api_service.dart';`,
|
|
111
|
-
models: `// export 'user_model.dart';`
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
return `${comments[dirName] || '// 导出文件'}
|
|
115
|
-
|
|
116
|
-
${examples[dirName] || '// export \'example.dart\';'}
|
|
117
|
-
`;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 生成模块根目录的 index.dart 内容
|
|
122
|
-
*/
|
|
123
|
-
function generateModuleIndexContent (moduleName) {
|
|
124
|
-
return `// ${moduleName} 模块导出
|
|
125
|
-
|
|
126
|
-
// 导出页面
|
|
127
|
-
export 'pages/index.dart';
|
|
128
|
-
|
|
129
|
-
// 导出 ViewModel
|
|
130
|
-
export 'viewmodels/index.dart';
|
|
131
|
-
|
|
132
|
-
// 导出 Widget
|
|
133
|
-
export 'widgets/index.dart';
|
|
134
|
-
|
|
135
|
-
// 导出 Service
|
|
136
|
-
export 'services/index.dart';
|
|
137
|
-
|
|
138
|
-
// 导出 Model
|
|
139
|
-
export 'models/index.dart';
|
|
140
|
-
`;
|
|
141
|
-
}
|