-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
731 lines (625 loc) · 26.9 KB
/
Copy pathsetup.ps1
File metadata and controls
731 lines (625 loc) · 26.9 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
#!/usr/bin/env pwsh
# ProjectForge - Project Setup Script
# Usage:
# .\setup.ps1 # Fully interactive mode - will guide you through all options
# .\setup.ps1 -ProjectName "MyProject" # Interactive mode with pre-set project name
# .\setup.ps1 -ProjectType "nextjs" # Interactive mode with pre-set project type
# .\setup.ps1 -ProjectType "nextjs" -ProjectName "MyApp" # Direct mode with all parameters
# .\setup.ps1 -ProjectType "nodejs" -ProjectName "MyAPI" # Direct mode with all parameters
# .\setup.ps1 -ProjectType "both" -ProjectName "MyFullStack" # Direct mode with all parameters
param(
[Parameter(Mandatory=$false)]
[string]$ProjectType,
[Parameter(Mandatory=$false)]
[string]$ProjectName,
[string]$ProjectPath = ".",
[switch]$UseYarn,
[switch]$UsePnpm,
[switch]$UseTypeScript = $true,
[switch]$UseSCSS = $true,
[switch]$Help
)
# Colors for output
$Colors = @{
Info = "Cyan"
Success = "Green"
Warning = "Yellow"
Error = "Red"
Default = "White"
}
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = "White"
)
# Check if the color exists in our colors hashtable, otherwise use White
if ($Colors.ContainsKey($Color)) {
Write-Host $Message -ForegroundColor $Colors[$Color]
} else {
Write-Host $Message -ForegroundColor "White"
}
}
function Show-ProjectTypeOptions {
Write-ColorOutput "" "Default"
Write-ColorOutput "=== Choose Project Type ===" "Info"
Write-ColorOutput "1. Next.js (Frontend React Framework)" "Default"
Write-ColorOutput "2. Node.js (Backend API Server)" "Default"
Write-ColorOutput "3. Both (Full-stack with Next.js + Node.js)" "Default"
Write-ColorOutput "" "Default"
do {
$choice = Read-Host "Enter your choice (1, 2, or 3)"
switch ($choice) {
"1" { return "nextjs" }
"2" { return "nodejs" }
"3" { return "both" }
default {
Write-ColorOutput "Invalid choice. Please enter 1, 2, or 3." "Error"
}
}
} while ($true)
}
function Get-ProjectName {
Write-ColorOutput "" "Default"
Write-ColorOutput "=== Project Name ===" "Info"
Write-ColorOutput "Enter a name for your project." "Default"
Write-ColorOutput "Examples: myapp, myapi, myfullstack, blogapp, ecommerceapi" "Default"
Write-ColorOutput "Note: Project names must be lowercase and contain only letters, numbers, and hyphens." "Warning"
Write-ColorOutput "" "Default"
do {
$name = Read-Host "Project name"
$isValid, $errorMessage = Test-ProjectName -Name $name
if ($isValid) {
return $name.Trim()
} else {
Write-ColorOutput $errorMessage "Error"
}
} while ($true)
}
function Test-Command {
param([string]$Command)
try {
Get-Command $Command -ErrorAction Stop | Out-Null
return $true
} catch {
return $false
}
}
function Test-ProjectName {
param([string]$Name)
if ($Name.Trim() -eq "") {
return $false, "Project name cannot be empty."
}
# Check for capital letters using character-by-character comparison
for ($i = 0; $i -lt $Name.Length; $i++) {
$char = $Name[$i]
if ($char -ge 'A' -and $char -le 'Z') {
return $false, "Project name cannot contain capital letters. Please use lowercase only."
}
}
# Check for invalid characters
for ($i = 0; $i -lt $Name.Length; $i++) {
$char = $Name[$i]
if (-not (($char -ge 'a' -and $char -le 'z') -or ($char -ge '0' -and $char -le '9') -or $char -eq '-')) {
return $false, "Project name can only contain lowercase letters, numbers, and hyphens."
}
}
# Check if starts with letter
if ($Name[0] -lt 'a' -or $Name[0] -gt 'z') {
return $false, "Project name must start with a lowercase letter."
}
# Check if starts or ends with hyphen
if ($Name[0] -eq '-' -or $Name[-1] -eq '-') {
return $false, "Project name cannot start or end with a hyphen."
}
return $true, ""
}
function Test-NodeVersion {
if (-not (Test-Command "node")) {
Write-ColorOutput "Node.js is not installed. Please install Node.js first." "Error"
exit 1
}
$nodeVersion = node --version
Write-ColorOutput "Using Node.js version: $nodeVersion" "Info"
}
function Test-PackageManager {
if ($UseYarn -and (Test-Command "yarn")) {
return "yarn"
} elseif ($UsePnpm -and (Test-Command "pnpm")) {
return "pnpm"
} elseif (Test-Command "npm") {
return "npm"
} else {
Write-ColorOutput "No package manager found. Installing npm..." "Warning"
return "npm"
}
}
function Create-ProjectDirectory {
param([string]$Path, [string]$Name)
$fullPath = Join-Path $Path $Name
if (Test-Path $fullPath) {
Write-ColorOutput "Project directory already exists: $fullPath" "Warning"
$response = Read-Host "Do you want to remove it and create a new one? (y/N)"
if ($response -eq "y" -or $response -eq "Y") {
Remove-Item $fullPath -Recurse -Force
} else {
Write-ColorOutput "Setup cancelled." "Error"
exit 1
}
}
New-Item -ItemType Directory -Path $fullPath -Force | Out-Null
return $fullPath
}
function Cleanup-FailedProject {
param([string]$ProjectPath)
if (Test-Path $ProjectPath) {
try {
Remove-Item $ProjectPath -Recurse -Force
Write-ColorOutput "Cleaned up failed project directory: $ProjectPath" "Info"
} catch {
Write-ColorOutput "Warning: Could not clean up failed project directory: $ProjectPath" "Warning"
}
}
}
function Setup-NextJSProject {
param([string]$PackageManager, [bool]$UseTS, [bool]$UseSCSS)
Write-ColorOutput "Setting up Next.js project..." "Info"
# Create Next.js project with latest version
$tsFlag = if ($UseTS) { "--typescript" } else { "" }
$scssFlag = if ($UseSCSS) { "--tailwind=false" } else { "" }
# Create the Next.js app in the current directory
if ($PackageManager -eq "yarn") {
yarn create next-app@latest . --yes $tsFlag $scssFlag
} elseif ($PackageManager -eq "pnpm") {
pnpm create next-app@latest . --yes $tsFlag $scssFlag
} else {
npx create-next-app@latest . --yes $tsFlag $scssFlag
}
# Create additional folder structure
$folders = @(
"components",
"components/ui",
"components/forms",
"components/layout",
"hooks",
"utils",
"types",
"constants",
"services",
"styles",
"public/images",
"public/icons"
)
foreach ($folder in $folders) {
New-Item -ItemType Directory -Path $folder -Force | Out-Null
}
# Create SCSS files if enabled
if ($UseSCSS) {
$scssFiles = @(
"styles/globals.scss",
"styles/variables.scss",
"styles/mixins.scss",
"styles/components.scss"
)
foreach ($file in $scssFiles) {
New-Item -ItemType File -Path $file -Force | Out-Null
}
# Add basic SCSS content
Set-Content "styles/globals.scss" "@import 'variables';`n@import 'mixins';`n@import 'components';"
Set-Content "styles/variables.scss" "// SCSS Variables`n`n// Colors`n`$primary-color: #007bff;`n`$secondary-color: #6c757d;`n`n// Typography`n`$font-family-base: 'Inter', sans-serif;`n`$font-size-base: 16px;"
Set-Content "styles/mixins.scss" "// SCSS Mixins`n`n@mixin flex-center {`n display: flex;`n align-items: center;`n justify-content: center;`n}`n`n@mixin responsive(`$breakpoint) {`n @media (max-width: `$breakpoint) {`n @content;`n }`n}"
Set-Content "styles/components.scss" "// Component Styles`n`n// Add your component styles here"
}
# Create basic component files
$componentFiles = @(
"components/layout/Header.tsx",
"components/layout/Footer.tsx",
"components/layout/Layout.tsx",
"components/ui/Button.tsx",
"components/ui/Card.tsx"
)
foreach ($file in $componentFiles) {
New-Item -ItemType File -Path $file -Force | Out-Null
}
# Create utility files
$utilityFiles = @(
"utils/helpers.ts",
"utils/validation.ts",
"types/index.ts",
"constants/config.ts"
)
foreach ($file in $utilityFiles) {
New-Item -ItemType File -Path $file -Force | Out-Null
}
Write-ColorOutput "Next.js project structure created successfully!" "Success"
}
function Setup-NodeJSProject {
param([string]$PackageManager, [bool]$UseTS)
Write-ColorOutput "Setting up Node.js project..." "Info"
# Initialize package.json
if ($PackageManager -eq "yarn") {
yarn init -y
} elseif ($PackageManager -eq "pnpm") {
pnpm init
} else {
npm init -y
}
# Create folder structure
$folders = @(
"src",
"src/controllers",
"src/models",
"src/routes",
"src/middleware",
"src/services",
"src/utils",
"src/types",
"src/config",
"tests",
"tests/unit",
"tests/integration",
"docs",
"logs"
)
foreach ($folder in $folders) {
New-Item -ItemType Directory -Path $folder -Force | Out-Null
}
# Create basic files
$files = @(
"src/app.ts",
"src/server.ts",
"src/routes/index.ts",
"src/controllers/index.ts",
"src/middleware/index.ts",
"src/config/database.ts",
"src/config/app.ts",
"src/types/index.ts",
"src/utils/logger.ts",
"tests/setup.ts",
"docs/README.md",
".env.example",
".gitignore"
)
foreach ($file in $files) {
New-Item -ItemType File -Path $file -Force | Out-Null
}
# Add content to key files
Set-Content ".gitignore" "node_modules/`n.env`nlogs/`n*.log`ndist/`nbuild/`ncoverage/`n.DS_Store"
Set-Content ".env.example" "# Environment Variables`nNODE_ENV=development`nPORT=3000`nDATABASE_URL=mongodb://localhost:27017/your-database"
# Install dependencies
$dependencies = @(
"express",
"cors",
"helmet",
"morgan",
"dotenv",
"winston"
)
$devDependencies = @(
"nodemon",
"jest",
"supertest",
"@types/node"
)
if ($UseTS) {
$dependencies += @("reflect-metadata")
$devDependencies += @(
"typescript",
"@types/express",
"@types/cors",
"@types/morgan",
"ts-node",
"ts-node-dev"
)
# Create tsconfig.json
$tsConfig = @{
compilerOptions = @{
target = "ES2020"
module = "commonjs"
lib = @("ES2020")
outDir = "./dist"
rootDir = "./src"
strict = $true
esModuleInterop = $true
skipLibCheck = $true
forceConsistentCasingInFileNames = $true
resolveJsonModule = $true
experimentalDecorators = $true
emitDecoratorMetadata = $true
}
include = @("src/**/*")
exclude = @("node_modules", "dist", "tests")
}
$tsConfig | ConvertTo-Json -Depth 10 | Set-Content "tsconfig.json"
}
# Install dependencies
if ($PackageManager -eq "yarn") {
yarn add $dependencies
yarn add -D $devDependencies
} elseif ($PackageManager -eq "pnpm") {
pnpm add $dependencies
pnpm add -D $devDependencies
} else {
npm install $dependencies
npm install -D $devDependencies
}
# Add scripts to package.json
$packageJson = Get-Content "package.json" | ConvertFrom-Json
if ($UseTS) {
$packageJson.scripts = @{
"start" = "node dist/server.js"
"dev" = "ts-node-dev --respawn --transpile-only src/server.ts"
"build" = "tsc"
"test" = "jest"
"test:watch" = "jest --watch"
"lint" = "eslint src/**/*.ts"
}
} else {
$packageJson.scripts = @{
"start" = "node src/server.js"
"dev" = "nodemon src/server.js"
"test" = "jest"
"test:watch" = "jest --watch"
}
}
$packageJson | ConvertTo-Json -Depth 10 | Set-Content "package.json"
Write-ColorOutput "Node.js project structure created successfully!" "Success"
}
# Function to help users clean up failed projects
function Show-CleanupHelp {
Write-ColorOutput "" "Default"
Write-ColorOutput "=== Cleanup Help ===" "Info"
Write-ColorOutput "If you have failed project directories, you can clean them up:" "Default"
Write-ColorOutput "1. Remove the failed project folder manually" "Default"
Write-ColorOutput "2. Run this script again with a valid project name" "Default"
Write-ColorOutput "" "Default"
Write-ColorOutput "Valid project names: lowercase letters, numbers, and hyphens only" "Warning"
Write-ColorOutput "Examples: myapp, myapi, blog-app, ecommerce-api" "Info"
Write-ColorOutput "" "Default"
}
# Main execution
try {
# Show help if requested
if ($Help) {
Write-ColorOutput "=== Project Setup Script Help ===" "Info"
Write-ColorOutput "" "Default"
Write-ColorOutput "Usage:" "Info"
Write-ColorOutput " .\setup.ps1 # Fully interactive mode" "Default"
Write-ColorOutput " .\setup.ps1 -ProjectName 'myapp' # Interactive with pre-set name" "Default"
Write-ColorOutput " .\setup.ps1 -ProjectType 'nextjs' # Interactive with pre-set type" "Default"
Write-ColorOutput " .\setup.ps1 -ProjectType 'nextjs' -ProjectName 'myapp'" "Default"
Write-ColorOutput " .\setup.ps1 -Help # Show this help" "Default"
Write-ColorOutput "" "Default"
Write-ColorOutput "Project Types:" "Info"
Write-ColorOutput " 1. nextjs - Next.js frontend application" "Default"
Write-ColorOutput " 2. nodejs - Node.js backend API server" "Default"
Write-ColorOutput " 3. both - Full-stack with Next.js + Node.js" "Default"
Write-ColorOutput "" "Default"
Write-ColorOutput "Important: Project names must be lowercase and contain only letters, numbers, and hyphens." "Warning"
Write-ColorOutput "Examples: myapp, myapi, blog-app, ecommerce-api" "Info"
Write-ColorOutput "" "Default"
Write-ColorOutput "Options:" "Info"
Write-ColorOutput " -UseTypeScript # Enable TypeScript (default: true)" "Default"
Write-ColorOutput " -UseSCSS # Enable SCSS (default: true)" "Default"
Write-ColorOutput " -UseYarn # Use Yarn instead of npm" "Default"
Write-ColorOutput " -UsePnpm # Use pnpm instead of npm" "Default"
Write-ColorOutput "" "Default"
exit 0
}
Write-ColorOutput "=== Project Setup Script ===" "Info"
Write-ColorOutput "This script will help you set up a new project." "Info"
Write-ColorOutput "You can run it with parameters or interactively." "Info"
Write-ColorOutput "" "Default"
Write-ColorOutput "Important: Project names must be lowercase and contain only letters, numbers, and hyphens." "Warning"
Write-ColorOutput "Examples: myapp, myapi, blog-app, ecommerce-api" "Info"
Write-ColorOutput "" "Default"
# Check for any existing project directories that might be failed setups
$existingProjects = Get-ChildItem -Path $ProjectPath -Directory -Name | Where-Object { $_ -match '^[a-z][a-z0-9-]*$' }
if ($existingProjects) {
Write-ColorOutput "Found existing project directories:" "Warning"
foreach ($project in $existingProjects) {
Write-ColorOutput " - $project" "Default"
}
Write-ColorOutput "" "Default"
$cleanupResponse = Read-Host "Do you want to clean up these directories before proceeding? (y/N)"
if ($cleanupResponse -eq "y" -or $cleanupResponse -eq "Y") {
foreach ($project in $existingProjects) {
$projectPath = Join-Path $ProjectPath $project
Write-ColorOutput "Cleaning up: $projectPath" "Info"
Cleanup-FailedProject -ProjectPath $projectPath
}
Write-ColorOutput "Cleanup completed!" "Success"
}
Write-ColorOutput "" "Default"
}
# Validate or get ProjectType
if (-not $ProjectType) {
$ProjectType = Show-ProjectTypeOptions
} else {
# Validate the provided ProjectType
$validTypes = @("nextjs", "nodejs", "both")
if ($ProjectType -notin $validTypes) {
Write-ColorOutput "Invalid ProjectType: '$ProjectType'" "Error"
Write-ColorOutput "Please choose from the available options:" "Info"
$ProjectType = Show-ProjectTypeOptions
}
}
# Get ProjectName interactively if not provided
if (-not $ProjectName) {
$ProjectName = Get-ProjectName
} else {
# Validate the provided ProjectName
$isValid, $errorMessage = Test-ProjectName -Name $ProjectName
if (-not $isValid) {
Write-ColorOutput "Invalid ProjectName: $errorMessage" "Error"
$ProjectName = Get-ProjectName
}
}
Write-ColorOutput "Project Type: $ProjectType" "Info"
Write-ColorOutput "Project Name: $ProjectName" "Info"
Write-ColorOutput "Project Path: $ProjectPath" "Info"
Write-ColorOutput "TypeScript: $UseTypeScript" "Info"
Write-ColorOutput "SCSS: $UseSCSS" "Info"
Write-ColorOutput "" "Default"
# Check for existing failed project directories and clean them up
$potentialFailedPath = Join-Path $ProjectPath $ProjectName
if (Test-Path $potentialFailedPath) {
Write-ColorOutput "Found existing project directory: $potentialFailedPath" "Warning"
Write-ColorOutput "This might be from a previous failed setup. Cleaning up..." "Info"
Cleanup-FailedProject -ProjectPath $potentialFailedPath
}
# Check prerequisites
Test-NodeVersion
$packageManager = Test-PackageManager
Write-ColorOutput "Using package manager: $packageManager" "Info"
# Setup projects based on type
switch ($ProjectType) {
"nextjs" {
# Create project directory for standalone Next.js
$fullProjectPath = Create-ProjectDirectory -Path $ProjectPath -Name $ProjectName
Write-ColorOutput "Created project directory: $fullProjectPath" "Success"
try {
Set-Location $fullProjectPath
Setup-NextJSProject -PackageManager $packageManager -UseTS $UseTypeScript -UseSCSS $UseSCSS
} catch {
Write-ColorOutput "Failed to setup Next.js project. Cleaning up..." "Error"
Cleanup-FailedProject -ProjectPath $fullProjectPath
throw
}
}
"nodejs" {
# Create project directory for standalone Node.js
$fullProjectPath = Create-ProjectDirectory -Path $ProjectPath -Name $ProjectName
Write-ColorOutput "Created project directory: $fullProjectPath" "Success"
try {
Set-Location $fullProjectPath
Setup-NodeJSProject -PackageManager $packageManager -UseTS $UseTypeScript
} catch {
Write-ColorOutput "Failed to setup Node.js project. Cleaning up..." "Error"
Cleanup-FailedProject -ProjectPath $fullProjectPath
throw
}
}
"both" {
# Create both projects in separate directories
Write-ColorOutput "Setting up both Next.js and Node.js projects..." "Info"
Write-ColorOutput "Starting from directory: $(Get-Location)" "Info"
# Step 1: Create the main project directory
$mainProjectPath = Create-ProjectDirectory -Path $ProjectPath -Name $ProjectName
Write-ColorOutput "Created main project directory: $mainProjectPath" "Success"
try {
# Step 2: Create backend and frontend subdirectories
$backendPath = Join-Path $mainProjectPath "backend"
$frontendPath = Join-Path $mainProjectPath "frontend"
# Convert to absolute paths to avoid any relative path issues
$mainProjectPath = [System.IO.Path]::GetFullPath($mainProjectPath)
$backendPath = [System.IO.Path]::GetFullPath($backendPath)
$frontendPath = [System.IO.Path]::GetFullPath($frontendPath)
Write-ColorOutput "Main project path (absolute): $mainProjectPath" "Info"
Write-ColorOutput "Backend path (absolute): $backendPath" "Info"
Write-ColorOutput "Frontend path (absolute): $frontendPath" "Info"
New-Item -ItemType Directory -Path $backendPath -Force | Out-Null
New-Item -ItemType Directory -Path $frontendPath -Force | Out-Null
Write-ColorOutput "Created backend directory: $backendPath" "Success"
Write-ColorOutput "Created frontend directory: $frontendPath" "Success"
# Step 3: Setup Node.js backend
Write-ColorOutput "Setting up Node.js backend..." "Info"
Write-ColorOutput "Current location before backend setup: $(Get-Location)" "Info"
# Verify backend path exists before navigating
if (-not (Test-Path $backendPath)) {
throw "Backend path does not exist: $backendPath"
}
Set-Location $backendPath
Write-ColorOutput "Current location after setting backend: $(Get-Location)" "Info"
Setup-NodeJSProject -PackageManager $packageManager -UseTS $UseTypeScript
# Step 4: Go back to root, then setup Next.js frontend
Write-ColorOutput "Going back to main project path: $mainProjectPath" "Info"
# Verify main project path exists before navigating back
if (-not (Test-Path $mainProjectPath)) {
throw "Main project path does not exist: $mainProjectPath"
}
Set-Location $mainProjectPath
Write-ColorOutput "Current location after going back to root: $(Get-Location)" "Info"
Write-ColorOutput "Setting up Next.js frontend..." "Info"
# Verify frontend path exists before navigating
if (-not (Test-Path $frontendPath)) {
throw "Frontend path does not exist: $frontendPath"
}
Set-Location $frontendPath
Write-ColorOutput "Current location before frontend setup: $(Get-Location)" "Info"
Setup-NextJSProject -PackageManager $packageManager -UseTS $UseTypeScript -UseSCSS $UseSCSS
# Step 5: Create root README and go back to root
Write-ColorOutput "Going back to main project path for README: $mainProjectPath" "Info"
# Verify main project path exists before final navigation
if (-not (Test-Path $mainProjectPath)) {
throw "Main project path does not exist for README creation: $mainProjectPath"
}
Set-Location $mainProjectPath
Write-ColorOutput "Current location for README creation: $(Get-Location)" "Info"
# Final verification that we're in the right place
$currentLocation = Get-Location
if ($currentLocation.Path -ne $mainProjectPath) {
Write-ColorOutput "Warning: Current location doesn't match expected main project path" "Warning"
Write-ColorOutput "Expected: $mainProjectPath" "Warning"
Write-ColorOutput "Actual: $($currentLocation.Path)" "Warning"
# Force navigation to the correct path
Set-Location $mainProjectPath
Write-ColorOutput "Forced navigation to: $(Get-Location)" "Info"
}
$readmeContent = @"
# $ProjectName
This project contains both a Next.js frontend and a Node.js backend.
## Project Structure
- \`frontend/\` - Next.js application
- \`backend/\` - Node.js API server
## Getting Started
### Frontend (Next.js)
\`\`\`bash
cd frontend
$packageManager install
$packageManager dev
\`\`\`
### Backend (Node.js)
\`\`\`bash
cd backend
$packageManager install
$packageManager dev
\`\`\`
## Development
- Frontend runs on: http://localhost:3000
- Backend runs on: http://localhost:3001
"@
Set-Content "README.md" $readmeContent
# Set the full project path for the success message
$fullProjectPath = $mainProjectPath
} catch {
Write-ColorOutput "Failed to setup projects. Cleaning up..." "Error"
Cleanup-FailedProject -ProjectPath $mainProjectPath
throw
}
}
}
Write-ColorOutput "" "Default"
Write-ColorOutput "=== Setup Complete! ===" "Success"
Write-ColorOutput "Your project has been created successfully at: $fullProjectPath" "Success"
if ($ProjectType -eq "both") {
Write-ColorOutput "" "Default"
Write-ColorOutput "Next steps:" "Info"
Write-ColorOutput "1. cd frontend && $packageManager install && $packageManager dev" "Info"
Write-ColorOutput "2. cd backend && $packageManager install && $packageManager dev" "Info"
} else {
Write-ColorOutput "" "Default"
Write-ColorOutput "Next steps:" "Info"
Write-ColorOutput "1. $packageManager install" "Info"
Write-ColorOutput "2. $packageManager dev" "Info"
}
} catch {
Write-ColorOutput "An error occurred: $($_.Exception.Message)" "Error"
Write-ColorOutput "" "Default"
Write-ColorOutput "If you have any failed project directories, you can clean them up manually:" "Info"
Write-ColorOutput "1. Remove the failed project folder" "Info"
Write-ColorOutput "2. Run the script again with a valid project name" "Info"
Write-ColorOutput "" "Default"
Write-ColorOutput "Valid project names: lowercase letters, numbers, and hyphens only" "Warning"
Write-ColorOutput "Examples: myapp, myapi, blog-app, ecommerce-api" "Info"
exit 1
}