
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const resourceTransform = require('../lib/resource-transform');

const resourceDir = path.join(__dirname, './resources');

function loadResource(filename) {
  const filePath = path.join(resourceDir, filename);
  const fileExt = path.extname(filePath);
  const fileBasename = path.basename(filePath, fileExt);
  const resourceName = fileBasename.toLowerCase();

  let doc;
  try {
    doc = yaml.safeLoad(fs.readFileSync(filePath, 'utf8'));
  } catch(e) {
    console.warn('Resource file could not be loaded');
  }

  return doc;
}

const resources = fs.readdirSync(resourceDir)
  .map(loadResource)
  .map(resourceTransform);


const resourceString = JSON.stringify(resources);

// Write JSON result to stdout. This will be consumed by the server to set up
// the routes and database.
console.log(resourceString);

// Temporarily write this out to the disk, so that we can check out our results.
fs.writeFileSync('./test.json', resourceString);

@@ -1,12 +1,26 @@
+'use strict';%0A
 %0Aconst _ = r
@@ -195,65 +195,391 @@
 );%0A%0A
-const resourceDir = path.join(__dirname, './resources');%0A
+// Store all of the resources you'd like in this directory. Eventually, these%0A// would be a edited via a Web UI, or even from the command line.%0Aconst resourceDir = path.join(__dirname, './resources');%0A%0A// This function reads and parses the resource from the disk, and returns it.%0A// At the moment, only YAML files are supported, although it'd be simple to add%0A// in support for JSON.
 %0Afun
@@ -984,16 +984,59 @@
 doc;%0A%7D%0A%0A
+// Loop through all files in the directory%0A
 const re
@@ -1079,55 +1079,486 @@
 )%0A  
-.map(loadResource)%0A  .map(resourceTransform);%0A%0A
+// Open them up and parse them as JSON%0A  .map(loadResource)%0A  // Transform them into a %22resource definition,%22 which is useful on the%0A  // generated server, and a migration, which is useful for configuring the DB.%0A  // Eventually, a series of migrations will be generated capturing changes%0A  // made to the resource definition. Also, all of this will be stored in the%0A  // DB.%0A  .map(resourceTransform);%0A%0A// Convert it to a JSON string so we can hand it off to the generated API.
 %0Acon
@@ -1748,21 +1748,9 @@
 %0A// 
-Temporarily w
+W
 rite
@@ -1774,45 +1774,47 @@
 disk
-, so that we can check out our result
+%C2%A0%E2%80%93 this is useful for debugging purpose
 s.%0Af

Add comments to example/index.js