forklaunch 1.20.1

Launch faster with forklaunch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
use std::{collections::HashSet, io::Write, path::Path};

use anyhow::{Context, Result};
use clap::{Arg, ArgAction, Command};
use convert_case::{Case, Casing};
use dialoguer::{MultiSelect, theme::ColorfulTheme};
use ramhorns::Template;
use rustyline::{Editor, history::DefaultHistory};
use termcolor::{ColorChoice, StandardStream, WriteColor};

use super::core::{
    change_database::{
        change_database_base_entity, change_database_env_variables,
        change_database_postinstall_script,
    },
    change_description::change_description as change_description_core,
    change_name::change_name as change_name_core,
};
use crate::{
    CliCommand,
    change::core::change_database::{change_database_retention_script, change_database_seed_script},
    constants::{
        Database, ERROR_FAILED_TO_PARSE_MANIFEST, ERROR_FAILED_TO_READ_DOCKER_COMPOSE,
        ERROR_FAILED_TO_READ_MANIFEST, ERROR_FAILED_TO_READ_PACKAGE_JSON, Infrastructure,
        WorkerType,
    },
    core::{
        ast::transformations::{
            transform_mikroorm_config_ts::transform_mikroorm_config_ts,
            transform_registrations_ts::transform_registrations_ts_worker_type,
            transform_test_utils_ts::{
                transform_test_utils_add_database, transform_test_utils_add_infrastructure,
                transform_test_utils_remove_database, transform_test_utils_remove_infrastructure,
            },
            transform_worker_to_service::transform_registrations_ts_worker_to_service,
        },
        base_path::{RequiredLocation, find_app_root_path, prompt_base_path},
        command::command,
        database::{get_database_variants, get_db_driver, is_in_memory_database},
        docker::{
            DockerCompose, add_database_to_docker_compose, add_kafka_to_docker_compose,
            add_redis_to_docker_compose, clean_up_unused_infrastructure_services,
            remove_service_from_docker_compose,
        },
        env::read_env_local_or_default,
        format::format_code,
        manifest::{
            InitializableManifestConfig, InitializableManifestConfigMetadata, ManifestData,
            MutableManifestData, ProjectInitializationMetadata, ProjectType,
            worker::WorkerManifestData,
        },
        move_template::{MoveTemplate, move_template_files},
        name::validate_name,
        package_json::{
            application_package_json::ApplicationPackageJson,
            package_json_constants::{
                BULLMQ_VERSION, INFRASTRUCTURE_REDIS_VERSION, IOREDIS_VERSION,
                MIKRO_ORM_CORE_VERSION, MIKRO_ORM_DATABASE_VERSION, MIKRO_ORM_MIGRATIONS_VERSION,
                WORKER_BULLMQ_VERSION, WORKER_DATABASE_VERSION,
                WORKER_KAFKA_VERSION, WORKER_REDIS_VERSION,
            },
            project_package_json::ProjectPackageJson,
        },
        removal_template::{RemovalTemplate, remove_template_files},
        rendered_template::{
            RenderedTemplate, RenderedTemplatesCache, TEMPLATES_DIR, write_rendered_templates,
        },
    },
    prompt::{ArrayCompleter, prompt_field_from_selections_with_validation},
};

#[derive(Debug)]
pub(super) struct WorkerCommand;

impl WorkerCommand {
    pub(super) fn new() -> Self {
        Self {}
    }
}

fn change_name(
    base_path: &Path,
    name: &str,
    confirm: bool,
    manifest_data: &mut WorkerManifestData,
    application_package_json: &mut ApplicationPackageJson,
    project_package_json: &mut ProjectPackageJson,
    docker_compose: &mut DockerCompose,
    rendered_templates_cache: &mut RenderedTemplatesCache,
    removal_templates: &mut Vec<RemovalTemplate>,
    stdout: &mut StandardStream,
) -> Result<MoveTemplate> {
    change_name_core(
        base_path,
        name,
        confirm,
        MutableManifestData::Worker(manifest_data),
        application_package_json,
        project_package_json,
        Some(docker_compose),
        rendered_templates_cache,
        removal_templates,
        stdout,
    )
}

fn change_description(
    description: &str,
    manifest_data: &mut WorkerManifestData,
    project_package_json: &mut ProjectPackageJson,
) -> Result<()> {
    change_description_core(
        description,
        MutableManifestData::Worker(manifest_data),
        project_package_json,
    )
}

fn change_type(
    base_path: &Path,
    r#type: &WorkerType,
    database: Option<Database>,
    manifest_data: &mut WorkerManifestData,
    application_package_json: &mut ApplicationPackageJson,
    project_package_json: &mut ProjectPackageJson,
    docker_compose_data: &mut DockerCompose,
    rendered_templates_cache: &mut RenderedTemplatesCache,
    removal_templates: &mut Vec<RemovalTemplate>,
) -> Result<()> {
    let next_redis_partition = crate::core::manifest::next_available_redis_partition(&manifest_data.projects);

    let project_entry = manifest_data
        .projects
        .iter_mut()
        .find(|project| project.name == manifest_data.worker_name)
        .unwrap();

    let existing_type = manifest_data.worker_type.parse::<WorkerType>()?;

    let existing_database = manifest_data
        .database
        .as_ref()
        .map(|db| db.parse::<Database>().unwrap());

    project_entry.metadata.as_mut().unwrap().r#type = Some(r#type.to_string());
    let resources = project_entry.resources.as_mut().unwrap();

    resources.database = None;
    resources.cache = None;
    resources.queue = None;

    rendered_templates_cache.insert(
        base_path.join("registrations.ts").to_string_lossy(),
        RenderedTemplate {
            path: base_path.join("registrations.ts").into(),
            content: transform_registrations_ts_worker_type(
                rendered_templates_cache,
                base_path,
                &manifest_data.app_name,
                &manifest_data.worker_name.to_case(Case::Pascal),
                &existing_type,
                r#type,
            )?,
            context: None,
        },
    );

    let dependencies = project_package_json.dependencies.as_mut().unwrap();

    dependencies.forklaunch_implementation_worker_bullmq = None;
    dependencies.forklaunch_implementation_worker_database = None;
    dependencies.forklaunch_implementation_worker_redis = None;
    dependencies.forklaunch_implementation_worker_kafka = None;

    let mut environment = docker_compose_data
        .services
        .get_mut(&format!("{}-worker", &manifest_data.worker_name))
        .unwrap()
        .environment
        .as_ref()
        .unwrap()
        .clone();

    let env_local_path = base_path.join(".env.local");
    let mut env_local_content =
        read_env_local_or_default(rendered_templates_cache, &env_local_path)?;

    env_local_content.db_name = None;
    env_local_content.db_host = None;
    env_local_content.db_user = None;
    env_local_content.db_password = None;
    env_local_content.db_port = None;
    env_local_content.redis_url = None;
    env_local_content.kafka_brokers = None;
    env_local_content.kafka_client_id = None;
    env_local_content.kafka_group_id = None;

    match r#type {
        WorkerType::BullMQCache => {
            dependencies.bullmq = Some(BULLMQ_VERSION.to_string());
            dependencies.forklaunch_implementation_worker_bullmq =
                Some(WORKER_BULLMQ_VERSION.to_string());
            dependencies.forklaunch_infrastructure_redis =
                Some(INFRASTRUCTURE_REDIS_VERSION.to_string());
            project_package_json
                .dependencies
                .as_mut()
                .unwrap()
                .ioredis = Some(IOREDIS_VERSION.to_string());
            resources.cache = Some(WorkerType::RedisCache.to_string());
            resources.redis_partition = Some(next_redis_partition);
            let _ = add_redis_to_docker_compose(
                &manifest_data.app_name,
                docker_compose_data,
                &mut environment,
                next_redis_partition,
            );
            env_local_content.redis_url = Some(format!("redis://localhost:6379/{}", next_redis_partition));
        }
        WorkerType::Database => {
            let db = database.unwrap();

            dependencies.databases = HashSet::from([db.clone()]);
            dependencies.mikro_orm_core = Some(MIKRO_ORM_CORE_VERSION.to_string());
            dependencies.mikro_orm_migrations = Some(MIKRO_ORM_MIGRATIONS_VERSION.to_string());
            dependencies.mikro_orm_database = Some(MIKRO_ORM_DATABASE_VERSION.to_string());
            dependencies.forklaunch_implementation_worker_database =
                Some(WORKER_DATABASE_VERSION.to_string());

            resources.database = Some(db.to_string());

            manifest_data.database = Some(database.unwrap().to_string());
            manifest_data.db_driver = Some(get_db_driver(&db));
            manifest_data.is_mongo = db == Database::MongoDB;
            manifest_data.is_in_memory_database = is_in_memory_database(&db);

            let _ = add_database_to_docker_compose(
                &ManifestData::Worker(&manifest_data),
                docker_compose_data,
                &mut environment,
            );

            if let Some(existing_database) = existing_database {
                let removal_template = change_database_base_entity(
                    base_path,
                    &db,
                    &existing_database,
                    manifest_data.projects.clone(),
                    &manifest_data.app_name,
                    rendered_templates_cache,
                )?;

                if let Some(removal_template) = removal_template {
                    removal_templates.push(removal_template);
                }
            } else {
                let database_entity = match db {
                    Database::MongoDB => "nosql.base.properties.ts",
                    _ => "sql.base.properties.ts",
                };
                let entity_template_path = TEMPLATES_DIR
                    .get_file(
                        Path::new("project")
                            .join("core")
                            .join("persistence")
                            .join(database_entity),
                    )
                    .unwrap();
                let base_entity_ts_content =
                    Template::new(entity_template_path.contents_utf8().unwrap())?
                        .render(&manifest_data.clone());
                rendered_templates_cache.insert(
                    database_entity,
                    RenderedTemplate {
                        path: base_path
                            .parent()
                            .unwrap()
                            .join("core")
                            .join("persistence")
                            .join(database_entity),
                        content: base_entity_ts_content,
                        context: None,
                    },
                );
            }

            change_database_env_variables(
                &mut env_local_content,
                &manifest_data.app_name,
                &manifest_data.worker_name,
                &db,
            );

            change_database_postinstall_script(application_package_json, &db);
            change_database_seed_script(project_package_json, &db);
            change_database_retention_script(
                project_package_json,
                &manifest_data.runtime.parse()?,
                true,
            );

            rendered_templates_cache.insert(
                base_path.join("mikro-orm.config.ts").to_string_lossy(),
                RenderedTemplate {
                    path: base_path.join("mikro-orm.config.ts").into(),
                    content: if !base_path.join("mikro-orm.config.ts").exists() {
                        Template::new(
                            TEMPLATES_DIR
                                .get_file(
                                    Path::new("project")
                                        .join("worker")
                                        .join("mikro-orm.config.ts"),
                                )
                                .unwrap()
                                .contents_utf8()
                                .unwrap(),
                        )?
                        .render(&manifest_data.clone())
                    } else {
                        transform_mikroorm_config_ts(
                            rendered_templates_cache,
                            base_path,
                            &existing_database,
                            &db,
                        )?
                    },
                    context: None,
                },
            );
        }
        WorkerType::RedisCache => {
            dependencies.forklaunch_implementation_worker_redis =
                Some(WORKER_REDIS_VERSION.to_string());
            dependencies.forklaunch_infrastructure_redis =
                Some(INFRASTRUCTURE_REDIS_VERSION.to_string());
            project_package_json
                .dependencies
                .as_mut()
                .unwrap()
                .ioredis = Some(IOREDIS_VERSION.to_string());
            resources.cache = Some(WorkerType::RedisCache.to_string());
            resources.redis_partition = Some(next_redis_partition);
            let _ = add_redis_to_docker_compose(
                &manifest_data.app_name,
                docker_compose_data,
                &mut environment,
                next_redis_partition,
            );
            env_local_content.redis_url = Some(format!("redis://localhost:6379/{}", next_redis_partition));
        }
        WorkerType::Kafka => {
            dependencies.forklaunch_implementation_worker_kafka =
                Some(WORKER_KAFKA_VERSION.to_string());
            resources.queue = Some(WorkerType::Kafka.to_string());
            let _ = add_kafka_to_docker_compose(
                &manifest_data.app_name,
                &manifest_data.worker_name,
                docker_compose_data,
                &mut environment,
            );
            env_local_content.kafka_brokers = Some("localhost:9092".to_string());
            env_local_content.kafka_client_id = Some(format!(
                "{}-{}-client",
                manifest_data.app_name, manifest_data.worker_name
            ));
            env_local_content.kafka_group_id = Some(format!(
                "{}-{}-group",
                manifest_data.app_name, manifest_data.worker_name
            ));
        }
    }

    rendered_templates_cache.insert(
        env_local_path.clone().to_string_lossy(),
        RenderedTemplate {
            path: env_local_path,
            content: serde_envfile::to_string(&env_local_content)?,
            context: None,
        },
    );

    let worker_service_name = format!("{}-worker", &manifest_data.worker_name);
    if let Some(worker_service) = docker_compose_data.services.get_mut(&worker_service_name) {
        worker_service.environment = Some(environment.clone());
    }

    // The worker's HTTP server service is usually "{name}-server", but apps
    // scaffolded by older CLI versions name it with the bare project name (or
    // "-service"). Resolve whichever key actually exists rather than assuming
    // "-server" and panicking on the unwrap when it is absent.
    let server_service_name = ["-server", "", "-service"]
        .iter()
        .map(|suffix| format!("{}{}", &manifest_data.worker_name, suffix))
        .find(|name| docker_compose_data.services.contains_key(name));
    if let Some(server_service) =
        server_service_name.and_then(|name| docker_compose_data.services.get_mut(&name))
    {
        server_service.environment = Some(environment.clone());
    }

    let _ = clean_up_unused_infrastructure_services(
        docker_compose_data,
        manifest_data.projects.clone(),
    );

    let test_utils_path = base_path.join("__test__").join("test-utils.ts");
    if test_utils_path.exists() {
        let _ = transform_test_utils_remove_database(rendered_templates_cache, &base_path);
        let _ = transform_test_utils_remove_infrastructure(
            rendered_templates_cache,
            &base_path,
            &Infrastructure::Redis,
        );

        let test_content = match r#type {
            WorkerType::Database => {
                let db = database.unwrap();
                let content =
                    transform_test_utils_add_database(rendered_templates_cache, &base_path, &db)?;
                use crate::core::ast::transformations::transform_test_utils_ts::transform_test_utils_remove_kafka;
                transform_test_utils_remove_kafka(rendered_templates_cache, &base_path)
                    .unwrap_or(content)
            }
            WorkerType::RedisCache | WorkerType::BullMQCache => {
                let content = transform_test_utils_add_infrastructure(
                    rendered_templates_cache,
                    &base_path,
                    &Infrastructure::Redis,
                )?;
                use crate::core::ast::transformations::transform_test_utils_ts::transform_test_utils_remove_kafka;
                transform_test_utils_remove_kafka(rendered_templates_cache, &base_path)
                    .unwrap_or(content)
            }
            WorkerType::Kafka => {
                use crate::core::ast::transformations::transform_test_utils_ts::transform_test_utils_add_kafka;
                transform_test_utils_add_kafka(rendered_templates_cache, &base_path)?
            }
        };

        rendered_templates_cache.insert(
            test_utils_path.to_string_lossy(),
            RenderedTemplate {
                path: test_utils_path.clone(),
                content: test_content,
                context: None,
            },
        );
    }

    // Update worker.ts import source based on new worker type
    let worker_ts_path = base_path.join("worker.ts");
    if let Some(template) = rendered_templates_cache.get(&worker_ts_path)? {
        let pascal_case_name = manifest_data.worker_name.to_case(Case::Pascal);
        let camel_case_name = manifest_data.worker_name.to_case(Case::Camel);
        let is_database_worker = *r#type == WorkerType::Database;
        let mut content = template.content.clone();

        if is_database_worker {
            // Switch import from types file to entities
            content = content.replace(
                &format!("import type {{ {}EventRecord }} from './domain/types/{}EventRecord.types';", pascal_case_name, camel_case_name),
                &format!("import type {{ {}EventRecord }} from './persistence/entities';", pascal_case_name),
            );
        } else {
            // Switch import from entities to types file
            content = content.replace(
                &format!("import type {{ {}EventRecord }} from './persistence/entities';", pascal_case_name),
                &format!("import type {{ {}EventRecord }} from './domain/types/{}EventRecord.types';", pascal_case_name, camel_case_name),
            );
        }

        rendered_templates_cache.insert(
            worker_ts_path.to_string_lossy().into_owned(),
            RenderedTemplate {
                path: worker_ts_path.clone(),
                content,
                context: None,
            },
        );
    }

    Ok(())
}

fn worker_to_service(
    base_path: &Path,
    manifest_data: &mut WorkerManifestData,
    project_package_json: &mut ProjectPackageJson,
    docker_compose: &mut DockerCompose,
    rendered_templates_cache: &mut RenderedTemplatesCache,
    stdout: &mut StandardStream,
) -> Result<()> {
    let project_name = base_path.file_name().unwrap().to_string_lossy().to_string();

    let registrations_path = base_path.join("registrations.ts");
    let registrations_key = registrations_path.to_string_lossy().into_owned();
    rendered_templates_cache.insert(
        registrations_key,
        RenderedTemplate {
            path: registrations_path.clone(),
            content: transform_registrations_ts_worker_to_service(
                rendered_templates_cache,
                base_path,
            )?,
            context: None,
        },
    );

    let worker_ts_path = base_path.join("worker.ts");
    if let Some(template) = rendered_templates_cache.get(&worker_ts_path)? {
        let content = template
            .content
            .lines()
            .map(|l| format!("// {}", l))
            .collect::<Vec<_>>()
            .join("\n");
        let worker_ts_key = worker_ts_path.to_string_lossy().into_owned();
        rendered_templates_cache.insert(
            worker_ts_key,
            RenderedTemplate {
                path: worker_ts_path.clone(),
                content,
                context: None,
            },
        );
    }

    // Remove the event record types file (no longer needed after worker→service)
    let camel_case_name = project_name.to_case(Case::Camel);
    let event_record_types_path = base_path
        .join("domain")
        .join("types")
        .join(format!("{}EventRecord.types.ts", camel_case_name));
    if event_record_types_path.exists() {
        rendered_templates_cache.insert(
            event_record_types_path.to_string_lossy().to_string(),
            RenderedTemplate {
                path: event_record_types_path.clone(),
                content: String::new(),
                context: None,
            },
        );
        // Mark for deletion
        std::fs::remove_file(&event_record_types_path).ok();
    }

    if let Some(scripts) = project_package_json.scripts.as_mut() {
        scripts.dev_worker = None;
        scripts.start_worker = None;
    }

    if let Some(deps) = project_package_json.dependencies.as_mut() {
        deps.forklaunch_interfaces_worker = None;
        deps.forklaunch_implementation_worker_bullmq = None;
        deps.forklaunch_implementation_worker_database = None;
        deps.forklaunch_implementation_worker_redis = None;
        deps.forklaunch_implementation_worker_kafka = None;
    }

    manifest_data.projects.iter_mut().for_each(|project| {
        if project.name == project_name {
            project.r#type = ProjectType::Service;
            if let Some(metadata) = project.metadata.as_mut() {
                metadata.r#type = None;
            }
        }
    });

    let worker_service_name = format!("{}-worker", project_name);
    remove_service_from_docker_compose(docker_compose, &worker_service_name)?;

    let readme_path = base_path.join("README-MIGRATION.md");
    let readme_key = readme_path.to_string_lossy().into_owned();
    let readme_content = format!(
        r#"# Worker to Service Migration

This worker has been converted back to a service.

## Changes Made

1. **registrations.ts** - Worker registrations removed.
2. **worker.ts** - File commented out.
3. **package.json** - Worker scripts and dependencies removed.
4. **manifest.toml** - Project type changed to Service.
5. **docker-compose.yaml** - Worker service removed.

## Next Steps

1. **Review server.ts** - Ensure your HTTP server and routes are active.
2. **Clean up** - You can delete `worker.ts` and `*EventRecord.entity.ts` if no longer needed.
3. **Install dependencies**:
   ```bash
   pnpm install
   ```
4. **Start the service**:
   ```bash
   pnpm dev
   ```
"#
    );
    rendered_templates_cache.insert(
        readme_key,
        RenderedTemplate {
            path: readme_path.clone(),
            content: readme_content,
            context: None,
        },
    );

    log_warn!(stdout, "Worker converted to service. See README-MIGRATION.md.");

    Ok(())
}

impl CliCommand for WorkerCommand {
    fn command(&self) -> Command {
        command("worker", "Change a forklaunch worker")
            .alias("wrk")
            .arg(
                Arg::new("base_path")
                    .short('p')
                    .long("path")
                    .help("The service path"),
            )
            .arg(Arg::new("name").short('N').help("The name of the service"))
            .arg(
                Arg::new("type")
                    .short('t')
                    .long("type")
                    .help("The type to use"),
            )
            .arg(
                Arg::new("to")
                    .long("to")
                    .help("Convert worker to another type (service)")
                    .value_parser(["service"]),
            )
            .arg(
                Arg::new("database")
                    .short('d')
                    .long("database")
                    .help("The database to use"),
            )
            .arg(
                Arg::new("description")
                    .short('D')
                    .long("description")
                    .help("The description of the service"),
            )
            .arg(
                Arg::new("dryrun")
                    .short('n')
                    .long("dryrun")
                    .help("Dry run the command")
                    .action(ArgAction::SetTrue),
            )
            .arg(
                Arg::new("confirm")
                    .short('c')
                    .long("confirm")
                    .help("Flag to confirm any prompts")
                    .action(ArgAction::SetTrue),
            )
    }

    fn handler(&self, matches: &clap::ArgMatches) -> Result<()> {
        let mut line_editor = Editor::<ArrayCompleter, DefaultHistory>::new()?;
        let mut stdout = StandardStream::stdout(ColorChoice::Always);
        let mut rendered_templates_cache = RenderedTemplatesCache::new();

        let (app_root_path, project_name) = find_app_root_path(matches, RequiredLocation::Project)?;
        let manifest_path = app_root_path.join(".forklaunch").join("manifest.toml");

        let existing_manifest_data: WorkerManifestData = toml::from_str::<WorkerManifestData>(
            &rendered_templates_cache
                .get(&manifest_path)
                .with_context(|| ERROR_FAILED_TO_READ_MANIFEST)?
                .unwrap()
                .content,
        )
        .with_context(|| ERROR_FAILED_TO_PARSE_MANIFEST)?;

        let worker_base_path = prompt_base_path(
            &app_root_path,
            &ManifestData::Worker(&existing_manifest_data),
            &project_name,
            &mut line_editor,
            &mut stdout,
            matches,
            1,
        )?;

        let mut manifest_data = existing_manifest_data.initialize(
            InitializableManifestConfigMetadata::Project(ProjectInitializationMetadata {
                project_name: worker_base_path
                    .file_name()
                    .unwrap()
                    .to_string_lossy()
                    .to_string()
                    .clone(),
                database: None,
                infrastructure: None,
                description: None,
                worker_type: None,
            }),
        );

        let runtime = manifest_data.runtime.parse()?;

        let name = matches.get_one::<String>("name");
        let to = matches.get_one::<String>("to");
        let r#type = matches.get_one::<String>("type");
        let database = matches.get_one::<String>("database");
        let description = matches.get_one::<String>("description");
        let dryrun = matches.get_flag("dryrun");
        let confirm = matches.get_flag("confirm");

        if let Some(to) = to {
            if to == "service" {
                let application_package_json_to_write =
                    serde_json::from_str::<ApplicationPackageJson>(
                        &rendered_templates_cache
                            .get(worker_base_path.parent().unwrap().join("package.json"))
                            .with_context(|| ERROR_FAILED_TO_READ_PACKAGE_JSON)?
                            .unwrap()
                            .content,
                    )?;

                let mut project_package_json_to_write = serde_json::from_str::<ProjectPackageJson>(
                    &rendered_templates_cache
                        .get(worker_base_path.join("package.json"))
                        .with_context(|| ERROR_FAILED_TO_READ_PACKAGE_JSON)?
                        .unwrap()
                        .content,
                )?;

                let docker_compose_path =
                    if let Some(docker_compose_path) = &manifest_data.docker_compose_path {
                        app_root_path.join(docker_compose_path)
                    } else {
                        app_root_path.join("docker-compose.yaml")
                    };
                let mut docker_compose_data = serde_yml::from_str::<DockerCompose>(
                    &rendered_templates_cache
                        .get(&docker_compose_path)
                        .with_context(|| ERROR_FAILED_TO_READ_DOCKER_COMPOSE)?
                        .unwrap()
                        .content,
                )?;

                worker_to_service(
                    &worker_base_path,
                    &mut manifest_data,
                    &mut project_package_json_to_write,
                    &mut docker_compose_data,
                    &mut rendered_templates_cache,
                    &mut stdout,
                )?;

                // Write modified files
                rendered_templates_cache.insert(
                    manifest_path.clone().to_string_lossy().to_string(),
                    RenderedTemplate {
                        path: manifest_path.to_path_buf(),
                        content: toml::to_string_pretty(&manifest_data)?,
                        context: None,
                    },
                );

                rendered_templates_cache.insert(
                    docker_compose_path.clone().to_string_lossy().to_string(),
                    RenderedTemplate {
                        path: docker_compose_path.into(),
                        content: serde_yml::to_string(&docker_compose_data)?,
                        context: None,
                    },
                );

                rendered_templates_cache.insert(
                    worker_base_path
                        .parent()
                        .unwrap()
                        .join("package.json")
                        .to_string_lossy()
                        .to_string(),
                    RenderedTemplate {
                        path: worker_base_path.parent().unwrap().join("package.json"),
                        content: serde_json::to_string_pretty(&application_package_json_to_write)?,
                        context: None,
                    },
                );

                rendered_templates_cache.insert(
                    worker_base_path
                        .join("package.json")
                        .to_string_lossy()
                        .to_string(),
                    RenderedTemplate {
                        path: worker_base_path.join("package.json"),
                        content: serde_json::to_string_pretty(&project_package_json_to_write)?,
                        context: None,
                    },
                );

                let rendered_templates: Vec<RenderedTemplate> = rendered_templates_cache
                    .drain()
                    .map(|(_, template)| template)
                    .collect();

                write_rendered_templates(&rendered_templates, dryrun, &mut stdout)?;

                return Ok(());
            }
        }

        let selected_options = if matches.ids().all(|id| id == "dryrun" || id == "confirm") {
            let options = vec!["name", "type", "description"];

            let selections = MultiSelect::with_theme(&ColorfulTheme::default())
                .with_prompt("What would you like to change?")
                .items(&options)
                .interact()?;

            if selections.is_empty() {
                writeln!(stdout, "No changes selected")?;
                return Ok(());
            }

            selections.iter().map(|i| options[*i]).collect()
        } else {
            vec![]
        };

        let name = prompt_field_from_selections_with_validation(
            "name",
            name,
            &selected_options,
            &mut line_editor,
            &mut stdout,
            matches,
            "worker name",
            None,
            |input: &str| validate_name(input) && !manifest_data.app_name.contains(input),
            |_| {
                "Worker name cannot be a substring of the application name, empty or include numbers or spaces. Please try again"
                    .to_string()
            },
        )?;

        let r#type = prompt_field_from_selections_with_validation(
            "type",
            r#type,
            &selected_options,
            &mut line_editor,
            &mut stdout,
            matches,
            "type",
            Some(&WorkerType::VARIANTS),
            |_input: &str| true,
            |_| "Invalid type. Please try again".to_string(),
        )?;

        let database = if r#type
            .clone()
            .map(|r#type| r#type.parse::<WorkerType>().unwrap() == WorkerType::Database)
            .unwrap_or(false)
        {
            let database_variants = get_database_variants(&runtime);
            prompt_field_from_selections_with_validation(
                "database",
                database,
                &["database"],
                &mut line_editor,
                &mut stdout,
                matches,
                "database",
                Some(database_variants),
                |input: &str| database_variants.contains(&input),
                |_| "Invalid database. Please try again".to_string(),
            )?
            .and_then(|database| database.parse::<Database>().ok())
        } else {
            None
        };

        let description = prompt_field_from_selections_with_validation(
            "description",
            description,
            &selected_options,
            &mut line_editor,
            &mut stdout,
            matches,
            "project description",
            None,
            |_input: &str| true,
            |_| "Invalid description. Please try again".to_string(),
        )?;

        let mut removal_templates = vec![];
        let mut move_templates = vec![];

        let application_package_json_path = worker_base_path.parent().unwrap().join("package.json");
        let application_package_json_data = rendered_templates_cache
            .get(&application_package_json_path)
            .with_context(|| ERROR_FAILED_TO_READ_PACKAGE_JSON)?
            .unwrap()
            .content;

        let mut application_package_json_to_write =
            serde_json::from_str::<ApplicationPackageJson>(&application_package_json_data)?;

        let project_package_json_path = worker_base_path.join("package.json");
        let project_package_json_data = rendered_templates_cache
            .get(&project_package_json_path)
            .with_context(|| ERROR_FAILED_TO_READ_PACKAGE_JSON)?
            .unwrap()
            .content;

        let mut project_json_to_write =
            serde_json::from_str::<ProjectPackageJson>(&project_package_json_data)?;

        let docker_compose_path =
            if let Some(docker_compose_path) = &manifest_data.docker_compose_path {
                app_root_path.join(docker_compose_path)
            } else {
                app_root_path.join("docker-compose.yaml")
            };
        let mut docker_compose_data = serde_yml::from_str::<DockerCompose>(
            &rendered_templates_cache
                .get(&docker_compose_path)
                .with_context(|| ERROR_FAILED_TO_READ_DOCKER_COMPOSE)?
                .unwrap()
                .content,
        )?;

        if let Some(r#type) = r#type {
            let requested_type = r#type.parse::<WorkerType>()?;
            let current_type = manifest_data.worker_type.parse::<WorkerType>().ok();
            let current_database = manifest_data
                .database
                .as_ref()
                .and_then(|db| db.parse::<Database>().ok());

            // Re-running a change to the type the worker already is (same type,
            // and same database for database workers) is a no-op. Skip the type
            // transformation instead of running it, which resets resources and
            // can panic on legacy docker-compose layouts.
            let already_that_type = current_type == Some(requested_type)
                && (requested_type != WorkerType::Database || current_database == database);

            if already_that_type {
                log_info!(
                    stdout,
                    "worker {} is already type {}, skipping type change",
                    manifest_data.worker_name,
                    requested_type.to_string()
                );
            } else {
                change_type(
                    &worker_base_path,
                    &requested_type,
                    database,
                    &mut manifest_data,
                    &mut application_package_json_to_write,
                    &mut project_json_to_write,
                    &mut docker_compose_data,
                    &mut rendered_templates_cache,
                    &mut removal_templates,
                )?
            }
        }

        if let Some(description) = description {
            change_description(&description, &mut manifest_data, &mut project_json_to_write)?;
        }

        if let Some(name) = name {
            move_templates.push(change_name(
                &worker_base_path,
                &name,
                confirm,
                &mut manifest_data,
                &mut application_package_json_to_write,
                &mut project_json_to_write,
                &mut docker_compose_data,
                &mut rendered_templates_cache,
                &mut removal_templates,
                &mut stdout,
            )?);
        }

        rendered_templates_cache.insert(
            manifest_path.clone().to_string_lossy(),
            RenderedTemplate {
                path: manifest_path.to_path_buf(),
                content: toml::to_string_pretty(&manifest_data)?,
                context: None,
            },
        );

        rendered_templates_cache.insert(
            docker_compose_path.clone().to_string_lossy(),
            RenderedTemplate {
                path: docker_compose_path.into(),
                content: serde_yml::to_string(&docker_compose_data)?,
                context: None,
            },
        );

        rendered_templates_cache.insert(
            application_package_json_path.clone().to_string_lossy(),
            RenderedTemplate {
                path: application_package_json_path.into(),
                content: serde_json::to_string_pretty(&application_package_json_to_write)?,
                context: None,
            },
        );

        rendered_templates_cache.insert(
            project_package_json_path
                .clone()
                .to_string_lossy()
                .to_string(),
            RenderedTemplate {
                path: project_package_json_path.into(),
                content: serde_json::to_string_pretty(&project_json_to_write)?,
                context: None,
            },
        );

        let rendered_templates: Vec<RenderedTemplate> = rendered_templates_cache
            .drain()
            .map(|(_, template)| template)
            .collect();

        remove_template_files(&removal_templates, dryrun, &mut stdout)?;
        write_rendered_templates(&rendered_templates, dryrun, &mut stdout)?;

        move_template_files(&move_templates, dryrun, &mut stdout)?;

        if !dryrun {
            log_ok!(stdout, "{} changed successfully!", &manifest_data.app_name);
            format_code(&worker_base_path, &manifest_data.runtime.parse()?);
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::fs::write;

    use indexmap::IndexMap;
    use tempfile::TempDir;

    use super::*;
    use crate::{
        constants::WorkerType,
        core::{
            docker::{DockerCompose, DockerService},
            manifest::{
                InitializableManifestConfig, InitializableManifestConfigMetadata,
                ProjectInitializationMetadata, worker::WorkerManifestData,
            },
            package_json::{
                application_package_json::ApplicationPackageJson,
                project_package_json::{ProjectDependencies, ProjectPackageJson},
            },
            rendered_template::RenderedTemplatesCache,
        },
    };

    // A real bullmq worker registrations.ts, as scaffolded by the CLI.
    const BULLMQ_WORKER_REGISTRATIONS_TS: &str =
        include_str!("test_fixtures/bullmq_worker_registrations.ts");

    // A worker manifest whose worker is already a bullmq worker.
    const BULLMQ_WORKER_MANIFEST_TOML: &str = r#"
id = "test-id"
cli_version = "0.0.0"
app_name = "test-app"
modules_path = "src/modules"
app_description = "test"
linter = "eslint"
formatter = "prettier"
validator = "zod"
http_framework = "express"
runtime = "node"
author = "test"
license = "MIT"

[project_peer_topology]

[[projects]]
type = "Worker"
name = "myworkr"
description = "test worker"
variant = "BullMq"
routers = ["myworkr"]

[projects.resources]
cache = "bullmq"
redis_partition = 0

[projects.metadata]
type = "bullmq"
"#;

    fn setup_bullmq_worker(
        server_service_name: &str,
    ) -> (
        TempDir,
        WorkerManifestData,
        DockerCompose,
        ApplicationPackageJson,
        ProjectPackageJson,
        RenderedTemplatesCache,
    ) {
        let tmp = TempDir::new().unwrap();
        let base = tmp.path();

        write(base.join("registrations.ts"), BULLMQ_WORKER_REGISTRATIONS_TS).unwrap();
        write(base.join(".env.local"), "").unwrap();

        let raw: WorkerManifestData = toml::from_str(BULLMQ_WORKER_MANIFEST_TOML).unwrap();
        let manifest = raw.initialize(InitializableManifestConfigMetadata::Project(
            ProjectInitializationMetadata {
                project_name: "myworkr".to_string(),
                database: None,
                infrastructure: None,
                description: None,
                worker_type: None,
            },
        ));

        // Reproduce the docker-compose layout that triggered the panic: the
        // worker's HTTP server service uses a caller-chosen key (older CLI
        // versions used the bare project name instead of "{name}-server").
        let mut docker_compose = DockerCompose::default();
        docker_compose.services.insert(
            server_service_name.to_string(),
            DockerService {
                environment: None,
                ..Default::default()
            },
        );
        docker_compose.services.insert(
            "myworkr-worker".to_string(),
            DockerService {
                environment: Some(IndexMap::new()),
                ..Default::default()
            },
        );

        let app_pkg = ApplicationPackageJson::default();
        let pkg = ProjectPackageJson {
            dependencies: Some(ProjectDependencies::default()),
            ..Default::default()
        };

        let cache = RenderedTemplatesCache::new();

        (tmp, manifest, docker_compose, app_pkg, pkg, cache)
    }

    // Changing a worker to the type it already is (bullmq -> bullmq) must not
    // panic, even when the server service is named with the bare project name
    // rather than "{name}-server". This is the exact shape that panicked on the
    // managed-apps worker (worker.rs:391 `.unwrap()` on a missing service).
    #[test]
    fn test_change_type_bullmq_same_type_bare_server_service_does_not_panic() {
        let (_tmp, mut manifest, mut docker, mut app_pkg, mut pkg, mut cache) =
            setup_bullmq_worker("myworkr");
        let base = _tmp.path();

        let mut removal_templates = vec![];

        change_type(
            base,
            &WorkerType::BullMQCache,
            None,
            &mut manifest,
            &mut app_pkg,
            &mut pkg,
            &mut docker,
            &mut cache,
            &mut removal_templates,
        )
        .expect("change_type to the same bullmq type must not panic or error");

        // The env must be applied to the bare-named server service, not lost.
        let server_env = docker.services["myworkr"]
            .environment
            .as_ref()
            .expect("bare-named server service must receive an environment");
        assert!(
            server_env.contains_key("REDIS_URL"),
            "expected REDIS_URL injected into the resolved server service, got: {:?}",
            server_env.keys().collect::<Vec<_>>()
        );
    }

    // The conventional "{name}-server" naming must keep working unchanged.
    #[test]
    fn test_change_type_bullmq_conventional_server_service_still_updates() {
        let (_tmp, mut manifest, mut docker, mut app_pkg, mut pkg, mut cache) =
            setup_bullmq_worker("myworkr-server");
        let base = _tmp.path();

        let mut removal_templates = vec![];

        change_type(
            base,
            &WorkerType::BullMQCache,
            None,
            &mut manifest,
            &mut app_pkg,
            &mut pkg,
            &mut docker,
            &mut cache,
            &mut removal_templates,
        )
        .expect("change_type must succeed for conventional -server naming");

        let server_env = docker.services["myworkr-server"]
            .environment
            .as_ref()
            .expect("conventional server service must receive an environment");
        assert!(
            server_env.contains_key("REDIS_URL"),
            "expected REDIS_URL injected into myworkr-server"
        );
    }
}