forked from Innei/ProcessReporterCli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-and-move.ps1
175 lines (147 loc) · 5.38 KB
/
build-and-move.ps1
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
# Check and request admin privileges if needed
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "Requesting administrator privileges..." -ForegroundColor Yellow
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
# Set encoding to UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
# Set strict mode and error handling
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# Define paths
$targetPath = "C:\ProcessReporterCli"
$sourcePath = $PSScriptRoot
function Write-Step {
param (
[Parameter(Mandatory)]
[string]$Message
)
Write-Host "`n[$(Get-Date -Format 'HH:mm:ss')] $Message" -ForegroundColor Cyan
}
function Test-CommandExists {
param (
[string]$Command
)
$null = Get-Command $Command -ErrorAction SilentlyContinue
return $?
}
function Check-Prerequisites {
Write-Step "Checking prerequisites..."
# Check Node.js
if (-not (Test-CommandExists "node")) {
throw "Node.js is not installed. Please install Node.js from https://nodejs.org/"
}
Write-Host " Node.js found: $(node --version)" -ForegroundColor Green
# Check Yarn
if (-not (Test-CommandExists "yarn")) {
throw "Yarn is not installed. Please install Yarn using 'npm install -g yarn'"
}
Write-Host " Yarn found: $(yarn --version)" -ForegroundColor Green
# Check .env file
if (-not (Test-Path (Join-Path $sourcePath ".env"))) {
throw ".env file not found. Please create .env file in the project root"
}
Write-Host " .env file found" -ForegroundColor Green
}
try {
# Check prerequisites
Check-Prerequisites
# Prepare target directory
Write-Step "Preparing target directory..."
if (Test-Path $targetPath) {
Write-Host " Removing existing directory..." -ForegroundColor Yellow
Remove-Item -Path $targetPath -Recurse -Force
}
# Copy entire project directory
Write-Step "Copying project files..."
$excludeItems = @(
# Version Control
'.git',
'.github',
# IDE and Editor files
'.vscode',
'.idea',
'*.tsbuildinfo',
# Build scripts
'build-and-move.ps1',
'build-and-move.bat',
'run-build.bat',
# Dependencies
'node_modules',
'/node_modules',
'.yarn',
'.pnp.cjs',
'.pnp.loader.mjs',
# Build outputs
'dist',
'/build',
'/lib',
'logs',
# Debug files
'*.log',
'npm-debug.log*',
'yarn-debug.log*',
'yarn-error.log*',
'.pnpm-debug.log*',
# Environment and local files
'.env*.local',
'data.db',
# Misc
'.DS_Store',
'*.pem',
'*.png'
)
$null = New-Item -ItemType Directory -Path $targetPath -Force
Get-ChildItem -Path $sourcePath -Exclude $excludeItems |
ForEach-Object {
Write-Host " Copying $($_.Name)..." -ForegroundColor Yellow
Copy-Item -Path $_.FullName -Destination $targetPath -Recurse -Force
}
# Copy .env file separately
Write-Host " Copying .env file..." -ForegroundColor Yellow
Copy-Item -Path (Join-Path $sourcePath ".env") -Destination $targetPath -Force
# Show success message
Write-Host "`n=================================" -ForegroundColor Green
Write-Host "Files copied successfully!" -ForegroundColor Green
Write-Host "Location: $targetPath" -ForegroundColor Green
Write-Host "=================================" -ForegroundColor Green
# Create a temporary script file for building
$tempScriptPath = Join-Path $targetPath "temp-build.ps1"
@"
Set-Location '$targetPath'
Write-Host 'Installing dependencies...' -ForegroundColor Yellow
yarn install --ignore-engines
if (`$?) {
Write-Host 'Building project...' -ForegroundColor Yellow
yarn run build
if (`$?) {
Write-Host 'Copying .env to dist directory...' -ForegroundColor Yellow
Copy-Item -Path '.env' -Destination 'dist\.env' -Force
Write-Host 'Creating start script...' -ForegroundColor Yellow
@'
@echo off
cd /d "%~dp0"
node index.js
'@ | Out-File -FilePath 'dist\start.bat' -Encoding utf8 -NoNewline
Write-Host 'Build completed successfully!' -ForegroundColor Green
}
}
Write-Host 'Press any key to exit...' -ForegroundColor Yellow
`$null = `$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
"@ | Out-File -FilePath $tempScriptPath -Encoding UTF8
# Start new PowerShell window with the script
Start-Process powershell.exe -ArgumentList "-NoExit", "-ExecutionPolicy", "Bypass", "-File", $tempScriptPath -Wait:$false
} catch {
# Error handling
Write-Host "`n[ERROR] An error occurred!" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
Write-Host "Stack trace:" -ForegroundColor Red
Write-Host $_.ScriptStackTrace -ForegroundColor DarkRed
} finally {
# Ensure script doesn't exit immediately
Write-Host "`nPress any key to exit..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}