Passed
Push — master ( a66138...55414e )
by Björn
02:22
created

lib/patternlibrary/build-search.js (1 issue)

Labels
Severity
1 1
var fs = require('fs');
2 1
var mkdirp = require('mkdirp');
3 1
var path = require('path');
4 1
var sanatizeType = require('../util/sanatize-patterntype');
5
6
/**
7
 * Generates a search file from the current tree of processed pages.
8
 * @param {string} outFile - Path to write to.
9
 * @param {function} cb - Callback to run when the search file is written to disk.
10
 * @todo Make hashes for search result types configurable
11
 */
12 1
module.exports = function(outFile, cb) {
13
  var tree = this.data.patterns;
14
  var results = [];
15
16
  results = results.concat(this.searchOptions.extra);
17
18
  // Each item in the tree is a page
19
  for (var i in tree) {
20
    var item = tree[i];
21
    
22
    // for now... only add to search if we have a linkable pattern file at hand
23 4
    if ( (typeof item.fileName != 'undefined') && (item.fileName != '') ) {
24
    	
25
        var link = sanatizeType(path.join(
26
        		this.options.basepath, 
27
        		this.options.patternspath, 
28
        		item.pattern.name, 
29
        		this.options.pattern.target
30
        ), true);
31
        var type = 'page';
32
33
        // By default pages are classified as a "page"
34
        // If it has code associated with it, then it's a "component" instead.
35 2
        if (keysInObject(item, Object.keys(this.adapters))) {
36
          type = 'component';
37
        }
38
39
        // Check for special page types
40
        for (var t in this.searchOptions.pageTypes) {
41
          var func = this.searchOptions.pageTypes[t];
42 2
          if (func(item)) {
43
            type = t;
44
          }
45
        }
46
47
        // Add the page itself as a search result
48
        results.push({
49
          type: type,
50
          name: item.title,
51
          description: item.description,
52
          link: link,
53
          tags: item.tags || []
54
        });
55
56
        // Run search builders for each adapter
57
        for (var a in this.adapters) {
58 4
          if (this.adapters[a].search && item[a]) {
59
            results = results.concat(this.adapters[a].search(item[a], link));
60
          }
61
        }
62
        
63
    }
64
  }
65
66
  // Re-order search results based on search config
67
  results.sort(function(a, b) {
68
    return this.searchOptions.sort.indexOf(a.type) - this.searchOptions.sort.indexOf(b.type);
69
  }.bind(this));
70
71
  // Write the finished results to disk
72
  mkdirp(path.dirname(outFile), function(err) {
73 2
    if (err) throw err;
74
    fs.writeFile(outFile, JSON.stringify(results, null, '  '), cb);
0 ignored issues
show
The variable results is changed as part of the for-each loop for example by results.concat(this.searchOptions.extra) on line 16. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
75
  });
76
}
77
78
/**
79
 * Determines if any key in an array exists on an object.
80
 * @param {object} obj - Object to check for keys.
81
 * @param {array} keys - Keys to check.
82
 * @returns {boolean} `true` if any key is found on the object, or `false` if not.
83
 */
84
function keysInObject(obj, keys) {
85
  for (var i in keys) {
86 2
    if (keys[i] in obj) return true;
87
  }
88
  return false;
89
}
90