node-app-build 5.22.0

Mini app developer CLI: scaffold, validate, package node-app-* Debian packages
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
//! `node-app new [name] [--type TYPE]`
//!
//! Scaffolds a new project by fetching a template from a GitHub repo
//! (or a local directory, for offline/CI use) via `gh repo clone --depth=1`.
//! When `--type` is omitted the interactive wizard guides the user.
//!
//! Template sources (resolved in order):
//!   1. `--templates <org/repo-or-path>` CLI flag
//!   2. `NODE_APP_TEMPLATES_REPO` environment variable
//!   3. DEFAULT_TEMPLATES_REPO const ("econ-v1/node-app-templates")
//!
//! If the resolved value is a path to an existing local directory, templates
//! are copied directly (no network, no `gh` required). Otherwise it is
//! treated as a GitHub `org/repo` slug and fetched via `gh repo clone`.
//!
//! Placeholders substituted in every text file:
//!   `{{name}}`          — app name (kebab-case)
//!   `{{description}}`   — one-line description
//!   `{{systemd_order}}` — systemd ordering directive (standalone only,
//!                         e.g. "Before=econ-v1.service")

use crate::AppKind;
use anyhow::{anyhow, bail, Context, Result};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use walkdir::WalkDir;

/// Default template repository slug (GitHub org/repo).
pub const DEFAULT_TEMPLATES_REPO: &str = "econ-v1/node-app-templates";

fn family_dir_name(kind: AppKind) -> &'static str {
    match kind {
        AppKind::Bun | AppKind::BunFullstack | AppKind::StandaloneBun => "bun",
        AppKind::Cdylib | AppKind::CdylibFullstack | AppKind::StandaloneRust => "cdylib",
    }
}

fn family_github_repo(kind: AppKind) -> &'static str {
    match kind {
        AppKind::Bun | AppKind::BunFullstack | AppKind::StandaloneBun =>
            "econ-v1/node-app-template-bun",
        AppKind::Cdylib | AppKind::CdylibFullstack | AppKind::StandaloneRust =>
            "econ-v1/node-app-template-cdylib",
    }
}

// ── Public entry point ────────────────────────────────────────────────────────

/// Entry point called from `main.rs`.
///
/// When `name_hint` or `kind_hint` is `None`, the interactive wizard fills
/// in the missing pieces. When both are `Some`, the wizard is skipped
/// entirely (backward-compatible scripted usage).
pub fn run(
    name_hint: Option<String>,
    kind_hint: Option<AppKind>,
    out: Option<PathBuf>,
    git: bool,
    github: Option<String>,
    no_deps_update: bool,
    templates_repo: String,
) -> Result<()> {
    let args = match (name_hint, kind_hint) {
        (Some(name), Some(kind)) => ScaffoldArgs {
            name,
            kind,
            description: "A Node mini app".to_string(),
            systemd_order: String::new(),
            out,
            git: git || github.is_some(),
            github,
            run_deps_update: !no_deps_update,
            templates_repo,
        },
        (name_hint, kind_hint) => {
            let wiz = wizard_prompt(name_hint, kind_hint, git, github)?;
            ScaffoldArgs {
                name: wiz.name,
                kind: wiz.kind,
                description: wiz.description,
                systemd_order: wiz.systemd_order,
                out,
                git: wiz.git,
                github: wiz.github,
                run_deps_update: !no_deps_update,
                templates_repo,
            }
        }
    };
    scaffold(args)
}

// ── Scaffold args ─────────────────────────────────────────────────────────────

struct ScaffoldArgs {
    name: String,
    kind: AppKind,
    description: String,
    systemd_order: String,
    out: Option<PathBuf>,
    git: bool,
    github: Option<String>,
    run_deps_update: bool,
    templates_repo: String,
}

fn scaffold(args: ScaffoldArgs) -> Result<()> {
    validate_name(&args.name)?;
    if let Some(ref slug) = args.github {
        validate_github_slug(slug)?;
    }

    let dest = args
        .out
        .clone()
        .unwrap_or_else(|| PathBuf::from(&args.name));

    if dest.exists() && fs::read_dir(&dest)?.next().is_some() {
        bail!(
            "destination '{}' already exists and is not empty",
            dest.display()
        );
    }
    fs::create_dir_all(&dest)
        .with_context(|| format!("create destination {}", dest.display()))?;

    fetch_and_write_template(
        &args.templates_repo,
        args.kind,
        &dest,
        &args.name,
        &args.description,
        &args.systemd_order,
    )?;

    strip_for_kind(&dest, args.kind)?;
    patch_sdk_versions(&dest);

    match args.kind {
        AppKind::Cdylib | AppKind::CdylibFullstack => {
            eprintln!(
                "⚠ Reminder: cdylib (native) apps must be GPG-signed by an econ-v1 \
                 org keyring to load as FirstParty tier in production. The standalone \
                 release.yml workflow handles this automatically when you push a tag."
            );
        }
        AppKind::StandaloneRust | AppKind::StandaloneBun => {
            eprintln!(
                "ℹ Standalone apps run as their own systemd service and are NOT \
                 loaded by the node platform. They communicate with the platform \
                 via /run/node/control.sock (JSON-RPC 2.0) when it is available."
            );
        }
        _ => {}
    }

    println!(
        "✓ Scaffolded {} app '{}' at {}",
        args.kind.label(),
        args.name,
        dest.display()
    );

    if args.run_deps_update {
        run_deps_update(&dest, args.kind)?;
    }

    if args.git {
        init_git(&dest, &args.name)?;
    }

    if let Some(ref slug) = args.github {
        create_github_repo(&dest, slug)?;
    }

    println!();
    println!("Next steps:");
    println!("  cd {}", dest.display());
    match args.github.as_deref() {
        None => println!("  node-app dev          # hot-reload inner-loop"),
        Some(slug) => println!(
            "  node-app dev          # hot-reload inner-loop, your repo is on GitHub at {}",
            slug
        ),
    }

    Ok(())
}

// ── Interactive wizard ────────────────────────────────────────────────────────

struct WizardResult {
    name: String,
    kind: AppKind,
    description: String,
    systemd_order: String,
    git: bool,
    github: Option<String>,
}

fn wizard_prompt(
    name_hint: Option<String>,
    kind_hint: Option<AppKind>,
    git_flag: bool,
    github_flag: Option<String>,
) -> Result<WizardResult> {
    cliclack::intro(" node-app new ").ok();

    // Step 1: App name
    let name = match name_hint {
        Some(n) => n,
        None => cliclack::input("App name")
            .validate(|s: &String| validate_name(s).map_err(|e| e.to_string()))
            .interact()
            .context("app name prompt")?,
    };

    // Step 2a: Language → Step 2b: Variant
    let kind = match kind_hint {
        Some(k) => k,
        None => {
            #[derive(Clone, PartialEq, Eq)]
            enum Lang { Bun, Rust }

            let lang = cliclack::select("Language")
                .item(Lang::Bun,  "TypeScript (Bun)", "Interpreted · Architecture: all · bun runtime")
                .item(Lang::Rust, "Native Rust",       "Compiled cdylib/.so or binary · amd64 + arm64")
                .interact()
                .context("language selection")?;

            match lang {
                Lang::Bun => cliclack::select("Variant")
                    .item(AppKind::Bun,         "Platform app",       "IPC capabilities, optional simple HTML UI · node-ctl deploy")
                    .item(AppKind::BunFullstack, "Platform fullstack", "Platform app + embedded React/Vite UI served by the platform")
                    .item(AppKind::StandaloneBun,"Standalone service", "Own systemd unit · /run/node/control.sock (optional)")
                    .interact()
                    .context("variant selection")?,
                Lang::Rust => cliclack::select("Variant")
                    .item(AppKind::Cdylib,        "Platform cdylib",    "Compiled .so loaded by the daemon · GPG-signed FirstParty")
                    .item(AppKind::CdylibFullstack,"Platform fullstack", "cdylib + embedded React/Vite/Tailwind UI")
                    .item(AppKind::StandaloneRust, "Standalone service", "Binary with own systemd unit · ideal for LCD/OTA/recovery")
                    .interact()
                    .context("variant selection")?,
            }
        }
    };

    // Step 3: Description
    let description: String = cliclack::input("Description")
        .placeholder("A Node mini app")
        .default_input("A Node mini app")
        .interact()
        .context("description prompt")?;

    // Step 4: Systemd ordering (standalone only)
    let systemd_order = if matches!(kind, AppKind::StandaloneRust | AppKind::StandaloneBun) {
        #[derive(Clone, PartialEq, Eq)]
        enum Order { Before, After }

        let order = cliclack::select("Systemd ordering")
            .item(Order::Before, "Before platform", "Before=econ-v1.service — LCD, recovery, OTA, pre-boot")
            .item(Order::After,  "After platform",  "After=econ-v1.service — depends on platform being up")
            .interact()
            .context("systemd ordering selection")?;

        match order {
            Order::Before => "Before=econ-v1.service".to_string(),
            Order::After  => "After=econ-v1.service".to_string(),
        }
    } else {
        String::new()
    };

    // Step 5: Git init
    let git = git_flag
        || github_flag.is_some()
        || cliclack::confirm("Initialize git repo?")
            .initial_value(true)
            .interact()
            .context("git confirm")?;

    // Step 6: GitHub repo (only if git enabled and not already provided)
    let github = if github_flag.is_some() {
        github_flag
    } else if git {
        let slug: String = cliclack::input("GitHub repo (org/repo, blank to skip)")
            .placeholder("econ-v1/node-app-my-app")
            .required(false)
            .interact()
            .context("github prompt")?;
        let trimmed = slug.trim().to_string();
        if trimmed.is_empty() { None } else { Some(trimmed) }
    } else {
        None
    };

    cliclack::outro(format!("Scaffolding {}...", name)).ok();

    Ok(WizardResult { name, kind, description, systemd_order, git, github })
}

// ── Template fetching ─────────────────────────────────────────────────────────

/// Resolve `templates_spec` to the appropriate template source and copy it to `dest`.
///
/// If `templates_spec` is an existing directory path, it is used directly
/// (offline / CI mode) and a `bun/` or `cdylib/` subdirectory is expected.
/// Otherwise it is treated as a GitHub `org/repo` slug and cloned with
/// `gh repo clone --depth=1`. When using the default repo constant, routing
/// is to the per-family repos (`econ-v1/node-app-template-bun` or
/// `econ-v1/node-app-template-cdylib`); user-specified repos are cloned
/// directly (with optional family-subdir fallback for monorepo-style repos).
fn fetch_and_write_template(
    templates_spec: &str,
    kind: AppKind,
    dest: &Path,
    name: &str,
    description: &str,
    systemd_order: &str,
) -> Result<()> {
    let local = PathBuf::from(templates_spec);

    if local.is_dir() {
        // Local mode: look for family-named subdir (bun/ or cdylib/)
        let subdir = local.join(family_dir_name(kind));
        if !subdir.is_dir() {
            bail!(
                "Local templates directory '{}' has no '{}/' subdirectory.\n\
                 Expected a '{}/' directory containing the base template.",
                templates_spec,
                family_dir_name(kind),
                family_dir_name(kind)
            );
        }
        return write_template_from_path(&subdir, dest, name, description, systemd_order);
    }

    // GitHub mode: resolve repo and clone
    ensure_gh()?;

    let repo = if templates_spec == DEFAULT_TEMPLATES_REPO {
        // Default: route to per-family repo, clone root (no subdir lookup)
        family_github_repo(kind)
    } else {
        // User-specified custom repo via --templates or NODE_APP_TEMPLATES_REPO
        templates_spec
    };

    println!("→ fetching {} template from {}...", kind.label(), repo);

    let tmp = tmp_dir()?;
    let clone_ok = Command::new("gh")
        .args([
            "repo",
            "clone",
            repo,
            tmp.to_str().unwrap_or_default(),
            "--",
            "--depth=1",
            "--quiet",
        ])
        .status()
        .with_context(|| format!("invoke `gh repo clone {}`", repo))?
        .success();

    if !clone_ok {
        let _ = fs::remove_dir_all(&tmp);
        bail!(
            "Failed to clone template repo '{}'.\n\
             Ensure you have read access and are authenticated (`gh auth login`).\n\
             Override with --templates <org/repo-or-path> or NODE_APP_TEMPLATES_REPO.",
            repo
        );
    }

    // For user-specified repos, check for a family-named subdir first (backward compat
    // with monorepo-style template repos); otherwise use the repo root.
    let template_src = if templates_spec != DEFAULT_TEMPLATES_REPO {
        let subdir = tmp.join(family_dir_name(kind));
        if subdir.is_dir() { subdir } else { tmp.clone() }
    } else {
        tmp.clone()
    };

    let result = write_template_from_path(&template_src, dest, name, description, systemd_order);
    let _ = fs::remove_dir_all(&tmp);
    result
}

fn write_template_from_path(
    src: &Path,
    dest: &Path,
    name: &str,
    description: &str,
    systemd_order: &str,
) -> Result<()> {
    for entry in WalkDir::new(src).min_depth(1) {
        let entry = entry.with_context(|| "iterate template files")?;
        let rel = entry
            .path()
            .strip_prefix(src)
            .expect("walkdir always under src");
        // Substitute placeholders in path components (e.g. "node-app-{{name}}.service").
        let rel_rendered: PathBuf = rel
            .components()
            .map(|c| render(c.as_os_str().to_string_lossy().as_ref(), name, description, systemd_order))
            .collect();
        let dest_path = dest.join(rel_rendered);

        if entry.file_type().is_dir() {
            fs::create_dir_all(&dest_path)
                .with_context(|| format!("create dir {}", dest_path.display()))?;
            continue;
        }

        if let Some(parent) = dest_path.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("create parent {}", parent.display()))?;
        }

        let raw = fs::read(entry.path())
            .with_context(|| format!("read {}", entry.path().display()))?;

        if raw.contains(&0u8) {
            // Binary file: copy verbatim
            fs::write(&dest_path, &raw)
                .with_context(|| format!("write {}", dest_path.display()))?;
        } else {
            let text = std::str::from_utf8(&raw).with_context(|| {
                format!("template file {} is not valid UTF-8", entry.path().display())
            })?;
            fs::write(&dest_path, render(text, name, description, systemd_order))
                .with_context(|| format!("write {}", dest_path.display()))?;
        }

        if is_executable_template(entry.path()) {
            set_executable(&dest_path)?;
        }
    }
    Ok(())
}

fn strip_for_kind(dest: &Path, kind: AppKind) -> Result<()> {
    let rm = |p: &str| -> Result<()> {
        let path = dest.join(p);
        if path.is_file() {
            fs::remove_file(&path).with_context(|| format!("remove {}", p))?;
        }
        Ok(())
    };
    let rm_dir = |p: &str| -> Result<()> {
        let path = dest.join(p);
        if path.is_dir() {
            fs::remove_dir_all(&path).with_context(|| format!("remove dir {}", p))?;
        }
        Ok(())
    };

    match kind {
        AppKind::Bun => {
            // Keep ui/index.html; remove React/Vite build setup and standalone files.
            rm("src/ipc.ts")?;
            rm_dir("systemd")?;
            rm_dir("ui/src")?;
            rm("ui/package.json")?;
            rm("ui/tsconfig.json")?;
            rm("ui/vite.config.ts")?;
            rm("ui/postcss.config.js")?;
            rm("ui/tailwind.config.js")?;
            rm("debian/postinst.standalone")?;
            rm("debian/prerm.standalone")?;
        }
        AppKind::BunFullstack => {
            // Keep full React UI; remove standalone files.
            rm("src/ipc.ts")?;
            rm_dir("systemd")?;
            rm("debian/postinst.standalone")?;
            rm("debian/prerm.standalone")?;
        }
        AppKind::StandaloneBun => {
            // Keep IPC client and systemd; remove platform UI.
            rm_dir("ui")?;
            // Promote standalone debian scripts
            promote_standalone_debian(dest)?;
        }
        AppKind::Cdylib => {
            // Platform cdylib: no UI, no binary, no systemd.
            rm("src/main.rs")?;
            rm("src/ipc.rs")?;
            rm("Cargo.standalone.toml")?;
            rm_dir("systemd")?;
            rm_dir("ui")?;
            rm("debian/postrm")?;
            rm("debian/postinst.standalone")?;
            rm("debian/prerm.standalone")?;
        }
        AppKind::CdylibFullstack => {
            // Platform cdylib with React UI.
            rm("src/main.rs")?;
            rm("src/ipc.rs")?;
            rm("Cargo.standalone.toml")?;
            rm_dir("systemd")?;
            rm("debian/postrm")?;
            rm("debian/postinst.standalone")?;
            rm("debian/prerm.standalone")?;
        }
        AppKind::StandaloneRust => {
            // Standalone binary: no cdylib lib.rs, no UI.
            rm("src/lib.rs")?;
            rm_dir("ui")?;
            // Swap Cargo.toml files: Cargo.standalone.toml → Cargo.toml
            let cargo_main = dest.join("Cargo.toml");
            let cargo_standalone = dest.join("Cargo.standalone.toml");
            if cargo_standalone.exists() {
                if cargo_main.exists() {
                    fs::remove_file(&cargo_main)
                        .with_context(|| "remove cdylib Cargo.toml")?;
                }
                fs::rename(&cargo_standalone, &cargo_main)
                    .with_context(|| "rename Cargo.standalone.toml -> Cargo.toml")?;
            }
            // Promote standalone debian scripts
            promote_standalone_debian(dest)?;
        }
    }

    patch_manifest(dest, kind)?;
    Ok(())
}

/// Replace debian/postinst and debian/prerm with their .standalone variants.
fn promote_standalone_debian(dest: &Path) -> Result<()> {
    for base in &["postinst", "prerm"] {
        let platform = dest.join("debian").join(base);
        let standalone = dest.join("debian").join(format!("{}.standalone", base));
        if standalone.exists() {
            if platform.exists() {
                fs::remove_file(&platform)
                    .with_context(|| format!("remove platform debian/{}", base))?;
            }
            fs::rename(&standalone, &platform)
                .with_context(|| format!("rename debian/{}.standalone -> debian/{}", base, base))?;
        }
    }
    Ok(())
}

fn patch_manifest(dest: &Path, kind: AppKind) -> Result<()> {
    let path = dest.join("manifest.json");
    if !path.exists() {
        return Ok(());
    }

    let raw = fs::read_to_string(&path).context("read manifest.json")?;
    let mut v: serde_json::Value = serde_json::from_str(&raw).context("parse manifest.json")?;

    let obj = v.as_object_mut().ok_or_else(|| anyhow!("manifest.json is not a JSON object"))?;

    match kind {
        AppKind::Bun => {
            obj.insert("app_type".into(), serde_json::json!("bun"));
            obj.insert("has_ui".into(), serde_json::json!(true));
            obj.insert("ui_path".into(), serde_json::json!("ui"));
        }
        AppKind::BunFullstack => {
            obj.insert("app_type".into(), serde_json::json!("bun"));
            obj.insert("has_ui".into(), serde_json::json!(true));
            // ui_path stays as-is from template (e.g. "dist" or "ui/dist")
        }
        AppKind::StandaloneBun => {
            obj.insert("app_type".into(), serde_json::json!("standalone"));
            obj.insert("has_ui".into(), serde_json::json!(false));
            obj.remove("ui_path");
            if let Some(caps) = obj.get_mut("capabilities") {
                if let Some(c) = caps.as_object_mut() {
                    c.insert("requires".into(), serde_json::json!([]));
                    c.insert("provides".into(), serde_json::json!([]));
                }
            }
            obj.remove("provides");
        }
        AppKind::Cdylib => {
            obj.insert("app_type".into(), serde_json::json!("native"));
            obj.insert("has_ui".into(), serde_json::json!(false));
            obj.remove("ui_path");
        }
        AppKind::CdylibFullstack => {
            obj.insert("app_type".into(), serde_json::json!("native"));
            obj.insert("has_ui".into(), serde_json::json!(true));
            // ui_path stays as-is
        }
        AppKind::StandaloneRust => {
            obj.insert("app_type".into(), serde_json::json!("standalone"));
            obj.insert("has_ui".into(), serde_json::json!(false));
            obj.remove("ui_path");
            if let Some(caps) = obj.get_mut("capabilities") {
                if let Some(c) = caps.as_object_mut() {
                    c.insert("requires".into(), serde_json::json!([]));
                    c.insert("provides".into(), serde_json::json!([]));
                }
            }
            obj.remove("provides");
        }
    }

    let out = serde_json::to_string_pretty(&v).context("serialize manifest.json")?;
    fs::write(&path, out + "\n").context("write manifest.json")?;
    Ok(())
}

fn render(template: &str, name: &str, description: &str, systemd_order: &str) -> String {
    template
        .replace("{{name}}", name)
        .replace("{{description}}", description)
        .replace("{{systemd_order}}", systemd_order)
}

// ── SDK version resolution ────────────────────────────────────────────────────

/// Scan the scaffolded project for `package.json` and `Cargo.toml` files that
/// reference the node SDK packages and replace their versions with the latest
/// published releases.  Failures are non-fatal — the template version is kept.
fn patch_sdk_versions(dest: &Path) {
    if let Some(v) = latest_npm_version("@econ-v1/app-sdk") {
        patch_npm_dep(dest, "@econ-v1/app-sdk", &v);
    }
    if let Some(v) = latest_cargo_version("node-app-sdk-rust") {
        patch_cargo_dep(dest, "node-app-sdk-rust", &v);
    }
}

fn latest_npm_version(pkg: &str) -> Option<String> {
    let encoded = pkg.replace('/', "%2F");
    let url = format!("https://registry.npmjs.org/{encoded}/latest");
    let resp: serde_json::Value = ureq::get(&url)
        .set("Accept", "application/json")
        .call()
        .ok()?
        .into_json()
        .ok()?;
    resp.get("version")?.as_str().map(String::from)
}

fn latest_cargo_version(krate: &str) -> Option<String> {
    let url = format!("https://crates.io/api/v1/crates/{krate}");
    let resp: serde_json::Value = ureq::get(&url)
        .set("Accept", "application/json")
        .set("User-Agent", "node-app-build/0.1.0 (https://github.com/econ-v1/node)")
        .call()
        .ok()?
        .into_json()
        .ok()?;
    resp.pointer("/crate/newest_version")?.as_str().map(String::from)
}

fn patch_npm_dep(dest: &Path, pkg: &str, version: &str) {
    let path = dest.join("package.json");
    if !path.exists() {
        return;
    }
    let Ok(text) = fs::read_to_string(&path) else { return };
    let Ok(mut json) = serde_json::from_str::<serde_json::Value>(&text) else { return };

    let patched = patch_json_dep(&mut json, pkg, version);
    if patched {
        if let Ok(out) = serde_json::to_string_pretty(&json) {
            let _ = fs::write(&path, out + "\n");
            println!("  → @econ-v1/app-sdk pinned to {version} (latest)");
        }
    }
}

fn patch_json_dep(json: &mut serde_json::Value, pkg: &str, version: &str) -> bool {
    let mut patched = false;
    for section in ["dependencies", "devDependencies", "peerDependencies"] {
        if let Some(deps) = json.get_mut(section).and_then(|d| d.as_object_mut()) {
            if deps.contains_key(pkg) {
                deps.insert(pkg.to_string(), serde_json::Value::String(version.to_string()));
                patched = true;
            }
        }
    }
    patched
}

fn patch_cargo_dep(dest: &Path, krate: &str, version: &str) {
    // Walk one level: Cargo.toml at root, or inside a workspace member.
    for candidate in [dest.join("Cargo.toml")] {
        if !candidate.exists() {
            continue;
        }
        let Ok(text) = fs::read_to_string(&candidate) else { continue };
        // Simple line-level replacement: handles `crate = "..."` and `crate = { version = "..." ... }`.
        let mut changed = false;
        let new_text: String = text
            .lines()
            .map(|line| {
                if line.trim_start().starts_with(krate)
                    && (line.contains("= \"") || line.contains("version ="))
                {
                    changed = true;
                    // Replace any quoted version string on this line.
                    replace_toml_version(line, version)
                } else {
                    line.to_string()
                }
            })
            .collect::<Vec<_>>()
            .join("\n")
            + "\n";
        if changed {
            let _ = fs::write(&candidate, new_text);
            println!("  → node-app-sdk-rust pinned to {version} (latest)");
        }
    }
}

/// Replace the first quoted version string in a TOML line, preserving the rest.
fn replace_toml_version(line: &str, new_version: &str) -> String {
    // Match `= "..."` or `version = "..."` — replace the content inside quotes.
    let mut result = String::new();
    let mut chars = line.chars().peekable();
    let mut replaced = false;
    while let Some(c) = chars.next() {
        if c == '"' && !replaced {
            result.push('"');
            // Skip until closing quote.
            for inner in chars.by_ref() {
                if inner == '"' {
                    break;
                }
            }
            result.push_str(new_version);
            result.push('"');
            replaced = true;
        } else {
            result.push(c);
        }
    }
    result
}

// ── Validation ────────────────────────────────────────────────────────────────

fn validate_name(name: &str) -> Result<()> {
    if name.is_empty() {
        bail!("name cannot be empty");
    }
    if name.starts_with("node-app-") {
        bail!("name must not start with 'node-app-'; the .deb script adds the prefix");
    }
    if !name
        .chars()
        .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
    {
        bail!("name '{}' must be lowercase alphanumeric + hyphens", name);
    }
    if !name
        .chars()
        .next()
        .map(|c| c.is_ascii_lowercase())
        .unwrap_or(false)
    {
        bail!("name '{}' must start with a lowercase letter", name);
    }
    Ok(())
}

fn validate_github_slug(slug: &str) -> Result<()> {
    let parts: Vec<&str> = slug.split('/').collect();
    if parts.len() != 2 || parts[0].is_empty() || parts[1].is_empty() {
        bail!("--github value '{}' must be in the form 'org/repo'", slug);
    }
    if slug.chars().any(|c| c.is_whitespace()) {
        bail!("--github value '{}' contains whitespace", slug);
    }
    Ok(())
}

// ── Post-scaffold steps ───────────────────────────────────────────────────────

fn run_deps_update(dest: &Path, kind: AppKind) -> Result<()> {
    match kind {
        AppKind::Bun | AppKind::BunFullstack | AppKind::StandaloneBun => {
            if which("bun").is_some() {
                run_in(dest, "bun", &["install"], "bun install")?;
            } else {
                eprintln!(
                    "→ skipping `bun install` (bun not found on PATH; install from https://bun.sh)"
                );
            }
        }
        AppKind::Cdylib | AppKind::CdylibFullstack | AppKind::StandaloneRust => {
            if which("cargo").is_some() {
                run_in(dest, "cargo", &["update"], "cargo update")?;
            } else {
                eprintln!(
                    "→ skipping `cargo update` (cargo not found on PATH; \
                     install Rust from https://rustup.rs)"
                );
            }
        }
    }
    Ok(())
}

fn init_git(dest: &Path, name: &str) -> Result<()> {
    if which("git").is_none() {
        eprintln!("→ skipping `git init` (git not found on PATH)");
        return Ok(());
    }

    let git_exists = dest.join(".git").is_dir();

    if !git_exists {
        run_in(dest, "git", &["init", "-q", "-b", "main"], "git init")?;
    }

    // Stage all files regardless — handles fresh init and re-scaffold into existing repo.
    run_in(dest, "git", &["add", "."], "git add .")?;

    // Commit only when there is something staged; skip silently otherwise.
    let has_staged = Command::new("git")
        .current_dir(dest)
        .args(["diff", "--cached", "--quiet"])
        .status()
        .map(|s| !s.success()) // exit 1 = changes staged
        .unwrap_or(false);

    if has_staged {
        let msg = format!("chore: scaffold {} via node-app new", name);
        run_in(dest, "git", &["commit", "-q", "-m", &msg], "git commit")?;
    }

    println!(
        "✓ Initialized git repo in {} (branch: main)",
        dest.display()
    );
    Ok(())
}

fn create_github_repo(dest: &Path, slug: &str) -> Result<()> {
    if which("gh").is_none() {
        eprintln!(
            "→ skipping `gh repo create` (gh CLI not found on PATH; install from https://cli.github.com).\n\
             Local scaffold is still ready. To bootstrap manually:\n  \
             gh repo create {} --private --source={} --push",
            slug,
            dest.display()
        );
        return Ok(());
    }

    let auth_ok = Command::new("gh")
        .args(["auth", "status"])
        .status()
        .map(|s| s.success())
        .unwrap_or(false);
    if !auth_ok {
        eprintln!(
            "→ `gh auth status` failed — running `gh auth login` first is recommended.\n\
             Continuing with repo creation; you may be prompted to authenticate."
        );
    }

    // Create the repo without --source/--push to avoid "Unable to add remote origin"
    // when origin already exists in the local git repo.
    let create_ok = Command::new("gh")
        .current_dir(dest)
        .args(["repo", "create", slug, "--private"])
        .status()
        .with_context(|| format!("invoke `gh repo create {}`", slug))?
        .success();
    if !create_ok {
        bail!(
            "gh repo create failed. Common causes: repo already exists, \
             org permissions missing, or auth expired. Local scaffold remains intact at {}.",
            dest.display()
        );
    }

    // Set or update origin to point at the new repo.
    let remote_url = format!("https://github.com/{}.git", slug);
    let has_origin = Command::new("git")
        .current_dir(dest)
        .args(["remote", "get-url", "origin"])
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);
    if has_origin {
        run_in(
            dest,
            "git",
            &["remote", "set-url", "origin", &remote_url],
            "git remote set-url origin",
        )?;
    } else {
        run_in(
            dest,
            "git",
            &["remote", "add", "origin", &remote_url],
            "git remote add origin",
        )?;
    }

    run_in(
        dest,
        "git",
        &["push", "-u", "origin", "HEAD"],
        "git push",
    )?;
    println!(
        "✓ Created GitHub repo https://github.com/{} and pushed initial commit",
        slug
    );

    upload_release_secrets(slug);
    Ok(())
}

fn upload_release_secrets(slug: &str) {
    let keys_dir = std::env::var_os("NODE_DEV_KEYS_DIR")
        .map(PathBuf::from)
        .unwrap_or_else(|| {
            std::env::var_os("HOME")
                .map(PathBuf::from)
                .unwrap_or_default()
                .join(".config/node")
        });

    let secrets: &[(&str, &str, &str)] = &[
        (
            "GPG_PRIVATE_KEY",
            "gpg-private-key.asc",
            "Generate with `gpg --armor --export-secret-keys <key-id>` and save to this path",
        ),
        (
            "GPG_PASSPHRASE",
            "gpg-passphrase.txt",
            "Plain-text passphrase matching GPG_PRIVATE_KEY",
        ),
        (
            "APT_REPO_DISPATCH_TOKEN",
            "apt-repo-dispatch-token.txt",
            "GitHub fine-grained PAT with `actions:write` on econ-v1/node-releases",
        ),
    ];

    for (name, basename, hint) in secrets {
        let path = keys_dir.join(basename);
        if !path.exists() {
            eprintln!(
                "→ secret {} not found at {}. {}.\n  \
                 Set manually later: `gh secret set {} --repo {} < /path/to/secret`",
                name,
                path.display(),
                hint,
                name,
                slug
            );
            continue;
        }
        let status = Command::new("gh")
            .args(["secret", "set", name, "--repo", slug])
            .stdin(fs::File::open(&path).expect("opened above"))
            .status();
        match status {
            Ok(s) if s.success() => println!("✓ Set GitHub secret {} on {}", name, slug),
            Ok(s) => eprintln!(
                "→ `gh secret set {}` exited {} — set manually with: \
                 `gh secret set {} --repo {} < {}`",
                name,
                s.code().unwrap_or(-1),
                name,
                slug,
                path.display()
            ),
            Err(e) => eprintln!(
                "→ failed to invoke `gh secret set {}`: {}. \
                 Set manually: `gh secret set {} --repo {} < {}`",
                name, e, name, slug, path.display()
            ),
        }
    }
}

// ── Utilities ─────────────────────────────────────────────────────────────────

fn ensure_gh() -> Result<()> {
    if which("gh").is_none() {
        bail!(
            "gh CLI not found on PATH.\n\
             Install from https://cli.github.com then authenticate with `gh auth login`.\n\
             Templates are fetched from GitHub — gh is required for remote repos.\n\
             For offline use, set NODE_APP_TEMPLATES_REPO to a local directory path."
        );
    }
    Ok(())
}

fn run_in(dest: &Path, cmd: &str, args: &[&str], label: &str) -> Result<()> {
    let status = Command::new(cmd)
        .current_dir(dest)
        .args(args)
        .status()
        .with_context(|| format!("invoke `{}`", label))?;
    if !status.success() {
        bail!("`{}` exited {}", label, status.code().unwrap_or(-1));
    }
    Ok(())
}

fn which(bin: &str) -> Option<PathBuf> {
    let path = std::env::var_os("PATH")?;
    for dir in std::env::split_paths(&path) {
        let candidate = dir.join(bin);
        if candidate.is_file() {
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                if let Ok(meta) = candidate.metadata() {
                    if meta.permissions().mode() & 0o111 != 0 {
                        return Some(candidate);
                    }
                }
            }
            #[cfg(not(unix))]
            {
                return Some(candidate);
            }
        }
    }
    None
}

fn tmp_dir() -> Result<PathBuf> {
    let ts = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    Ok(std::env::temp_dir().join(format!("node-app-templates-{ts}")))
}

fn is_executable_template(path: &Path) -> bool {
    matches!(
        path.file_name().and_then(|s| s.to_str()),
        Some("postinst") | Some("prerm") | Some("postrm") | Some("preinst")
    )
}

fn set_executable(path: &Path) -> Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perm = fs::metadata(path)?.permissions();
        perm.set_mode(0o755);
        fs::set_permissions(path, perm)?;
    }
    #[cfg(not(unix))]
    {
        let _ = path;
    }
    Ok(())
}