Repository: opentable/oc
Pull request #773 (2017-11-29 12:42:30)

[DX-283] Dependencies handler on packaging
@matteofigus is this what you were envision to fix the programmatic package via cli?


Patch:
diff --git a/src/cli/facade/package.js b/src/cli/facade/package.js
index 92b79e30..5b10fcbf 100644
--- a/src/cli/facade/package.js
+++ b/src/cli/facade/package.js
@@ -4,6 +4,7 @@ const strings = require('../../resources/index');
 const wrapCliCallback = require('../wrap-cli-callback');
 const format = require('stringformat');
 const path = require('path');
+const handleDependencies = require('../domain/handle-dependencies');
 
 module.exports = function(dependencies) {
   const local = dependencies.local,
@@ -17,36 +18,47 @@ module.exports = function(dependencies) {
     callback = wrapCliCallback(callback);
 
     logger.warn(format(strings.messages.cli.PACKAGING, packageDir));
-    const packageOptions = {
-      production: true,
-      componentPath: path.resolve(componentPath)
-    };
-    local.package(packageOptions, (err, component) => {
-      if (err) {
-        logger.err(format(strings.errors.cli.PACKAGE_CREATION_FAIL, err));
-        return callback(err);
-      }
-
-      logger.ok(format(strings.messages.cli.PACKAGED, packageDir));
-
-      if (opts.compress) {
-        logger.warn(
-          format(strings.messages.cli.COMPRESSING, compressedPackagePath)
-        );
-
-        local.compress(packageDir, compressedPackagePath, err => {
+    handleDependencies(
+      { components: [path.resolve(componentPath)], logger },
+      (err, dependencies) => {
+        if (err) {
+          logger.err(err);
+          return callback(err);
+        }
+        const packageOptions = {
+          production: true,
+          componentPath: path.resolve(componentPath)
+        };
+        local.package(packageOptions, (err, component) => {
           if (err) {
             logger.err(format(strings.errors.cli.PACKAGE_CREATION_FAIL, err));
             return callback(err);
           }
-          logger.ok(
-            format(strings.messages.cli.COMPRESSED, compressedPackagePath)
-          );
-          callback(null, component);
+
+          logger.ok(format(strings.messages.cli.PACKAGED, packageDir));
+
+          if (opts.compress) {
+            logger.warn(
+              format(strings.messages.cli.COMPRESSING, compressedPackagePath)
+            );
+
+            local.compress(packageDir, compressedPackagePath, err => {
+              if (err) {
+                logger.err(
+                  format(strings.errors.cli.PACKAGE_CREATION_FAIL, err)
+                );
+                return callback(err);
+              }
+              logger.ok(
+                format(strings.messages.cli.COMPRESSED, compressedPackagePath)
+              );
+              callback(null, component);
+            });
+          } else {
+            callback(null, component);
+          }
         });
-      } else {
-        callback(null, component);
       }
-    });
+    );
   };
 };


Test patch:
diff --git a/test/unit/cli-facade-package.js b/test/unit/cli-facade-package.js
index 9017519e..c65fb00a 100644
--- a/test/unit/cli-facade-package.js
+++ b/test/unit/cli-facade-package.js
@@ -101,15 +101,22 @@ describe('cli : facade : package', () => {
 
             execute(false, () => {
               local.compress.restore();
-              const warnMessage = logSpy.warn.args[0][0],
-                okMessage = logSpy.ok.args[0][0],
-                re = new RegExp('\\' + path.sep, 'g'),
-                warnMessageWithSlashesOnPath = warnMessage.replace(re, '/'),
-                okMessageWithSlashesOnPath = okMessage.replace(re, '/');
-
+              const warnMessage = logSpy.warn.args[0][0];
+              const warnDependenciesMessage = logSpy.warn.args[1][0];
+              const okMessage = logSpy.ok.args[0][0];
+              const re = new RegExp('\\' + path.sep, 'g');
+              const warnMessageWithSlashesOnPath = warnMessage.replace(re, '/');
+              const warnDependenciesMessageWithSlashesOnPath = warnDependenciesMessage.replace(
+                re,
+                '/'
+              );
+              const okMessageWithSlashesOnPath = okMessage.replace(re, '/');
+              expect(warnDependenciesMessageWithSlashesOnPath).to.include(
+                'Ensuring dependencies are loaded...'
+              );
               expect(warnMessageWithSlashesOnPath).to.include('Packaging -> ');
               expect(okMessageWithSlashesOnPath).to.include('Packaged -> ');
-              expect(logSpy.warn.args[1]).to.be.undefined;
+              expect(logSpy.warn.args[2]).to.be.undefined;
               done();
             });
           });
@@ -125,12 +132,23 @@ describe('cli : facade : package', () => {
 
             it('should show a message for success', done => {
               execute(true, () => {
-                const warnMessage = logSpy.warn.args[1][0],
-                  okMessage = logSpy.ok.args[1][0],
-                  re = new RegExp('\\' + path.sep, 'g'),
-                  warnMessageWithSlashesOnPath = warnMessage.replace(re, '/'),
-                  okMessageWithSlashesOnPath = okMessage.replace(re, '/');
+                const warnDependenciesMessage = logSpy.warn.args[1][0];
+                const warnMessage = logSpy.warn.args[2][0];
+                const okMessage = logSpy.ok.args[1][0];
+                const re = new RegExp('\\' + path.sep, 'g');
+                const warnMessageWithSlashesOnPath = warnMessage.replace(
+                  re,
+                  '/'
+                );
+                const warnDependenciesMessageWithSlashesOnPath = warnDependenciesMessage.replace(
+                  re,
+                  '/'
+                );
+                const okMessageWithSlashesOnPath = okMessage.replace(re, '/');
 
+                expect(warnDependenciesMessageWithSlashesOnPath).to.include(
+                  'Ensuring dependencies are loaded...'
+                );
                 expect(warnMessageWithSlashesOnPath).to.include(
                   'Compressing -> '
                 );
@@ -157,11 +175,22 @@ describe('cli : facade : package', () => {
 
             it('should show a message for failure', done => {
               execute(true, () => {
-                const warnMessage = logSpy.warn.args[1][0],
-                  errorMessage = logSpy.err.args[0][0],
-                  re = new RegExp('\\' + path.sep, 'g'),
-                  warnMessageWithSlashesOnPath = warnMessage.replace(re, '/');
+                const warnDependenciesMessage = logSpy.warn.args[1][0];
+                const warnMessage = logSpy.warn.args[2][0];
+                const errorMessage = logSpy.err.args[0][0];
+                const re = new RegExp('\\' + path.sep, 'g');
+                const warnMessageWithSlashesOnPath = warnMessage.replace(
+                  re,
+                  '/'
+                );
+                const warnDependenciesMessageWithSlashesOnPath = warnDependenciesMessage.replace(
+                  re,
+                  '/'
+                );
 
+                expect(warnDependenciesMessageWithSlashesOnPath).to.include(
+                  'Ensuring dependencies are loaded...'
+                );
                 expect(warnMessageWithSlashesOnPath).to.include(
                   'Compressing -> '
                 );