1 | var path = require('path')
|
2 | , fs = require('fs')
|
3 | , _ = require('underscore');
|
4 |
|
5 | var markdownExts = ['.md', '.markdown'];
|
6 | var ignoredDirs = ['.', '..', '.git', 'node_modules'];
|
7 |
|
8 | function separateFilesAndDirs(fileInfos) {
|
9 | return {
|
10 | directories : _(fileInfos).filter(function (x) {
|
11 | return x.isDirectory() && !_(ignoredDirs).include(x.name);
|
12 | }),
|
13 | markdownFiles : _(fileInfos).filter(function (x) {
|
14 | return x.isFile() && _(markdownExts).include(path.extname(x.name));
|
15 | })
|
16 | };
|
17 | }
|
18 |
|
19 | function findRec(currentPath) {
|
20 | function getStat (entry) {
|
21 | var target = path.join(currentPath, entry),
|
22 | stat = fs.statSync(target);
|
23 |
|
24 | return _(stat).extend({
|
25 | name: entry,
|
26 | path: target
|
27 | });
|
28 | }
|
29 |
|
30 | function process (fileInfos) {
|
31 | var res = separateFilesAndDirs(fileInfos);
|
32 | var tgts = _(res.directories).pluck('path');
|
33 |
|
34 | if (res.markdownFiles.length > 0)
|
35 | console.log('\nFound %s in "%s"', _(res.markdownFiles).pluck('name').join(', '), currentPath);
|
36 | else
|
37 | console.log('\nFound nothing in "%s"', currentPath);
|
38 |
|
39 | return {
|
40 | markdownFiles : res.markdownFiles,
|
41 | subdirs : tgts
|
42 | };
|
43 | }
|
44 |
|
45 | var stats = _(fs.readdirSync(currentPath)).map(getStat)
|
46 | , res = process(stats)
|
47 | , markdownsInSubdirs = _(res.subdirs).map(findRec)
|
48 | , allMarkdownsHereAndSub = res.markdownFiles.concat(markdownsInSubdirs);
|
49 |
|
50 | return _(allMarkdownsHereAndSub).flatten();
|
51 | }
|
52 |
|
53 |
|
54 |
|
55 | exports.findMarkdownFiles = function(dir) {
|
56 | return findRec(dir);
|
57 | };
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|