-
-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathtemplate-manager.ts
More file actions
958 lines (831 loc) · 35.2 KB
/
template-manager.ts
File metadata and controls
958 lines (831 loc) · 35.2 KB
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
import fs from "fs-extra";
import path from "node:path";
import { glob } from "tinyglobby";
import type { ProjectConfig } from "../../types";
import { PKG_ROOT } from "../../constants";
import { processTemplate } from "../../utils/template-processor";
export async function processAndCopyFiles(
sourcePattern: string | string[],
baseSourceDir: string,
destDir: string,
context: ProjectConfig,
overwrite = true,
ignorePatterns?: string[],
) {
const sourceFiles = await glob(sourcePattern, {
cwd: baseSourceDir,
dot: true,
onlyFiles: true,
absolute: false,
ignore: ignorePatterns,
});
for (const relativeSrcPath of sourceFiles) {
const srcPath = path.join(baseSourceDir, relativeSrcPath);
let relativeDestPath = relativeSrcPath;
if (relativeSrcPath.endsWith(".hbs")) {
relativeDestPath = relativeSrcPath.slice(0, -4);
}
const basename = path.basename(relativeDestPath);
if (basename === "_gitignore") {
relativeDestPath = path.join(path.dirname(relativeDestPath), ".gitignore");
} else if (basename === "_npmrc") {
relativeDestPath = path.join(path.dirname(relativeDestPath), ".npmrc");
} else if (basename === "_envrc") {
relativeDestPath = path.join(path.dirname(relativeDestPath), ".envrc");
}
const destPath = path.join(destDir, relativeDestPath);
await fs.ensureDir(path.dirname(destPath));
if (!overwrite && (await fs.pathExists(destPath))) {
continue;
}
await processTemplate(srcPath, destPath, context);
}
}
export async function copyBaseTemplate(projectDir: string, context: ProjectConfig) {
const templateDir = path.join(PKG_ROOT, "templates/base");
await processAndCopyFiles(["**/*"], templateDir, projectDir, context);
}
export async function setupFrontendTemplates(projectDir: string, context: ProjectConfig) {
const hasReactWeb = context.frontend.some((f) =>
["tanstack-router", "react-router", "tanstack-start", "next"].includes(f),
);
const hasNuxtWeb = context.frontend.includes("nuxt");
const hasSvelteWeb = context.frontend.includes("svelte");
const hasSolidWeb = context.frontend.includes("solid");
const hasNativeBare = context.frontend.includes("native-bare");
const hasNativeUniwind = context.frontend.includes("native-uniwind");
const hasUnistyles = context.frontend.includes("native-unistyles");
const _hasNative = hasNativeBare || hasNativeUniwind || hasUnistyles;
const isConvex = context.backend === "convex";
if (hasReactWeb || hasNuxtWeb || hasSvelteWeb || hasSolidWeb) {
const webAppDir = path.join(projectDir, "apps/web");
await fs.ensureDir(webAppDir);
if (hasReactWeb) {
const webBaseDir = path.join(PKG_ROOT, "templates/frontend/react/web-base");
if (await fs.pathExists(webBaseDir)) {
await processAndCopyFiles("**/*", webBaseDir, webAppDir, context);
} else {
}
const reactFramework = context.frontend.find((f) =>
["tanstack-router", "react-router", "tanstack-start", "next"].includes(f),
);
if (reactFramework) {
const frameworkSrcDir = path.join(PKG_ROOT, `templates/frontend/react/${reactFramework}`);
if (await fs.pathExists(frameworkSrcDir)) {
await processAndCopyFiles("**/*", frameworkSrcDir, webAppDir, context);
} else {
}
if (!isConvex && context.api !== "none") {
const apiWebBaseDir = path.join(PKG_ROOT, `templates/api/${context.api}/web/react/base`);
if (await fs.pathExists(apiWebBaseDir)) {
await processAndCopyFiles("**/*", apiWebBaseDir, webAppDir, context);
} else {
}
}
if (
context.backend === "self" &&
(reactFramework === "next" || reactFramework === "tanstack-start") &&
context.api !== "none"
) {
const apiFullstackDir = path.join(
PKG_ROOT,
`templates/api/${context.api}/fullstack/${reactFramework}`,
);
if (await fs.pathExists(apiFullstackDir)) {
await processAndCopyFiles("**/*", apiFullstackDir, webAppDir, context);
}
}
}
} else if (hasNuxtWeb) {
const nuxtBaseDir = path.join(PKG_ROOT, "templates/frontend/nuxt");
if (await fs.pathExists(nuxtBaseDir)) {
await processAndCopyFiles("**/*", nuxtBaseDir, webAppDir, context);
} else {
}
if (!isConvex && context.api === "orpc") {
const apiWebNuxtDir = path.join(PKG_ROOT, `templates/api/${context.api}/web/nuxt`);
if (await fs.pathExists(apiWebNuxtDir)) {
await processAndCopyFiles("**/*", apiWebNuxtDir, webAppDir, context);
} else {
}
}
} else if (hasSvelteWeb) {
const svelteBaseDir = path.join(PKG_ROOT, "templates/frontend/svelte");
if (await fs.pathExists(svelteBaseDir)) {
await processAndCopyFiles("**/*", svelteBaseDir, webAppDir, context);
} else {
}
if (!isConvex && context.api === "orpc") {
const apiWebSvelteDir = path.join(PKG_ROOT, `templates/api/${context.api}/web/svelte`);
if (await fs.pathExists(apiWebSvelteDir)) {
await processAndCopyFiles("**/*", apiWebSvelteDir, webAppDir, context);
} else {
}
}
} else if (hasSolidWeb) {
const solidBaseDir = path.join(PKG_ROOT, "templates/frontend/solid");
if (await fs.pathExists(solidBaseDir)) {
await processAndCopyFiles("**/*", solidBaseDir, webAppDir, context);
} else {
}
if (!isConvex && context.api === "orpc") {
const apiWebSolidDir = path.join(PKG_ROOT, `templates/api/${context.api}/web/solid`);
if (await fs.pathExists(apiWebSolidDir)) {
await processAndCopyFiles("**/*", apiWebSolidDir, webAppDir, context);
} else {
}
}
}
}
if (hasNativeBare || hasNativeUniwind || hasUnistyles) {
const nativeAppDir = path.join(projectDir, "apps/native");
await fs.ensureDir(nativeAppDir);
const nativeBaseCommonDir = path.join(PKG_ROOT, "templates/frontend/native/base");
if (await fs.pathExists(nativeBaseCommonDir)) {
await processAndCopyFiles("**/*", nativeBaseCommonDir, nativeAppDir, context);
} else {
}
let nativeFrameworkPath = "";
if (hasNativeBare) {
nativeFrameworkPath = "bare";
} else if (hasNativeUniwind) {
nativeFrameworkPath = "uniwind";
} else if (hasUnistyles) {
nativeFrameworkPath = "unistyles";
}
const nativeSpecificDir = path.join(
PKG_ROOT,
`templates/frontend/native/${nativeFrameworkPath}`,
);
if (await fs.pathExists(nativeSpecificDir)) {
await processAndCopyFiles("**/*", nativeSpecificDir, nativeAppDir, context, true);
}
if (!isConvex && (context.api === "trpc" || context.api === "orpc")) {
const apiNativeSrcDir = path.join(PKG_ROOT, `templates/api/${context.api}/native`);
if (await fs.pathExists(apiNativeSrcDir)) {
await processAndCopyFiles("**/*", apiNativeSrcDir, nativeAppDir, context);
}
}
}
}
async function setupApiPackage(projectDir: string, context: ProjectConfig) {
if (context.api === "none") return;
const apiPackageDir = path.join(projectDir, "packages/api");
await fs.ensureDir(apiPackageDir);
const apiServerDir = path.join(PKG_ROOT, `templates/api/${context.api}/server`);
if (await fs.pathExists(apiServerDir)) {
await processAndCopyFiles("**/*", apiServerDir, apiPackageDir, context);
}
}
async function setupConfigPackage(projectDir: string, context: ProjectConfig) {
const configPackageDir = path.join(projectDir, "packages/config");
await fs.ensureDir(configPackageDir);
const configBaseDir = path.join(PKG_ROOT, "templates/packages/config");
if (await fs.pathExists(configBaseDir)) {
await processAndCopyFiles("**/*", configBaseDir, configPackageDir, context);
}
}
async function setupDbPackage(projectDir: string, context: ProjectConfig) {
if (context.database === "none" || context.orm === "none") return;
const dbPackageDir = path.join(projectDir, "packages/db");
await fs.ensureDir(dbPackageDir);
const dbBaseDir = path.join(PKG_ROOT, "templates/db/base");
if (await fs.pathExists(dbBaseDir)) {
await processAndCopyFiles("**/*", dbBaseDir, dbPackageDir, context);
}
const dbOrmBaseDir = path.join(PKG_ROOT, `templates/db/${context.orm}/base`);
if (await fs.pathExists(dbOrmBaseDir)) {
await processAndCopyFiles("**/*", dbOrmBaseDir, dbPackageDir, context);
}
const dbOrmSrcDir = path.join(PKG_ROOT, `templates/db/${context.orm}/${context.database}`);
if (await fs.pathExists(dbOrmSrcDir)) {
await processAndCopyFiles("**/*", dbOrmSrcDir, dbPackageDir, context);
}
}
async function setupConvexBackend(projectDir: string, context: ProjectConfig) {
const serverAppDir = path.join(projectDir, "apps/server");
if (await fs.pathExists(serverAppDir)) {
await fs.remove(serverAppDir);
}
const convexBackendDestDir = path.join(projectDir, "packages/backend");
const convexSrcDir = path.join(PKG_ROOT, "templates/backend/convex/packages/backend");
await fs.ensureDir(convexBackendDestDir);
if (await fs.pathExists(convexSrcDir)) {
await processAndCopyFiles("**/*", convexSrcDir, convexBackendDestDir, context);
}
}
async function setupServerApp(projectDir: string, context: ProjectConfig) {
const serverAppDir = path.join(projectDir, "apps/server");
await fs.ensureDir(serverAppDir);
const serverBaseDir = path.join(PKG_ROOT, "templates/backend/server/base");
if (await fs.pathExists(serverBaseDir)) {
await processAndCopyFiles("**/*", serverBaseDir, serverAppDir, context);
}
const frameworkSrcDir = path.join(PKG_ROOT, `templates/backend/server/${context.backend}`);
if (await fs.pathExists(frameworkSrcDir)) {
await processAndCopyFiles("**/*", frameworkSrcDir, serverAppDir, context, true);
}
}
async function setupEnvPackage(projectDir: string, context: ProjectConfig) {
const hasWebFrontend = context.frontend.some((f) =>
[
"tanstack-router",
"react-router",
"tanstack-start",
"next",
"nuxt",
"svelte",
"solid",
].includes(f),
);
const hasNative = context.frontend.some((f) =>
["native-bare", "native-uniwind", "native-unistyles"].includes(f),
);
if (!hasWebFrontend && !hasNative && context.backend === "none") {
return;
}
const envPackageDir = path.join(projectDir, "packages/env");
await fs.ensureDir(envPackageDir);
const envBaseDir = path.join(PKG_ROOT, "templates/packages/env");
const packageJsonSrc = path.join(envBaseDir, "package.json.hbs");
if (await fs.pathExists(packageJsonSrc)) {
await processAndCopyFiles("package.json.hbs", envBaseDir, envPackageDir, context);
}
const tsconfigSrc = path.join(envBaseDir, "tsconfig.json.hbs");
if (await fs.pathExists(tsconfigSrc)) {
await processAndCopyFiles("tsconfig.json.hbs", envBaseDir, envPackageDir, context);
}
const needsServerEnv = context.backend !== "none" && context.backend !== "convex";
if (needsServerEnv) {
const serverSrc = path.join(envBaseDir, "src/server.ts.hbs");
if (await fs.pathExists(serverSrc)) {
await fs.ensureDir(path.join(envPackageDir, "src"));
await processAndCopyFiles("src/server.ts.hbs", envBaseDir, envPackageDir, context);
}
}
if (hasWebFrontend) {
const webSrc = path.join(envBaseDir, "src/web.ts.hbs");
if (await fs.pathExists(webSrc)) {
await fs.ensureDir(path.join(envPackageDir, "src"));
await processAndCopyFiles("src/web.ts.hbs", envBaseDir, envPackageDir, context);
}
}
if (hasNative) {
const nativeSrc = path.join(envBaseDir, "src/native.ts.hbs");
if (await fs.pathExists(nativeSrc)) {
await fs.ensureDir(path.join(envPackageDir, "src"));
await processAndCopyFiles("src/native.ts.hbs", envBaseDir, envPackageDir, context);
}
}
const packageJsonPath = path.join(envPackageDir, "package.json");
if (await fs.pathExists(packageJsonPath)) {
const packageJson = await fs.readJson(packageJsonPath);
const exports: Record<string, string> = {};
if (needsServerEnv) {
exports["./server"] = "./src/server.ts";
}
if (hasWebFrontend) {
exports["./web"] = "./src/web.ts";
}
if (hasNative) {
exports["./native"] = "./src/native.ts";
}
packageJson.exports = exports;
await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 });
}
}
export async function setupBackendFramework(projectDir: string, context: ProjectConfig) {
await setupConfigPackage(projectDir, context);
await setupEnvPackage(projectDir, context);
if (context.backend === "none") {
return;
}
if (context.backend === "convex") {
await setupConvexBackend(projectDir, context);
return;
}
if (context.backend === "self") {
await setupApiPackage(projectDir, context);
await setupDbPackage(projectDir, context);
return;
}
await setupServerApp(projectDir, context);
await setupApiPackage(projectDir, context);
await setupDbPackage(projectDir, context);
}
export async function setupAuthTemplate(projectDir: string, context: ProjectConfig) {
if (!context.auth || context.auth === "none") return;
const serverAppDir = path.join(projectDir, "apps/server");
const webAppDir = path.join(projectDir, "apps/web");
const nativeAppDir = path.join(projectDir, "apps/native");
const serverAppDirExists = await fs.pathExists(serverAppDir);
const webAppDirExists = await fs.pathExists(webAppDir);
const nativeAppDirExists = await fs.pathExists(nativeAppDir);
const hasReactWeb = context.frontend.some((f) =>
["tanstack-router", "react-router", "tanstack-start", "next"].includes(f),
);
const hasNuxtWeb = context.frontend.includes("nuxt");
const hasSvelteWeb = context.frontend.includes("svelte");
const hasSolidWeb = context.frontend.includes("solid");
const hasNativeBare = context.frontend.includes("native-bare");
const hasUniwind = context.frontend.includes("native-uniwind");
const hasUnistyles = context.frontend.includes("native-unistyles");
const hasNative = hasNativeBare || hasUniwind || hasUnistyles;
const authProvider = context.auth;
if (context.backend === "convex" && authProvider === "clerk") {
const convexBackendDestDir = path.join(projectDir, "packages/backend");
const convexClerkBackendSrc = path.join(PKG_ROOT, "templates/auth/clerk/convex/backend");
if (await fs.pathExists(convexClerkBackendSrc)) {
await fs.ensureDir(convexBackendDestDir);
await processAndCopyFiles("**/*", convexClerkBackendSrc, convexBackendDestDir, context);
}
if (webAppDirExists) {
const reactFramework = context.frontend.find((f) =>
["tanstack-router", "react-router", "tanstack-start", "next"].includes(f),
);
if (reactFramework) {
const convexClerkWebSrc = path.join(
PKG_ROOT,
`templates/auth/clerk/convex/web/react/${reactFramework}`,
);
if (await fs.pathExists(convexClerkWebSrc)) {
await processAndCopyFiles("**/*", convexClerkWebSrc, webAppDir, context);
}
}
}
if (nativeAppDirExists) {
const convexClerkNativeBaseSrc = path.join(
PKG_ROOT,
"templates/auth/clerk/convex/native/base",
);
if (await fs.pathExists(convexClerkNativeBaseSrc)) {
await processAndCopyFiles("**/*", convexClerkNativeBaseSrc, nativeAppDir, context);
}
let nativeFrameworkPath = "";
if (hasNativeBare) nativeFrameworkPath = "bare";
else if (hasUniwind) nativeFrameworkPath = "uniwind";
else if (hasUnistyles) nativeFrameworkPath = "unistyles";
if (nativeFrameworkPath) {
const convexClerkNativeFrameworkSrc = path.join(
PKG_ROOT,
`templates/auth/clerk/convex/native/${nativeFrameworkPath}`,
);
if (await fs.pathExists(convexClerkNativeFrameworkSrc)) {
await processAndCopyFiles("**/*", convexClerkNativeFrameworkSrc, nativeAppDir, context);
}
}
}
return;
}
if (context.backend === "convex" && authProvider === "better-auth") {
const convexBackendDestDir = path.join(projectDir, "packages/backend");
const convexBetterAuthBackendSrc = path.join(
PKG_ROOT,
"templates/auth/better-auth/convex/backend",
);
if (await fs.pathExists(convexBetterAuthBackendSrc)) {
await fs.ensureDir(convexBackendDestDir);
await processAndCopyFiles("**/*", convexBetterAuthBackendSrc, convexBackendDestDir, context);
}
if (webAppDirExists && hasReactWeb) {
const convexBetterAuthWebBaseSrc = path.join(
PKG_ROOT,
"templates/auth/better-auth/convex/web/react/base",
);
if (await fs.pathExists(convexBetterAuthWebBaseSrc)) {
await processAndCopyFiles("**/*", convexBetterAuthWebBaseSrc, webAppDir, context);
}
const reactFramework = context.frontend.find((f) =>
["tanstack-router", "react-router", "tanstack-start", "next"].includes(f),
);
if (reactFramework) {
const convexBetterAuthWebSrc = path.join(
PKG_ROOT,
`templates/auth/better-auth/convex/web/react/${reactFramework}`,
);
if (await fs.pathExists(convexBetterAuthWebSrc)) {
await processAndCopyFiles("**/*", convexBetterAuthWebSrc, webAppDir, context);
}
}
}
if (nativeAppDirExists) {
const convexBetterAuthNativeBaseSrc = path.join(
PKG_ROOT,
"templates/auth/better-auth/convex/native/base",
);
if (await fs.pathExists(convexBetterAuthNativeBaseSrc)) {
await processAndCopyFiles("**/*", convexBetterAuthNativeBaseSrc, nativeAppDir, context);
}
let nativeFrameworkPath = "";
if (hasNativeBare) nativeFrameworkPath = "bare";
else if (hasUniwind) nativeFrameworkPath = "uniwind";
else if (hasUnistyles) nativeFrameworkPath = "unistyles";
if (nativeFrameworkPath) {
const convexBetterAuthNativeFrameworkSrc = path.join(
PKG_ROOT,
`templates/auth/better-auth/convex/native/${nativeFrameworkPath}`,
);
if (await fs.pathExists(convexBetterAuthNativeFrameworkSrc)) {
await processAndCopyFiles(
"**/*",
convexBetterAuthNativeFrameworkSrc,
nativeAppDir,
context,
);
}
}
}
return;
}
if ((serverAppDirExists || context.backend === "self") && context.backend !== "convex") {
const authPackageDir = path.join(projectDir, "packages/auth");
await fs.ensureDir(authPackageDir);
const authServerBaseSrc = path.join(PKG_ROOT, `templates/auth/${authProvider}/server/base`);
if (await fs.pathExists(authServerBaseSrc)) {
await processAndCopyFiles("**/*", authServerBaseSrc, authPackageDir, context);
}
if (context.orm !== "none" && context.database !== "none") {
const dbPackageDir = path.join(projectDir, "packages/db");
await fs.ensureDir(dbPackageDir);
const orm = context.orm;
const db = context.database;
let authDbSrc = "";
if (orm === "drizzle") {
authDbSrc = path.join(PKG_ROOT, `templates/auth/${authProvider}/server/db/drizzle/${db}`);
} else if (orm === "prisma") {
authDbSrc = path.join(PKG_ROOT, `templates/auth/${authProvider}/server/db/prisma/${db}`);
} else if (orm === "mongoose") {
authDbSrc = path.join(PKG_ROOT, `templates/auth/${authProvider}/server/db/mongoose/${db}`);
}
if (authDbSrc && (await fs.pathExists(authDbSrc))) {
await processAndCopyFiles("**/*", authDbSrc, dbPackageDir, context);
}
}
}
if ((hasReactWeb || hasNuxtWeb || hasSvelteWeb || hasSolidWeb) && webAppDirExists) {
if (hasReactWeb) {
const authWebBaseSrc = path.join(PKG_ROOT, `templates/auth/${authProvider}/web/react/base`);
if (await fs.pathExists(authWebBaseSrc)) {
await processAndCopyFiles("**/*", authWebBaseSrc, webAppDir, context);
}
const reactFramework = context.frontend.find((f) =>
["tanstack-router", "react-router", "tanstack-start", "next"].includes(f),
);
if (reactFramework) {
const authWebFrameworkSrc = path.join(
PKG_ROOT,
`templates/auth/${authProvider}/web/react/${reactFramework}`,
);
if (await fs.pathExists(authWebFrameworkSrc)) {
await processAndCopyFiles("**/*", authWebFrameworkSrc, webAppDir, context);
}
if (
context.backend === "self" &&
(reactFramework === "next" || reactFramework === "tanstack-start")
) {
const authFullstackSrc = path.join(
PKG_ROOT,
`templates/auth/${authProvider}/fullstack/${reactFramework}`,
);
if (await fs.pathExists(authFullstackSrc)) {
await processAndCopyFiles("**/*", authFullstackSrc, webAppDir, context);
}
}
}
} else if (hasNuxtWeb) {
const authWebNuxtSrc = path.join(PKG_ROOT, `templates/auth/${authProvider}/web/nuxt`);
if (await fs.pathExists(authWebNuxtSrc)) {
await processAndCopyFiles("**/*", authWebNuxtSrc, webAppDir, context);
}
} else if (hasSvelteWeb) {
const authWebSvelteSrc = path.join(PKG_ROOT, `templates/auth/${authProvider}/web/svelte`);
if (await fs.pathExists(authWebSvelteSrc)) {
await processAndCopyFiles("**/*", authWebSvelteSrc, webAppDir, context);
}
} else if (hasSolidWeb) {
const authWebSolidSrc = path.join(PKG_ROOT, `templates/auth/${authProvider}/web/solid`);
if (await fs.pathExists(authWebSolidSrc)) {
await processAndCopyFiles("**/*", authWebSolidSrc, webAppDir, context);
}
}
}
if (hasNative && nativeAppDirExists) {
const authNativeBaseSrc = path.join(PKG_ROOT, `templates/auth/${authProvider}/native/base`);
if (await fs.pathExists(authNativeBaseSrc)) {
await processAndCopyFiles("**/*", authNativeBaseSrc, nativeAppDir, context);
}
let nativeFrameworkAuthPath = "";
if (hasNativeBare) {
nativeFrameworkAuthPath = "bare";
} else if (hasUniwind) {
nativeFrameworkAuthPath = "uniwind";
} else if (hasUnistyles) {
nativeFrameworkAuthPath = "unistyles";
}
if (nativeFrameworkAuthPath) {
const authNativeFrameworkSrc = path.join(
PKG_ROOT,
`templates/auth/${authProvider}/native/${nativeFrameworkAuthPath}`,
);
if (await fs.pathExists(authNativeFrameworkSrc)) {
await processAndCopyFiles("**/*", authNativeFrameworkSrc, nativeAppDir, context);
}
}
}
}
export async function setupPaymentsTemplate(projectDir: string, context: ProjectConfig) {
if (!context.payments || context.payments === "none") return;
const serverAppDir = path.join(projectDir, "apps/server");
const webAppDir = path.join(projectDir, "apps/web");
const serverAppDirExists = await fs.pathExists(serverAppDir);
const webAppDirExists = await fs.pathExists(webAppDir);
if ((serverAppDirExists || context.backend === "self") && context.backend !== "convex") {
const authPackageDir = path.join(projectDir, "packages/auth");
await fs.ensureDir(authPackageDir);
const paymentsServerSrc = path.join(
PKG_ROOT,
`templates/payments/${context.payments}/server/base`,
);
if (await fs.pathExists(paymentsServerSrc)) {
await processAndCopyFiles("**/*", paymentsServerSrc, authPackageDir, context);
}
}
const hasReactWeb = context.frontend.some((f) =>
["tanstack-router", "react-router", "tanstack-start", "next"].includes(f),
);
const hasNuxtWeb = context.frontend.includes("nuxt");
const hasSvelteWeb = context.frontend.includes("svelte");
const hasSolidWeb = context.frontend.includes("solid");
if (webAppDirExists && (hasReactWeb || hasNuxtWeb || hasSvelteWeb || hasSolidWeb)) {
if (hasReactWeb) {
const reactFramework = context.frontend.find((f) =>
["tanstack-router", "react-router", "tanstack-start", "next"].includes(f),
);
if (reactFramework) {
const paymentsWebSrc = path.join(
PKG_ROOT,
`templates/payments/${context.payments}/web/react/${reactFramework}`,
);
if (await fs.pathExists(paymentsWebSrc)) {
await processAndCopyFiles("**/*", paymentsWebSrc, webAppDir, context);
}
}
} else if (hasNuxtWeb) {
const paymentsWebNuxtSrc = path.join(
PKG_ROOT,
`templates/payments/${context.payments}/web/nuxt`,
);
if (await fs.pathExists(paymentsWebNuxtSrc)) {
await processAndCopyFiles("**/*", paymentsWebNuxtSrc, webAppDir, context);
}
} else if (hasSvelteWeb) {
const paymentsWebSvelteSrc = path.join(
PKG_ROOT,
`templates/payments/${context.payments}/web/svelte`,
);
if (await fs.pathExists(paymentsWebSvelteSrc)) {
await processAndCopyFiles("**/*", paymentsWebSvelteSrc, webAppDir, context);
}
} else if (hasSolidWeb) {
const paymentsWebSolidSrc = path.join(
PKG_ROOT,
`templates/payments/${context.payments}/web/solid`,
);
if (await fs.pathExists(paymentsWebSolidSrc)) {
await processAndCopyFiles("**/*", paymentsWebSolidSrc, webAppDir, context);
}
}
}
}
export async function setupAddonsTemplate(projectDir: string, context: ProjectConfig) {
if (!context.addons || context.addons.length === 0) return;
for (const addon of context.addons) {
if (addon === "none") continue;
let addonSrcDir = path.join(PKG_ROOT, `templates/addons/${addon}`);
let addonDestDir = projectDir;
if (addon === "pwa") {
const webAppDir = path.join(projectDir, "apps/web");
if (!(await fs.pathExists(webAppDir))) {
continue;
}
addonDestDir = webAppDir;
if (context.frontend.includes("next")) {
addonSrcDir = path.join(PKG_ROOT, "templates/addons/pwa/apps/web/next");
} else if (
context.frontend.some((f) => ["tanstack-router", "react-router", "solid"].includes(f))
) {
addonSrcDir = path.join(PKG_ROOT, "templates/addons/pwa/apps/web/vite");
} else {
continue;
}
}
if (await fs.pathExists(addonSrcDir)) {
await processAndCopyFiles("**/*", addonSrcDir, addonDestDir, context);
} else {
}
}
}
export async function setupExamplesTemplate(projectDir: string, context: ProjectConfig) {
if (!context.examples || context.examples.length === 0 || context.examples[0] === "none") {
return;
}
const serverAppDir = path.join(projectDir, "apps/server");
const webAppDir = path.join(projectDir, "apps/web");
const serverAppDirExists = await fs.pathExists(serverAppDir);
const webAppDirExists = await fs.pathExists(webAppDir);
const nativeAppDir = path.join(projectDir, "apps/native");
const nativeAppDirExists = await fs.pathExists(nativeAppDir);
const hasReactWeb = context.frontend.some((f) =>
["tanstack-router", "react-router", "tanstack-start", "next"].includes(f),
);
const hasNuxtWeb = context.frontend.includes("nuxt");
const hasSvelteWeb = context.frontend.includes("svelte");
const hasSolidWeb = context.frontend.includes("solid");
for (const example of context.examples) {
if (example === "none") continue;
const exampleBaseDir = path.join(PKG_ROOT, `templates/examples/${example}`);
if (context.backend === "convex") {
const convexBackendDestDir = path.join(projectDir, "packages/backend");
const convexExampleSrc = path.join(exampleBaseDir, "convex/packages/backend");
if (await fs.pathExists(convexExampleSrc)) {
await processAndCopyFiles("**/*", convexExampleSrc, convexBackendDestDir, context, false);
}
} else if ((serverAppDirExists || context.backend === "self") && context.backend !== "none") {
const exampleServerSrc = path.join(exampleBaseDir, "server");
if (context.api !== "none") {
const apiPackageDir = path.join(projectDir, "packages/api");
await fs.ensureDir(apiPackageDir);
const exampleOrmBaseSrc = path.join(exampleServerSrc, context.orm, "base");
if (await fs.pathExists(exampleOrmBaseSrc)) {
await processAndCopyFiles("**/*", exampleOrmBaseSrc, apiPackageDir, context, false);
}
}
if (context.orm !== "none" && context.database !== "none") {
const dbPackageDir = path.join(projectDir, "packages/db");
await fs.ensureDir(dbPackageDir);
const exampleDbSchemaSrc = path.join(exampleServerSrc, context.orm, context.database);
if (await fs.pathExists(exampleDbSchemaSrc)) {
await processAndCopyFiles("**/*", exampleDbSchemaSrc, dbPackageDir, context, false);
}
}
}
if (webAppDirExists) {
if (hasReactWeb) {
const exampleWebSrc = path.join(exampleBaseDir, "web/react");
if (await fs.pathExists(exampleWebSrc)) {
const reactFramework = context.frontend.find((f) =>
["next", "react-router", "tanstack-router", "tanstack-start"].includes(f),
);
if (reactFramework) {
const exampleWebFrameworkSrc = path.join(exampleWebSrc, reactFramework);
if (await fs.pathExists(exampleWebFrameworkSrc)) {
await processAndCopyFiles("**/*", exampleWebFrameworkSrc, webAppDir, context, false);
} else {
}
if (
context.backend === "self" &&
(reactFramework === "next" || reactFramework === "tanstack-start")
) {
const exampleFullstackSrc = path.join(exampleBaseDir, `fullstack/${reactFramework}`);
if (await fs.pathExists(exampleFullstackSrc)) {
await processAndCopyFiles("**/*", exampleFullstackSrc, webAppDir, context, false);
}
}
}
}
} else if (hasNuxtWeb) {
const exampleWebNuxtSrc = path.join(exampleBaseDir, "web/nuxt");
if (await fs.pathExists(exampleWebNuxtSrc)) {
await processAndCopyFiles("**/*", exampleWebNuxtSrc, webAppDir, context, false);
} else {
}
} else if (hasSvelteWeb) {
const exampleWebSvelteSrc = path.join(exampleBaseDir, "web/svelte");
if (await fs.pathExists(exampleWebSvelteSrc)) {
await processAndCopyFiles("**/*", exampleWebSvelteSrc, webAppDir, context, false);
} else {
}
} else if (hasSolidWeb) {
const exampleWebSolidSrc = path.join(exampleBaseDir, "web/solid");
if (await fs.pathExists(exampleWebSolidSrc)) {
await processAndCopyFiles("**/*", exampleWebSolidSrc, webAppDir, context, false);
} else {
}
}
}
if (nativeAppDirExists) {
const hasNativeBare = context.frontend.includes("native-bare");
const hasUniwind = context.frontend.includes("native-uniwind");
const hasUnistyles = context.frontend.includes("native-unistyles");
if (hasNativeBare || hasUniwind || hasUnistyles) {
let nativeFramework = "";
if (hasNativeBare) {
nativeFramework = "bare";
} else if (hasUniwind) {
nativeFramework = "uniwind";
} else if (hasUnistyles) {
nativeFramework = "unistyles";
}
const exampleNativeSrc = path.join(exampleBaseDir, `native/${nativeFramework}`);
if (await fs.pathExists(exampleNativeSrc)) {
await processAndCopyFiles("**/*", exampleNativeSrc, nativeAppDir, context, false);
}
}
}
}
}
export async function handleExtras(projectDir: string, context: ProjectConfig) {
const extrasDir = path.join(PKG_ROOT, "templates/extras");
const hasNativeBare = context.frontend.includes("native-bare");
const hasUniwind = context.frontend.includes("native-uniwind");
const hasUnistyles = context.frontend.includes("native-unistyles");
const hasNative = hasNativeBare || hasUniwind || hasUnistyles;
if (context.packageManager === "pnpm") {
const pnpmWorkspaceSrc = path.join(extrasDir, "pnpm-workspace.yaml");
const pnpmWorkspaceDest = path.join(projectDir, "pnpm-workspace.yaml");
if (await fs.pathExists(pnpmWorkspaceSrc)) {
await processTemplate(pnpmWorkspaceSrc, pnpmWorkspaceDest, context);
}
}
if (context.packageManager === "bun") {
const bunfigSrc = path.join(extrasDir, "bunfig.toml.hbs");
if (await fs.pathExists(bunfigSrc)) {
await processAndCopyFiles("bunfig.toml.hbs", extrasDir, projectDir, context);
}
}
if (context.packageManager === "pnpm" && (hasNative || context.frontend.includes("nuxt"))) {
const npmrcTemplateSrc = path.join(extrasDir, "_npmrc.hbs");
if (await fs.pathExists(npmrcTemplateSrc)) {
await processAndCopyFiles("_npmrc.hbs", extrasDir, projectDir, context);
}
}
}
export async function setupDockerComposeTemplates(projectDir: string, context: ProjectConfig) {
if (context.dbSetup !== "docker" || context.database === "none") {
return;
}
const dbPackageDir = path.join(projectDir, "packages/db");
const dockerSrcDir = path.join(PKG_ROOT, `templates/db-setup/docker-compose/${context.database}`);
if (await fs.pathExists(dockerSrcDir)) {
await processAndCopyFiles("**/*", dockerSrcDir, dbPackageDir, context);
}
}
export async function setupDeploymentTemplates(projectDir: string, context: ProjectConfig) {
const isBackendSelf = context.backend === "self";
if (context.webDeploy === "cloudflare" || context.serverDeploy === "cloudflare") {
const infraTemplateSrc = path.join(PKG_ROOT, "templates/packages/infra");
const infraDir = path.join(projectDir, "packages/infra");
if (await fs.pathExists(infraTemplateSrc)) {
await fs.ensureDir(infraDir);
await processAndCopyFiles("package.json.hbs", infraTemplateSrc, infraDir, context);
await processAndCopyFiles("alchemy.run.ts.hbs", infraTemplateSrc, infraDir, context);
}
if (!isBackendSelf) {
const envTemplateSrc = path.join(PKG_ROOT, "templates/packages/env");
const envDir = path.join(projectDir, "packages/env");
const envDtsTemplatePath = path.join(envTemplateSrc, "env.d.ts.hbs");
if (await fs.pathExists(envDtsTemplatePath)) {
const envDtsPath = path.join(envDir, "env.d.ts");
await processTemplate(envDtsTemplatePath, envDtsPath, context);
}
}
}
if (context.webDeploy !== "none" && context.webDeploy !== "cloudflare") {
const webAppDir = path.join(projectDir, "apps/web");
if (await fs.pathExists(webAppDir)) {
const frontends = context.frontend;
const templateMap: Record<string, string> = {
"tanstack-router": "react/tanstack-router",
"tanstack-start": "react/tanstack-start",
"react-router": "react/react-router",
solid: "solid",
next: "react/next",
nuxt: "nuxt",
svelte: "svelte",
};
for (const f of frontends) {
if (templateMap[f]) {
const deployTemplateSrc = path.join(
PKG_ROOT,
`templates/deploy/${context.webDeploy}/web/${templateMap[f]}`,
);
if (await fs.pathExists(deployTemplateSrc)) {
await processAndCopyFiles("**/*", deployTemplateSrc, webAppDir, context);
}
}
}
}
}
if (context.serverDeploy !== "none" && context.serverDeploy !== "cloudflare" && !isBackendSelf) {
const serverAppDir = path.join(projectDir, "apps/server");
if (await fs.pathExists(serverAppDir)) {
const deployTemplateSrc = path.join(
PKG_ROOT,
`templates/deploy/${context.serverDeploy}/server`,
);
if (await fs.pathExists(deployTemplateSrc)) {
await processAndCopyFiles("**/*", deployTemplateSrc, serverAppDir, context);
}
}
}
}