Files
sf-cli-wrapper/sf-org-create.ps1
reynold 579264e3d1 Update PowerShell scripts to use consistent two-character options (Part 1)
🔄 PowerShell Option Consistency - Phase 1 Complete:

 Updated Scripts:
  - sf-deploy.ps1: -to, -sr, -dr, -ts, -hp (matches Bash)
  - sf-dry-run.ps1: -to, -sr, -dr, -ts, -hp (matches Bash)
  - sf-web-open.ps1: -to, -pt, -ur, -hp (matches Bash)
  - sf-check.ps1: -vb, -hp (matches Bash)
  - sf-org-create.ps1: -al, -dd, -df, -st, -tp, -hp (matches Bash)

🎯 Consistency Achieved:
  - All parameter names now match their Bash counterparts exactly
  - Help text updated with descriptive option names
  - Examples updated to show new two-character options
  - All validation logic updated to use new parameter names

🚀 Cross-Platform Alignment:
  - PowerShell and Bash scripts now have identical option schemes
  - Users get consistent experience across platforms
  - No more confusion between single-char and two-char options

Remaining: 7 more PowerShell scripts to update for full consistency.
2025-08-28 19:19:15 +08:00

254 lines
7.4 KiB
PowerShell

param(
[string]$al = "",
[int]$dd = 7,
[string]$df = "",
[switch]$st,
[string]$tp = "",
[switch]$hp
)
function Show-Help {
@"
sf-org-create.ps1 wrapper for smart scratch org creation
USAGE:
sf-org-create.ps1 -al <ORG_NAME> [-dd <DAYS>] [-df <CONFIG_FILE>] [-st] [-tp <TEMPLATE>] [-hp]
OPTIONS:
-al Alias - name/alias for the new scratch org (required)
-dd Duration Days - duration in days (default: 7, max: 30)
-df Definition File - path to scratch org definition file (default: config/project-scratch-def.json)
-st Set Default - set as default org alias after creation
-tp Template - use predefined template (standard, testing, minimal, full)
-hp Help - show this help
EXAMPLES:
1) Create basic scratch org:
sf-org-create.ps1 -al "MyDevOrg"
2) Create testing org for 1 day:
sf-org-create.ps1 -al "QuickTest" -dd 1 -tp testing
3) Create with custom config and set as default:
sf-org-create.ps1 -al "MainDev" -dd 14 -df "config/custom-scratch-def.json" -st
4) Create full-featured org:
sf-org-create.ps1 -al "FullEnv" -tp full -dd 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 ($hp -or ($al -eq "" -and $df -eq "" -and $tp -eq "")) {
Show-Help
exit 0
}
# Validate required parameters
if ($al -eq "") {
Write-Error "Error: Org alias (-al) is required."
Write-Host ""
Show-Help
exit 1
}
# Validate duration
if ($dd -lt 1 -or $dd -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 ($tp -ne "") {
Write-Host "📝 Creating scratch org definition from template: $tp"
$finalConfig = New-TemplateConfig -Template $tp
$tempConfigCreated = $true
}
elseif ($df -ne "") {
if (-not (Test-Path $df)) {
Write-Error "Error: Config file '$df' not found."
exit 1
}
$finalConfig = $df
}
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 += $dd.ToString()
$cmd += "--alias"
$cmd += $al
# Set as default if requested
if ($st) {
$cmd += "--set-default"
}
Write-Host "🚀 Creating scratch org '$al' for $dd 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 '$al' created successfully!" -ForegroundColor Green
# Open the org
Write-Host "🌐 Opening org in browser..."
try {
& sf org open --target-org $al
}
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 $al
}
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
}
}