Repository: bustlelabs/shep (js)

Issue:
Return user-friendly error when using Node version other than 4.3
current error is:

```
/Users/zfoster/.nvm/v0.10.36/lib/node_modules/shep/lib/commands/build.js:26
const command = exports.command = 'build [env] [functions..]';
^^^^^
SyntaxError: Use of const in strict mode.
```

Best solution likely involves validating against `lambda.json`

Pull request:
Warn user when using run with different node version than AWS Lambda
Closes #62 

Patch:
diff --git a/src/commands/run.js b/src/commands/run.js
index 84f4139..ba94f6a 100644
--- a/src/commands/run.js
+++ b/src/commands/run.js
@@ -11,8 +11,11 @@ export function builder (yargs){
   .describe('environment', 'Environment variables to use')
   .describe('event', 'Event to use')
   .default('environment', 'development')
+  .describe('build', 'Build functions before running. Use --no-build to skip this step')
+  .default('build', true)
   .example('shep run', 'Launch an interactive CLI')
   .example('shep run foo', 'Runs the `foo` function for all events')
+  .example('shep run foo --no-build', 'Run the already built `foo` function in the dist folder')
   .example('shep run foo --event default', 'Runs the `foo` function for just the `default` event')
   .example('shep run foo --environment production', 'Runs the `foo` function with production environment')
 }
diff --git a/src/run/index.js b/src/run/index.js
index 7a86bb7..c82cabc 100644
--- a/src/run/index.js
+++ b/src/run/index.js
@@ -11,9 +11,17 @@ const ui = cliui({ width: 80 })
 
 const results = { success: 'SUCCESS' , error: 'ERROR', exception: 'EXCEPTION' }
 
+const awsNodeVersion = '4.3.2'
+const processVersion = process.versions.node
+
 export default function(opts){
   AWS.config.update({region: opts.region})
 
+  if (processVersion !== awsNodeVersion ){
+    console.log(`Warning: Lambda currently runs node v${awsNodeVersion} but you are using v${processVersion}`)
+  }
+
+  const performBuild = opts.build
   const name = opts.name
   const env = opts.environemnt || 'development'
   const lambdaConfig = loadLambdaConfig(name)
@@ -22,7 +30,8 @@ export default function(opts){
 
   const context = {}
 
-  return build({ functions: name, env: env })
+  return Promise.resolve()
+  .then(() => { if (performBuild === true) return build({ functions: name, env: env }) })
   .then(() => requireProject(`dist/${name}/${fileName}`)[handler] )
   .then((func) => {
     return Promise.map(events, (eventFilename) => {
@@ -51,33 +60,37 @@ export default function(opts){
       })
     })
   })
-  .tap((outputs) => {
-    if(outputs.length === 1){
-      console.log(outputs[0].response)
-    }
-  })
-  .map((output) => {
-      ui.div(
-        {
-          text: output.name,
-          width: 20,
-        },
-        {
-          text: formatResult(output),
-          width: 15
-        },
-        {
-          text: formatDate(output),
-          width: 10
-        },
-        {
-          text: formatResponse(output)
-        }
-      )
-  })
+  .tap(logOutput)
+  .map(formatOutput)
   .then(() => console.log(ui.toString()))
 }
 
+function logOutput(outputs){
+  if(outputs.length === 1){
+    console.log(outputs[0].response)
+  }
+}
+
+function formatOutput(output){
+    ui.div(
+      {
+        text: output.name,
+        width: 20,
+      },
+      {
+        text: formatResult(output),
+        width: 15
+      },
+      {
+        text: formatDate(output),
+        width: 10
+      },
+      {
+        text: formatResponse(output)
+      }
+    )
+}
+
 function formatResponse({ result, response }){
   if (response){
     if (result === results.success ){


Test patch:
diff --git a/test/run/index.js b/test/run/index.js
index fa4f53b..90c5728 100644
--- a/test/run/index.js
+++ b/test/run/index.js
@@ -13,12 +13,17 @@ td.config({ promiseConstructor: Promise })
 test.before(() => {
   return create('run')
   .then(() => generateFunction({ name: 'foo' }) )
+  .then(() => generateFunction({ name: 'bar' }) )
 })
 
 test('Calls the function', (t)=>{
-  t.truthy(run({ name: 'foo' }))
+  t.truthy(run({ name: 'foo', build: true }))
 })
 
 test('Throws an exception for an event that does not exist', (t)=>{
-  t.throws(() => run({ name: 'foo', event: 'doesnotexist' }))
+  t.throws(() => run({ name: 'foo', build: true, event: 'doesnotexist' }))
+})
+
+test('Throws an exception for unbuilt function', (t)=>{
+  t.throws(run({ name: 'bar', build: false }))
 })