|
|
@@ -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);
|
|
|
}
|
|
|
|
|
|
}
|