254 lines
7.2 KiB
PowerShell
254 lines
7.2 KiB
PowerShell
param(
|
|
[string]$n = "",
|
|
[int]$d = 7,
|
|
[string]$f = "",
|
|
[switch]$a,
|
|
[string]$t = "",
|
|
[switch]$h
|
|
)
|
|
|
|
function Show-Help {
|
|
@"
|
|
sf-org-create.ps1 — wrapper for smart scratch org creation
|
|
|
|
USAGE:
|
|
sf-org-create.ps1 -n <ORG_NAME> [-d <DAYS>] [-f <CONFIG_FILE>] [-a] [-t <TEMPLATE>] [-h]
|
|
|
|
OPTIONS:
|
|
-n Name/alias for the new scratch org (required)
|
|
-d Duration in days (default: 7, max: 30)
|
|
-f Path to scratch org definition file (default: config/project-scratch-def.json)
|
|
-a Set as default org alias after creation
|
|
-t Use predefined template (standard, testing, minimal, full)
|
|
-h Show this help
|
|
|
|
EXAMPLES:
|
|
1) Create basic scratch org:
|
|
sf-org-create.ps1 -n "MyDevOrg"
|
|
|
|
2) Create testing org for 1 day:
|
|
sf-org-create.ps1 -n "QuickTest" -d 1 -t testing
|
|
|
|
3) Create with custom config and set as default:
|
|
sf-org-create.ps1 -n "MainDev" -d 14 -f "config/custom-scratch-def.json" -a
|
|
|
|
4) Create full-featured org:
|
|
sf-org-create.ps1 -n "FullEnv" -t full -d 30
|
|
|
|
TEMPLATES:
|
|
- standard: Basic scratch org with common features
|
|
- testing: Optimized for running tests (minimal data)
|
|
- minimal: Bare-bones org for quick tasks
|
|
- full: All available features enabled
|
|
|
|
Notes:
|
|
- If no config file is specified, will look for config/project-scratch-def.json
|
|
- Templates override config file settings
|
|
- Org will be automatically opened in browser after creation
|
|
"@
|
|
}
|
|
|
|
# Show help if requested or no parameters
|
|
if ($h -or ($n -eq "" -and $f -eq "" -and $t -eq "")) {
|
|
Show-Help
|
|
exit 0
|
|
}
|
|
|
|
# Validate required parameters
|
|
if ($n -eq "") {
|
|
Write-Error "Error: Org name (-n) is required."
|
|
Write-Host ""
|
|
Show-Help
|
|
exit 1
|
|
}
|
|
|
|
# Validate duration
|
|
if ($d -lt 1 -or $d -gt 30) {
|
|
Write-Error "Error: Duration must be between 1 and 30 days."
|
|
exit 1
|
|
}
|
|
|
|
# Silent environment check
|
|
try {
|
|
Get-Command sf -ErrorAction Stop | Out-Null
|
|
}
|
|
catch {
|
|
Write-Host "❌ Salesforce CLI (sf) not found!" -ForegroundColor Red
|
|
Write-Host ""
|
|
Write-Host "Running environment check to help you get started..."
|
|
Write-Host ""
|
|
|
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
$sfCheckPath = Join-Path $scriptDir "sf-check.ps1"
|
|
|
|
if (Test-Path $sfCheckPath) {
|
|
& $sfCheckPath
|
|
}
|
|
elseif (Get-Command sf-check.ps1 -ErrorAction SilentlyContinue) {
|
|
sf-check.ps1
|
|
}
|
|
else {
|
|
Write-Host "sf-check.ps1 not found. Please install the Salesforce CLI from:"
|
|
Write-Host "https://developer.salesforce.com/tools/sfdxcli"
|
|
}
|
|
exit 1
|
|
}
|
|
|
|
# Function to create template configs
|
|
function New-TemplateConfig {
|
|
param([string]$Template)
|
|
|
|
$tempConfig = Join-Path $env:TEMP "sf-org-create-$(Get-Random).json"
|
|
|
|
switch ($Template) {
|
|
"standard" {
|
|
@{
|
|
orgName = "Standard Scratch Org"
|
|
edition = "Developer"
|
|
features = @("EnableSetPasswordInApi", "MultiCurrency", "AuthorApex")
|
|
settings = @{
|
|
lightningExperienceSettings = @{
|
|
enableS1DesktopEnabled = $true
|
|
}
|
|
mobileSettings = @{
|
|
enableS1EncryptedStoragePref2 = $false
|
|
}
|
|
}
|
|
} | ConvertTo-Json -Depth 4 | Out-File $tempConfig -Encoding UTF8
|
|
}
|
|
"testing" {
|
|
@{
|
|
orgName = "Testing Scratch Org"
|
|
edition = "Developer"
|
|
features = @("EnableSetPasswordInApi", "AuthorApex")
|
|
settings = @{
|
|
lightningExperienceSettings = @{
|
|
enableS1DesktopEnabled = $true
|
|
}
|
|
}
|
|
} | ConvertTo-Json -Depth 4 | Out-File $tempConfig -Encoding UTF8
|
|
}
|
|
"minimal" {
|
|
@{
|
|
orgName = "Minimal Scratch Org"
|
|
edition = "Developer"
|
|
features = @("AuthorApex")
|
|
} | ConvertTo-Json -Depth 4 | Out-File $tempConfig -Encoding UTF8
|
|
}
|
|
"full" {
|
|
@{
|
|
orgName = "Full-Featured Scratch Org"
|
|
edition = "Enterprise"
|
|
features = @(
|
|
"EnableSetPasswordInApi", "MultiCurrency", "AuthorApex",
|
|
"ContactsToMultipleAccounts", "ServiceCloud", "SalesCloud",
|
|
"Communities", "Sites", "PersonAccounts"
|
|
)
|
|
settings = @{
|
|
lightningExperienceSettings = @{
|
|
enableS1DesktopEnabled = $true
|
|
}
|
|
mobileSettings = @{
|
|
enableS1EncryptedStoragePref2 = $false
|
|
}
|
|
enhancedEmailSettings = @{
|
|
enableRestrictTlsAndRequireSSL = $true
|
|
}
|
|
}
|
|
} | ConvertTo-Json -Depth 4 | Out-File $tempConfig -Encoding UTF8
|
|
}
|
|
default {
|
|
Write-Error "Error: Unknown template '$Template'. Available: standard, testing, minimal, full"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
return $tempConfig
|
|
}
|
|
|
|
# Determine config file to use
|
|
$finalConfig = ""
|
|
$tempConfigCreated = $false
|
|
|
|
if ($t -ne "") {
|
|
Write-Host "📝 Creating scratch org definition from template: $t"
|
|
$finalConfig = New-TemplateConfig -Template $t
|
|
$tempConfigCreated = $true
|
|
}
|
|
elseif ($f -ne "") {
|
|
if (-not (Test-Path $f)) {
|
|
Write-Error "Error: Config file '$f' not found."
|
|
exit 1
|
|
}
|
|
$finalConfig = $f
|
|
}
|
|
elseif (Test-Path "config/project-scratch-def.json") {
|
|
Write-Host "📝 Using default config: config/project-scratch-def.json"
|
|
$finalConfig = "config/project-scratch-def.json"
|
|
}
|
|
else {
|
|
Write-Host "⚠️ No config file found, creating minimal scratch org"
|
|
$finalConfig = New-TemplateConfig -Template "minimal"
|
|
$tempConfigCreated = $true
|
|
}
|
|
|
|
# Build the command
|
|
$cmd = @("sf", "org", "create", "scratch")
|
|
$cmd += "--definition-file"
|
|
$cmd += $finalConfig
|
|
$cmd += "--duration-days"
|
|
$cmd += $d.ToString()
|
|
$cmd += "--alias"
|
|
$cmd += $n
|
|
|
|
# Set as default if requested
|
|
if ($a) {
|
|
$cmd += "--set-default"
|
|
}
|
|
|
|
Write-Host "🚀 Creating scratch org '$n' for $d days..."
|
|
Write-Host ">>> Running: $($cmd -join ' ')" -ForegroundColor Yellow
|
|
Write-Host ""
|
|
|
|
try {
|
|
& $cmd[0] $cmd[1..($cmd.Length-1)]
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host ""
|
|
Write-Host "✅ Scratch org '$n' created successfully!" -ForegroundColor Green
|
|
|
|
# Open the org
|
|
Write-Host "🌐 Opening org in browser..."
|
|
try {
|
|
& sf org open --target-org $n
|
|
}
|
|
catch {
|
|
Write-Host "⚠️ Could not open org in browser" -ForegroundColor Yellow
|
|
}
|
|
|
|
# Show org info
|
|
Write-Host ""
|
|
Write-Host "📊 Org Information:"
|
|
try {
|
|
& sf org display --target-org $n
|
|
}
|
|
catch {
|
|
Write-Host "Could not display org info"
|
|
}
|
|
}
|
|
else {
|
|
Write-Host "❌ Failed to create scratch org" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
}
|
|
catch {
|
|
Write-Error "Failed to execute sf command: $_"
|
|
exit 1
|
|
}
|
|
finally {
|
|
# Clean up temp config if we created one
|
|
if ($tempConfigCreated -and (Test-Path $finalConfig)) {
|
|
Remove-Item $finalConfig -Force
|
|
}
|
|
}
|