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) { |
||
0 ignored issues
–
show
|
|||
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) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
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) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
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; |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
74 | fs.writeFile(outFile, JSON.stringify(results, null, ' '), cb); |
||
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) { |
||
0 ignored issues
–
show
A for in loop automatically includes the property of any prototype object, consider checking the key using
hasOwnProperty .
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: var someObject;
for (var key in someObject) {
if ( ! someObject.hasOwnProperty(key)) {
continue; // Skip keys from the prototype.
}
doSomethingWith(key);
}
Loading history...
|
|||
86 | 2 | if (keys[i] in obj) return true; |
|
0 ignored issues
–
show
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later. Consider: if (a > 0)
b = 42;
If you or someone else later decides to put another statement in, only the first statement will be executed. if (a > 0)
console.log("a > 0");
b = 42;
In this case the statement if (a > 0) {
console.log("a > 0");
b = 42;
}
ensures that the proper code will be executed conditionally no matter how many statements are added or removed.
Loading history...
|
|||
87 | } |
||
88 | return false; |
||
89 | } |
||
90 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: