handleWriteFile method

Future<void> handleWriteFile(
  1. String fullName,
  2. String name
)

Implementation

Future<void> handleWriteFile(String fullName, String name) async {
  try {
    // 创建 controller 文件
    String controllerContent = pageCategory == '多选'
        ? TemplateAppMutilSelect.templateController(name, selectModule)
        : TemplateAppDefault.templateController(name, selectModule);

    await File(path.join(fullName, 'controller.dart')).writeAsString(controllerContent);
    print('创建 $name > Controller 文件成功');

    // 创建 index 文件
    String indexContent = pageCategory == '多选'
        ? TemplateAppMutilSelect.templateIndex(name, selectModule)
        : TemplateAppDefault.templateIndex(name, selectModule);

    await File(path.join(fullName, 'index.dart')).writeAsString(indexContent);
    print('创建 $name > Index 文件成功');

    // 创建 view 文件
    String viewContent =
        pageCategory == '多选' ? TemplateAppMutilSelect.templateView(name) : TemplateAppDefault.templateView(name);

    await File(path.join(fullName, 'view.dart')).writeAsString(viewContent);
    print('创建 $name > View 文件成功');

    // 创建 API 文件
    String apiDir = path.join('./lib/apis', selectModule);
    await Directory(apiDir).create(recursive: true);

    String apiContent = pageCategory == '多选'
        ? TemplateAppMutilSelect.templateApi(name, selectModule)
        : TemplateAppDefault.templateApi(name, selectModule);

    await File(path.join(apiDir, '${_snakeCase(name)}.dart')).writeAsString(apiContent);
    print('创建 $name > API 文件成功');

    // 更新路由
    await updateRoutes(name);
  } catch (error) {
    print('写入文件失败: $error');
  }
}