Repository: bustlelabs/shep
Pull request #130 (2016-11-29 04:45:57)

Fix some assumptions about project state
- Add some getting start notes to readme
- Use mkdirp to make parent directories if they don't already exist (fixes failure on first function when there's no functions directory)
- Don't assume there's an api.json or that it has a paths object when generating an endpoint


Patch:
diff --git a/README.md b/README.md
index 146033d..333be45 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,22 @@ Shep will require your amazon credentials and will load them using the same meth
 
 ### Installation
 
-`npm install -g shep`
+```bash
+npm install -g shep
+npm install --save-dev shep // the global shep will run the local shep
+```
+
+Add a few lines to your `package.json`. Your [account id](https://console.aws.amazon.com/billing/home?#/account),
+
+```json
+{
+  "name": "my-great-package",
+  "shep": {
+    "accountId": "XXXXX",
+    "region": "us-east-1"
+  }
+}
+```
 
 ### Creating a new API
 
diff --git a/src/generate-endpoint/index.js b/src/generate-endpoint/index.js
index 7c22469..da6dea6 100644
--- a/src/generate-endpoint/index.js
+++ b/src/generate-endpoint/index.js
@@ -16,7 +16,7 @@ module.exports = function (opts) {
     throw new Error('Unable to determine your AWS Account ID. Please set it in the `shep` section of package.json')
   }
 
-  const api = load.api()
+  const api = load.api() || {}
 
   const name = `${path} ${method}`
   const { shortName, fullName } = genName(name)
@@ -46,6 +46,10 @@ module.exports = function (opts) {
 function addPath (api, path, method, accountId, functionName) {
   if (method === 'any') { method = 'x-amazon-apigateway-any-method' }
 
+  if (!api.paths) {
+    api.paths = {}
+  }
+
   api.paths[path] = api.paths[path] || {}
   if (api.paths[path][method] !== undefined) { throw new Error(`Method '${method}' on path '${path}' already exists`) }
   api.paths[path][method] = api.paths[path][method] || {}
diff --git a/src/generate-function/index.js b/src/generate-function/index.js
index 70d31b3..cb4ca45 100644
--- a/src/generate-function/index.js
+++ b/src/generate-function/index.js
@@ -1,4 +1,4 @@
-import { mkdir, writeFile } from '../util/modules/fs'
+import { mkdirp, writeFile } from '../util/modules/fs'
 import genName from '../util/generate-name'
 import * as templates from './templates'
 import listr from '../util/modules/listr'
@@ -10,11 +10,11 @@ export default function (opts) {
   const tasks = listr([
     {
       title: `Create functions/${shortName}/`,
-      task: () => mkdir(`./functions/${shortName}`)
+      task: () => mkdirp(`./functions/${shortName}`)
     },
     {
       title: `Create functions/${shortName}/events`,
-      task: () => mkdir(`./functions/${shortName}/events`)
+      task: () => mkdirp(`./functions/${shortName}/events`)
     },
     {
       title: 'Create files',
diff --git a/src/new/index.js b/src/new/index.js
index a97a71d..b9d7a22 100644
--- a/src/new/index.js
+++ b/src/new/index.js
@@ -1,4 +1,4 @@
-import { mkdir, writeFile } from '../util/modules/fs'
+import { mkdirp, writeFile } from '../util/modules/fs'
 import { getRole, createRole, attachPolicy } from '../util/aws/iam'
 import * as templates from './templates'
 import Promise from 'bluebird'
@@ -17,7 +17,7 @@ export default function run (opts) {
     },
     {
       title: `Create ${path}/`,
-      task: () => mkdir(path)
+      task: () => mkdirp(path)
     },
     {
       title: 'Create Subdirectories',
@@ -59,8 +59,8 @@ function setupIam (context) {
 
 function createSubDirs ({ path }) {
   return Promise.all([
-    mkdir(path + '/functions'),
-    mkdir(path + '/config')
+    mkdirp(path + '/functions'),
+    mkdirp(path + '/config')
   ])
 }
 
diff --git a/src/util/modules/fs.js b/src/util/modules/fs.js
index fc50e51..a4c33a5 100644
--- a/src/util/modules/fs.js
+++ b/src/util/modules/fs.js
@@ -1,6 +1,6 @@
 import fs from 'fs-extra-promise'
 
-export const mkdir = fs.mkdirAsync
+export const mkdirp = fs.mkdirpAsync
 export const writeFile = fs.writeFileAsync
 export const writeJSON = fs.writeJSONAsync
 export const readJSON = fs.readJSONAsync


Test patch:
diff --git a/test/helpers/fs.js b/test/helpers/fs.js
index 745fe05..e5f558e 100644
--- a/test/helpers/fs.js
+++ b/test/helpers/fs.js
@@ -3,7 +3,7 @@ import td from 'testdouble'
 const fs = td.replace('../../src/util/modules/fs')
 
 function createdDir (t, dirName) {
-  td.verify(fs.mkdir(td.matchers.contains(dirName)))
+  td.verify(fs.mkdirp(td.matchers.contains(dirName)))
 }
 createdDir.title = (providedTitle, dirName) => `Created dir ${dirName}`