start.ps1 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # This script starts the backend and frontend in development mode, with live reload.
  2. # It also installs frontend dependencies.
  3. # For more details on setting-up a development environment, check the docs:
  4. # * https://usememos.com/docs/contribution/development
  5. # * https://github.com/usememos/memos/blob/main/docs/development.md
  6. # Usage: ./scripts/start.ps1
  7. foreach ($dir in @(".", "../")) {
  8. if (Test-Path (Join-Path $dir ".gitignore")) {
  9. $repoRoot = (Resolve-Path $dir).Path
  10. break
  11. }
  12. }
  13. ##
  14. $frontendPort = 3001
  15. # Tasks to run, in order
  16. $runTasks = @(
  17. @{
  18. Desc = "install frontend dependencies";
  19. Exe = "powershell.exe";
  20. Args = (
  21. "-Command",
  22. "pnpm i"
  23. );
  24. Dir = "$repoRoot/web"
  25. Wait = $true;
  26. },
  27. @{
  28. Desc = "start backend with live reload";
  29. Exe = "air.exe";
  30. Args = (
  31. "-c",
  32. ".\scripts\.air-windows.toml"
  33. );
  34. Dir = "$repoRoot";
  35. Wait = $false;
  36. },
  37. @{
  38. Desc = "start frontend with live reload";
  39. Exe = "powershell.exe";
  40. Args = (
  41. "-Command",
  42. "pnpm dev"
  43. );
  44. Dir = "$repoRoot/web";
  45. Wait = $false;
  46. }
  47. )
  48. ##
  49. if (!$repoRoot) {
  50. Write-Host "Could not find repository root!" -f Red
  51. Write-Host "cd into the repository root and run the script again."
  52. Exit 1
  53. }
  54. Write-Host "Repository root is $repoRoot"
  55. Write-Host "Starting development environment...`n"
  56. Write-Host @"
  57. ███╗ ███╗███████╗███╗ ███╗ ██████╗ ███████╗
  58. ████╗ ████║██╔════╝████╗ ████║██╔═══██╗██╔════╝
  59. ██╔████╔██║█████╗ ██╔████╔██║██║ ██║███████╗
  60. ██║╚██╔╝██║██╔══╝ ██║╚██╔╝██║██║ ██║╚════██║
  61. ██║ ╚═╝ ██║███████╗██║ ╚═╝ ██║╚██████╔╝███████║
  62. ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝
  63. "@
  64. function Stop-ProcessTree {
  65. Param([int]$ParentProcessId)
  66. if (!$ParentProcessId) {
  67. Write-Host "Stop-ProcessTree: unspecified ParentProcessId!" -f Red
  68. return
  69. }
  70. Write-Host "Terminating pid $($ParentProcessId) with all its child processes" -f DarkGray
  71. Get-CimInstance Win32_Process | Where-Object {
  72. $_.ParentProcessId -eq $ParentProcessId
  73. } | ForEach-Object {
  74. Stop-ProcessTree $_.ProcessId
  75. }
  76. Stop-Process -Id $ParentProcessId -ErrorAction SilentlyContinue
  77. }
  78. $maxDescLength = ( $runTasks | ForEach-Object { $_.Desc.Length } | Measure-Object -Maximum).Maximum
  79. $spawnedPids = @()
  80. foreach ($task in $runTasks) {
  81. Write-Host ("Running task ""$($task.Desc)""...").PadRight($maxDescLength + 20) -f Blue -NoNewline
  82. $task.Dir = (Resolve-Path $task.Dir).Path
  83. try {
  84. $process = Start-Process -PassThru -WorkingDirectory $task.Dir -FilePath $task.Exe -ArgumentList $task.Args -Wait:$task.Wait
  85. if ($process.ExitCode -and $process.ExitCode -ne 0) {
  86. # ExitCode only works for processes started with -Wait:$true
  87. throw "Process exited with code $($process.ExitCode)"
  88. }
  89. Write-Host "[OK]" -f Green
  90. $spawnedPids += $process.Id
  91. }
  92. catch {
  93. Write-Host "[FAILED]" -f Red
  94. Write-Host "Error: $_" -f Red
  95. Write-Host "Unable to execute: $($task.Exe) $($task.Args)" -f Red
  96. Write-Host "Process working directory: $($task.Dir)" -f Red
  97. foreach ($spawnedPid in $spawnedPids) {
  98. Stop-ProcessTree -ParentProcessId $spawnedPid
  99. }
  100. Exit $process.ExitCode
  101. }
  102. }
  103. Write-Host "Front-end should be accessible at:" -f Green
  104. $ipAddresses = (Get-NetIPAddress -AddressFamily IPv4) | Select-Object -ExpandProperty IPAddress | Sort-Object
  105. $ipAddresses += "localhost"
  106. foreach ($ip in $ipAddresses) {
  107. Write-Host "· http://$($ip):$($frontendPort)" -f Cyan
  108. }
  109. Write-Host "`nPress" -NoNewline
  110. Write-Host " Ctrl + C" -f DarkYellow -NoNewline
  111. Write-Host " or" -NoNewline
  112. Write-Host " Esc" -f DarkYellow -NoNewline
  113. Write-Host " to terminate running servers." -f DarkYellow
  114. [Console]::TreatControlCAsInput = $true
  115. $lastPoll = 0
  116. $noWaitTasks = $runTasks | Where-Object { $_.Wait -eq $false }
  117. while ($true) {
  118. if ([Console]::KeyAvailable) {
  119. $readkey = [Console]::ReadKey("AllowCtrlC,IncludeKeyUp,NoEcho")
  120. if ($readkey.Modifiers -eq "Control" -and $readkey.Key -eq "C") {
  121. break
  122. }
  123. if ($readkey.Key -eq "Escape") {
  124. Break
  125. }
  126. }
  127. # Poll for processes that exited unexpectedly
  128. # Do this every 5 seconds to avoid excessive CPU usage
  129. if (([DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $lastPoll) -ge 5000) {
  130. $noWaitTasks | ForEach-Object {
  131. $name = $_.Exe.TrimEnd(".exe")
  132. if (!(Get-Process -Name $name -ErrorAction SilentlyContinue)) {
  133. Write-Host "Process " -f Red -NoNewline
  134. Write-Host $name -NoNewline -f DarkYellow
  135. Write-Host " is not running anymore!" -f Red
  136. break
  137. }
  138. }
  139. $lastPoll = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
  140. }
  141. Start-Sleep -Milliseconds 500
  142. }
  143. foreach ($spawnedPid in $spawnedPids) {
  144. Stop-ProcessTree -ParentProcessId $spawnedPid
  145. }
  146. Write-Host "Exiting..."