upload-target.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /** Upload one validated Desktop release to its Tencent COS update directory. */
  2. import { resolve } from 'node:path'
  3. import { parseArgs } from 'node:util'
  4. import { S3Client } from '@aws-sdk/client-s3'
  5. import type { DesktopPackageTargetName } from './package-target.ts'
  6. import { loadDesktopPackageEnvironment } from './desktop-package-environment.mjs'
  7. import { createDesktopUploadPlan } from './desktop-upload-plan.ts'
  8. import { uploadDesktopRelease } from './desktop-upload-run.ts'
  9. const SUPPORTED_TARGETS = new Set<DesktopPackageTargetName>(['mac-arm64', 'mac-x64', 'win-x64'])
  10. function targetName(value: string): DesktopPackageTargetName {
  11. if (!SUPPORTED_TARGETS.has(value as DesktopPackageTargetName)) {
  12. throw new Error(`desktop upload: unsupported target ${JSON.stringify(value)}; expected ${[...SUPPORTED_TARGETS].join(', ')}`)
  13. }
  14. return value as DesktopPackageTargetName
  15. }
  16. function requiredEnvironmentValue(environment: NodeJS.ProcessEnv, name: string): string {
  17. const value = environment[name]?.trim()
  18. if (value === undefined || value === '') {
  19. throw new Error(`desktop upload: ${name} must be set to a non-empty value`)
  20. }
  21. return value
  22. }
  23. async function main(): Promise<void> {
  24. const { positionals } = parseArgs({ args: process.argv.slice(2), allowPositionals: true })
  25. const target = positionals[0]
  26. if (target === undefined || positionals.length !== 1) {
  27. throw new Error('desktop upload: expected exactly one target')
  28. }
  29. const name = targetName(target)
  30. const environment = loadDesktopPackageEnvironment(name === 'win-x64' ? 'win32' : 'darwin')
  31. const plan = await createDesktopUploadPlan(name, { environment })
  32. const client = new S3Client({
  33. maxAttempts: 1,
  34. region: 'Auto',
  35. endpoint: 'https://cos.ap-beijing.myqcloud.com',
  36. credentials: {
  37. accessKeyId: requiredEnvironmentValue(environment, plan.secretIdEnvName),
  38. secretAccessKey: requiredEnvironmentValue(environment, plan.secretKeyEnvName),
  39. },
  40. })
  41. process.stdout.write(`desktop upload: ${plan.target} ${plan.version} -> ${plan.publicUrl}\n`)
  42. try {
  43. await uploadDesktopRelease(plan, client, resolve(import.meta.dirname, '../.desktop-build/upload-records'))
  44. }
  45. finally {
  46. client.destroy()
  47. }
  48. }
  49. if (process.argv[1] !== undefined && import.meta.filename === resolve(process.argv[1])) {
  50. main().catch(() => {
  51. process.stderr.write('desktop upload: failed; inspect the printed record directory if allocated. No automatic retry; reconcile remote state before another upload.\n')
  52. process.exitCode = 1
  53. })
  54. }