Multi-akun clasp + gitignore clasp (dev/prod/bdn)
Lint / lint (push) Waiting to run

- Use-Clasp.ps1: swap profil login global per akun clasp
- scripts/clasp-env.ps1: wrapper satu pintu swap akun+project, guard PROD/BDN
- package.json: script use:dev/prod/bdn
- .gitignore: pola generik .clasp.*.json + package-lock.json (repo pakai yarn)
This commit is contained in:
2026-09-25 20:10:28 +07:00
parent 248a90023c
commit 8e0b928a80
4 changed files with 123 additions and 0 deletions
+5
View File
@@ -1,8 +1,13 @@
# npm # npm
node_modules node_modules
package-lock.json # repo pakai yarn.lock, jangan commit keduanya
# clasp files # clasp files
creds.json creds.json
# multi-account: token OAuth + scriptId per env, JANGAN commit
.clasprc*.json
.clasp.json
.clasp.*.json
# certs # certs
*.pem *.pem
+48
View File
@@ -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\<env>.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
}
+3
View File
@@ -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", "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", "open": "clasp open --addon",
"push": "clasp push", "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", "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:dev": "tsc && vite build --mode development",
"build": "tsc && vite build --mode production", "build": "tsc && vite build --mode production",
+67
View File
@@ -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.<env>.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
}