// read front matter on demand
var matter = require('gray-matter');
var path = require('path');
var fs = require('fs');
var config = require('./config.js');

/** Returns true if file exists */
function isFile(dir, file){
  var fullPath = path.join(config.sourceRoot, dir, file);
  try {
    fs.statSync(fullPath);
  } catch (e) {
    return false;
  }
  return true;
}

/** read front-matter from file, fail gracefully. */
function frontmatter(filename) {
  var filepath = path.join(config.lessonRoot, config.sourceFolder, filename);
  var m;
  try {
    m = matter.read(filepath);
  } catch (e) {
    return {};
  }
  return m.data;
}


/**
 * remove files from build which have external set
 */
function removeExternal(files, _, done) {
  Object.keys(files).forEach(function(key){
    if (files[key].external) {
      delete files[key];
    }
  });
  done();
}


module.exports = {
  isFile: isFile,
  frontmatter: frontmatter,
  removeExternal: removeExternal
};

@@ -59,17 +59,16 @@
 matter')
-;
 %0Avar pat
@@ -86,17 +86,16 @@
 ('path')
-;
 %0Avar fs 
@@ -109,17 +109,16 @@
 re('fs')
-;
 %0Avar con
@@ -145,17 +145,16 @@
 fig.js')
-;
 %0A%0A/** Re
@@ -197,16 +197,17 @@
 n isFile
+ 
 (dir, fi
@@ -209,16 +209,17 @@
 r, file)
+ 
 %7B%0A  var 
@@ -268,17 +268,16 @@
 r, file)
-;
 %0A  try %7B
@@ -298,25 +298,24 @@
 nc(fullPath)
-;
 %0A  %7D catch (
@@ -335,17 +335,16 @@
 rn false
-;
 %0A  %7D%0A  r
@@ -353,17 +353,16 @@
 urn true
-;
 %0A%7D%0A%0A/** 
@@ -430,16 +430,17 @@
 ntmatter
+ 
 (filenam
@@ -524,17 +524,16 @@
 ame)
-;
 %0A  var m
 ;%0A  
@@ -528,17 +528,16 @@
 %0A  var m
-;
 %0A  try %7B
@@ -566,17 +566,16 @@
 ilepath)
-;
 %0A  %7D cat
@@ -596,17 +596,16 @@
 eturn %7B%7D
-;
 %0A  %7D%0A  r
@@ -616,22 +616,20 @@
 n m.data
-;
 %0A%7D%0A%0A
-%0A
 /**%0A * r
@@ -702,16 +702,17 @@
 External
+ 
 (files, 
@@ -763,13 +763,15 @@
 tion
+ 
 (key)
+ 
 %7B%0A  
@@ -822,17 +822,16 @@
 les%5Bkey%5D
-;
 %0A    %7D%0A 
@@ -833,17 +833,16 @@
   %7D%0A  %7D)
-;
 %0A  done(
@@ -846,14 +846,12 @@
 ne()
-;
 %0A%7D%0A%0A
-%0A
 modu
@@ -949,6 +949,5 @@
 al%0A%7D
-;
 %0A

use standard code style