Преглед изворни кода

BugFix: Promisify CdnResourcesDownloader.downloadAndWriteToFS

Yuki Takei пре 7 година
родитељ
комит
59e7410ad4
3 измењених фајлова са 48 додато и 5 уклоњено
  1. 1 0
      package.json
  2. 17 5
      src/lib/service/cdn-resources-downloader.js
  3. 30 0
      yarn.lock

+ 1 - 0
package.json

@@ -201,6 +201,7 @@
     "sinon": "^7.0.0",
     "sinon-chai": "^3.2.0",
     "socket.io-client": "^2.0.3",
+    "stream-to-promise": "^2.2.0",
     "style-loader": "^0.23.0",
     "throttle-debounce": "^2.0.0",
     "toastr": "^2.1.2",

+ 17 - 5
src/lib/service/cdn-resources-downloader.js

@@ -4,6 +4,7 @@ const { URL } = require('url');
 const urljoin = require('url-join');
 const fs = require('graceful-fs');
 const replaceStream = require('replacestream');
+const streamToPromise = require('stream-to-promise');
 
 
 /**
@@ -28,10 +29,14 @@ class CdnResourcesDownloader {
    * @param {any} options
    */
   async downloadScripts(cdnResources, options) {
+    this.logger.debug('Downloading scripts', cdnResources);
+
     const opts = Object.assign({}, options);
     const ext = opts.ext || 'js';
 
     const promises = cdnResources.map(cdnResource => {
+      this.logger.info(`Processing CdnResource '${cdnResource.name}'`);
+
       return this.downloadAndWriteToFS(
         cdnResource.url,
         path.join(cdnResource.outDir, `${cdnResource.name}.${ext}`));
@@ -47,12 +52,16 @@ class CdnResourcesDownloader {
    * @param {any} options
    */
   async downloadStyles(cdnResources, options) {
+    this.logger.debug('Downloading styles', cdnResources);
+
     const opts = Object.assign({}, options);
     const ext = opts.ext || 'css';
 
     // styles
     const assetsResourcesStore = [];
     const promisesForStyle = cdnResources.map(cdnResource => {
+      this.logger.info(`Processing CdnResource '${cdnResource.name}'`);
+
       let urlReplacer = null;
 
       // generate replaceStream instance
@@ -66,14 +75,15 @@ class CdnResourcesDownloader {
         urlReplacer);
     });
 
-
     // wait until all styles are downloaded
     await Promise.all(promisesForStyle);
 
-    this.logger.info(assetsResourcesStore);
+    this.logger.debug('Downloading assets', assetsResourcesStore);
 
     // assets in css
     const promisesForAssets = assetsResourcesStore.map(cdnResource => {
+      this.logger.info(`Processing assts in css '${cdnResource.name}'`);
+
       // create dir if dir does not exist
       if (!fs.existsSync(cdnResource.outDir)) {
         fs.mkdirSync(cdnResource.outDir);
@@ -108,7 +118,7 @@ class CdnResourcesDownloader {
           : new URL(url, cdnResource.url);  // when url is relative
         const basename = path.basename(parsedUrl.pathname);
 
-        this.logger.info(cdnResource.name, parsedUrl.toString());
+        this.logger.debug(`${cdnResource.name} has ${parsedUrl.toString()}`);
 
         // add assets metadata to download later
         assetsResourcesStore.push(
@@ -130,9 +140,11 @@ class CdnResourcesDownloader {
     // replace and write
     let stream = response.data;
     if (replacestream != null) {
-      stream = response.data.pipe(replacestream);
+      stream = stream.pipe(replacestream);
     }
-    return stream.pipe(fs.createWriteStream(file));
+    stream = stream.pipe(fs.createWriteStream(file));
+
+    return streamToPromise(stream);
   }
 
 }

+ 30 - 0
yarn.lock

@@ -409,6 +409,10 @@ ansistyles@~0.1.1:
   version "0.1.3"
   resolved "https://registry.yarnpkg.com/ansistyles/-/ansistyles-0.1.3.tgz#5de60415bda071bb37127854c864f41b23254539"
 
+any-promise@^1.1.0, any-promise@~1.3.0:
+  version "1.3.0"
+  resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"
+
 anymatch@^1.3.0:
   version "1.3.2"
   resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-1.3.2.tgz#553dcb8f91e3c889845dfdba34c77721b90b9d7a"
@@ -2944,6 +2948,12 @@ end-of-stream@^1.0.0, end-of-stream@^1.1.0:
   dependencies:
     once "^1.4.0"
 
+end-of-stream@~1.1.0:
+  version "1.1.0"
+  resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.1.0.tgz#e9353258baa9108965efc41cb0ef8ade2f3cfb07"
+  dependencies:
+    once "~1.3.0"
+
 engine.io-client@~3.1.0:
   version "3.1.4"
   resolved "https://registry.yarnpkg.com/engine.io-client/-/engine.io-client-3.1.4.tgz#4fcf1370b47163bd2ce9be2733972430350d4ea1"
@@ -6453,6 +6463,12 @@ once@^1.3.0, once@^1.3.1, once@^1.3.3, once@^1.4.0:
   dependencies:
     wrappy "1"
 
+once@~1.3.0:
+  version "1.3.3"
+  resolved "https://registry.yarnpkg.com/once/-/once-1.3.3.tgz#b2e261557ce4c314ec8304f3fa82663e4297ca20"
+  dependencies:
+    wrappy "1"
+
 onetime@^2.0.0:
   version "2.0.1"
   resolved "https://registry.yarnpkg.com/onetime/-/onetime-2.0.1.tgz#067428230fd67443b2794b22bba528b6867962d4"
@@ -8664,6 +8680,20 @@ stream-throttle@^0.1.3:
     commander "^2.2.0"
     limiter "^1.0.5"
 
+stream-to-array@~2.3.0:
+  version "2.3.0"
+  resolved "https://registry.yarnpkg.com/stream-to-array/-/stream-to-array-2.3.0.tgz#bbf6b39f5f43ec30bc71babcb37557acecf34353"
+  dependencies:
+    any-promise "^1.1.0"
+
+stream-to-promise@^2.2.0:
+  version "2.2.0"
+  resolved "https://registry.yarnpkg.com/stream-to-promise/-/stream-to-promise-2.2.0.tgz#b1edb2e1c8cb11289d1b503c08d3f2aef51e650f"
+  dependencies:
+    any-promise "~1.3.0"
+    end-of-stream "~1.1.0"
+    stream-to-array "~2.3.0"
+
 streamsearch@0.1.2:
   version "0.1.2"
   resolved "https://registry.yarnpkg.com/streamsearch/-/streamsearch-0.1.2.tgz#808b9d0e56fc273d809ba57338e929919a1a9f1a"