diff --git a/index.js b/index.js index 5fea4fc..56df593 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ var fs, handle, path, tree, yaml, marked; fs = require('fs'); jsyaml = require('js-yaml'); -marked = require('marked'); // markdown processor +marked = require('marked'); // markdown processor path = require('path'); var util = require('gulp-util'); @@ -21,7 +21,7 @@ var generateTree = function(param){ var stream = through.obj(function (file, enc, callback) { /* File passed in */ - + var parsedFile = tree(patternsPath) @@ -33,7 +33,7 @@ var generateTree = function(param){ /** * If patternsPath doesnt exist */ - + fs.stat(patternsPath, function(err, stats){ if(err && erro.errno == 34){ this.emit('error', new PluginError('gulp-tree', 'Patterns directory doesnt exist')); @@ -44,48 +44,45 @@ var generateTree = function(param){ /** * Write the file */ - - fs.stat(jsonPath, function(err, stats){ - - if(err && err.errno == 34){ - - fs.mkdir(jsonPath) - } - }) + try { + fs.mkdirSync(jsonPath); + } catch(e) { + if ( e.code != 'EEXIST' ) throw e; + } var files = []; for(var i = 0; i< s.length; i++){ - + if(s[i].children && s[i].children.length){ - + files.push(jsonPath+ '/' + s[i].slug + '.json') fs.writeFile(jsonPath + '/'+ s[i].slug+".json", JSON.stringify(s[i].children, null, 4), function(err){ if(err) { console.log(err); } else { - + console.log("Generated json in " + jsonPath); } - }) + }) } - + } return files; - - - + + + }); var excludedExtension = ['.png', '.jpg', '.git', '.jpeg', '.gif', '.html']; var tree = function(root){ - + var info, ring; root = root.replace(/\/+$/, ""); if (fs.existsSync(root)) { @@ -102,10 +99,10 @@ var generateTree = function(param){ if (ring.isDirectory()) { - var children = fs.readdirSync(root) - + var children = fs.readdirSync(root) + var filtered = children.filter(function(c){ - + var filepath = root + '/' + c, isFile = fs.lstatSync(filepath).isFile(), content = true; @@ -113,12 +110,12 @@ var generateTree = function(param){ if(isFile && fs.readFileSync(filepath) == ""){ content = false } - + return c != '.DS_Store' && excludedExtension.indexOf(path.extname(c)) == -1 && content }) - delete(info.path) + delete(info.path) info.children = filtered.map(function(child) { return tree(root + '/' + child); @@ -129,15 +126,15 @@ var generateTree = function(param){ var contents = fs.readFileSync(root, {encoding: 'utf8'}); var extname = path.extname(root); - + var re = /^(-{3}(?:\n|\r)([\w\W]+?)-{3})?([\w\W]*)*/ , results = re.exec(contents.trim()) , conf = {} - , yamlOrJson; + , yamlOrJson; if((yamlOrJson = results[2])) { - - if(yamlOrJson.charAt(0) === '{') { + + if(yamlOrJson.charAt(0) === '{') { conf = JSON.parse(yamlOrJson); } else { conf = jsyaml.load(yamlOrJson); @@ -148,7 +145,7 @@ var generateTree = function(param){ info.name = conf.name }else{ - + return true; } @@ -156,22 +153,22 @@ var generateTree = function(param){ /** * Add meta variables */ - + info.meta = {}; - + for(var key in conf){ if(key != "name" && key != "description"){ if(conf.hasOwnProperty(key)){ - info.meta[key] = conf[key] - } + info.meta[key] = conf[key] + } } } if(Object.keys(info.meta).length < 1){ delete(info.meta) } - - + + } else if (ring.isSymbolicLink()) { info.type = 'link'; } else { @@ -185,8 +182,8 @@ var generateTree = function(param){ } -module.exports = generateTree; - +module.exports = generateTree; + } else if (ring.isSymbolicLink()) { info.type = 'link'; } else {