posimai-root/scripts/new-app.sh

127 lines
5.8 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# ═══════════════════════════════════════════════════════════════
# Posimai New App Generator
# 使い方: bash scripts/new-app.sh <app-id> "<表示名>" "<説明>"
# 例: bash scripts/new-app.sh posimai-timer "Posimai Timer" "シンプルなタイマーアプリ"
# ═══════════════════════════════════════════════════════════════
set -e # エラーで即停止
# ── 引数チェック ──────────────────────────────────────────────
APP_ID="${1}"
APP_NAME="${2}"
APP_DESC="${3:-Posimai App}"
if [[ -z "$APP_ID" || -z "$APP_NAME" ]]; then
echo "使い方: bash scripts/new-app.sh <app-id> \"<表示名>\" \"<説明>\""
echo "例: bash scripts/new-app.sh posimai-timer \"Posimai Timer\" \"シンプルなタイマーアプリ\""
exit 1
fi
# ── 設定 ──────────────────────────────────────────────────────
BASE_DIR="$(cd "$(dirname "$0")/.." && pwd)"
TEMPLATE_DIR="$BASE_DIR/_template"
APP_DIR="$BASE_DIR/$APP_ID"
GITEA_URL="http://100.76.7.3:3000"
GITEA_USER="mai"
GITEA_PASS=$(printf "protocol=http\nhost=100.76.7.3\n" | git credential fill 2>/dev/null | grep ^password | cut -d= -f2)
GITHUB_ORG="posimai"
# ── 既存チェック ──────────────────────────────────────────────
if [[ -d "$APP_DIR" ]]; then
echo "エラー: $APP_DIR はすでに存在します"
exit 1
fi
echo ""
echo "═══════════════════════════════════════"
echo " 新アプリ作成: $APP_NAME"
echo " ID: $APP_ID"
echo "═══════════════════════════════════════"
# ── Step 1: テンプレートをコピー ──────────────────────────────
echo ""
echo "[ 1/6 ] テンプレートをコピー中..."
cp -r "$TEMPLATE_DIR" "$APP_DIR"
# プレースホルダーを置換(全ファイル対象)
find "$APP_DIR" -type f \( -name "*.html" -o -name "*.json" -o -name "*.js" -o -name "*.md" \) | while read f; do
sed -i "s/APP_NAME/$APP_NAME/g; s/APP_ID/$APP_ID/g; s/APP_DESCRIPTION/$APP_DESC/g" "$f"
done
echo " 完了: $APP_DIR"
# ── Step 2: Git 初期化 ─────────────────────────────────────────
echo ""
echo "[ 2/6 ] Git 初期化..."
cd "$APP_DIR"
git init -b main
git add .
git commit -m "init: $APP_NAME"
echo " 完了"
# ── Step 3: Gitea リポジトリ作成 ─────────────────────────────
echo ""
echo "[ 3/6 ] Gitea にリポジトリ作成中..."
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
-X POST "$GITEA_URL/api/v1/user/repos" \
-H "Content-Type: application/json" \
-u "$GITEA_USER:$GITEA_PASS" \
-d "{\"name\":\"$APP_ID\",\"private\":true,\"auto_init\":false}")
if [[ "$HTTP_STATUS" == "201" ]]; then
echo " 完了: $GITEA_URL/$GITEA_USER/$APP_ID"
else
echo " 警告: Gitea API status=$HTTP_STATUS (既存または接続不可)"
fi
git remote add gitea "$GITEA_URL/$GITEA_USER/$APP_ID.git"
git push gitea main
echo " Push 完了"
# ── Step 4: GitHub リポジトリ作成 ────────────────────────────
echo ""
echo "[ 4/6 ] GitHub にリポジトリ作成中..."
gh repo create "$GITHUB_ORG/$APP_ID" --private --description "$APP_DESC" 2>&1
git remote add github "https://github.com/$GITHUB_ORG/$APP_ID.git"
git push github main
echo " 完了: https://github.com/$GITHUB_ORG/$APP_ID"
# ── Step 5: Vercel 接続 ───────────────────────────────────────
echo ""
echo "[ 5/6 ] Vercel に接続中..."
# github リモートを選択するため "2" を入力gitea=1, github=2
printf "2\n" | vercel git connect "https://github.com/$GITHUB_ORG/$APP_ID.git" --yes 2>&1 || true
echo " 完了"
# ── Step 6: git hooks インストール ───────────────────────────
echo ""
echo "[ 6/6 ] Git フック設定..."
cat > "$APP_DIR/.git/hooks/post-commit" << 'HOOK'
#!/bin/sh
echo ""
echo "📦 コミット完了。デプロイするには:"
echo " npm run deploy"
echo " (Gitea + GitHub → Vercel 自動デプロイ)"
echo ""
HOOK
chmod +x "$APP_DIR/.git/hooks/post-commit"
echo " 完了"
# ── 完了メッセージ ────────────────────────────────────────────
echo ""
echo "═══════════════════════════════════════"
echo " $APP_NAME — セットアップ完了!"
echo "═══════════════════════════════════════"
echo ""
echo " ディレクトリ : $APP_DIR"
echo " Gitea : $GITEA_URL/$GITEA_USER/$APP_ID"
echo " GitHub : https://github.com/$GITHUB_ORG/$APP_ID"
echo " Vercel : https://$APP_ID.vercel.app (初回デプロイ後)"
echo ""
echo " 次のコマンドで開発開始:"
echo " cd $APP_ID"
echo " # コードを編集して..."
echo " git add . && git commit -m \"feat: ...\""
echo " npm run deploy"
echo ""