Просмотр исходного кода

organize NO_CDN related models and services

Yuki Takei 7 лет назад
Родитель
Сommit
5d75cb8d37

+ 5 - 3
bin/download-cdn-resources.js

@@ -15,13 +15,15 @@ if (!noCdn) {
   process.exit(0);
 }
 
+logger.info('This is NO_CDN mode. Start to download resources.');
+
+const CdnResourcesDownloader = require('@commons/service/cdn-resources-downloader');
 const CdnResourcesService = require('@commons/service/cdn-resources-service');
 
+const downloader = new CdnResourcesDownloader();
 const service = new CdnResourcesService();
 
-logger.info('This is NO_CDN mode. Start to download resources.');
-
-service.downloadAndWriteAll()
+service.downloadAndWriteAll(downloader)
   .then(() => {
     logger.info('Download is terminated successfully');
   })

+ 12 - 0
src/lib/models/cdn-resource.js

@@ -0,0 +1,12 @@
+/**
+ * Value Object
+ */
+class CdnResource {
+  constructor(name, url, outDir) {
+    this.name = name;
+    this.url = url;
+    this.outDir = outDir;
+  }
+}
+
+module.exports = CdnResource;

+ 1 - 11
src/lib/service/cdn-resources-downloader.js

@@ -7,17 +7,8 @@ const mkdirp = require('mkdirp');
 const replaceStream = require('replacestream');
 const streamToPromise = require('stream-to-promise');
 
+const CdnResource = require('../models/cdn-resource');
 
-/**
- * Value Object
- */
-class CdnResource {
-  constructor(name, url, outDir) {
-    this.name = name;
-    this.url = url;
-    this.outDir = outDir;
-  }
-}
 
 class CdnResourcesDownloader {
   constructor() {
@@ -152,5 +143,4 @@ class CdnResourcesDownloader {
 
 }
 
-CdnResourcesDownloader.CdnResource = CdnResource;
 module.exports = CdnResourcesDownloader;

+ 11 - 7
src/lib/service/cdn-resources-service.js

@@ -3,9 +3,6 @@ const urljoin = require('url-join');
 
 const helpers = require('@commons/util/helpers');
 
-const CdnResourcesDownloader = require('./cdn-resources-downloader');
-const CdnResource = CdnResourcesDownloader.CdnResource;
-
 const cdnLocalScriptRoot = 'public/js/cdn';
 const cdnLocalScriptWebRoot = '/js/cdn';
 const cdnLocalStyleRoot = 'public/styles/cdn';
@@ -39,8 +36,15 @@ class CdnResourcesService {
     return (manifests.length > 0) ? manifests[0] : null;
   }
 
-  async downloadAndWriteAll() {
-    const downloader = new CdnResourcesDownloader();
+  /**
+   * download all resources from CDN and write to FS
+   *
+   * !! This method should be invoked only by /bin/download-cdn-resources when build client !!
+   *
+   * @param {CdnResourceDownloader} cdnResourceDownloader
+   */
+  async downloadAndWriteAll(cdnResourceDownloader) {
+    const CdnResource = require('@commons/models/cdn-resource');
 
     const cdnScriptResources = this.cdnManifests.js.map(manifest => {
       const outDir = helpers.root(cdnLocalScriptRoot);
@@ -58,8 +62,8 @@ class CdnResourcesService {
     };
 
     return Promise.all([
-      downloader.downloadScripts(cdnScriptResources),
-      downloader.downloadStyles(cdnStyleResources, dlStylesOptions),
+      cdnResourceDownloader.downloadScripts(cdnScriptResources),
+      cdnResourceDownloader.downloadStyles(cdnStyleResources, dlStylesOptions),
     ]);
   }