start.ps1 5.5 KB

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