Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var tree = require("gulp-tree");
gulp.src('./src/*.html')
.pipe(tree({
patternsPath: './src/patterns',
jsonPath: './src/json/'
jsonPath: './src/json/',
appPath: './src/'
}))
```

Expand Down
79 changes: 28 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
var fs, handle, path, tree, yaml, marked;
var fs, handle, jsyaml, path, tree, yaml;

fs = require('fs');
jsyaml = require('js-yaml');
marked = require('marked'); // markdown processor
path = require('path');
var util = require('gulp-util');

var through = require("through2");
var es = require("event-stream");


var generateTree = function(param){

'use strict'

var params = param || {},
patternsPath = path.resolve(params.patternsPath || './src/patterns'),
jsonPath = path.resolve(params.jsonPath || './src/json/')
jsonPath = path.resolve(params.jsonPath || './src/json/'),
appPath = path.resolve(params.appPath || './src/')

var stream = through.obj(function (file, enc, callback) {

/* File passed in */

var parsedFile = tree(patternsPath)


Expand All @@ -33,7 +30,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'));
Expand All @@ -44,7 +41,7 @@ var generateTree = function(param){
/**
* Write the file
*/

fs.stat(jsonPath, function(err, stats){

if(err && err.errno == 34){
Expand All @@ -57,35 +54,31 @@ var generateTree = function(param){
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)) {
Expand All @@ -94,31 +87,30 @@ var generateTree = function(param){
return 'error: root does not exist';
}
info = {
path: root.replace(path.join(process.cwd(),'src/'), ''),
path: root.replace(path.join(appPath), ''),
name: path.basename(root).replace(/^\d./, ''),
slug: path.basename(root)
};


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;

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);
Expand All @@ -129,15 +121,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);
Expand All @@ -148,45 +140,30 @@ var generateTree = function(param){

info.name = conf.name
}else{

return true;
}


/**
* 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 {
info.type = 'unknown';
}
return info;

}

return stream;

}

module.exports = generateTree;

} else if (ring.isSymbolicLink()) {
info.type = 'link';
} else {
Expand All @@ -200,4 +177,4 @@ module.exports = generateTree;

}

module.exports = generateTree;
module.exports = generateTree;