242 lines
6.0 KiB
Bash
Executable File
242 lines
6.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
show_help() {
|
|
cat <<'EOF'
|
|
sf-org-create — wrapper for smart scratch org creation
|
|
|
|
USAGE:
|
|
sf-org-create -n <ORG_NAME> [-d <DAYS>] [-f <CONFIG_FILE>] [-a <ALIAS>] [-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 -n "MyDevOrg"
|
|
|
|
2) Create testing org for 1 day:
|
|
sf-org-create -n "QuickTest" -d 1 -t testing
|
|
|
|
3) Create with custom config and set as default:
|
|
sf-org-create -n "MainDev" -d 14 -f "config/custom-scratch-def.json" -a
|
|
|
|
4) Create full-featured org:
|
|
sf-org-create -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
|
|
EOF
|
|
}
|
|
|
|
# Default values
|
|
ORG_NAME=""
|
|
DURATION=7
|
|
CONFIG_FILE=""
|
|
SET_DEFAULT=false
|
|
TEMPLATE=""
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
show_help
|
|
exit 0
|
|
fi
|
|
|
|
while getopts ":n:d:f:at:h" opt; do
|
|
case "$opt" in
|
|
n) ORG_NAME="$OPTARG" ;;
|
|
d) DURATION="$OPTARG" ;;
|
|
f) CONFIG_FILE="$OPTARG" ;;
|
|
a) SET_DEFAULT=true ;;
|
|
t) TEMPLATE="$OPTARG" ;;
|
|
h) show_help; exit 0 ;;
|
|
\?) echo "Unknown option: -$OPTARG" >&2; echo; show_help; exit 1 ;;
|
|
:) echo "Option -$OPTARG requires an argument." >&2; echo; show_help; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
# Validate required parameters
|
|
if [[ -z "$ORG_NAME" ]]; then
|
|
echo "Error: Org name (-n) is required." >&2
|
|
echo
|
|
show_help
|
|
exit 1
|
|
fi
|
|
|
|
# Validate duration
|
|
if ! [[ "$DURATION" =~ ^[0-9]+$ ]] || [[ "$DURATION" -lt 1 ]] || [[ "$DURATION" -gt 30 ]]; then
|
|
echo "Error: Duration must be a number between 1 and 30." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Silent environment check
|
|
if ! command -v sf >/dev/null 2>&1; then
|
|
echo "❌ Salesforce CLI (sf) not found!"
|
|
echo
|
|
echo "Running environment check to help you get started..."
|
|
echo
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
if [[ -x "$SCRIPT_DIR/sf-check" ]]; then
|
|
"$SCRIPT_DIR/sf-check"
|
|
elif command -v sf-check >/dev/null 2>&1; then
|
|
sf-check
|
|
else
|
|
echo "sf-check not found. Please install the Salesforce CLI from:"
|
|
echo "https://developer.salesforce.com/tools/sfdxcli"
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
# Create temporary config file for templates
|
|
create_template_config() {
|
|
local template="$1"
|
|
local temp_config="/tmp/sf-org-create-$$.json"
|
|
|
|
case "$template" in
|
|
"standard")
|
|
cat > "$temp_config" <<EOF
|
|
{
|
|
"orgName": "Standard Scratch Org",
|
|
"edition": "Developer",
|
|
"features": ["EnableSetPasswordInApi", "MultiCurrency", "AuthorApex"],
|
|
"settings": {
|
|
"lightningExperienceSettings": {
|
|
"enableS1DesktopEnabled": true
|
|
},
|
|
"mobileSettings": {
|
|
"enableS1EncryptedStoragePref2": false
|
|
}
|
|
}
|
|
}
|
|
EOF
|
|
;;
|
|
"testing")
|
|
cat > "$temp_config" <<EOF
|
|
{
|
|
"orgName": "Testing Scratch Org",
|
|
"edition": "Developer",
|
|
"features": ["EnableSetPasswordInApi", "AuthorApex"],
|
|
"settings": {
|
|
"lightningExperienceSettings": {
|
|
"enableS1DesktopEnabled": true
|
|
}
|
|
}
|
|
}
|
|
EOF
|
|
;;
|
|
"minimal")
|
|
cat > "$temp_config" <<EOF
|
|
{
|
|
"orgName": "Minimal Scratch Org",
|
|
"edition": "Developer",
|
|
"features": ["AuthorApex"]
|
|
}
|
|
EOF
|
|
;;
|
|
"full")
|
|
cat > "$temp_config" <<EOF
|
|
{
|
|
"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
|
|
}
|
|
}
|
|
}
|
|
EOF
|
|
;;
|
|
*)
|
|
echo "Error: Unknown template '$template'. Available: standard, testing, minimal, full" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
echo "$temp_config"
|
|
}
|
|
|
|
# Determine config file to use
|
|
FINAL_CONFIG=""
|
|
if [[ -n "$TEMPLATE" ]]; then
|
|
echo "📝 Creating scratch org definition from template: $TEMPLATE"
|
|
FINAL_CONFIG=$(create_template_config "$TEMPLATE")
|
|
elif [[ -n "$CONFIG_FILE" ]]; then
|
|
if [[ ! -f "$CONFIG_FILE" ]]; then
|
|
echo "Error: Config file '$CONFIG_FILE' not found." >&2
|
|
exit 1
|
|
fi
|
|
FINAL_CONFIG="$CONFIG_FILE"
|
|
elif [[ -f "config/project-scratch-def.json" ]]; then
|
|
echo "📝 Using default config: config/project-scratch-def.json"
|
|
FINAL_CONFIG="config/project-scratch-def.json"
|
|
else
|
|
echo "⚠️ No config file found, creating minimal scratch org"
|
|
FINAL_CONFIG=$(create_template_config "minimal")
|
|
fi
|
|
|
|
# Build the command
|
|
CMD=(sf org create scratch)
|
|
CMD+=(--definition-file "$FINAL_CONFIG")
|
|
CMD+=(--duration-days "$DURATION")
|
|
CMD+=(--alias "$ORG_NAME")
|
|
|
|
# Set as default if requested
|
|
if [[ "$SET_DEFAULT" == "true" ]]; then
|
|
CMD+=(--set-default)
|
|
fi
|
|
|
|
echo "🚀 Creating scratch org '$ORG_NAME' for $DURATION days..."
|
|
echo ">>> Running: ${CMD[*]}"
|
|
echo
|
|
|
|
if "${CMD[@]}"; then
|
|
echo
|
|
echo "✅ Scratch org '$ORG_NAME' created successfully!"
|
|
|
|
# Clean up temp config if we created one
|
|
if [[ -n "$TEMPLATE" ]]; then
|
|
rm -f "$FINAL_CONFIG"
|
|
fi
|
|
|
|
# Open the org
|
|
echo "🌐 Opening org in browser..."
|
|
sf org open --target-org "$ORG_NAME" || echo "⚠️ Could not open org in browser"
|
|
|
|
# Show org info
|
|
echo
|
|
echo "📊 Org Information:"
|
|
sf org display --target-org "$ORG_NAME" || echo "Could not display org info"
|
|
|
|
else
|
|
# Clean up temp config if we created one
|
|
if [[ -n "$TEMPLATE" ]]; then
|
|
rm -f "$FINAL_CONFIG"
|
|
fi
|
|
echo "❌ Failed to create scratch org"
|
|
exit 1
|
|
fi
|