publish.ps1 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. param(
  2. [string]$Tarball,
  3. [switch]$AuthOnly
  4. )
  5. $ErrorActionPreference = 'Stop'
  6. $Root = Resolve-Path (Join-Path $PSScriptRoot '..\..\..')
  7. $PackageRoot = Join-Path $Root 'claude-code\claude-code-qiwe-assistant'
  8. $SharedEnvPath = Join-Path $PSScriptRoot '..\claude-code-voc-intelligence\.env.local'
  9. function Read-LocalToken([string]$FilePath) {
  10. if (-not (Test-Path -LiteralPath $FilePath)) { return '' }
  11. foreach ($raw in Get-Content -LiteralPath $FilePath) {
  12. $line = $raw.Trim()
  13. if (-not $line -or $line.StartsWith('#') -or -not $line.Contains('=')) { continue }
  14. $parts = $line.Split('=', 2)
  15. if ($parts[0].Trim() -in @('NPM_TOKEN', 'NODE_AUTH_TOKEN')) {
  16. return $parts[1].Trim().Trim('"').Trim("'")
  17. }
  18. }
  19. return ''
  20. }
  21. $token = if ($env:NPM_TOKEN) {
  22. $env:NPM_TOKEN
  23. } elseif ($env:NODE_AUTH_TOKEN) {
  24. $env:NODE_AUTH_TOKEN
  25. } else {
  26. Read-LocalToken $SharedEnvPath
  27. }
  28. if (-not $token) { throw 'npm token is not configured in the local release environment.' }
  29. $npmrc = Join-Path $env:TEMP ('fmode-qiwei-' + [guid]::NewGuid().ToString('N') + '.npmrc')
  30. try {
  31. Set-Content -LiteralPath $npmrc -Value "//registry.npmjs.org/:_authToken=$token" -Encoding ascii
  32. $env:NPM_CONFIG_USERCONFIG = $npmrc
  33. $account = npm whoami --registry=https://registry.npmjs.org/
  34. if ($LASTEXITCODE -ne 0) { throw 'npm authentication check failed.' }
  35. Write-Host "[auth] npm account: $account"
  36. if ($AuthOnly) { return }
  37. if (-not $Tarball) { throw 'Tarball is required unless -AuthOnly is used.' }
  38. $resolvedTarball = Resolve-Path -LiteralPath $Tarball
  39. npm publish $resolvedTarball --access public --registry=https://registry.npmjs.org/
  40. if ($LASTEXITCODE -ne 0) { throw 'npm publish failed.' }
  41. } finally {
  42. Remove-Item Env:NPM_CONFIG_USERCONFIG -ErrorAction SilentlyContinue
  43. Remove-Item -LiteralPath $npmrc -Force -ErrorAction SilentlyContinue
  44. $token = $null
  45. }