upload-to-cdn.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env node
  2. // Upload Uppy releases to Edgly.net CDN. Copyright (c) 2018, Transloadit Ltd.
  3. //
  4. // This file:
  5. //
  6. // - Assumes EDGLY_KEY and EDGLY_SECRET are available (e.g. set via Travis secrets)
  7. // - Assumes a fully built uppy is in root dir (unless a specific tag was specified, then it's fetched from npm)
  8. // - Collects dist/ files that would be in an npm package release, and uploads to eg. https://transloadit.edgly.net/releases/uppy/v1.0.1/uppy.css
  9. // - Uses local package by default, if [version] argument was specified, takes package from npm
  10. //
  11. // Run as:
  12. //
  13. // npm run uploadcdn <package-name> [version]
  14. //
  15. // To override an existing release (DANGER!)
  16. //
  17. // npm run uploadcdn <package-name> [version] -- --force
  18. //
  19. // Authors:
  20. //
  21. // - Kevin van Zonneveld <kevin@transloadit.com>
  22. const path = require('path')
  23. const AWS = require('aws-sdk')
  24. const packlist = require('npm-packlist')
  25. const tar = require('tar')
  26. const pacote = require('pacote')
  27. const concat = require('concat-stream')
  28. const mime = require('mime-types')
  29. const { promisify } = require('util')
  30. const readFile = promisify(require('fs').readFile)
  31. const finished = promisify(require('stream').finished)
  32. function delay (ms) {
  33. return new Promise(resolve => setTimeout(resolve, ms))
  34. }
  35. const AWS_REGION = 'us-east-1'
  36. const AWS_BUCKET = 'crates.edgly.net'
  37. const AWS_DIRECTORY = '756b8efaed084669b02cb99d4540d81f/default'
  38. /**
  39. * Get remote dist/ files by fetching the tarball for the given version
  40. * from npm and filtering it down to package/dist/ files.
  41. *
  42. * @param {string} Package name, eg. @uppy/robodog
  43. * @param {string} Package version, eg. "1.2.0"
  44. * @returns a Map<string, Buffer>, filename → content
  45. */
  46. async function getRemoteDistFiles (packageName, version) {
  47. const files = new Map()
  48. const tarball = pacote.tarball.stream(`${packageName}@${version}`)
  49. .pipe(new tar.Parse())
  50. tarball.on('entry', (readEntry) => {
  51. if (readEntry.path.startsWith('package/dist/')) {
  52. readEntry
  53. .pipe(concat((buf) => {
  54. files.set(readEntry.path.replace(/^package\/dist\//, ''), buf)
  55. }))
  56. .on('error', (err) => {
  57. tarball.emit('error', err)
  58. })
  59. } else {
  60. readEntry.resume()
  61. }
  62. })
  63. await finished(tarball)
  64. return files
  65. }
  66. /**
  67. * Get local dist/ files by asking npm-packlist what files would be added
  68. * to an npm package during publish, and filtering those down to just dist/ files.
  69. *
  70. * @param {string} Base file path of the package, eg. ./packages/@uppy/locales
  71. * @returns a Map<string, Buffer>, filename → content
  72. */
  73. async function getLocalDistFiles (packagePath) {
  74. const files = (await packlist({ path: packagePath }))
  75. .filter(f => f.startsWith('dist/'))
  76. .map(f => f.replace(/^dist\//, ''))
  77. const entries = await Promise.all(
  78. files.map(async (f) => [
  79. f,
  80. await readFile(path.join(packagePath, 'dist', f))
  81. ])
  82. )
  83. return new Map(entries)
  84. }
  85. async function main (packageName, version) {
  86. if (!packageName) {
  87. console.error('usage: upload-to-cdn <packagename> [version]')
  88. console.error('Must provide a package name')
  89. process.exit(1)
  90. }
  91. if (!process.env.EDGLY_KEY || !process.env.EDGLY_SECRET) {
  92. console.error('Missing EDGLY_KEY or EDGLY_SECRET env variables, bailing')
  93. process.exit(1)
  94. }
  95. // version should only be a positional arg and semver string
  96. // this deals with usage like `npm run uploadcdn uppy -- --force`
  97. // where we force push a local build
  98. if (version && version.startsWith('-')) version = undefined
  99. const s3 = new AWS.S3({
  100. credentials: new AWS.Credentials({
  101. accessKeyId: process.env.EDGLY_KEY,
  102. secretAccessKey: process.env.EDGLY_SECRET
  103. }),
  104. region: AWS_REGION
  105. })
  106. const remote = !!version
  107. if (!remote) {
  108. version = require(`../packages/${packageName}/package.json`).version
  109. }
  110. // Warn if uploading a local build not from CI:
  111. // - If we're on CI, this should be a release commit.
  112. // - If we're local, normally we should upload a released version, not a local build.
  113. if (!remote && !process.env.CI) {
  114. console.log('Warning, writing a local build to the CDN, this is usually not what you want. Sleeping 3s. Press CTRL+C!')
  115. await delay(3000)
  116. }
  117. const packagePath = remote
  118. ? `${packageName}@${version}`
  119. : path.join(__dirname, '..', 'packages', packageName)
  120. // uppy → releases/uppy/
  121. // @uppy/robodog → releases/uppy/robodog/
  122. // @uppy/locales → releases/uppy/locales/
  123. const dirName = packageName.startsWith('@uppy/')
  124. ? packageName.replace(/^@/, '')
  125. : 'uppy'
  126. const outputPath = path.posix.join('releases', dirName, `v${version}`)
  127. const { Contents: existing } = await s3.listObjects({
  128. Bucket: AWS_BUCKET,
  129. Prefix: `${AWS_DIRECTORY}/${outputPath}/`
  130. }).promise()
  131. if (existing.length > 0) {
  132. if (process.argv.includes('--force')) {
  133. console.warn(`WARN Release files for ${dirName} v${version} already exist, overwriting...`)
  134. } else {
  135. console.error(`Release files for ${dirName} v${version} already exist, exiting...`)
  136. process.exit(1)
  137. }
  138. }
  139. const files = remote
  140. ? await getRemoteDistFiles(packageName, version)
  141. : await getLocalDistFiles(packagePath)
  142. for (const [filename, buffer] of files.entries()) {
  143. const key = path.posix.join(AWS_DIRECTORY, outputPath, filename)
  144. console.log(`pushing s3://${AWS_BUCKET}/${key}`)
  145. await s3.putObject({
  146. Bucket: AWS_BUCKET,
  147. Key: key,
  148. ContentType: mime.lookup(filename),
  149. Body: buffer
  150. }).promise()
  151. }
  152. }
  153. main(...process.argv.slice(2)).catch((err) => {
  154. console.error(err.stack)
  155. process.exit(1)
  156. })