diff --git a/lib/files.js b/lib/files.js index 9975de8..ff83a8b 100644 --- a/lib/files.js +++ b/lib/files.js @@ -10,13 +10,30 @@ function ReadFile(filePath, filesJson) { try { // Synchronous readdir - files = fs.readdirSync(filePath); + files = fs.readdirSync(filePath) + // sort the files: directories first, afterwards files + .map(function(v) { + var stat = fs.statSync(path.resolve(filePath, v)); + return { + name: v, + isDirectory: stat.isDirectory() + }; + }) + .sort(function(a, b) { + if (a.isDirectory && !b.isDirectory) return -1; + if (!a.isDirectory && b.isDirectory) return 1; + return a.name.localeCompare(b.name); + }) + .map(function(v) { + return v.name; + }); + files.forEach(walk); - } catch (error) { - filesJson = null; //fixme + } catch (error) { + filesJson = null; //fixme console.log(color.red(error.message)); } - + function walk(file) { var newpath = path.join(filePath, file); if (typeof newpath !== 'undefined') { @@ -25,6 +42,10 @@ function ReadFile(filePath, filesJson) { if (state.isDirectory()) { filesJson[file] = {}; ReadFile(newpath, filesJson[file]); + // filter empty directories + if (Object.keys(filesJson[file]).length < 1) { + delete filesJson[file]; + } } else { // Parse the file. var obj = path.parse(newpath); diff --git a/lib/summary/index.js b/lib/summary/index.js index 4b88b2a..6c05d66 100644 --- a/lib/summary/index.js +++ b/lib/summary/index.js @@ -58,7 +58,7 @@ function Summary(options) { next(); }else{ next(color.red("Sorry, something is going wrong or no markdowns.")); - } + } }], parse: ['files', function(next) { @@ -88,7 +88,7 @@ function Summary(options) { // Mark it to skip skip = key; - } + } // The file is `readme.md` if (_.isString(n['readme']) || _.isString(n['Readme']) || _.isString(n['README'])) { @@ -108,7 +108,7 @@ function Summary(options) { return; } - desc += _.repeat(' ', step + 2) + formatCatalog(key) + n; + desc += _.repeat(' ', step) + formatCatalog(key) + n; } } }); @@ -121,11 +121,11 @@ function getFiles(root) { filesJson = {}; readFile(root, filesJson); - + if(filesJson){ result = _.pick(filesJson, filterRules); } - + return result; } @@ -143,7 +143,7 @@ function filterRules(n, key) { } else { result = _.includes(catalog, key) && !_.includes(ignores, key); } - + return result; }