forklaunch 1.15.0

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
use std::{io::Write, path::Path};

use anyhow::{Context, Result};
use serde_json::{from_str as json_from_str, to_string_pretty as json_to_string_pretty};
use serde_yml::{from_str as yaml_from_str, to_string as yaml_to_string};
use termcolor::{StandardStream, WriteColor};
use toml::{from_str as toml_from_str, to_string_pretty as toml_to_string_pretty};

use crate::{
    constants::{
        Database, ERROR_FAILED_TO_ADD_PROJECT_METADATA_TO_PNPM_WORKSPACE,
        ERROR_FAILED_TO_PARSE_DOCKER_COMPOSE, ERROR_FAILED_TO_WRITE_DOCKER_COMPOSE, Infrastructure,
        Runtime, WorkerType,
    },
    core::{
        client_sdk::{regenerate_client_sdk_compliance, remove_project_from_client_sdk},
        docker::{
            DockerCompose, add_service_definition_to_docker_compose,
            add_worker_definition_to_docker_compose, remove_service_from_docker_compose,
            remove_worker_from_docker_compose,
        },
        manifest::{
            InitializableManifestConfig, InitializableManifestConfigMetadata, ProjectEntry,
            ProjectInitializationMetadata, ProjectMetadata, ProjectType, ResourceInventory,
            application::ApplicationManifestData, remove_project_definition_from_manifest,
            service::ServiceManifestData, worker::WorkerManifestData,
        },
        package_json::{
            application_package_json::ApplicationPackageJson,
            project_package_json::ProjectPackageJson,
        },
        pnpm_workspace::PnpmWorkspace,
        rendered_template::{RenderedTemplate, RenderedTemplatesCache},
        sync::detection::detect_routers_from_service,
        tsconfig::add_project_to_modules_tsconfig,
    },
};

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ArtifactType {
    Manifest,
    DockerCompose,
    Runtime,
    ClientSdk,
    ModulesTsconfig,
}

#[derive(Clone, Debug)]
pub struct ProjectSyncMetadata {
    pub project_type: ProjectType,
    pub project_name: String,
    pub description: String,
    pub database: Option<Database>,
    pub infrastructure: Vec<Infrastructure>,
    pub worker_type: Option<WorkerType>,
}

impl ProjectSyncMetadata {
    fn to_resource_inventory(&self) -> Option<ResourceInventory> {
        let database = self.database.as_ref().map(|d| d.to_string());

        let cache = self.infrastructure.iter().find_map(|infra| match infra {
            Infrastructure::Redis => Some(Infrastructure::Redis.metadata().id.to_string()),
            Infrastructure::S3 => None,
        });

        let queue = self.worker_type.and_then(|wt| match wt {
            WorkerType::Kafka => Some(wt.to_string()),
            WorkerType::Database | WorkerType::RedisCache | WorkerType::BullMQCache => None,
        });

        let object_store = self.infrastructure.iter().find_map(|infra| match infra {
            Infrastructure::S3 => Some(Infrastructure::S3.metadata().id.to_string()),
            Infrastructure::Redis => None,
        });

        if database.is_some() || cache.is_some() || queue.is_some() || object_store.is_some() {
            Some(ResourceInventory {
                database,
                cache,
                queue,
                object_store,
                redis_partition: None,
            })
        } else {
            None
        }
    }

    fn to_project_metadata(&self) -> Option<ProjectMetadata> {
        self.worker_type.map(|wt| ProjectMetadata {
            r#type: Some(wt.to_string()),
            hosting_type: None,
            privileged: None,
        })
    }
}

/// Sync a project to multiple artifacts using RenderedTemplatesCache
///
/// This is the core sync function that implements the pattern:
/// - Check if project is present in artifacts
/// - Add if not present
/// - Save changes back to cache
///
/// Matches the pattern used in change command.
pub fn sync_project_to_artifacts(
    cache: &mut RenderedTemplatesCache,
    manifest_data: &mut ApplicationManifestData,
    metadata: &ProjectSyncMetadata,
    artifacts: &[ArtifactType],
    app_root_path: &Path,
    modules_path: &Path,
    stdout: &mut StandardStream,
) -> Result<()> {
    for artifact_type in artifacts {
        match artifact_type {
            ArtifactType::Manifest => {
                sync_to_manifest(manifest_data, metadata, modules_path, stdout)?;
            }
            ArtifactType::DockerCompose => {
                sync_to_docker_compose(manifest_data, cache, metadata, app_root_path, stdout)?;
            }
            ArtifactType::Runtime => {
                sync_to_runtime(manifest_data, cache, metadata, modules_path, stdout)?;
            }
            ArtifactType::ClientSdk => {
                sync_to_client_sdk(manifest_data, cache, metadata, modules_path, stdout)?;
            }
            ArtifactType::ModulesTsconfig => {
                sync_to_modules_tsconfig(cache, metadata, modules_path, stdout)?;
            }
        }
    }

    Ok(())
}

fn sync_to_manifest(
    manifest_data: &mut ApplicationManifestData,
    metadata: &ProjectSyncMetadata,
    modules_path: &Path,
    stdout: &mut StandardStream,
) -> Result<()> {
    if manifest_data
        .projects
        .iter()
        .any(|p| p.name == metadata.project_name)
    {
        log_info!(stdout, "Already in manifest: {}", metadata.project_name);
        return Ok(());
    }

    let routers = detect_routers_for_project(
        &metadata.project_type,
        &metadata.project_name,
        modules_path,
        stdout,
    )?;

    manifest_data.projects.push(ProjectEntry {
        name: metadata.project_name.clone(),
        r#type: metadata.project_type.clone(),
        description: metadata.description.clone(),
        variant: None,
        resources: metadata.to_resource_inventory(),
        routers,
        metadata: metadata.to_project_metadata(),
        serves: None,
    });

    manifest_data
        .project_peer_topology
        .entry(manifest_data.app_name.clone())
        .or_insert_with(Vec::new)
        .push(metadata.project_name.clone());

    log_ok!(stdout, "Added to manifest: {}", metadata.project_name);

    Ok(())
}

/// Detect routers for a project based on its type
fn detect_routers_for_project(
    project_type: &ProjectType,
    project_name: &str,
    modules_path: &Path,
    stdout: &mut StandardStream,
) -> Result<Option<Vec<String>>> {
    let routers = match project_type {
        ProjectType::Service | ProjectType::Worker => {
            let project_path = modules_path.join(project_name);
            let detected_routers = detect_routers_from_service(&project_path)?;

            if !detected_routers.is_empty() {
                log_ok!(stdout, "Detected {} router(s): {}", detected_routers.len(), detected_routers.join(", "));
                Some(detected_routers)
            } else {
                None
            }
        }
        ProjectType::Library => None,
    };

    Ok(routers)
}

/// Sync to docker-compose.yaml - delegated to existing functions
///
/// Docker sync is handled separately because it requires complex ServiceManifestData/WorkerManifestData
/// structures with 50+ template fields. The existing validation functions handle this complexity.
/// This function just checks presence - the actual addition is handled by the caller.
fn sync_to_docker_compose(
    manifest_data: &mut ApplicationManifestData,
    cache: &mut RenderedTemplatesCache,
    metadata: &ProjectSyncMetadata,
    app_root_path: &Path,
    stdout: &mut StandardStream,
) -> Result<()> {
    if !matches!(
        metadata.project_type,
        ProjectType::Service | ProjectType::Worker
    ) {
        return Ok(());
    }

    let docker_path = app_root_path.join(
        manifest_data
            .docker_compose_path
            .clone()
            .unwrap_or("docker-compose.yaml".to_string()),
    );

    let template = cache
        .get(&docker_path)?
        .context("Docker compose file not found")?;

    let mut docker_compose_content = template.content.clone();

    let docker_compose: DockerCompose =
        yaml_from_str(&docker_compose_content).context(ERROR_FAILED_TO_PARSE_DOCKER_COMPOSE)?;

    if docker_compose.services.contains_key(&metadata.project_name) {
        log_info!(stdout, "Already in docker-compose: {}", metadata.project_name);
        return Ok(());
    }

    match metadata.project_type {
        ProjectType::Service => {
            let service_manifest_data: &ServiceManifestData =
                &toml_from_str(&toml_to_string_pretty(manifest_data)?)?;
            let service_manifest_data = service_manifest_data.initialize(
                InitializableManifestConfigMetadata::Project(ProjectInitializationMetadata {
                    project_name: metadata.project_name.clone(),
                    database: Some(metadata.database.clone().unwrap()),
                    infrastructure: Some(metadata.infrastructure.clone()),
                    description: Some(metadata.description.clone()),
                    worker_type: metadata.worker_type.clone(),
                }),
            );
            docker_compose_content = add_service_definition_to_docker_compose(
                &service_manifest_data,
                app_root_path,
                Some(template.content),
            )?;
        }
        ProjectType::Worker => {
            let worker_manifest_data: &WorkerManifestData =
                &toml_from_str(&toml_to_string_pretty(manifest_data)?)?;
            let worker_manifest_data = worker_manifest_data.initialize(
                InitializableManifestConfigMetadata::Project(ProjectInitializationMetadata {
                    project_name: metadata.project_name.clone(),
                    database: Some(metadata.database.clone().unwrap()),
                    infrastructure: Some(metadata.infrastructure.clone()),
                    description: Some(metadata.description.clone()),
                    worker_type: metadata.worker_type.clone(),
                }),
            );
            docker_compose_content = add_worker_definition_to_docker_compose(
                &worker_manifest_data,
                app_root_path,
                Some(template.content),
            )?;
        }
        ProjectType::Library => (),
    };

    cache.insert(
        docker_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: docker_path.clone(),
            content: docker_compose_content,
            context: Some(ERROR_FAILED_TO_WRITE_DOCKER_COMPOSE.to_string()),
        },
    );

    Ok(())
}

fn sync_to_runtime(
    manifest_data: &mut ApplicationManifestData,
    cache: &mut RenderedTemplatesCache,
    metadata: &ProjectSyncMetadata,
    modules_path: &Path,
    stdout: &mut StandardStream,
) -> Result<()> {
    let runtime: Runtime = manifest_data.runtime.parse()?;

    match runtime {
        Runtime::Bun => {
            sync_to_package_json(cache, metadata, modules_path, stdout)?;
        }
        Runtime::Node => {
            sync_to_pnpm_workspace(cache, metadata, modules_path, stdout)?;
        }
    }

    Ok(())
}

fn sync_to_package_json(
    cache: &mut RenderedTemplatesCache,
    metadata: &ProjectSyncMetadata,
    modules_path: &Path,
    stdout: &mut StandardStream,
) -> Result<()> {
    let pkg_json_path = modules_path.join("package.json");

    let template = cache
        .get(&pkg_json_path)?
        .context("package.json not found")?;

    let mut pkg_json: ApplicationPackageJson =
        json_from_str(&template.content).context("Failed to parse package.json")?;

    let workspaces = pkg_json.workspaces.get_or_insert_with(Vec::new);
    if workspaces.contains(&metadata.project_name) {
        log_info!(stdout, "Already in package.json: {}", metadata.project_name);
        return Ok(());
    }

    workspaces.push(metadata.project_name.clone());

    cache.insert(
        pkg_json_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: pkg_json_path.clone(),
            content: json_to_string_pretty(&pkg_json)
                .context("Failed to serialize package.json")?,
            context: Some("Failed to write package.json".to_string()),
        },
    );

    log_ok!(stdout, "Added to package.json: {}", metadata.project_name);

    Ok(())
}

fn sync_to_client_sdk(
    manifest_data: &mut ApplicationManifestData,
    cache: &mut RenderedTemplatesCache,
    metadata: &ProjectSyncMetadata,
    modules_path: &Path,
    stdout: &mut StandardStream,
) -> Result<()> {
    if metadata.project_type != ProjectType::Service {
        return Ok(());
    }

    let sdk_ts_path = modules_path.join("client-sdk").join("clientSdk.ts");
    let sdk_json_path = modules_path.join("client-sdk").join("package.json");

    let ts_template = cache.get(&sdk_ts_path)?.context("clientSdk.ts not found")?;
    let json_template = cache
        .get(&sdk_json_path)?
        .context("SDK package.json not found")?;

    let mut sdk_pkg_json: ProjectPackageJson = json_from_str(&json_template.content)?;

    // Use existing SDK function (it handles the AST transformation)
    let (new_ts_content, new_pkg_json) = crate::core::client_sdk::add_project_vec_to_client_sdk(
        &manifest_data.app_name,
        &vec![metadata.project_name.clone()],
        &ts_template.content,
        &mut sdk_pkg_json,
    )?;

    cache.insert(
        sdk_ts_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: sdk_ts_path.clone(),
            content: new_ts_content,
            context: Some("Failed to write universal SDK".to_string()),
        },
    );

    cache.insert(
        sdk_json_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: sdk_json_path.clone(),
            content: json_to_string_pretty(&new_pkg_json)?,
            context: Some("Failed to write SDK package.json".to_string()),
        },
    );

    log_ok!(stdout, "Added to universal SDK: {}", metadata.project_name);

    regenerate_client_sdk_compliance(cache, modules_path, &manifest_data.projects)?;

    Ok(())
}

fn sync_to_modules_tsconfig(
    cache: &mut RenderedTemplatesCache,
    metadata: &ProjectSyncMetadata,
    modules_path: &Path,
    stdout: &mut StandardStream,
) -> Result<()> {
    let rendered_template = add_project_to_modules_tsconfig(modules_path, &metadata.project_name)?;

    cache.insert(
        rendered_template.path.to_string_lossy().to_string(),
        rendered_template,
    );

    log_ok!(stdout, "Added to modules/tsconfig.json: {}", metadata.project_name);

    Ok(())
}

fn sync_to_pnpm_workspace(
    cache: &mut RenderedTemplatesCache,
    metadata: &ProjectSyncMetadata,
    modules_path: &Path,
    stdout: &mut StandardStream,
) -> Result<()> {
    let workspace_path = modules_path.join("pnpm-workspace.yaml");

    let template = cache
        .get(&workspace_path)?
        .context("pnpm-workspace.yaml not found")?;

    let mut workspace: PnpmWorkspace =
        yaml_from_str(&template.content).context("Failed to parse pnpm-workspace.yaml")?;

    if workspace.packages.contains(&metadata.project_name) {
        log_info!(stdout, "Already in pnpm-workspace: {}", metadata.project_name);
        return Ok(());
    }

    workspace.packages.push(metadata.project_name.clone());

    cache.insert(
        workspace_path.to_string_lossy().to_string(),
        RenderedTemplate {
            path: workspace_path.clone(),
            content: yaml_to_string(&workspace)
                .context(ERROR_FAILED_TO_ADD_PROJECT_METADATA_TO_PNPM_WORKSPACE)?,
            context: Some(ERROR_FAILED_TO_ADD_PROJECT_METADATA_TO_PNPM_WORKSPACE.to_string()),
        },
    );

    log_ok!(stdout, "Added to pnpm-workspace: {}", metadata.project_name);

    Ok(())
}

pub fn remove_project_from_artifacts(
    rendered_templates_cache: &mut RenderedTemplatesCache,
    manifest_data: &mut ApplicationManifestData,
    project_name: &str,
    project_type: ProjectType,
    artifact_types: &[ArtifactType],
    app_root_path: &Path,
    modules_path: &Path,
    stdout: &mut StandardStream,
) -> Result<()> {
    for artifact_type in artifact_types {
        match artifact_type {
            ArtifactType::Manifest => {
                remove_project_definition_from_manifest(manifest_data, &project_name.to_string())?;
                log_ok!(stdout, "Removed from manifest");
            }
            ArtifactType::DockerCompose => {
                if matches!(project_type, ProjectType::Service | ProjectType::Worker) {
                    let docker_compose_path = app_root_path.join("docker-compose.yaml");

                    if docker_compose_path.exists() {
                        let docker_compose_template =
                            rendered_templates_cache.get(&docker_compose_path)?;
                        let mut docker_compose: DockerCompose =
                            if let Some(template) = docker_compose_template {
                                yaml_from_str(&template.content)
                                    .context("Failed to parse docker-compose")?
                            } else {
                                yaml_from_str(&std::fs::read_to_string(&docker_compose_path)?)
                                    .context("Failed to parse docker-compose")?
                            };

                        match project_type {
                            ProjectType::Service => {
                                remove_service_from_docker_compose(
                                    &mut docker_compose,
                                    project_name,
                                )?;
                            }
                            ProjectType::Worker => {
                                remove_worker_from_docker_compose(
                                    &mut docker_compose,
                                    project_name,
                                )?;
                            }
                            _ => {}
                        }

                        rendered_templates_cache.insert(
                            docker_compose_path.to_string_lossy(),
                            RenderedTemplate {
                                path: docker_compose_path.clone(),
                                content: yaml_to_string(&docker_compose)
                                    .context("Failed to serialize docker-compose")?,
                                context: None,
                            },
                        );

                        log_ok!(stdout, "Removed from docker-compose");
                    }
                }
            }
            ArtifactType::Runtime => {
                let runtime: Runtime = manifest_data.runtime.parse()?;

                match runtime {
                    Runtime::Node => {
                        let pnpm_workspace_path = modules_path.join("pnpm-workspace.yaml");
                        if pnpm_workspace_path.exists() {
                            let pnpm_template =
                                rendered_templates_cache.get(&pnpm_workspace_path)?;
                            let mut pnpm_workspace: PnpmWorkspace =
                                if let Some(template) = pnpm_template {
                                    yaml_from_str(&template.content)
                                        .context("Failed to parse pnpm-workspace")?
                                } else {
                                    yaml_from_str(&std::fs::read_to_string(&pnpm_workspace_path)?)
                                        .context("Failed to parse pnpm-workspace")?
                                };

                            if let Some(pos) = pnpm_workspace
                                .packages
                                .iter()
                                .position(|p| p == project_name)
                            {
                                pnpm_workspace.packages.remove(pos);
                            }

                            rendered_templates_cache.insert(
                                pnpm_workspace_path.to_string_lossy(),
                                RenderedTemplate {
                                    path: pnpm_workspace_path.clone(),
                                    content: yaml_to_string(&pnpm_workspace)?,
                                    context: None,
                                },
                            );

                            log_ok!(stdout, "Removed from pnpm-workspace");
                        }
                    }
                    Runtime::Bun => {
                        let package_json_path = modules_path.join("package.json");
                        if package_json_path.exists() {
                            let pkg_template = rendered_templates_cache.get(&package_json_path)?;
                            let mut package_json: ApplicationPackageJson =
                                if let Some(template) = pkg_template {
                                    json_from_str(&template.content)
                                        .context("Failed to parse package.json")?
                                } else {
                                    json_from_str(&std::fs::read_to_string(&package_json_path)?)
                                        .context("Failed to parse package.json")?
                                };

                            if let Some(ref mut workspaces) = package_json.workspaces {
                                if let Some(pos) = workspaces.iter().position(|p| p == project_name)
                                {
                                    workspaces.remove(pos);
                                }
                            }

                            rendered_templates_cache.insert(
                                package_json_path.to_string_lossy(),
                                RenderedTemplate {
                                    path: package_json_path.clone(),
                                    content: json_to_string_pretty(&package_json)?,
                                    context: None,
                                },
                            );

                            log_ok!(stdout, "Removed from package.json workspaces");
                        }
                    }
                }
            }
            ArtifactType::ClientSdk => {
                if project_type == ProjectType::Service || project_type == ProjectType::Worker {
                    remove_project_from_client_sdk(
                        rendered_templates_cache,
                        modules_path,
                        &manifest_data.app_name,
                        project_name,
                    )?;

                    regenerate_client_sdk_compliance(
                        rendered_templates_cache,
                        modules_path,
                        &manifest_data.projects,
                    )?;

                    log_ok!(stdout, "Removed from universal SDK");
                }
            }
            ArtifactType::ModulesTsconfig => {
                writeln!(
                    stdout,
                    "[INFO] Modules tsconfig.json cleanup not yet implemented"
                )?;
            }
        }
    }

    Ok(())
}