upload-to-cdn.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // Authors:
  16. //
  17. // - Kevin van Zonneveld <kevin@transloadit.com>
  18. const path = require('path')
  19. const AWS = require('aws-sdk')
  20. const packlist = require('npm-packlist')
  21. const tar = require('tar')
  22. const pacote = require('pacote')
  23. const concat = require('concat-stream')
  24. const { promisify } = require('util')
  25. const readFile = promisify(require('fs').readFile)
  26. const finished = promisify(require('stream').finished)
  27. function delay (ms) {
  28. return new Promise(resolve => setTimeout(resolve, ms))
  29. }
  30. const AWS_REGION = 'us-east-1'
  31. const AWS_BUCKET = 'crates.edgly.net'
  32. const AWS_DIRECTORY = '756b8efaed084669b02cb99d4540d81f/default'
  33. /**
  34. * Get remote dist/ files by fetching the tarball for the given version
  35. * from npm and filtering it down to package/dist/ files.
  36. *
  37. * @param {string} Package name, eg. @uppy/robodog
  38. * @param {string} Package version, eg. "1.2.0"
  39. * @returns a Map<string, Buffer>, filename → content
  40. */
  41. async function getRemoteDistFiles (packageName, version) {
  42. const files = new Map()
  43. const tarball = pacote.tarball.stream(`${packageName}@${version}`)
  44. .pipe(new tar.Parse())
  45. tarball.on('entry', (readEntry) => {
  46. if (readEntry.path.startsWith('package/dist/')) {
  47. readEntry
  48. .pipe(concat((buf) => {
  49. files.set(readEntry.path.replace(/^package\/dist\//, ''), buf)
  50. }))
  51. .on('error', (err) => {
  52. tarball.emit('error', err)
  53. })
  54. } else {
  55. readEntry.resume()
  56. }
  57. })
  58. await finished(tarball)
  59. return files
  60. }
  61. /**
  62. * Get local dist/ files by asking npm-packlist what files would be added
  63. * to an npm package during publish, and filtering those down to just dist/ files.
  64. *
  65. * @param {string} Base file path of the package, eg. ./packages/@uppy/locales
  66. * @returns a Map<string, Buffer>, filename → content
  67. */
  68. async function getLocalDistFiles (packagePath) {
  69. const files = (await packlist({ path: packagePath }))
  70. .filter(f => f.startsWith('dist/'))
  71. .map(f => f.replace(/^dist\//, ''))
  72. const entries = await Promise.all(
  73. files.map(async (f) => [
  74. f,
  75. await readFile(path.join(packagePath, 'dist', f))
  76. ])
  77. )
  78. return new Map(entries)
  79. }
  80. async function main (packageName, version) {
  81. if (!packageName) {
  82. console.error('usage: upload-to-cdn <packagename> [version]')
  83. console.error('Must provide a package name')
  84. process.exit(1)
  85. }
  86. if (!process.env.EDGLY_KEY || !process.env.EDGLY_SECRET) {
  87. console.error('Missing EDGLY_KEY or EDGLY_SECRET env variables, bailing')
  88. process.exit(1)
  89. }
  90. const s3 = new AWS.S3({
  91. credentials: new AWS.Credentials({
  92. accessKeyId: process.env.EDGLY_KEY,
  93. secretAccessKey: process.env.EDGLY_SECRET
  94. }),
  95. region: AWS_REGION
  96. })
  97. const remote = !!version
  98. if (!remote) {
  99. version = require(`../packages/${packageName}/package.json`).version
  100. }
  101. // Warn if uploading a local build not from CI:
  102. // - If we're on CI, this should be a release commit.
  103. // - If we're local, normally we should upload a released version, not a local build.
  104. if (!remote && !process.env.CI) {
  105. console.log('Warning, writing a local build to the CDN, this is usually not what you want. Sleeping 3s. Press CTRL+C!')
  106. await delay(3000)
  107. }
  108. const packagePath = remote
  109. ? `${packageName}@${version}`
  110. : path.join(__dirname, '..', 'packages', packageName)
  111. const files = remote
  112. ? await getRemoteDistFiles(packageName, version)
  113. : await getLocalDistFiles(packagePath)
  114. // uppy → releases/uppy/
  115. // @uppy/robodog → releases/uppy/robodog/
  116. // @uppy/locales → releases/uppy/locales/
  117. const dirName = packageName.startsWith('@uppy/')
  118. ? packageName.replace(/^@/, '')
  119. : 'uppy'
  120. const outputPath = path.posix.join('releases', dirName, `v${version}`)
  121. for (const [filename, buffer] of files.entries()) {
  122. const key = path.posix.join(AWS_DIRECTORY, outputPath, filename)
  123. console.log(`pushing s3://${AWS_BUCKET}/${key}`)
  124. await s3.putObject({
  125. Bucket: AWS_BUCKET,
  126. Key: key,
  127. Body: buffer
  128. }).promise()
  129. }
  130. }
  131. main(...process.argv.slice(2)).catch((err) => {
  132. console.error(err.stack)
  133. process.exit(1)
  134. })