diff --git a/.gitignore b/.gitignore index 7c24f20..e442b33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,13 @@ # npm node_modules +package-lock.json # repo pakai yarn.lock, jangan commit keduanya # clasp files creds.json +# multi-account: token OAuth + scriptId per env, JANGAN commit +.clasprc*.json +.clasp.json +.clasp.*.json # certs *.pem diff --git a/Use-Clasp.ps1 b/Use-Clasp.ps1 new file mode 100644 index 0000000..c080b8c --- /dev/null +++ b/Use-Clasp.ps1 @@ -0,0 +1,48 @@ +#Requires -Version 5.1 +<# +.SYNOPSIS + Aktifkan salah satu akun clasp (dev / prod / bdn) dengan swap file kredensial global. +.DESCRIPTION + clasp hanya mengenal SATU login global (%USERPROFILE%\.clasprc.json) dan tidak + punya flag --local (terverifikasi di clasp 2.5.0). Skrip ini meng-copy profil + tersimpan (.clasp-accounts\.clasprc.json) menjadi login aktif, lalu + verifikasi dengan `clasp login --status`. +.EXAMPLE + powershell -NoProfile -ExecutionPolicy Bypass -File .\Use-Clasp.ps1 dev +#> +param( + [ValidateSet("dev", "prod", "bdn")] + [string]$Account = "dev" +) + +$ErrorActionPreference = "Stop" + +$profileDir = Join-Path $env:USERPROFILE ".clasp-accounts" +$src = Join-Path $profileDir ("$Account.clasprc.json") +$dst = Join-Path $env:USERPROFILE ".clasprc.json" + +if (-not (Test-Path -LiteralPath $src)) { + Write-Output "Profil '$Account' belum ada: $src" + Write-Output "Cara buat (sekali per akun):" + Write-Output " 1. clasp login # login sebagai akun-$Account di browser" + Write-Output " 2. New-Item -ItemType Directory -Force '$profileDir'" + Write-Output " 3. Copy-Item '$dst' '$src'" + exit 1 +} + +Copy-Item -LiteralPath $src -Destination $dst -Force +Write-Output "Akun clasp aktif: $Account" + +$claspCmd = Get-Command clasp -ErrorAction SilentlyContinue +if (-not $claspCmd) { + $localClasp = Join-Path $PSScriptRoot "node_modules/.bin/clasp.ps1" + if (Test-Path -LiteralPath $localClasp) { $claspCmd = $localClasp } + else { $claspCmd = "clasp" } +} + +& $claspCmd login --status +if ($LASTEXITCODE -ne 0) { + Write-Output "Token profil '$Account' tidak valid/kadaluarsa." + Write-Output "Perbaiki: clasp login (sebagai akun-$Account), lalu Copy-Item '$dst' '$src' -Force" + exit 1 +} diff --git a/package.json b/package.json index f9e484c..55ffe72 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,9 @@ "setup": "rimraf .clasp.json && mkdirp dist && clasp create --type sheets --title \"My React Project\" --rootDir ./dist && mv ./dist/.clasp.json ./.clasp.json && rimraf dist", "open": "clasp open --addon", "push": "clasp push", + "use:dev": "powershell -NoProfile -ExecutionPolicy Bypass -File ./Use-Clasp.ps1 dev", + "use:prod": "powershell -NoProfile -ExecutionPolicy Bypass -File ./Use-Clasp.ps1 prod", + "use:bdn": "powershell -NoProfile -ExecutionPolicy Bypass -File ./Use-Clasp.ps1 bdn", "setup:https": "mkdirp certs && mkcert -key-file ./certs/key.pem -cert-file ./certs/cert.pem localhost 127.0.0.1", "build:dev": "tsc && vite build --mode development", "build": "tsc && vite build --mode production", diff --git a/scripts/clasp-env.ps1 b/scripts/clasp-env.ps1 new file mode 100644 index 0000000..3de95d8 --- /dev/null +++ b/scripts/clasp-env.ps1 @@ -0,0 +1,67 @@ +#Requires -Version 5.1 +<# +.SYNOPSIS + Wrapper satu pintu untuk clasp tiga akun + tiga project (dev / prod / bdn). +.DESCRIPTION + Urutan kerja: swap akun (Use-Clasp.ps1) -> salin .clasp..json menjadi + .clasp.json -> guard khusus prod -> jalankan clasp. Jangan panggil + `clasp push` langsung; selalu lewat wrapper ini / npm script. +.EXAMPLE + powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\clasp-env.ps1 -Env dev -Cmd use + powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\clasp-env.ps1 -Env dev -Cmd push + powershell -NoProfile -ExecutionPolicy Bypass -File .\scripts\clasp-env.ps1 -Env prod -Cmd push +#> +param( + [ValidateSet("dev", "prod", "bdn")] + [string]$Env = "dev", + [ValidateSet("use", "push")] + [string]$Cmd = "use" +) + +$ErrorActionPreference = "Stop" +$repoRoot = Split-Path $PSScriptRoot -Parent + +# 1. Aktifkan akun yang sesuai (berhenti bila profil/token bermasalah). +& powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $repoRoot "Use-Clasp.ps1") $Env +if ($LASTEXITCODE -ne 0) { exit 1 } + +# 2. Arahkan ke project script yang sesuai. +$projectFile = Join-Path $repoRoot ".clasp.$Env.json" +$activeFile = Join-Path $repoRoot ".clasp.json" +if (-not (Test-Path -LiteralPath $projectFile)) { + Write-Output "File project belum ada: $projectFile" + Write-Output "Cara buat: clasp create --type sheets --title ... --rootDir ./dist" + Write-Output "lalu isi scriptId project-$Env, atau copy dari .clasp.json.SAMPLE" + exit 1 +} +Copy-Item -LiteralPath $projectFile -Destination $activeFile -Force + +$scriptId = (Get-Content -LiteralPath $activeFile -Raw | ConvertFrom-Json).scriptId +Write-Output "Project aktif [$Env]: $scriptId" + +if ($Cmd -eq "use") { + Write-Output "Siap. Perintah clasp berikutnya memakai akun-$Env + project-$Env." + exit 0 +} + +# 3. Guard prod/bdn: wajib konfirmasi eksplisit. +if ($Env -eq "prod" -or $Env -eq "bdn") { + $need = $Env.ToUpper() + $answer = Read-Host "Ketik $need untuk push ke PROYEK $need ($scriptId)" + if ($answer -cne $need) { + Write-Output "Dibatalkan. Tidak ada yang di-push." + exit 1 + } +} + +# 4. Push (build dulu agar dist/ selalu segar dari src/). +Push-Location $repoRoot +try { + & npm run build + if ($LASTEXITCODE -ne 0) { exit 1 } + & clasp push + exit $LASTEXITCODE +} +finally { + Pop-Location +}