Repository: motdotla/node-lambda
Pull request #331 (2017-06-22 03:55:23)

Refactoring _npmInstall


Patch:
diff --git a/lib/main.js b/lib/main.js
index daf5b9c..9e3124d 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -293,7 +293,7 @@ Lambda.prototype._fileCopy = (program, src, dest, excludeNodeModules, callback)
   })
 }
 
-Lambda.prototype._npmInstall = (program, codeDirectory, callback) => {
+Lambda.prototype._npmInstall = (program, codeDirectory) => {
   const dockerBaseOptions = [
     'run', '--rm', '-v', `${codeDirectory}:/var/task`,
     program.dockerImage,
@@ -338,15 +338,14 @@ Lambda.prototype._npmInstall = (program, codeDirectory, callback) => {
     }
   })()
 
-  execFile(params.command, params.options, {
-    maxBuffer: maxBufferSize,
-    env: process.env
-  }, (err) => {
-    if (err) {
-      return callback(err)
-    }
-
-    return callback(null, true)
+  return new Promise((resolve, reject) => {
+    execFile(params.command, params.options, {
+      maxBuffer: maxBufferSize,
+      env: process.env
+    }, (err) => {
+      if (err) return reject(err)
+      resolve()
+    })
   })
 }
 
@@ -532,11 +531,7 @@ Lambda.prototype._buildAndArchive = function (program, archiveCallback) {
         return archiveCallback(err)
       }
       console.log('=> Running npm install --production')
-      _this._npmInstall(program, codeDirectory, (err) => {
-        if (err) {
-          return archiveCallback(err)
-        }
-
+      _this._npmInstall(program, codeDirectory).then(() => {
         _this._postInstallScript(program, codeDirectory, (err) => {
           if (err) {
             return archiveCallback(err)
@@ -545,6 +540,8 @@ Lambda.prototype._buildAndArchive = function (program, archiveCallback) {
           console.log('=> Zipping deployment package')
           _this._zip(program, codeDirectory, archiveCallback)
         })
+      }).catch((err) => {
+        return archiveCallback(err)
       })
     })
   }).catch((err) => {


Test patch:
diff --git a/test/main.js b/test/main.js
index 7574dd5..cc35875 100644
--- a/test/main.js
+++ b/test/main.js
@@ -392,14 +392,12 @@ describe('lib/main', function () {
       })
     })
 
-    it('_npm adds node_modules', function (done) {
+    it('_npm adds node_modules', function () {
       _timeout({ this: this, sec: 30 }) // give it time to build the node modules
 
-      lambda._npmInstall(program, codeDirectory, (err, result) => {
-        assert.isNull(err)
+      return lambda._npmInstall(program, codeDirectory).then(() => {
         const contents = fs.readdirSync(codeDirectory)
         assert.include(contents, 'node_modules')
-        done()
       })
     })
   })
@@ -428,14 +426,12 @@ describe('lib/main', function () {
       codeDirectory = lambda._codeDirectory()
     })
 
-    it('_npm adds node_modules', function (done) {
+    it('_npm adds node_modules', function () {
       _timeout({ this: this, sec: 30 }) // give it time to build the node modules
 
-      lambda._npmInstall(program, codeDirectory, (err, result) => {
-        assert.isNull(err)
+      return lambda._npmInstall(program, codeDirectory).then(() => {
         const contents = fs.readdirSync(codeDirectory)
         assert.include(contents, 'node_modules')
-        done()
       })
     })
   })
@@ -507,19 +503,18 @@ describe('lib/main', function () {
     })
   })
 
-  describe('_zip', function () {
+  describe('_zip', () => {
     beforeEach(function (done) {
       _timeout({ this: this, sec: 30 }) // give it time to build the node modules
       lambda._cleanDirectory(codeDirectory).then(() => {
-        lambda._fileCopy(program, '.', codeDirectory, true, function (err) {
+        lambda._fileCopy(program, '.', codeDirectory, true, (err) => {
           if (err) {
             return done(err)
           }
-          lambda._npmInstall(program, codeDirectory, function (err) {
-            if (err) {
-              return done(err)
-            }
+          lambda._npmInstall(program, codeDirectory).then(() => {
             done()
+          }).catch((err) => {
+            done(err)
           })
         })
       }).catch((err) => {