diff --git a/scripts/build.ps1 b/scripts/build.ps1 index f19af302..052b5c52 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -43,6 +43,7 @@ if ([string]::IsNullOrWhiteSpace($repoRoot)) { Write-Host "Repository root: " -NoNewline Write-Host $repoRoot -f Blue +Push-Location Set-Location "$repoRoot/web" if (-not (Get-Command pnpm -ErrorAction SilentlyContinue)) { @@ -50,6 +51,7 @@ if (-not (Get-Command pnpm -ErrorAction SilentlyContinue)) { npm install -g pnpm if (!$?) { Write-Host -BackgroundColor red -ForegroundColor white "Could not install pnpm. See above." + Pop-Location Exit 1 } } @@ -58,39 +60,37 @@ Write-Host "`nInstalling frontend dependencies..." -f DarkYellow pnpm i --frozen-lockfile if (!$?) { Write-Host -BackgroundColor red -ForegroundColor white "Could not install frontend dependencies. See above." + Pop-Location Exit 1 } Write-Host "Frontend dependencies installed!" -f green +Write-Host "`nRemoving previous frontend build from ./build/dist ..." -f Magenta +Remove-Item "$repoRoot/build/dist" -Recurse -Force -ErrorAction SilentlyContinue +if (!$?) { + Write-Host -BackgroundColor red -ForegroundColor white "Could not remove frontend from ./build/dist. See above." + Pop-Location + Exit 1 +} + Write-Host "`nBuilding frontend..." -f DarkYellow $frontendTime = Measure-Command { &pnpm build | Out-Host } if (!$?) { Write-Host -BackgroundColor red -ForegroundColor white "Could not build frontend. See above." + Pop-Location Exit 1 -} else { - Write-Host "Frontend built!" -f green } - -if (!$?) { - Write-Host -BackgroundColor red -ForegroundColor white "Could not generate buf types. See above." - Exit 1 -} else { - Write-Host "buf types generated!" -f green -} - -Write-Host "`nBacking up frontend placeholder..." -f Magenta -Move-Item "$repoRoot/server/frontend/dist" "$repoRoot/server/frontend/dist.bak" -Force -ErrorAction Stop -if (!$?) { - Write-Host -BackgroundColor red -ForegroundColor white "Could not backup frontend placeholder. See above." - Exit 1 +else { + Write-Host "Frontend built!" -f green } -Write-Host "Moving frontend build to ./server/frontend/dist..." -f Magenta -Move-Item "$repoRoot/web/dist" "$repoRoot/server/" -Force -ErrorAction Stop +Write-Host "Moving frontend build to ./build/dist..." -f Magenta +Move-Item "$repoRoot/web/dist" "$repoRoot/build/" -Force -ErrorAction Stop if (!$?) { - Write-Host -BackgroundColor red -ForegroundColor white "Could not move frontend build to /server/frontend/dist. See above." + Write-Host -BackgroundColor red -ForegroundColor white "Could not move frontend build to ./build/dist. See above." + Pop-Location Exit 1 } @@ -122,20 +122,6 @@ Write-Host "Backend built!" -f green Write-Host "`nFrontend build took $($frontendTime.TotalSeconds) seconds." -f Cyan Write-Host "Backend builds took $($backendTime.TotalSeconds) seconds." -f Cyan -Write-Host "`nRemoving frontend from ./server/frontend/dist ..." -f Magenta -Remove-Item "$repoRoot/server/frontend/dist" -Recurse -Force -ErrorAction SilentlyContinue -if (!$?) { - Write-Host -BackgroundColor red -ForegroundColor white "Could not remove frontend from /server/frontend/dist. See above." - Exit 1 -} - -Write-Host "Restoring frontend placeholder..." -f Magenta -Move-Item "$repoRoot/server/frontend/dist.bak" "$repoRoot/server/frontend/dist" -Force -ErrorAction Stop -if (!$?) { - Write-Host -BackgroundColor red -ForegroundColor white "Could not restore frontend placeholder. See above." - Exit 1 -} - Write-Host "`nBuilds:" -f White foreach ($build in $goBuilds) { $output = [IO.Path]::Combine($repoRoot, "build", "memos-$os-$arch") diff --git a/scripts/build.sh b/scripts/build.sh index 15bcad97..f0308946 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -11,7 +11,7 @@ # * node.js # * npm -# Usage: +# Usage: # chmod +x ./scripts/build.sh # ./scripts/build.sh # @@ -53,15 +53,15 @@ else echo -e "Repository root: \033[0;34m$repo_root\033[0m" fi - +pushd $repo_root cd "$repo_root/web" -if ! command -v pnpm &> /dev/null -then +if ! command -v pnpm &>/dev/null; then echo -e "\n\033[35mInstalling pnpm...\033[0m" npm install -g pnpm if [ $? -ne 0 ]; then echo -e "\033[0;31mFailed to install pnpm! Exiting.\033[0m" + popd exit 1 fi fi @@ -70,31 +70,35 @@ echo -e "\n\033[33mInstalling frontend dependencies...\033[0m" pnpm i --frozen-lockfile if [ $? -ne 0 ]; then echo -e "\033[0;31mFrontend dependencies failed to install! Exiting.\033[0m" + popd exit 1 fi echo -e "\033[32mFrontend dependencies installed!\033[0m" +echo -e "\n\033[35mRemoving previous frontend build from ./build/dist...\033[0m" +rm -rf $repo_root/build/dist +if [ $? -ne 0 ]; then + echo -e "\033[93mCould not remove frontend from ./build/dist.\033[0m" + popd + exit 1 +fi + echo -e "\n\033[33mBuilding frontend...\033[0m" pnpm build if [ $? -ne 0 ]; then echo -e "\033[0;31mFrontend build failed! Exiting.\033[0m" + popd exit 1 fi echo -e "\033[32mFrontend built!\033[0m" cd $repo_root -echo -e "\n\033[35mBacking up frontend placeholder...\033[0m" -mv -f "$repo_root/server/frontend/dist" "$repo_root/server/frontend/dist.bak" -if [ $? -ne 0 ]; then - echo -e "\033[0;31mFailed to backup frontend placeholder! Exiting.\033[0m" - exit 1 -fi - -echo -e "\033[35mMoving frontend build to ./server/frontend/dist...\033[0m" -mv -f "$repo_root/web/dist" "$repo_root/server/" +echo -e "\033[35mMoving frontend build to ./build/dist...\033[0m" +mv -f "$repo_root/web/dist" "$repo_root/build/" if [ $? -ne 0 ]; then echo -e "\033[0;31mFailed to move frontend build! Exiting.\033[0m" + popd exit 1 fi @@ -109,7 +113,7 @@ for build in "${goBuilds[@]}"; do if [ "$os" = "windows" ]; then output="$output.exe" fi - + CGO_ENABLED=0 GOOS=$os GOARCH=$arch go build -trimpath -ldflags="${ldFlags[*]}" -o "$output" ./bin/memos/main.go echo -e "\033[34mBuilding $os/$arch to $output...\033[0m" @@ -121,22 +125,6 @@ done echo -e "\033[32mBackend built!\033[0m" -echo -e "\n\033[35mRemoving frontend from ./server/frontend/dist...\033[0m" -rm -rf $repo_root/server/frontend/dist -if [ $? -ne 0 ] -then - echo -e "\033[93mCould not remove frontend from /server/frontend/dist.\033[0m" - exit 1 -fi - -echo -e "\033[35mRestoring frontend placeholder...\033[0m" -mv $repo_root/server/frontend/dist.bak $repo_root/server/frontend/dist -if [ $? -ne 0 ] -then - echo -e "\033[93mCould not restore frontend placeholder.\033e[0m" - exit 1 -fi - echo -e "\n\033[37mBuilds:\033[0m" for build in "${goBuilds[@]}"; do os=$(echo $build | cut -d'/' -f1)