Files
sf-cli-wrapper/sf-org-create
reynold 39b7f11646 Update core wrapper scripts to use two-character option scheme
- Updated sf-deploy: -o → -to, -s → -sr, -d → -dr, -t → -ts
- Updated sf-dry-run: same options as sf-deploy for consistency
- Updated sf-web-open: -o → -to, -p → -pt, -U → -ur
- Updated sf-org-create: -n → -al, -d → -dd, -f → -df, -a → -st, -t → -tp
- All scripts now use manual argument parsing to support two-character options
- Help sections updated with both short and long option forms
- Maintains backward compatibility with long options
- Consistent with README documentation and two-character scheme
2025-08-28 18:28:23 +08:00

265 lines
6.5 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 -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, --def-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 -al "MyDevOrg"
2) Create testing org for 1 day:
sf-org-create -al "QuickTest" -dd 1 -tp testing
3) Create with custom config and set as default:
sf-org-create -al "MainDev" -dd 14 -df "config/custom-scratch-def.json" -st
4) Create full-featured org:
sf-org-create -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
EOF
}
# Default values
ORG_NAME=""
DURATION=7
CONFIG_FILE=""
SET_DEFAULT=false
TEMPLATE=""
if [[ $# -eq 0 ]]; then
show_help
exit 0
fi
# Parse command line arguments using manual parsing for two-character options
while [[ $# -gt 0 ]]; do
case $1 in
-al|--alias)
ORG_NAME="$2"
shift 2
;;
-dd|--duration-days)
DURATION="$2"
shift 2
;;
-df|--def-file)
CONFIG_FILE="$2"
shift 2
;;
-st|--set-default)
SET_DEFAULT=true
shift
;;
-tp|--template)
TEMPLATE="$2"
shift 2
;;
-hp|--help)
show_help
exit 0
;;
*)
echo "Unknown option: $1" >&2
echo
show_help
exit 1
;;
esac
done
# Validate required parameters
if [[ -z "$ORG_NAME" ]]; then
echo "Error: Org alias (-al) 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