Browse Source

do the upload-to-cdn.js thing

Renée Kooi 6 years ago
parent
commit
1abebd7506
3 changed files with 103 additions and 41 deletions
  1. 73 12
      bin/upload-to-cdn.js
  2. 30 28
      package-lock.json
  3. 0 1
      package.json

+ 73 - 12
bin/upload-to-cdn.js

@@ -27,14 +27,51 @@ const AWS = require('aws-sdk')
 const packlist = require('npm-packlist')
 const tar = require('tar')
 const pacote = require('pacote')
-const tempy = require('tempy')
+const concat = require('concat-stream')
+const { promisify } = require('util')
+const readFile = promisify(require('fs').readFile)
+const finished = promisify(require('stream').finished)
 
-async function loadRemoteDist (packageName, version) {
+const AWS_REGION = 'us-east-1'
+const AWS_BUCKET = 'crates.edgly.net'
+const AWS_DIRECTORY = '756b8efaed084669b02cb99d4540d81f/default'
+
+async function getRemoteDistFiles (packageName, version) {
+  const files = new Map()
   const tarball = pacote.tarball.stream(`${packageName}@${version}`)
-    .pipe(tar.Parse())
+    .pipe(new tar.Parse())
+
   tarball.on('entry', (readEntry) => {
+    if (readEntry.path.startsWith('package/dist/')) {
+      readEntry
+        .pipe(concat((buf) => {
+          files.set(readEntry.path.replace(/^package\/dist\//, ''), buf)
+        }))
+        .on('error', (err) => {
+          tarball.emit('error', err)
+        })
+    } else {
+      readEntry.resume()
+    }
   })
+
   await finished(tarball)
+  return files
+}
+
+async function getLocalDistFiles (packagePath) {
+  const files = (await packlist({ path: packagePath }))
+    .filter(f => f.startsWith('dist/'))
+    .map(f => f.replace(/^dist\//, ''))
+
+  const entries = await Promise.all(
+    files.map(async (f) => [
+      f,
+      await readFile(path.join(packagePath, 'dist', f))
+    ])
+  )
+
+  return new Map(entries)
 }
 
 async function main (packageName, version) {
@@ -44,6 +81,19 @@ async function main (packageName, version) {
     process.exit(1)
   }
 
+  if (!process.env.EDGLY_KEY || !process.env.EDGLY_SECRET) {
+    console.error('Missing EDGLY_KEY or EDGLY_SECRET env variables, bailing')
+    process.exit(1)
+  }
+
+  const s3 = new AWS.S3({
+    credentials: new AWS.Credentials({
+      accessKeyId: process.env.EDGLY_KEY,
+      secretAccessKey: process.env.EDGLY_SECRET
+    }),
+    region: AWS_REGION
+  })
+
   const remote = !!version
   if (!remote) {
     version = require(`../packages/${packageName}/package.json`).version
@@ -52,17 +102,28 @@ async function main (packageName, version) {
   const packagePath = remote
     ? `${packageName}@${version}`
     : path.join(__dirname, '..', 'packages', packageName)
-  const temporaryPath = tempy.directory()
 
-  const proc = spawn('npm', ['pack', packagePath], {
-    cwd: temporaryPath,
-    stdio: 'inherit'
-  })
+  const files = remote
+    ? await getRemoteDistFiles(packageName, version)
+    : await getLocalDistFiles(packagePath)
 
-  const code = await once(proc, 'exit')
-  if (code !== 0) {
-    console.error('packing failed')
-    process.exit(1)
+  // uppy → releases/uppy/
+  // @uppy/robodog → releases/uppy/robodog/
+  // @uppy/locales → releases/uppy/locales/
+  const dirName = packageName.startsWith('@uppy/')
+    ? packageName.replace(/^@/, '')
+    : 'uppy'
+
+  const outputPath = path.posix.join('releases', dirName, `v${version}`)
+
+  for (const [filename, buffer] of files.entries()) {
+    const key = path.posix.join(AWS_DIRECTORY, outputPath, filename)
+    console.log(`pushing s3://${AWS_BUCKET}/${key}`)
+    await s3.putObject({
+      Bucket: AWS_BUCKET,
+      Key: key,
+      Body: buffer
+    }).promise()
   }
 }
 

+ 30 - 28
package-lock.json

@@ -9017,7 +9017,8 @@
         "ansi-regex": {
           "version": "2.1.1",
           "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz",
-          "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8="
+          "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=",
+          "optional": true
         },
         "aproba": {
           "version": "1.2.0",
@@ -9038,12 +9039,14 @@
         "balanced-match": {
           "version": "1.0.0",
           "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
-          "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
+          "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
+          "optional": true
         },
         "brace-expansion": {
           "version": "1.1.11",
           "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
           "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
+          "optional": true,
           "requires": {
             "balanced-match": "^1.0.0",
             "concat-map": "0.0.1"
@@ -9058,17 +9061,20 @@
         "code-point-at": {
           "version": "1.1.0",
           "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz",
-          "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c="
+          "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=",
+          "optional": true
         },
         "concat-map": {
           "version": "0.0.1",
           "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
-          "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
+          "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
+          "optional": true
         },
         "console-control-strings": {
           "version": "1.1.0",
           "resolved": "https://registry.npmjs.org/console-control-strings/-/console-control-strings-1.1.0.tgz",
-          "integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4="
+          "integrity": "sha1-PXz0Rk22RG6mRL9LOVB/mFEAjo4=",
+          "optional": true
         },
         "core-util-is": {
           "version": "1.0.2",
@@ -9185,7 +9191,8 @@
         "inherits": {
           "version": "2.0.3",
           "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
-          "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
+          "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=",
+          "optional": true
         },
         "ini": {
           "version": "1.3.5",
@@ -9197,6 +9204,7 @@
           "version": "1.0.0",
           "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz",
           "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=",
+          "optional": true,
           "requires": {
             "number-is-nan": "^1.0.0"
           }
@@ -9211,6 +9219,7 @@
           "version": "3.0.4",
           "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
           "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
+          "optional": true,
           "requires": {
             "brace-expansion": "^1.1.7"
           }
@@ -9218,12 +9227,14 @@
         "minimist": {
           "version": "0.0.8",
           "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
-          "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
+          "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
+          "optional": true
         },
         "minipass": {
           "version": "2.3.5",
           "resolved": "https://registry.npmjs.org/minipass/-/minipass-2.3.5.tgz",
           "integrity": "sha512-Gi1W4k059gyRbyVUZQ4mEqLm0YIUiGYfvxhF6SIlk3ui1WVxMTGfGdQ2SInh3PDrRTVvPKgULkpJtT4RH10+VA==",
+          "optional": true,
           "requires": {
             "safe-buffer": "^5.1.2",
             "yallist": "^3.0.0"
@@ -9242,6 +9253,7 @@
           "version": "0.5.1",
           "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",
           "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
+          "optional": true,
           "requires": {
             "minimist": "0.0.8"
           }
@@ -9322,7 +9334,8 @@
         "number-is-nan": {
           "version": "1.0.1",
           "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz",
-          "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0="
+          "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=",
+          "optional": true
         },
         "object-assign": {
           "version": "4.1.1",
@@ -9334,6 +9347,7 @@
           "version": "1.4.0",
           "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
           "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
+          "optional": true,
           "requires": {
             "wrappy": "1"
           }
@@ -9419,7 +9433,8 @@
         "safe-buffer": {
           "version": "5.1.2",
           "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
-          "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
+          "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
+          "optional": true
         },
         "safer-buffer": {
           "version": "2.1.2",
@@ -9455,6 +9470,7 @@
           "version": "1.0.2",
           "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz",
           "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=",
+          "optional": true,
           "requires": {
             "code-point-at": "^1.0.0",
             "is-fullwidth-code-point": "^1.0.0",
@@ -9474,6 +9490,7 @@
           "version": "3.0.1",
           "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",
           "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=",
+          "optional": true,
           "requires": {
             "ansi-regex": "^2.0.0"
           }
@@ -9517,12 +9534,14 @@
         "wrappy": {
           "version": "1.0.2",
           "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
-          "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
+          "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
+          "optional": true
         },
         "yallist": {
           "version": "3.0.3",
           "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz",
-          "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A=="
+          "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==",
+          "optional": true
         }
       }
     },
@@ -21470,17 +21489,6 @@
         }
       }
     },
-    "tempy": {
-      "version": "0.3.0",
-      "resolved": "https://registry.npmjs.org/tempy/-/tempy-0.3.0.tgz",
-      "integrity": "sha512-WrH/pui8YCwmeiAoxV+lpRH9HpRtgBhSR2ViBPgpGb/wnYDzp21R4MN45fsCGvLROvY67o3byhJRYRONJyImVQ==",
-      "dev": true,
-      "requires": {
-        "temp-dir": "^1.0.0",
-        "type-fest": "^0.3.1",
-        "unique-string": "^1.0.0"
-      }
-    },
     "term-size": {
       "version": "1.2.0",
       "resolved": "https://registry.npmjs.org/term-size/-/term-size-1.2.0.tgz",
@@ -21902,12 +21910,6 @@
       "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==",
       "dev": true
     },
-    "type-fest": {
-      "version": "0.3.1",
-      "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.3.1.tgz",
-      "integrity": "sha512-cUGJnCdr4STbePCgqNFbpVNCepa+kAVohJs1sLhxzdH+gnEoOd8VhbYa7pD3zZYGiURWM2xzEII3fQcRizDkYQ==",
-      "dev": true
-    },
     "type-is": {
       "version": "1.6.16",
       "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.16.tgz",

+ 0 - 1
package.json

@@ -74,7 +74,6 @@
     "stringify-object": "^3.3.0",
     "tar": "^4.4.8",
     "temp-write": "^3.4.0",
-    "tempy": "^0.3.0",
     "tinyify": "^2.5.0",
     "touch": "^3.1.0",
     "tsify": "^4.0.1",