create method

Stream<ServerUniverseApiStatus> create({
  1. required String newName,
  2. required Directory directoryBase,
})

Server Universe

Implementation

Stream<ServerUniverseApiStatus> create({
  required String newName,
  required Directory directoryBase,
}) async* {
  Directory directory_project = await Future(() async {
    return Directory(Directory(path.join(directoryBase.uri.toFilePath(), newName.trim())).uri.toFilePath());
  });
  String project_name = path.basename(directory_project.path);

  File file_pubspec = File(path.join(directory_project.path, "pubspec.yaml"));
  if (!file_pubspec.existsSync()) {
    yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.info, value: "Start Create Project: ${newName}");

    Process process = await Process.start(
      "dart",
      [
        "create",
        newName,
        "--no-pub",
        "--force",
      ],
      workingDirectory: directory_project.parent.uri.toFilePath(),
    );
    process.stderr.listen((event) {
      stderr.add(event);
    });
    process.stdout.listen((event) {
      stdout.add(event);
    });
    int exit_code = await (process.exitCode);
    if (exit_code != 0) {
      yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.failed, value: "Failed Create Project: ${newName}");
      return;
    } else {
      yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.succes, value: "Succes Create Project: ${newName}");
    }
  }
  Map yaml_code = (yaml.loadYaml(file_pubspec.readAsStringSync(), recover: true) as Map);
  PubspecServerUniverse pubspec_server_universe = PubspecServerUniverse(yaml_code.clone());

  File file_guide = File(path.join(directory_project.path, "guide-server-universe.md"));
  if (file_guide.existsSync()) {
    yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.info, value: "Rewrite: ${path.basename(file_guide.path)}");
  } else {
    yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.info, value: "Create: ${path.basename(file_guide.path)}");
  }

  await file_guide.writeAsString(guide_server_universe_markdown(
    name_project: project_name,
  ));
  // supabase file script
  File file_script_native = File(path.join(directory_project.path, "bin", "${project_name}_native.dart"));
  if (file_script_native.parent.existsSync() == false) {
    file_script_native.parent.createSync(recursive: true);
  }
  if (!file_script_native.existsSync()) {
    yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.info, value: "Create: ${path.basename(file_script_native.path)}");

    await file_script_native.writeAsString(script_server_universe_native());
  }

  // supabase file script
  File file_script_supabase = File(path.join(directory_project.path, "bin", "${project_name}_supabase.dart"));

  if (!file_script_supabase.existsSync()) {
    yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.info, value: "Create: ${path.basename(file_script_supabase.path)}");
    await file_script_supabase.writeAsString(script_server_universe_supabase());
  }

  // supabase directory deploy
  Directory directory_script_supabase = Directory(path.join(directory_project.path, "supabase", "functions", project_name));

  // default configuration pubspec
  PubspecServerUniverse pubspec_server_default = PubspecServerUniverse.create(
    repository: "https://github.com/azkadev/server_universe",
    homepage: "https://github.com/azkadev/server_universe",
    issue_tracker: "https://github.com/azkadev/server_universe/issues",
    documentation: "https://github.com/azkadev/server_universe/tree/main/docs",
    funding: [
      "https://github.com/sponsors/azkadev",
    ],
    dependencies: PubspecServerUniverseDependencies({
      "server_universe": "any",
      "general_lib": "^0.0.43",
    }),
    server_universe: PubspecServerUniverseConfiguration.create(
      supabase: PubspecServerUniverseConfigurationSupabase.create(
        input_file: path.relative(
          file_script_supabase.path,
          from: directory_project.path,
        ),
        output_directory: path.relative(
          directory_script_supabase.path,
          from: directory_project.path,
        ),
      ),
    ),
  );

  // update pubspec default
  yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.info, value: "Start Update Pubspec: ${path.basename(file_pubspec.path)}");

  pubspec_server_universe.rawData.general_lib_utils_updateMapIfNotSameOrEmptyOrNull(
    data: pubspec_server_default.toJson(),
    ignoreKeys: [
      "@type",
    ],
  );
  pubspec_server_universe.rawData.general_lib_utils_removeRecursiveByKeys(
    keyDatas: [
      "@type",
    ],
  );
  yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.succes, value: "Succes Update Pubspec: ${path.basename(file_pubspec.path)}");
  yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.info, value: "Start Save Pubspec: ${path.basename(file_pubspec.path)}");

  String yaml_documents_new = YamlWriter().write(pubspec_server_universe.toJson());

  await file_pubspec.writeAsString(yaml_documents_new);
  yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.succes, value: "Succes Save Pubspec: ${path.basename(file_pubspec.path)}");
  yield ServerUniverseApiStatus(serverUniverseApiStatusType: ServerUniverseApiStatusType.succes, value: "Finished Create Project: ${newName}");

  // finished update pubspec
}